Commit 70ac30cc authored by twanvl's avatar twanvl

file/import/export formats

parent 85133302
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <data/format/formats.hpp>
#include <data/set.hpp>
DECLARE_POINTER_TYPE(FileFormat);
DECLARE_TYPEOF_COLLECTION(FileFormatP);
// ----------------------------------------------------------------------------- : Formats
// All supported file formats
vector<FileFormatP> fileFormats;
void initFileFormats() {
//fileFormats.push_back(new_shared<MSE2FileFilter>());
//fileFormats.push_back(new_shared<MSE1FileFilter>());
//fileFormats.push_back(new_shared<MtgEditorFileFilter>());
}
String importFormats() {
String allExtensions; // type1;type2
String typeStrings; // |name1|type1|name2|type2
FOR_EACH(f, fileFormats) {
if (f->canImport()) {
if (!allExtensions.empty()) allExtensions += _(";");
allExtensions += _("*.") + f->extension();
typeStrings += _("|") + f->name() + _("|*.") + f->extension();
}
}
return _("Set files|") + allExtensions + typeStrings + _("|All files (*.*)|*.*");
}
String exportFormats(const Game& game) {
String typeStrings; // name1|type1|name2|type2
FOR_EACH(f, fileFormats) {
if (f->canExport(game)) {
if (!typeStrings.empty()) typeStrings += _("|");
typeStrings += f->name() + _("|*.") + f->extension();
}
}
return typeStrings;
}
void exportSet(const Set& set, const String& filename, size_t formatType) {
FileFormatP format = fileFormats.at(formatType);
if (!format->canExport(*set.game)) {
throw InternalError(_("File format doesn't apply to set"));
}
format->exportSet(set, filename);
}
SetP importSet(String name) {
size_t pos = name.find_last_of(_('.'));
String extension = pos==String::npos ? _("") : name.substr(pos + 1);
// determine format
FOR_EACH(f, fileFormats) {
if (f->extension() == extension) {
return f->importSet(name);
}
}
// default : use first format = MSE2 format
assert(!fileFormats.empty() && fileFormats[0]->canImport());
return fileFormats[0]->importSet(name);
}
\ No newline at end of file
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_DATA_FORMAT_FORMATS
#define HEADER_DATA_FORMAT_FORMATS
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/error.hpp>
class Game;
DECLARE_POINTER_TYPE(Set);
// ----------------------------------------------------------------------------- : FileFormat
/// A filter for a specific file format
class FileFormat {
public:
/// File extension used by this file format
virtual String extension() = 0;
/// Name of the filter
virtual String name() = 0;
/// Can it be used for importing sets?
virtual bool canImport() = 0;
/// Can it be used for exporting sets for a particular game?
virtual bool canExport(const Game&) = 0;
/// Import using this filter
virtual SetP importSet(const String& filename) {
throw InternalError(_("Import not supported by this file format"));
}
/// Export using this filter
virtual void exportSet(const Set& set, const String& filename) {
throw InternalError(_("Export not supported by this file format"));
}
};
// ----------------------------------------------------------------------------- : Formats
/// Initialize the list of file formats
/** Must be called before any other methods of this header */
void initFileFormats();
/// List of supported import formats
/** Formated as _("All supported (type1,...)|type1,...|name|type|...|All files(*.*)|*.*").
* For use in file selection dialogs.
*/
String importFormats();
// List of supported export formats that a set in a specific game can be exported.
/** Similair format as importFormats, except for _('all supported') and _('all files')
*/
String exportFormats(const Game& game);
/// Opens a set with the specified filename.
/** File format is chosen based on the extension, default is fileFormats[0]
* (which is the MSE2 file filter)
* throws on error, always returns a valid set
*
* NOTE: String parameter must be passed by valueso we get a copy, otherwise
* changing the recent set list could change the filename while we are opening it
* (which would be bad)
*/
SetP importSet(String name);
/// Save a set under the specified name.
/** filterType specifies what format to use for saving, used as index in the list of file formats
*/
void exportSet(const Set& set, const String& filename, size_t formatType);
// ----------------------------------------------------------------------------- : Export
// ----------------------------------------------------------------------------- : EOF
#endif
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