diff --git a/include/oopless-parser.hpp b/include/oopless-parser.hpp index c93856d..2677d60 100644 --- a/include/oopless-parser.hpp +++ b/include/oopless-parser.hpp @@ -25,12 +25,7 @@ void write(const std::vector &, void (*f) (std::stringstream &, const std::vector &), EntityConverter &); -void parse_prefab(std::ifstream &, - std::ofstream &, - void (*f) (std::stringstream &, const std::vector &), - std::vector > &); - -bool convertmap(const char * const, +bool convertmap(std::stringstream &, const char * const, void (*f) (std::stringstream &, const std::vector &), std::vector > &); @@ -180,4 +175,19 @@ using namespace std; return output; } -#endif \ No newline at end of file +template +void parse_prefab(ISTR &f, + std::ofstream &out, + void (*b) (std::stringstream &, const std::vector &), + std::vector > &entities) { + using namespace std; + string l; + getline(f, l); + if (l.find(KEYWORD_BRUSH) != string::npos) { + write(parse_brush(f), out, b); + } else if (l.find(KEYWORD_ENTITY) != string::npos) { + entities.push_back(get_entity(f)); + } +} + +#endif diff --git a/src/main.cpp b/src/main.cpp index 2830e00..529e0b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,6 +57,22 @@ cxxopts::Options arguments(int ac, char ** av) { return o; } +stringstream loadmap(const char *filename) { + ifstream fin; + fin.open(filename); + if (!fin.good()) { + cout << "error: can not open input file." << endl; + } + vector v; + string line; + stringstream output; + while (getline(fin, line)) { + v.push_back(line); + } + move(v.begin(), v.end(), ostream_iterator(output, "\n")); + return output; +} + bool convert_worldspawn(const cxxopts::Options &o, vector > &q) { bool is_ok = false; @@ -64,9 +80,11 @@ bool convert_worldspawn(const cxxopts::Options &o, if (o.count(ARG_BRUSHFORMAT)) { fn = &brushdef_gtk; } + string inputfile = o[ARG_INPUT_SHORTALIAS].as(); + stringstream mapdata = loadmap(inputfile.c_str()); try { is_ok = convertmap( - o[ARG_INPUT_SHORTALIAS].as().c_str(), // in file + mapdata, // in file o[ARG_OUTPUT_SHORTALIAS].as().c_str(), // out file fn, // brush definition q); // queue of entities diff --git a/src/oopless-parser.cpp b/src/oopless-parser.cpp index a196697..4ce5cf8 100644 --- a/src/oopless-parser.cpp +++ b/src/oopless-parser.cpp @@ -101,29 +101,10 @@ void write(const vector &entity, fo << "}" << endl; } -void parse_prefab(ifstream &f, - ofstream &out, - void (*b) (stringstream &, const vector &), - vector > &entities) { - string l; - getline(f, l); - if (l.find(KEYWORD_BRUSH) != string::npos) { - write(parse_brush(f), out, b); - } else if (l.find(KEYWORD_ENTITY) != string::npos) { - entities.push_back(get_entity(f)); - } -} - -bool convertmap(const char * const infile, +bool convertmap(stringstream &mapdata, const char * const outfile, void (*brushdef) (stringstream &, const vector &), vector > &entities) { - ifstream fin; - fin.open(infile); - if (!fin.good()){ - cerr << "error: failed to open input file" << endl; - return false; - } ofstream fout; fout.open(outfile); if (!fout.good()) { @@ -133,18 +114,17 @@ bool convertmap(const char * const infile, fout << "{\n" << "\"classname\" \"worldspawn\"" << endl; string line; - while (getline(fin, line)) { + while (getline(mapdata, line)) { if (line.find(KEYWORD_PREFAB) != string::npos || line.find(KEYWORD_GLOBAL) != string::npos) { - parse_prefab(fin, fout, brushdef, entities); + parse_prefab(mapdata, fout, brushdef, entities); } else if (line.find(KEYWORD_ENTITY) != string::npos) { - entities.push_back(get_entity(fin)); + entities.push_back(get_entity(mapdata)); } else if (line.find(KEYWORD_BRUSH) != string::npos) { - write(parse_brush(fin), fout, brushdef); + write(parse_brush(mapdata), fout, brushdef); } } fout << "}" << endl; - fin.close(); fout.close(); return true; }