forked from teamnwah/openmw-tes3coop
use std::map<std::string, std::vector<std::string> > instead of std::map<std::string, std::string>
This commit is contained in:
parent
6d875dfd54
commit
1d596d6c72
3 changed files with 78 additions and 56 deletions
|
@ -23,11 +23,11 @@ void MwIniImporter::setVerbose(bool verbose) {
|
||||||
mVerbose = 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::cout << "load ini file: " << filename << std::endl;
|
||||||
|
|
||||||
std::string section("");
|
std::string section("");
|
||||||
std::map<std::string, std::string> map;
|
MwIniImporter::multistrmap map;
|
||||||
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
|
@ -54,16 +54,23 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
map.insert(std::make_pair<std::string, std::string>(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<std::string, std::vector<std::string> > (key, std::vector<std::string>() ) );
|
||||||
|
}
|
||||||
|
map[key].push_back(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
strmap MwIniImporter::loadCfgFile(std::string filename) {
|
MwIniImporter::multistrmap MwIniImporter::loadCfgFile(std::string filename) {
|
||||||
std::cout << "load cfg file: " << filename << std::endl;
|
std::cout << "load cfg file: " << filename << std::endl;
|
||||||
|
|
||||||
std::map<std::string, std::string> map;
|
MwIniImporter::multistrmap map;
|
||||||
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
|
@ -84,34 +91,43 @@ strmap MwIniImporter::loadCfgFile(std::string filename) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
map.insert(std::make_pair<std::string, std::string>(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<std::string, std::vector<std::string> > (key, std::vector<std::string>() ) );
|
||||||
|
}
|
||||||
|
map[key].push_back(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MwIniImporter::merge(strmap &cfg, strmap &ini) {
|
void MwIniImporter::merge(multistrmap &cfg, multistrmap &ini) {
|
||||||
strmap::iterator cfgIt;
|
multistrmap::iterator cfgIt;
|
||||||
strmap::iterator iniIt;
|
multistrmap::iterator iniIt;
|
||||||
for(strmap::iterator it=mMergeMap.begin(); it!=mMergeMap.end(); it++) {
|
for(strmap::iterator it=mMergeMap.begin(); it!=mMergeMap.end(); it++) {
|
||||||
if((iniIt = ini.find(it->second)) != ini.end()) {
|
if((iniIt = ini.find(it->second)) != ini.end()) {
|
||||||
cfg.erase(it->first);
|
cfg.erase(it->first);
|
||||||
if(!this->specialMerge(it->first, it->second, cfg, ini)) {
|
if(!this->specialMerge(it->first, it->second, cfg, ini)) {
|
||||||
cfg.insert(std::make_pair<std::string, std::string>(it->first, iniIt->second));
|
cfg.insert(std::make_pair<std::string, std::vector<std::string> >(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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles) {
|
void MwIniImporter::importGameFiles(multistrmap &cfg, multistrmap &ini) {
|
||||||
|
std::vector<std::string> esmFiles;
|
||||||
|
std::vector<std::string> espFiles;
|
||||||
std::string baseGameFile("Game Files:GameFile");
|
std::string baseGameFile("Game Files:GameFile");
|
||||||
std::string gameFile("");
|
std::string gameFile("");
|
||||||
|
|
||||||
strmap::iterator it = ini.begin();
|
multistrmap::iterator it = ini.begin();
|
||||||
for(int i=0; it != ini.end(); i++) {
|
for(int i=0; it != ini.end(); i++) {
|
||||||
gameFile = baseGameFile;
|
gameFile = baseGameFile;
|
||||||
gameFile.append(1,i+'0');
|
gameFile.append(1,i+'0');
|
||||||
|
@ -121,35 +137,48 @@ void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini, std::vector<std::s
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filetype(it->second.substr(it->second.length()-4, 3));
|
for(std::vector<std::string>::iterator entry = it->second.begin(); entry!=it->second.end(); entry++) {
|
||||||
std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
|
std::string filetype(entry->substr(entry->length()-4, 3));
|
||||||
|
std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
|
||||||
|
|
||||||
if(filetype.compare("esm") == 0) {
|
if(filetype.compare("esm") == 0) {
|
||||||
esmFiles.push_back(it->second);
|
esmFiles.push_back(*entry);
|
||||||
}
|
}
|
||||||
else if(filetype.compare("esp") == 0) {
|
else if(filetype.compare("esp") == 0) {
|
||||||
espFiles.push_back(it->second);
|
espFiles.push_back(*entry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gameFile = "";
|
gameFile = "";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void MwIniImporter::writeGameFiles(boost::iostreams::stream<boost::iostreams::file_sink> &out, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles) {
|
if(!esmFiles.empty()) {
|
||||||
for(std::vector<std::string>::iterator it=esmFiles.begin(); it != esmFiles.end(); it++) {
|
multistrmap::iterator it;
|
||||||
out << "master=" << *it << std::endl;
|
cfg.erase("master");
|
||||||
|
cfg.insert( std::make_pair<std::string, std::vector<std::string> > ("master", std::vector<std::string>() ) );
|
||||||
|
|
||||||
|
for(std::vector<std::string>::iterator it=esmFiles.begin(); it!=esmFiles.end(); it++) {
|
||||||
|
cfg["master"].push_back(*it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for(std::vector<std::string>::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<std::string, std::vector<std::string> > ("plugin", std::vector<std::string>() ) );
|
||||||
|
|
||||||
|
for(std::vector<std::string>::iterator it=espFiles.begin(); it!=espFiles.end(); it++) {
|
||||||
|
cfg["plugin"].push_back(*it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MwIniImporter::writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, strmap &cfg) {
|
void MwIniImporter::writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, multistrmap &cfg) {
|
||||||
cfg.erase("master");
|
|
||||||
cfg.erase("plugin");
|
|
||||||
|
|
||||||
for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) {
|
for(multistrmap::iterator it=cfg.begin(); it != cfg.end(); it++) {
|
||||||
out << (it->first) << "=" << (it->second) << std::endl;
|
for(std::vector<std::string>::iterator entry=it->second.begin(); entry != it->second.end(); entry++) {
|
||||||
|
out << (it->first) << "=" << (*entry) << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,23 +8,21 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> strmap;
|
|
||||||
|
|
||||||
class MwIniImporter {
|
class MwIniImporter {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
typedef std::map<std::string, std::string> strmap;
|
||||||
|
typedef std::map<std::string, std::vector<std::string> > multistrmap;
|
||||||
|
|
||||||
MwIniImporter();
|
MwIniImporter();
|
||||||
void setVerbose(bool verbose);
|
void setVerbose(bool verbose);
|
||||||
strmap loadIniFile(std::string filename);
|
multistrmap loadIniFile(std::string filename);
|
||||||
strmap loadCfgFile(std::string filename);
|
multistrmap loadCfgFile(std::string filename);
|
||||||
void merge(strmap &cfg, strmap &ini);
|
void merge(multistrmap &cfg, multistrmap &ini);
|
||||||
void importGameFiles(strmap &cfg, strmap &ini, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles);
|
void importGameFiles(multistrmap &cfg, multistrmap &ini);
|
||||||
void writeGameFiles(boost::iostreams::stream<boost::iostreams::file_sink> &out, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles);
|
void writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, multistrmap &cfg);
|
||||||
void writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, strmap &cfg);
|
|
||||||
|
|
||||||
private:
|
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;
|
bool mVerbose;
|
||||||
strmap mMergeMap;
|
strmap mMergeMap;
|
||||||
};
|
};
|
||||||
|
|
|
@ -59,22 +59,17 @@ int main(int argc, char *argv[]) {
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MwIniImporter importer;
|
MwIniImporter importer;
|
||||||
importer.setVerbose(vm.count("verbose"));
|
importer.setVerbose(vm.count("verbose"));
|
||||||
boost::iostreams::stream<boost::iostreams::file_sink> file(outputFile);
|
boost::iostreams::stream<boost::iostreams::file_sink> file(outputFile);
|
||||||
|
|
||||||
std::map<std::string, std::string>ini = importer.loadIniFile(iniFile);
|
MwIniImporter::multistrmap ini = importer.loadIniFile(iniFile);
|
||||||
std::map<std::string, std::string>cfg = importer.loadCfgFile(cfgFile);
|
MwIniImporter::multistrmap cfg = importer.loadCfgFile(cfgFile);
|
||||||
|
|
||||||
importer.merge(cfg, ini);
|
importer.merge(cfg, ini);
|
||||||
|
|
||||||
if(vm.count("game-files")) {
|
if(vm.count("game-files")) {
|
||||||
std::vector<std::string> esmFiles;
|
importer.importGameFiles(cfg, ini);
|
||||||
std::vector<std::string> espFiles;
|
|
||||||
|
|
||||||
importer.importGameFiles(cfg, ini, esmFiles, espFiles);
|
|
||||||
importer.writeGameFiles(file, esmFiles, espFiles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "write to: " << outputFile << std::endl;
|
std::cout << "write to: " << outputFile << std::endl;
|
||||||
|
|
Loading…
Reference in a new issue