From e8e7e10fa08a77839066f26308e14d2935c2e8f8 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Tue, 21 Apr 2020 00:23:42 +0200 Subject: [PATCH] Make extension optional in --export command. Added add_extension function Added ExportTemplate::byName Use ExportTemplate::byName in main --- src/data/export_template.cpp | 5 +++++ src/data/export_template.hpp | 6 ++++-- src/main.cpp | 2 +- src/util/file_utils.cpp | 8 ++++++++ src/util/file_utils.hpp | 7 +++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/data/export_template.cpp b/src/data/export_template.cpp index 4de192fd..25b9b409 100644 --- a/src/data/export_template.cpp +++ b/src/data/export_template.cpp @@ -11,6 +11,7 @@ #include #include #include +#include // ----------------------------------------------------------------------------- : Export template, basics @@ -19,6 +20,10 @@ ExportTemplate::ExportTemplate() , create_directory(false) {} +ExportTemplateP ExportTemplate::byName(const String& name) { + return package_manager.open(add_extension(name, _(".mse-export-template"))); +} + String ExportTemplate::typeNameStatic() { return _("export-template"); } String ExportTemplate::typeName() const { return _("export-template"); } Version ExportTemplate::fileVersion() const { return file_version_export_template; } diff --git a/src/data/export_template.hpp b/src/data/export_template.hpp index f7abb220..042369cd 100644 --- a/src/data/export_template.hpp +++ b/src/data/export_template.hpp @@ -24,7 +24,7 @@ DECLARE_POINTER_TYPE(Package); /// A template for exporting sets to HTML or text format class ExportTemplate : public Packaged { - public: +public: ExportTemplate(); GameP game; ///< Game this template is for @@ -38,7 +38,9 @@ class ExportTemplate : public Packaged { virtual String typeName() const; Version fileVersion() const; virtual void validate(Version = app_version); - private: + /// Loads the export template with a particular name + static ExportTemplateP byName(const String & name); +private: DECLARE_REFLECTION(); }; diff --git a/src/main.cpp b/src/main.cpp index 8e7e0e2c..5ab378a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -258,7 +258,7 @@ int MSE::OnRun() { throw Error(_("No input set file specified for --export")); } String export_template = args[1]; - ExportTemplateP exp = package_manager.open(export_template); + ExportTemplateP exp = ExportTemplate::byName(export_template); SetP set = import_set(args[2]); String out = args.size() >= 4 ? args[3] : _(""); ScriptValueP result = export_set(set, set->cards, exp, out); diff --git a/src/util/file_utils.cpp b/src/util/file_utils.cpp index 72b07bb9..a814697e 100644 --- a/src/util/file_utils.cpp +++ b/src/util/file_utils.cpp @@ -38,6 +38,14 @@ bool ignore_file(const String& name) { return name == _("Thumbs.db"); // winXP explorer thumbnails } +String add_extension(const String& filename, String const& extension) { + if (extension.size() <= filename.size() && is_substr(filename, filename.size() - extension.size(), extension)) { + return filename; + } else { + return filename + extension; + } +} + bool is_filename_char(Char c) { return isAlnum(c) || c == _(' ') || c == _('_') || c == _('-') || c == _('.'); } diff --git a/src/util/file_utils.hpp b/src/util/file_utils.hpp index 640f424a..d6b4ff56 100644 --- a/src/util/file_utils.hpp +++ b/src/util/file_utils.hpp @@ -25,6 +25,13 @@ String normalize_internal_filename(const String& filename); /** true for hidden OS and version control files */ bool ignore_file(const String& name); +/// Add an extension to a filename if it is not already present +/** add_extension("test",".txt") == "test.txt" + * add_extension("test.txt",".txt") == "test.txt" + * add_extension("test.xyz",".txt") == "test.xyz.txt" + */ +String add_extension(const String& filename, String const& extension); + /// Make sure a string is safe to use as a filename String clean_filename(const String& name);