diff --git a/apps/mwiniimporter/importer.cpp b/apps/mwiniimporter/importer.cpp index 937073632d..f7ddb2bf07 100644 --- a/apps/mwiniimporter/importer.cpp +++ b/apps/mwiniimporter/importer.cpp @@ -23,11 +23,11 @@ void MwIniImporter::setVerbose(bool verbose) { mVerbose = verbose; } -strmap MwIniImporter::loadIniFile(std::string filename) { +MwIniImporter::multistrmap MwIniImporter::loadIniFile(std::string filename) { std::cout << "load ini file: " << filename << std::endl; std::string section(""); - std::map map; + MwIniImporter::multistrmap map; boost::iostreams::streamfile(filename.c_str()); std::string line; @@ -54,16 +54,23 @@ strmap MwIniImporter::loadIniFile(std::string filename) { continue; } - map.insert(std::make_pair(section + ":" + line.substr(0,pos), line.substr(pos+1))); + std::string key(section + ":" + line.substr(0,pos)); + std::string value(line.substr(pos+1)); + + multistrmap::iterator it; + if((it = map.find(key)) == map.end()) { + map.insert( std::make_pair > (key, std::vector() ) ); + } + map[key].push_back(value); } return map; } -strmap MwIniImporter::loadCfgFile(std::string filename) { +MwIniImporter::multistrmap MwIniImporter::loadCfgFile(std::string filename) { std::cout << "load cfg file: " << filename << std::endl; - std::map map; + MwIniImporter::multistrmap map; boost::iostreams::streamfile(filename.c_str()); std::string line; @@ -84,34 +91,43 @@ strmap MwIniImporter::loadCfgFile(std::string filename) { continue; } - map.insert(std::make_pair(line.substr(0,pos), line.substr(pos+1))); + std::string key(line.substr(0,pos)); + std::string value(line.substr(pos+1)); + + multistrmap::iterator it; + if((it = map.find(key)) == map.end()) { + map.insert( std::make_pair > (key, std::vector() ) ); + } + map[key].push_back(value); } return map; } -void MwIniImporter::merge(strmap &cfg, strmap &ini) { - strmap::iterator cfgIt; - strmap::iterator iniIt; +void MwIniImporter::merge(multistrmap &cfg, multistrmap &ini) { + multistrmap::iterator cfgIt; + multistrmap::iterator iniIt; for(strmap::iterator it=mMergeMap.begin(); it!=mMergeMap.end(); it++) { if((iniIt = ini.find(it->second)) != ini.end()) { cfg.erase(it->first); if(!this->specialMerge(it->first, it->second, cfg, ini)) { - cfg.insert(std::make_pair(it->first, iniIt->second)); + cfg.insert(std::make_pair >(it->first, iniIt->second)); } } } } -bool MwIniImporter::specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini) { +bool MwIniImporter::specialMerge(std::string cfgKey, std::string iniKey, multistrmap &cfg, multistrmap &ini) { return false; } -void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini, std::vector &esmFiles, std::vector &espFiles) { +void MwIniImporter::importGameFiles(multistrmap &cfg, multistrmap &ini) { + std::vector esmFiles; + std::vector espFiles; std::string baseGameFile("Game Files:GameFile"); std::string gameFile(""); - strmap::iterator it = ini.begin(); + multistrmap::iterator it = ini.begin(); for(int i=0; it != ini.end(); i++) { gameFile = baseGameFile; gameFile.append(1,i+'0'); @@ -121,35 +137,48 @@ void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini, std::vectorsecond.substr(it->second.length()-4, 3)); - std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower); - - if(filetype.compare("esm") == 0) { - esmFiles.push_back(it->second); - } - else if(filetype.compare("esp") == 0) { - espFiles.push_back(it->second); + for(std::vector::iterator entry = it->second.begin(); entry!=it->second.end(); entry++) { + std::string filetype(entry->substr(entry->length()-4, 3)); + std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower); + + if(filetype.compare("esm") == 0) { + esmFiles.push_back(*entry); + } + else if(filetype.compare("esp") == 0) { + espFiles.push_back(*entry); + } } gameFile = ""; } -} - -void MwIniImporter::writeGameFiles(boost::iostreams::stream &out, std::vector &esmFiles, std::vector &espFiles) { - for(std::vector::iterator it=esmFiles.begin(); it != esmFiles.end(); it++) { - out << "master=" << *it << std::endl; + + if(!esmFiles.empty()) { + multistrmap::iterator it; + cfg.erase("master"); + cfg.insert( std::make_pair > ("master", std::vector() ) ); + + for(std::vector::iterator it=esmFiles.begin(); it!=esmFiles.end(); it++) { + cfg["master"].push_back(*it); + } } - for(std::vector::iterator it=espFiles.begin(); it != espFiles.end(); it++) { - out << "plugin=" << *it << std::endl; + + if(!espFiles.empty()) { + multistrmap::iterator it; + cfg.erase("plugin"); + cfg.insert( std::make_pair > ("plugin", std::vector() ) ); + + for(std::vector::iterator it=espFiles.begin(); it!=espFiles.end(); it++) { + cfg["plugin"].push_back(*it); + } } } -void MwIniImporter::writeToFile(boost::iostreams::stream &out, strmap &cfg) { - cfg.erase("master"); - cfg.erase("plugin"); +void MwIniImporter::writeToFile(boost::iostreams::stream &out, multistrmap &cfg) { - for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) { - out << (it->first) << "=" << (it->second) << std::endl; + for(multistrmap::iterator it=cfg.begin(); it != cfg.end(); it++) { + for(std::vector::iterator entry=it->second.begin(); entry != it->second.end(); entry++) { + out << (it->first) << "=" << (*entry) << std::endl; + } } } diff --git a/apps/mwiniimporter/importer.hpp b/apps/mwiniimporter/importer.hpp index d7250f5e25..454dc209af 100644 --- a/apps/mwiniimporter/importer.hpp +++ b/apps/mwiniimporter/importer.hpp @@ -8,23 +8,21 @@ #include #include - -typedef std::map strmap; - class MwIniImporter { - public: + typedef std::map strmap; + typedef std::map > multistrmap; + MwIniImporter(); void setVerbose(bool verbose); - strmap loadIniFile(std::string filename); - strmap loadCfgFile(std::string filename); - void merge(strmap &cfg, strmap &ini); - void importGameFiles(strmap &cfg, strmap &ini, std::vector &esmFiles, std::vector &espFiles); - void writeGameFiles(boost::iostreams::stream &out, std::vector &esmFiles, std::vector &espFiles); - void writeToFile(boost::iostreams::stream &out, strmap &cfg); + multistrmap loadIniFile(std::string filename); + multistrmap loadCfgFile(std::string filename); + void merge(multistrmap &cfg, multistrmap &ini); + void importGameFiles(multistrmap &cfg, multistrmap &ini); + void writeToFile(boost::iostreams::stream &out, multistrmap &cfg); private: - bool specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini); + bool specialMerge(std::string cfgKey, std::string iniKey, multistrmap &cfg, multistrmap &ini); bool mVerbose; strmap mMergeMap; }; diff --git a/apps/mwiniimporter/main.cpp b/apps/mwiniimporter/main.cpp index 7426e71ea0..9a6e61645d 100644 --- a/apps/mwiniimporter/main.cpp +++ b/apps/mwiniimporter/main.cpp @@ -59,22 +59,17 @@ int main(int argc, char *argv[]) { return -4; } - MwIniImporter importer; importer.setVerbose(vm.count("verbose")); boost::iostreams::stream file(outputFile); - std::mapini = importer.loadIniFile(iniFile); - std::mapcfg = importer.loadCfgFile(cfgFile); + MwIniImporter::multistrmap ini = importer.loadIniFile(iniFile); + MwIniImporter::multistrmap cfg = importer.loadCfgFile(cfgFile); importer.merge(cfg, ini); if(vm.count("game-files")) { - std::vector esmFiles; - std::vector espFiles; - - importer.importGameFiles(cfg, ini, esmFiles, espFiles); - importer.writeGameFiles(file, esmFiles, espFiles); + importer.importGameFiles(cfg, ini); } std::cout << "write to: " << outputFile << std::endl;