works, sort of

actorid
Sebastian Wick 13 years ago
parent fbd626baf6
commit c160bc7080

@ -4,7 +4,6 @@ set(MWINIIMPORT
)
set(MWINIIMPORT_HEADER
main.hpp
importer.hpp
)

@ -1,6 +1,96 @@
#include "importer.hpp"
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
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<std::string, std::string> map;
boost::iostreams::stream<boost::iostreams::file_source>file(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<std::string,std::string>(
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<std::string, std::string> map;
boost::iostreams::stream<boost::iostreams::file_source>file(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<std::string,std::string>(
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<std::string,std::string>(
ini_it->first, ini_it->second
));
}
}
}
void MwIniImporter::writeToFile(std::string file, strmap &cfg) {
boost::iostreams::stream<boost::iostreams::file_sink> out(file);
for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) {
out << (it->first) << "=" << (it->second) << std::endl;
}
}

@ -2,14 +2,28 @@
#define MWINIIMPORTER_IMPORTER 1
#include <string>
#include <map>
#include <exception>
typedef std::map<std::string, std::string> 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;
};

@ -1,39 +1,75 @@
#include "main.hpp"
#include "importer.hpp"
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
namespace bpo = boost::program_options;
int main(int argc, char *argv[]) {
bpo::options_description desc("Syntax: mwiniimporter <options>\nAllowed options");
desc.add_options()
("help,h", "produce help message")
("in,i", bpo::value<std::string>()->required(), "morrowind.ini input file")
("out,o", bpo::value<std::string>()->required(), "openmw.cfg output file")
("verbose,v", "verbose output")
("ini,i", bpo::value<std::string>()->required(), "morrowind.ini file")
("cfg,c", bpo::value<std::string>()->required(), "openmw.cfg file")
("output,o", bpo::value<std::string>()->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>();
std::string cfgFile = vm["cfg"].as<std::string>();
// if no output is given, write back to cfg file
std::string outputFile(vm["output"].as<std::string>());
if(vm["output"].defaulted()) {
outputFile = vm["cfg"].as<std::string>();
}
std::cout << "in:" << vm["in"].as<std::string>() << 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::map<std::string, std::string>ini = importer.loadIniFile(iniFile);
std::map<std::string, std::string>cfg = importer.loadCfgFile(cfgFile);
importer.merge(cfg, ini);
std::cout << "write to: " << outputFile << std::endl;
importer.writeToFile(outputFile, cfg);
return 0;
}

@ -1,11 +0,0 @@
#ifndef MWINIIMPORTER_MAIN
#define MWINIIMPORTER_MAIN 1
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
namespace bpo = boost::program_options;
#endif
Loading…
Cancel
Save