| 7 | { |
| 8 | |
| 9 | const bool XDataLoader::importDef( const std::string& definitionName, XDataMap& target, const std::string& filename ) |
| 10 | { |
| 11 | // Initialization: |
| 12 | _errorList.clear(); |
| 13 | _newXData.reset(); |
| 14 | target.clear(); |
| 15 | StringList files; |
| 16 | |
| 17 | if (filename != "") |
| 18 | { |
| 19 | //Check fileextension: |
| 20 | if (filename.substr( filename.rfind(".")+1 ) != "xd") |
| 21 | return reportError("[XDataLoader::import] Error: File-extension is not .xd: " + filename + "\n"); |
| 22 | files.push_back(filename); |
| 23 | } |
| 24 | else |
| 25 | { |
| 26 | //Find the files in which the definition can be found. |
| 27 | retrieveXdInfo(); |
| 28 | StringVectorMap::iterator it = _defMap.find(definitionName); |
| 29 | if (it == _defMap.end()) |
| 30 | return reportError("[XDataLoader::importDef] Error: Specified definition " + definitionName + " not found.\n"); |
| 31 | files = it->second; |
| 32 | if (files.size() > 1) //Definition contained in multiple files. |
| 33 | reportError("[XData::import] Warning: The requested definition " + definitionName + " exists in multiple files.\n"); |
| 34 | } |
| 35 | |
| 36 | // Parse the requested definition from all files: |
| 37 | for (std::size_t n = 0; n < files.size(); n++) |
| 38 | { |
| 39 | // Attempt to open the file in text mode and retrieve DefTokeniser |
| 40 | ArchiveTextFilePtr file = |
| 41 | GlobalFileSystem().openTextFile(files[n]); |
| 42 | |
| 43 | if (file == NULL) |
| 44 | return reportError("[XDataLoader::importDef] Error: Failed to open file " + files[n] + "\n"); |
| 45 | std::istream is(&(file->getInputStream())); |
| 46 | parser::BasicDefTokeniser<std::istream> tok(is); |
| 47 | |
| 48 | // Parse the desired definition: |
| 49 | while (tok.hasMoreTokens() && !parseXDataDef(tok,definitionName)) {} |
| 50 | if (_newXData) |
| 51 | target.insert(XDataMap::value_type(file->getModName() + "/" + file->getName(),_newXData)); |
| 52 | else |
| 53 | reportError("[XDataLoader::importDef] Error: Failed to load " + definitionName + " from file " + files[n] + ".\n"); |
| 54 | } |
| 55 | |
| 56 | //Summarizing report: |
| 57 | if (target.size() == 0) |
| 58 | { |
| 59 | if (filename == "") |
| 60 | reportError("[XDataLoader::importDef] Error: Failed to load " + definitionName + ".\n"); |
| 61 | return false; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | _errorList.push_back("[XDataLoader::importDef] " + definitionName + " loaded successfully with " |
| 66 | + string::to_string(_errorList.size()) + " error(s)/warning(s).\n"); |
no test coverage detected