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. Compile CasADi with WITH_GC=ON.\n"
67 "Alternatively, manually unzip it into a direcory, "
68 "and pass this directory name instead of the zip file name.");
74 dir_ = lock_file_.substr(0, lock_file_.size()-5) +
".unzipped";
77 "Unzipping '" + path_ +
"' requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
78 "Alternatively, manually unzip it into a direcory, "
79 "and pass this directory name instead of the zip file name.");
86 std::string zip_file =
"zip";
88 dir_ = lock_file_.substr(0, lock_file_.size()-5) +
".unzipped";
90 "Unzipping stream requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
91 "Alternatively, save with serialize option set to link. ");
96 blob_.seekg(0, std::ios::beg);
107 blob_ << src.rdbuf();
112 stream <<
"ZipResource(\"" << path_ <<
"\") -> \"" << dir_ <<
"\"";
116 stream <<
"ZipMemResource(blob) -> \"" << dir_ <<
"\"";
123 casadi_warning(
"Error: Cannot remove temporary directory: " + dir_);
128 casadi_warning(
"Error: Cannot remove lock file: " + lock_file_);
136 casadi_warning(
"Error: Cannot remove temporary directory: " + dir_);
141 casadi_warning(
"Error: Cannot remove lock file: " + lock_file_);
146 s.
version(
"ResourceInternal", 1);
165 s.
version(
"ResourceInternal", 1);
175 casadi_error(
"Cannot deserialize type '" +
class_name +
"'");
196 s.
unpack(
"ZipResource::path", path_);
201 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
202 std::lock_guard<std::mutex> lock(mutex_blob_);
204 s.
version(
"ZipMemResource", 1);
205 s.
unpack(
"ZipMemResource::blob", blob_);
211 s.
unpack(
"DirResource::path", path_);
246 std::ifstream binary(path_, std::ios_base::binary);
247 casadi_assert(binary.good(),
248 "Could not open zip file '" + path_ +
"'.");
249 s.
pack(
"ZipMemResource::blob", binary);
251 s.
pack(
"ZipResource::path", path_);
260 std::stringstream ss;
263 ss.seekg(0, std::ios::beg);
264 s.
pack(
"ZipMemResource::blob", ss);
266 s.
pack(
"DirResource::path", path_);
273 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
274 std::lock_guard<std::mutex> lock(mutex_blob_);
277 s.
version(
"ZipMemResource", 1);
278 s.
pack(
"ZipMemResource::blob", blob_);
282 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 filename(const std::string &path)
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)