26 #include "resource_internal.hpp"
27 #include "casadi_misc.hpp"
28 #include "archiver_impl.hpp"
30 #include "filesystem_impl.hpp"
47 stream <<
"DirResource(\"" << path_ <<
"\")";
55 if (option_name==
"serialize_mode") {
58 "Invalid serialization mode: " +
serialize_mode_ +
". Pick 'link' or 'embed'.");
60 casadi_error(
"Option '" + option_name +
"' does not exist");
66 "Unzipping '" + path_ +
"' requires advanced filesystem access. "
67 "Compile CasADi with WITH_GHC_FILESYSTEM=ON.\n"
68 "Alternatively, manually unzip it into a direcory, "
69 "and pass this directory name instead of the zip file name.");
76 dir_ = lock_file_.substr(0, lock_file_.size()-5) +
".unzipped";
79 "Unzipping '" + path_ +
"' requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
80 "Alternatively, manually unzip it into a direcory, "
81 "and pass this directory name instead of the zip file name.");
88 std::string zip_file =
"zip";
90 dir_ = lock_file_.substr(0, lock_file_.size()-5) +
".unzipped";
92 "Unzipping stream requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
93 "Alternatively, save with serialize option set to link. ");
98 blob_.seekg(0, std::ios::beg);
109 blob_ << src.rdbuf();
114 stream <<
"ZipResource(\"" << path_ <<
"\") -> \"" << dir_ <<
"\"";
118 stream <<
"ZipMemResource(blob) -> \"" << dir_ <<
"\"";
125 casadi_warning(
"Error: Cannot remove temporary directory: " + dir_);
130 casadi_warning(
"Error: Cannot remove lock file: " + lock_file_);
138 casadi_warning(
"Error: Cannot remove temporary directory: " + dir_);
143 casadi_warning(
"Error: Cannot remove lock file: " + lock_file_);
148 s.
version(
"ResourceInternal", 1);
167 s.
version(
"ResourceInternal", 1);
177 casadi_error(
"Cannot deserialize type '" +
class_name +
"'");
198 s.
unpack(
"ZipResource::path", path_);
203 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
204 std::lock_guard<std::mutex> lock(mutex_blob_);
206 s.
version(
"ZipMemResource", 1);
207 s.
unpack(
"ZipMemResource::blob", blob_);
213 s.
unpack(
"DirResource::path", path_);
249 casadi_assert(binary_ptr,
250 "Could not open zip file '" + path_ +
"'.");
251 s.
pack(
"ZipMemResource::blob", *binary_ptr);
253 s.
pack(
"ZipResource::path", path_);
262 std::stringstream ss;
265 ss.seekg(0, std::ios::beg);
266 s.
pack(
"ZipMemResource::blob", ss);
268 s.
pack(
"DirResource::path", path_);
275 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
276 std::lock_guard<std::mutex> lock(mutex_blob_);
279 s.
version(
"ZipMemResource", 1);
280 s.
pack(
"ZipMemResource::blob", blob_);
284 blob_.seekg(0, std::ios::beg);
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void serialize_type(SerializingStream &s) const override
std::string class_name() const override
Get type name.
DirResource(const std::string &path)
Initialize with a path.
void serialize_body(SerializingStream &s) const override
const std::string & path() const override
Get path for a consumer.
void disp(std::ostream &stream, bool more) const override
Print description.
static ResourceInternal * deserialize(DeserializingStream &s)
static casadi_int remove_all(const std::string &path)
static std::string absolute(const std::string &path)
static std::string filename(const std::string &path)
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
static bool remove(const std::string &path)
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
std::string to_string() const
Convert to a type.
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
RAII class base for reading from resources.
ResourceInternal()
Initialize with a path.
void serialize(SerializingStream &s) const
virtual void serialize_body(SerializingStream &s) const
void change_option(const std::string &option_name, const GenericType &option_value)
virtual void serialize_type(SerializingStream &s) const
static ResourceInternal * deserialize(DeserializingStream &s)
std::string serialize_mode_
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
virtual std::string class_name() const =0
Readable name of the internal class.
RAII class for reading from a zip held in memory.
void serialize_body(SerializingStream &s) const override
ZipMemResource(const std::istream &src)
void disp(std::ostream &stream, bool more) const override
Print description.
~ZipMemResource() override
static ResourceInternal * deserialize(DeserializingStream &s)
RAII class for reading from a zip file.
void disp(std::ostream &stream, bool more) const override
Print description.
static ResourceInternal * deserialize(DeserializingStream &s)
void serialize_body(SerializingStream &s) const override
void serialize_type(SerializingStream &s) const override
Potentially decay into ZipMemResource.
const std::string & path() const override
Get path for a consumer.
ZipResource(const std::string &path)
Initialize with a path.
std::string class_name() const override
Get type name.
std::string temporary_file(const std::string &prefix, const std::string &suffix)
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)