Make extension optional in --export command.

Added add_extension function
Added ExportTemplate::byName
Use ExportTemplate::byName in main
This commit is contained in:
Twan van Laarhoven
2020-04-21 00:23:42 +02:00
parent 59d59e97fd
commit e8e7e10fa0
5 changed files with 25 additions and 3 deletions
+5
View File
@@ -11,6 +11,7 @@
#include <data/game.hpp>
#include <data/set.hpp>
#include <data/field.hpp>
#include <util/io/package_manager.hpp>
// ----------------------------------------------------------------------------- : Export template, basics
@@ -19,6 +20,10 @@ ExportTemplate::ExportTemplate()
, create_directory(false)
{}
ExportTemplateP ExportTemplate::byName(const String& name) {
return package_manager.open<ExportTemplate>(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; }
+4 -2
View File
@@ -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();
};
+1 -1
View File
@@ -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<ExportTemplate>(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);
+8
View File
@@ -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 == _('.');
}
+7
View File
@@ -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);