Commit d1297e23 authored by twanvl's avatar twanvl

allow reading images from the current directory in the CLI interface

parent b13371ad
...@@ -26,8 +26,8 @@ CLISetInterface::CLISetInterface(const SetP& set, bool quiet) ...@@ -26,8 +26,8 @@ CLISetInterface::CLISetInterface(const SetP& set, bool quiet)
if (!cli.haveConsole()) { if (!cli.haveConsole()) {
throw Error(_("Can not run command line interface without a console;\nstart MSE with \"mse.com --cli\"")); 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; ei.allow_writes_outside = true;
setExportInfoCwd();
setSet(set); setSet(set);
run(); run();
} }
...@@ -61,6 +61,14 @@ void CLISetInterface::onChangeSet() { ...@@ -61,6 +61,14 @@ void CLISetInterface::onChangeSet() {
ei.set = set; 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 // ----------------------------------------------------------------------------- : Running
...@@ -151,7 +159,7 @@ void CLISetInterface::handleCommand(const String& command) { ...@@ -151,7 +159,7 @@ void CLISetInterface::handleCommand(const String& command) {
if (!wxSetWorkingDirectory(arg)) { if (!wxSetWorkingDirectory(arg)) {
cli.showError(_("Can't change working directory to ")+arg); cli.showError(_("Can't change working directory to ")+arg);
} else { } else {
ei.directory_relative = ei.directory_absolute = wxGetCwd(); setExportInfoCwd();
} }
} }
} else if (before == _(":pwd") || before == _(":p")) { } else if (before == _(":pwd") || before == _(":p")) {
......
...@@ -44,6 +44,7 @@ class CLISetInterface : public SetView { ...@@ -44,6 +44,7 @@ class CLISetInterface : public SetView {
// export info, so we can write files // export info, so we can write files
ExportInfo ei; ExportInfo ei;
void setExportInfoCwd();
}; };
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
......
...@@ -18,6 +18,7 @@ DECLARE_POINTER_TYPE(Set); ...@@ -18,6 +18,7 @@ DECLARE_POINTER_TYPE(Set);
DECLARE_POINTER_TYPE(Field); DECLARE_POINTER_TYPE(Field);
DECLARE_POINTER_TYPE(Style); DECLARE_POINTER_TYPE(Style);
DECLARE_POINTER_TYPE(ExportTemplate); DECLARE_POINTER_TYPE(ExportTemplate);
DECLARE_POINTER_TYPE(Package);
// ----------------------------------------------------------------------------- : ExportTemplate // ----------------------------------------------------------------------------- : ExportTemplate
...@@ -48,7 +49,8 @@ struct ExportInfo { ...@@ -48,7 +49,8 @@ struct ExportInfo {
ExportInfo(); ExportInfo();
SetP set; ///< The set that is being exported 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) String directory_relative; ///< The directory for storing extra files (or "" if !export->create_directory)
/// This is just the directory name /// This is just the directory name
String directory_absolute; ///< The absolute path of the directory String directory_absolute; ///< The absolute path of the directory
......
...@@ -418,7 +418,7 @@ SCRIPT_FUNCTION(write_image_file) { ...@@ -418,7 +418,7 @@ SCRIPT_FUNCTION(write_image_file) {
SCRIPT_OPTIONAL_PARAM_(int, height); SCRIPT_OPTIONAL_PARAM_(int, height);
ScriptObject<CardP>* card = dynamic_cast<ScriptObject<CardP>*>(input.get()); // is it a card? ScriptObject<CardP>* card = dynamic_cast<ScriptObject<CardP>*>(input.get()); // is it a card?
Image image; 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) { if (card) {
image = conform_image(export_bitmap(ei.set, card->getValue()).ConvertToImage(), options); image = conform_image(export_bitmap(ei.set, card->getValue()).ConvertToImage(), options);
} else { } else {
......
...@@ -66,7 +66,7 @@ const String& Package::absoluteFilename() const { ...@@ -66,7 +66,7 @@ const String& Package::absoluteFilename() const {
return filename; return filename;
} }
void Package::open(const String& n) { void Package::open(const String& n, bool fast) {
assert(!isOpened()); // not already opened assert(!isOpened()); // not already opened
// get absolute path // get absolute path
wxFileName fn(n); wxFileName fn(n);
...@@ -78,7 +78,7 @@ void Package::open(const String& n) { ...@@ -78,7 +78,7 @@ void Package::open(const String& n) {
} }
// type of package // type of package
if (wxDirExists(filename)) { if (wxDirExists(filename)) {
openDirectory(); openDirectory(fast);
} else if (wxFileExists(filename)) { } else if (wxFileExists(filename)) {
openZipfile(); openZipfile();
} else { } else {
...@@ -207,16 +207,15 @@ InputStreamP Package::openIn(const String& file) { ...@@ -207,16 +207,15 @@ InputStreamP Package::openIn(const String& file) {
if (filename.find(_(".mse-")) != String::npos) { if (filename.find(_(".mse-")) != String::npos) {
throw PackageError(_ERROR_2_("file not found package like", file, filename)); throw PackageError(_ERROR_2_("file not found package like", file, filename));
} }
throw FileNotFoundError(file, filename);
} }
InputStreamP stream; InputStreamP stream;
if (it->second.wasWritten()) { if (it != files.end() && it->second.wasWritten()) {
// written to this file, open the temp file // written to this file, open the temp file
stream = shared(new BufferedFileInputStream(it->second.tempName)); stream = shared(new BufferedFileInputStream(it->second.tempName));
} else if (wxFileExists(filename+_("/")+file)) { } else if (wxFileExists(filename+_("/")+file)) {
// a file in directory package // a file in directory package
stream = shared(new BufferedFileInputStream(filename+_("/")+file)); 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 // a file in a zip archive
// somebody in wx thought seeking was no longer needed, it now only works with the 'compatability constructor' // 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())); stream = shared(new wxZipInputStream(filename, it->second.zipEntry->GetInternalName()));
...@@ -339,9 +338,8 @@ void Package::loadZipStream() { ...@@ -339,9 +338,8 @@ void Package::loadZipStream() {
zipStream->CloseEntry(); zipStream->CloseEntry();
} }
void Package::openDirectory() { void Package::openDirectory(bool fast) {
zipfile = false; if (!fast) openSubdir(wxEmptyString);
openSubdir(wxEmptyString);
} }
void Package::openSubdir(const String& name) { void Package::openSubdir(const String& name) {
...@@ -368,7 +366,6 @@ void Package::openSubdir(const String& name) { ...@@ -368,7 +366,6 @@ void Package::openSubdir(const String& name) {
} }
void Package::openZipfile() { void Package::openZipfile() {
zipfile = true;
// close old streams // close old streams
delete fileStream; fileStream = nullptr; delete fileStream; fileStream = nullptr;
delete zipStream; zipStream = nullptr; delete zipStream; zipStream = nullptr;
...@@ -382,7 +379,6 @@ void Package::openZipfile() { ...@@ -382,7 +379,6 @@ void Package::openZipfile() {
} }
void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_copy) { void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_copy) {
zipfile = false;
// write to a directory // write to a directory
VCSP vcs = getVCS(); VCSP vcs = getVCS();
FOR_EACH(f, files) { FOR_EACH(f, files) {
...@@ -414,7 +410,6 @@ void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_ ...@@ -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) { void Package::saveToZipfile(const String& saveAs, bool remove_unused, bool is_copy) {
zipfile = true;
// create a temporary zip file name // create a temporary zip file name
String tempFile = saveAs + _(".tmp"); String tempFile = saveAs + _(".tmp");
wxRemoveFile(tempFile); wxRemoveFile(tempFile);
......
...@@ -69,15 +69,24 @@ class Package : public IntrusivePtrVirtualBase { ...@@ -69,15 +69,24 @@ class Package : public IntrusivePtrVirtualBase {
/// The time this package was last modified /// The time this package was last modified
inline wxDateTime lastModified() const { return modified; } inline wxDateTime lastModified() const { return modified; }
/// Open a package, should only be called when the package is constructed using the default constructor! /// Open a package
/// @pre open not called before [TODO] /**
void open(const String& 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 /// Saves the package
/// it was already a directory /**
/** If remove_unused=true all files that were in the file and * 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! * 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); void save(bool remove_unused = true);
...@@ -140,8 +149,8 @@ class Package : public IntrusivePtrVirtualBase { ...@@ -140,8 +149,8 @@ class Package : public IntrusivePtrVirtualBase {
// TODO: I dislike putting this here very much. There ought to be a better way. // TODO: I dislike putting this here very much. There ought to be a better way.
virtual VCSP getVCS() { return intrusive(new VCS()); } virtual VCSP getVCS() { return intrusive(new VCS()); }
/// true if this is a zip file, false if a directory (updated on open/save) /// true if this is a zip file, false if a directory
bool isZipfile() { return zipfile; } bool isZipfile() const { return !wxDirExists(filename); }
// --------------------------------------------------- : Private stuff // --------------------------------------------------- : Private stuff
private: private:
...@@ -163,9 +172,6 @@ class Package : public IntrusivePtrVirtualBase { ...@@ -163,9 +172,6 @@ class Package : public IntrusivePtrVirtualBase {
/// Last modified time /// Last modified time
DateTime modified; DateTime modified;
/// Zipfile flag
bool zipfile;
public: public:
/// Information on files in the package /// Information on files in the package
/** Note: must be public for DECLARE_TYPEOF to work */ /** Note: must be public for DECLARE_TYPEOF to work */
...@@ -182,7 +188,7 @@ class Package : public IntrusivePtrVirtualBase { ...@@ -182,7 +188,7 @@ class Package : public IntrusivePtrVirtualBase {
wxZipInputStream* zipStream; wxZipInputStream* zipStream;
void loadZipStream(); void loadZipStream();
void openDirectory(); void openDirectory(bool fast = false);
void openSubdir(const String&); void openSubdir(const String&);
void openZipfile(); void openZipfile();
void reopen(); void reopen();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment