From b23040989e44f050b5fbe2d32abf777a6b3cb217 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 19 Apr 2007 21:03:16 +0000 Subject: [PATCH] fixed handling of duplicate filenames for images export git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@268 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/images_export_window.cpp | 38 ++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/gui/images_export_window.cpp b/src/gui/images_export_window.cpp index cc6a2428..3c931403 100644 --- a/src/gui/images_export_window.cpp +++ b/src/gui/images_export_window.cpp @@ -56,10 +56,8 @@ ImagesExportWindow::ImagesExportWindow(Window* parent, const SetP& set) // ----------------------------------------------------------------------------- : Exporting the images -// filename.3.ext -> filename.4.ext -void inc_number_filename(String& filename) { - // TODO - throw "TODO"; +bool is_filename_char(Char c) { + return isAlnum(c) || c == _(' ') || c == _('_') || c == _('-') || c == _('.'); } void ImagesExportWindow::onOk(wxCommandEvent&) { @@ -75,7 +73,7 @@ void ImagesExportWindow::onOk(wxCommandEvent&) { ScriptP filename_script = parse(gs.images_export_filename, true); // Select filename String name = wxFileSelector(_TITLE_("export images"),_(""), _LABEL_("filename is ignored"),_(""), - _LABEL_("all files")+_("|*.*"), wxSAVE, this); + _LABEL_("filename is ignored")+_("|*.*"), wxSAVE, this); if (name.empty()) return; wxFileName fn(name); // Export @@ -86,25 +84,41 @@ void ImagesExportWindow::onOk(wxCommandEvent&) { Context& ctx = set->getContext(card); String filename = untag(ctx.eval(*filename_script)->toString()); if (!filename) continue; // no filename -> no saving - fn.SetName(filename); - filename = fn.GetFullPath(); - if (wxFileExists(filename)) { + // sanitize filename + String clean_filename; + FOR_EACH(c, filename) { + if (is_filename_char(c)) { + clean_filename += c; + } + } + if (clean_filename.empty() || starts_with(clean_filename, _("."))) { + clean_filename = _("no-name") + clean_filename; + } + fn.SetFullName(clean_filename); + // does the file exist? + if (fn.FileExists()) { // file exists, what to do? switch (gs.images_export_conflicts) { case CONFLICT_KEEP_OLD: goto next_card; case CONFLICT_OVERWRITE: break; case CONFLICT_NUMBER: { + int i = 0; + String ext = fn.GetExt(); do { - inc_number_filename(filename); - } while(wxFileExists(filename)); + fn.SetExt(String() << ++i << _(".") << ext); + } while(fn.FileExists()); } case CONFLICT_NUMBER_OVERWRITE: { - while(used.find(filename) != used.end()) { - inc_number_filename(filename); + int i = 0; + String ext = fn.GetExt(); + while(used.find(fn.GetFullPath()) != used.end()) { + fn.SetExt(String() << ++i << _(".") << ext); } } } } + // write image + filename = fn.GetFullPath(); used.insert(filename); export_image(set, card, filename); }