From c160bc708091039cccdea53bc584704713193852 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Fri, 30 Mar 2012 22:58:54 +0200 Subject: [PATCH] works, sort of --- apps/mwiniimporter/CMakeLists.txt | 1 - apps/mwiniimporter/importer.cpp | 94 ++++++++++++++++++++++++++++++- apps/mwiniimporter/importer.hpp | 18 +++++- apps/mwiniimporter/main.cpp | 58 +++++++++++++++---- apps/mwiniimporter/main.hpp | 11 ---- 5 files changed, 155 insertions(+), 27 deletions(-) delete mode 100644 apps/mwiniimporter/main.hpp diff --git a/apps/mwiniimporter/CMakeLists.txt b/apps/mwiniimporter/CMakeLists.txt index 1d7b7b624c..2a8c0f5fea 100644 --- a/apps/mwiniimporter/CMakeLists.txt +++ b/apps/mwiniimporter/CMakeLists.txt @@ -4,7 +4,6 @@ set(MWINIIMPORT ) set(MWINIIMPORT_HEADER - main.hpp importer.hpp ) diff --git a/apps/mwiniimporter/importer.cpp b/apps/mwiniimporter/importer.cpp index 225b1667c4..d0cfe4a04e 100644 --- a/apps/mwiniimporter/importer.cpp +++ b/apps/mwiniimporter/importer.cpp @@ -1,6 +1,96 @@ #include "importer.hpp" +#include +#include +#include -void MwIniImporter::test() { - +void MwIniImporter::setVerbose(bool verbose) { + mVerbose = verbose; } +strmap MwIniImporter::loadIniFile(std::string filename) { + std::cout << "load ini file: " << filename << std::endl; + + std::map map; + boost::iostreams::streamfile(filename.c_str()); + + std::string line; + while (std::getline(file, line)) { + + // ignore sections for now + if(line.empty() || line[0] == ';' || line[0] == '[') { + continue; + } + + int pos = line.find("="); + if(pos < 1) { + throw IniParseException(); + } + + map.insert(std::pair( + line.substr(0,pos), line.substr(pos+1) + )); + } + + return map; +} + +strmap MwIniImporter::loadCfgFile(std::string filename) { + std::cout << "load cfg file: " << filename << std::endl; + + std::map map; + boost::iostreams::streamfile(filename.c_str()); + + std::string line; + while (std::getline(file, line)) { + + if(line[0] == '[') { // section + continue; // ignore for now + } + + // we cant say comment by only looking at first char anymore + int comment_pos = line.find("#"); + if(comment_pos > 0) { + line = line.substr(0,comment_pos); + } + + if(line.empty()) { + continue; + } + + int pos = line.find("="); + if(pos < 1) { + throw IniParseException(); + } + + map.insert(std::pair( + line.substr(0,pos), line.substr(pos+1) + )); + } + + return map; +} + +void MwIniImporter::merge(strmap &cfg, strmap &ini) { + strmap::iterator ini_it; + for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) { + ini_it = ini.find(it->first); + + // found a key in both files + if(ini_it != ini.end()) { + cfg.erase(it); + cfg.insert(std::pair( + ini_it->first, ini_it->second + )); + } + } +} + +void MwIniImporter::writeToFile(std::string file, strmap &cfg) { + boost::iostreams::stream out(file); + + for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) { + out << (it->first) << "=" << (it->second) << std::endl; + } +} + + diff --git a/apps/mwiniimporter/importer.hpp b/apps/mwiniimporter/importer.hpp index 026ed55bf9..1933830f21 100644 --- a/apps/mwiniimporter/importer.hpp +++ b/apps/mwiniimporter/importer.hpp @@ -2,14 +2,28 @@ #define MWINIIMPORTER_IMPORTER 1 #include +#include +#include + +typedef std::map strmap; + +class IniParseException : public std::exception { + virtual const char* what() const throw() { + return "unexpected end of line"; + } +}; class MwIniImporter { public: - void test(); + void setVerbose(bool verbose); + strmap loadIniFile(std::string filename); + strmap loadCfgFile(std::string filename); + void merge(strmap &cfg, strmap &ini); + void writeToFile(std::string file, strmap &cfg); private: - + bool mVerbose; }; diff --git a/apps/mwiniimporter/main.cpp b/apps/mwiniimporter/main.cpp index 46cb37e7f4..fa7a5c512f 100644 --- a/apps/mwiniimporter/main.cpp +++ b/apps/mwiniimporter/main.cpp @@ -1,39 +1,75 @@ -#include "main.hpp" #include "importer.hpp" +#include +#include +#include +#include + +namespace bpo = boost::program_options; + int main(int argc, char *argv[]) { bpo::options_description desc("Syntax: mwiniimporter \nAllowed options"); desc.add_options() ("help,h", "produce help message") - ("in,i", bpo::value()->required(), "morrowind.ini input file") - ("out,o", bpo::value()->required(), "openmw.cfg output file") + ("verbose,v", "verbose output") + ("ini,i", bpo::value()->required(), "morrowind.ini file") + ("cfg,c", bpo::value()->required(), "openmw.cfg file") + ("output,o", bpo::value()->default_value(""), "openmw.cfg file") ; bpo::variables_map vm; try { bpo::store(boost::program_options::parse_command_line(argc, argv, desc), vm); + + // parse help before calling notify because we dont want it to throw an error if help is set + if(vm.count("help")) { + std::cout << desc; + return 0; + } + bpo::notify(vm); } catch(std::exception& e) { - std::cout << "Error:" << e.what() << std::endl; + std::cerr << "Error:" << e.what() << std::endl; return -1; } catch(...) { - std::cout << "Error" << std::endl; - return -1; + std::cerr << "Error" << std::endl; + return -2; } - if(vm.count("help")) { - std::cout << desc; - return 0; + std::string iniFile = vm["ini"].as(); + std::string cfgFile = vm["cfg"].as(); + + // if no output is given, write back to cfg file + std::string outputFile(vm["output"].as()); + if(vm["output"].defaulted()) { + outputFile = vm["cfg"].as(); } - std::cout << "in:" << vm["in"].as() << std::endl; + if(!boost::filesystem::exists(iniFile)) { + std::cerr << "ini file does not exist" << std::endl; + return -3; + } + if(!boost::filesystem::exists(cfgFile)) { + std::cerr << "cfg file does not exist" << std::endl; + return -4; + } MwIniImporter importer; - importer.test(); + importer.setVerbose(vm.count("verbose")); + + std::mapini = importer.loadIniFile(iniFile); + std::mapcfg = importer.loadCfgFile(cfgFile); + + importer.merge(cfg, ini); + + std::cout << "write to: " << outputFile << std::endl; + importer.writeToFile(outputFile, cfg); return 0; } + + diff --git a/apps/mwiniimporter/main.hpp b/apps/mwiniimporter/main.hpp deleted file mode 100644 index f93de7b078..0000000000 --- a/apps/mwiniimporter/main.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef MWINIIMPORTER_MAIN -#define MWINIIMPORTER_MAIN 1 - -#include -#include -#include - -namespace bpo = boost::program_options; - - -#endif