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)
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")) {
......
......@@ -44,6 +44,7 @@ class CLISetInterface : public SetView {
// export info, so we can write files
ExportInfo ei;
void setExportInfoCwd();
};
// ----------------------------------------------------------------------------- : EOF
......
......@@ -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
......
......@@ -418,7 +418,7 @@ SCRIPT_FUNCTION(write_image_file) {
SCRIPT_OPTIONAL_PARAM_(int, height);
ScriptObject<CardP>* card = dynamic_cast<ScriptObject<CardP>*>(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 {
......
......@@ -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);
......
......@@ -69,13 +69,22 @@ 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.
*/
......@@ -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();
......
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