| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
#include "XData.h" |
| 5 |
|
| 6 |
#include "parser/DefTokeniser.h" |
| 7 |
#include <map> |
| 8 |
#include "ifilesystem.h" |
| 9 |
|
| 10 |
namespace XData |
| 11 |
{ |
| 12 |
|
| 13 |
namespace |
| 14 |
{ |
| 15 |
const std::string XDATA_DIR = "xdata/"; |
| 16 |
const std::string XDATA_EXT = "xd"; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
typedef std::set<std::string> StringSet; |
| 21 |
typedef std::map<std::string, XDataPtr> XDataMap; |
| 22 |
typedef std::map<std::string, std::vector<std::string>> StringVectorMap; |
| 23 |
typedef std::map<std::string, std::string > StringMap; |
| 24 |
typedef std::vector<std::pair<std::string, std::string>> StringPairList; |
| 25 |
|
| 26 |
|
| 27 |
///////////////////////////// XDataSelector: |
| 28 |
// Class for importing XData from files and retrieving fileinfos: duplicated definitions, |
| 29 |
// List of all definitions with their corresponding filename. |
| 30 |
class XDataLoader |
| 31 |
{ |
| 32 |
// Notes: |
| 33 |
// -Importer cannot cope with multiple definitions in a single file currently. |
| 34 |
public: |
| 35 |
// Imports a MultiMap of XData-Pointers sorted by name from the specified File (just the name, not the path). |
| 36 |
// Returns false if import failed. The import-breaking error-message is the last element of _errorList, which can |
| 37 |
// be retrieved by calling getImportSummary() and also stores other errors and warnings. |
| 38 |
// -target: Key Value = definitionName, Mapped Value = XData object. |
| 39 |
const bool import(const std::string& filename, XDataMap& target); |
| 40 |
|
| 41 |
// Imports a single Definition from the specified file (filename) if defined or all duplicated definitions if only |
| 42 |
// the definitionName is specified. Returns false if import failed. The import-breaking error-message is the last |
| 43 |
// element of _errorList, which can be retrieved by calling getImportSummary() and also stores other errors and warnings. |
| 44 |
// -target: Key Value = filepath INCLUDING ModPath(!!), Mapped Value = XData object. |
| 45 |
const bool importDef(const std::string& definitionName, XDataMap& target, const std::string& filename = ""); |
| 46 |
|
| 47 |
virtual ~XDataLoader() |
| 48 |
{ |
| 49 |
_defMap.clear(); |
| 50 |
_duplicatedDefs.clear(); |
| 51 |
_fileSet.clear(); |
| 52 |
_errorList.clear(); |
| 53 |
_guiPageError.clear(); |
| 54 |
_guiPage.clear(); |
| 55 |
} |
| 56 |
|
| 57 |
//Getters: |
| 58 |
// Returns StringVector with errors and warnings of the last import process |
| 59 |
// as well as a brief summary in the last element of the vector. |
| 60 |
const StringList& getImportSummary() const |
| 61 |
{ |
| 62 |
return _errorList; |
| 63 |
} |
| 64 |
|
| 65 |
// Returns Map of duplicated definitions. (Data might be outdated, maybe use retrieveXdInfo() before) |
| 66 |
// Key Value = DefinitionNames, Mapped Value = StringVector of corresponding filenames. |
| 67 |
const StringVectorMap& getDuplicateDefinitions() const |
| 68 |
{ |
| 69 |
if (_duplicatedDefs.empty()) |
| 70 |
{ |
| 71 |
throw std::runtime_error("No Data available. Call retrieveXdInfo() before."); |
| 72 |
} |
| 73 |
|
| 74 |
return _duplicatedDefs; |
| 75 |
} |
| 76 |
|
| 77 |
// Returns StringSet of all .xd-files in the VFS. (Data might be outdated, maybe use retrieveXdInfo() before) |
| 78 |
const StringSet& getXdFilenameList() const |
| 79 |
{ |
| 80 |
if (_fileSet.empty()) |
| 81 |
throw std::runtime_error("No Data available. Call retrieveXdInfo() before."); |
| 82 |
return _fileSet; |
| 83 |
} |
| 84 |
|
| 85 |
// Returns a map of all Definitions and their corresponding filenames found in the VFS. The filenames are stored |
| 86 |
// in a vector in case a definition exists multiple times. (Data might be outdated, maybe use retrieveXdInfo() before) |
| 87 |
const StringVectorMap& getDefinitionList() const |
| 88 |
{ |
| 89 |
if (_defMap.empty()) |
| 90 |
throw std::runtime_error("No Data available. Call retrieveXdInfo() before."); |
| 91 |
return _defMap; |
| 92 |
} |
| 93 |
|
| 94 |
// Retrieves all XData-related information found in the VFS. |
| 95 |
void retrieveXdInfo(); |
| 96 |
|
| 97 |
// Functor operator: Adds all definitions found in the target file to the _defMap. |
| 98 |
void loadFromFile(const std::string& filename); |
| 99 |
|
| 100 |
private: |
| 101 |
// Issues the ErrorMessage to the cerr console and appends it to the _errorList. Returns always false, so that it can be used after a return statement. |
| 102 |
const bool reportError(const std::string& ErrorMessage) |
| 103 |
{ |
| 104 |
rConsoleError() << ErrorMessage; |
| 105 |
_errorList.push_back(ErrorMessage); |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
// Additionally to what the upper version of reportError(..) does, this method also tries to jump to the next definition by calling jumpOutOfBrackets. |
| 110 |
const bool reportError(parser::DefTokeniser& tok, const std::string& ErrorMessage, std::size_t currentDepth = 1) |
| 111 |
{ |
| 112 |
reportError(ErrorMessage); |
| 113 |
jumpOutOfBrackets(tok, currentDepth); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Opens the file in which sourceDef is contained, while handling duplicate definitions. |
| 118 |
// -statements: Key value = Statements in sourceDef, Mapped value = Statement in the calling definition to whom the imported content shall be parsed. |
| 119 |
// -defName: Name of the definition that has induced the recursiveImport. This is just used for error reporting. |
| 120 |
// -importContent: Vector of stringpairs. First = Name of the destination Statement in the calling definition, Second = Imported Content. |
| 121 |
const bool recursiveImport(const std::string& sourceDef, const StringMap& statements, const std::string& defName, StringPairList& importContent); |
| 122 |
|
| 123 |
// Parses the contents of the import-statement. |
| 124 |
const bool getImportParameters(parser::DefTokeniser& tok, StringMap& statements, std::string& sourceDef, const std::string& defName); |
| 125 |
|
| 126 |
// Checks where the content following in the tokenizer has to be stored. DefName is the name of the |
| 127 |
// definition for whom content is parsed and is just used for error-messages. If tok is NULL, the string content is stored. This |
| 128 |
// is only induced by the import-directive, which on the contrary must have tok!= NULL. |
| 129 |
const bool storeContent(const std::string& statement, parser::DefTokeniser* tok, const std::string& defName, const std::string& content = ""); |
| 130 |
|
| 131 |
// Parses a single definition from a stream into an XData object and generates warning and error messages. If definitionName |
| 132 |
// is defined, only matching definitions will be parsed and return false otherwise. |
| 133 |
const bool parseXDataDef(parser::DefTokeniser& tok, const std::string& definitionName = ""); |
| 134 |
|
| 135 |
// Skips the ":" and parses the following SingleLine or MultiLine content into What. |
| 136 |
const bool readLines(parser::DefTokeniser& tok, std::string& what) const; |
| 137 |
|
| 138 |
// Used to jump out of a definition. Can lead to undefined behavior on Syntax-errors. |
| 139 |
void jumpOutOfBrackets(parser::DefTokeniser& tok, std::size_t currentDepth = 1) const; |
| 140 |
|
| 141 |
//General Member variables: |
| 142 |
StringList _errorList; |
| 143 |
StringVectorMap _defMap; |
| 144 |
StringSet _fileSet; |
| 145 |
StringVectorMap _duplicatedDefs; |
| 146 |
|
| 147 |
//Helper-variables for import: |
| 148 |
XDataPtr _newXData; |
| 149 |
std::string _name; |
| 150 |
StringList _guiPageError; |
| 151 |
std::size_t _maxPageCount; |
| 152 |
std::size_t _maxGuiNumber; |
| 153 |
std::string _guiPageDef; |
| 154 |
std::size_t _numPages; |
| 155 |
std::string _sndPageTurn; |
| 156 |
StringList _guiPage; |
| 157 |
}; // XDataLoader class |
| 158 |
typedef std::shared_ptr<XDataLoader> XDataLoaderPtr; |
| 159 |
|
| 160 |
} // namespace XData |