diff --git a/src/cli/cli_main.cpp b/src/cli/cli_main.cpp index dd6995ff..e5e9ffc2 100644 --- a/src/cli/cli_main.cpp +++ b/src/cli/cli_main.cpp @@ -26,8 +26,8 @@ CLISetInterface::CLISetInterface(const SetP& set, bool quiet) if (!cli.haveConsole()) { throw Error(_("Can not run command line interface without a console;\nstart MSE with \"mse.com --cli\"")); } - ei.directory_relative = ei.directory_absolute = wxGetCwd(); ei.allow_writes_outside = true; + setExportInfoCwd(); setSet(set); run(); } @@ -61,6 +61,14 @@ void CLISetInterface::onChangeSet() { ei.set = set; } +void CLISetInterface::setExportInfoCwd() { + // write to the current directory + ei.directory_relative = ei.directory_absolute = wxGetCwd(); + // read from the current directory + ei.export_template = intrusive(new Package()); + ei.export_template->open(ei.directory_absolute, true); +} + // ----------------------------------------------------------------------------- : Running @@ -151,7 +159,7 @@ void CLISetInterface::handleCommand(const String& command) { if (!wxSetWorkingDirectory(arg)) { cli.showError(_("Can't change working directory to ")+arg); } else { - ei.directory_relative = ei.directory_absolute = wxGetCwd(); + setExportInfoCwd(); } } } else if (before == _(":pwd") || before == _(":p")) { diff --git a/src/cli/cli_main.hpp b/src/cli/cli_main.hpp index 874d039c..90ae9454 100644 --- a/src/cli/cli_main.hpp +++ b/src/cli/cli_main.hpp @@ -44,6 +44,7 @@ class CLISetInterface : public SetView { // export info, so we can write files ExportInfo ei; + void setExportInfoCwd(); }; // ----------------------------------------------------------------------------- : EOF diff --git a/src/data/export_template.hpp b/src/data/export_template.hpp index 44a899a2..a7395b92 100644 --- a/src/data/export_template.hpp +++ b/src/data/export_template.hpp @@ -18,6 +18,7 @@ DECLARE_POINTER_TYPE(Set); DECLARE_POINTER_TYPE(Field); DECLARE_POINTER_TYPE(Style); DECLARE_POINTER_TYPE(ExportTemplate); +DECLARE_POINTER_TYPE(Package); // ----------------------------------------------------------------------------- : ExportTemplate @@ -48,7 +49,8 @@ struct ExportInfo { ExportInfo(); SetP set; ///< The set that is being exported - ExportTemplateP export_template; ///< The export template used + PackageP export_template; ///< The export template used + /// When using the CLI, this can be a fake package to allow reading from the cwd String directory_relative; ///< The directory for storing extra files (or "" if !export->create_directory) /// This is just the directory name String directory_absolute; ///< The absolute path of the directory diff --git a/src/script/functions/export.cpp b/src/script/functions/export.cpp index 2c26916c..d7fd72c3 100644 --- a/src/script/functions/export.cpp +++ b/src/script/functions/export.cpp @@ -418,7 +418,7 @@ SCRIPT_FUNCTION(write_image_file) { SCRIPT_OPTIONAL_PARAM_(int, height); ScriptObject* card = dynamic_cast*>(input.get()); // is it a card? Image image; - GeneratedImage::Options options(width, height, ei.export_template.get(),ei.set.get()); + GeneratedImage::Options options(width, height, ei.export_template.get(), ei.set.get()); if (card) { image = conform_image(export_bitmap(ei.set, card->getValue()).ConvertToImage(), options); } else { diff --git a/src/util/io/package.cpp b/src/util/io/package.cpp index 39e061af..5ab9dde2 100644 --- a/src/util/io/package.cpp +++ b/src/util/io/package.cpp @@ -66,7 +66,7 @@ const String& Package::absoluteFilename() const { return filename; } -void Package::open(const String& n) { +void Package::open(const String& n, bool fast) { assert(!isOpened()); // not already opened // get absolute path wxFileName fn(n); @@ -78,7 +78,7 @@ void Package::open(const String& n) { } // type of package if (wxDirExists(filename)) { - openDirectory(); + openDirectory(fast); } else if (wxFileExists(filename)) { openZipfile(); } else { @@ -207,16 +207,15 @@ InputStreamP Package::openIn(const String& file) { if (filename.find(_(".mse-")) != String::npos) { throw PackageError(_ERROR_2_("file not found package like", file, filename)); } - throw FileNotFoundError(file, filename); } InputStreamP stream; - if (it->second.wasWritten()) { + if (it != files.end() && it->second.wasWritten()) { // written to this file, open the temp file stream = shared(new BufferedFileInputStream(it->second.tempName)); } else if (wxFileExists(filename+_("/")+file)) { // a file in directory package stream = shared(new BufferedFileInputStream(filename+_("/")+file)); - } else if (wxFileExists(filename) && it->second.zipEntry) { + } else if (wxFileExists(filename) && it != files.end() && it->second.zipEntry) { // a file in a zip archive // somebody in wx thought seeking was no longer needed, it now only works with the 'compatability constructor' stream = shared(new wxZipInputStream(filename, it->second.zipEntry->GetInternalName())); @@ -339,9 +338,8 @@ void Package::loadZipStream() { zipStream->CloseEntry(); } -void Package::openDirectory() { - zipfile = false; - openSubdir(wxEmptyString); +void Package::openDirectory(bool fast) { + if (!fast) openSubdir(wxEmptyString); } void Package::openSubdir(const String& name) { @@ -368,7 +366,6 @@ void Package::openSubdir(const String& name) { } void Package::openZipfile() { - zipfile = true; // close old streams delete fileStream; fileStream = nullptr; delete zipStream; zipStream = nullptr; @@ -382,7 +379,6 @@ void Package::openZipfile() { } void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_copy) { - zipfile = false; // write to a directory VCSP vcs = getVCS(); FOR_EACH(f, files) { @@ -414,7 +410,6 @@ void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_ } void Package::saveToZipfile(const String& saveAs, bool remove_unused, bool is_copy) { - zipfile = true; // create a temporary zip file name String tempFile = saveAs + _(".tmp"); wxRemoveFile(tempFile); diff --git a/src/util/io/package.hpp b/src/util/io/package.hpp index 97104ff5..aa6ac841 100644 --- a/src/util/io/package.hpp +++ b/src/util/io/package.hpp @@ -69,15 +69,24 @@ class Package : public IntrusivePtrVirtualBase { /// The time this package was last modified inline wxDateTime lastModified() const { return modified; } - /// Open a package, should only be called when the package is constructed using the default constructor! - /// @pre open not called before [TODO] - void open(const String& package); + /// Open a package + /** + * Should only be called when the package is constructed using the default constructor! + * + * If 'fast' is set, then for directories a full directory listing is not performed. + * This means that the file_infos will not be fully initialized. + * + * @pre open not called before [TODO] + */ + void open(const String& package, bool fast = false); - /// Saves the package, by default saves as a zip file, unless - /// it was already a directory - /** If remove_unused=true all files that were in the file and + /// Saves the package + /** + * By default saves as a zip file, unless it was already a directory. + * + * If remove_unused=true all files that were in the file and * are not touched with referenceFile will be deleted from the new archive! - * This is a form of garbage collection, to get rid of old picture files for example. + * This is a form of garbage collection, to get rid of old picture files for example. */ void save(bool remove_unused = true); @@ -140,8 +149,8 @@ class Package : public IntrusivePtrVirtualBase { // TODO: I dislike putting this here very much. There ought to be a better way. virtual VCSP getVCS() { return intrusive(new VCS()); } - /// true if this is a zip file, false if a directory (updated on open/save) - bool isZipfile() { return zipfile; } + /// true if this is a zip file, false if a directory + bool isZipfile() const { return !wxDirExists(filename); } // --------------------------------------------------- : Private stuff private: @@ -163,9 +172,6 @@ class Package : public IntrusivePtrVirtualBase { /// Last modified time DateTime modified; - /// Zipfile flag - bool zipfile; - public: /// Information on files in the package /** Note: must be public for DECLARE_TYPEOF to work */ @@ -182,7 +188,7 @@ class Package : public IntrusivePtrVirtualBase { wxZipInputStream* zipStream; void loadZipStream(); - void openDirectory(); + void openDirectory(bool fast = false); void openSubdir(const String&); void openZipfile(); void reopen();