diff --git a/ReflexToQ3/includes/oopless-parser.cpp b/ReflexToQ3/includes/oopless-parser.cpp index a2c905c..a196697 100644 --- a/ReflexToQ3/includes/oopless-parser.cpp +++ b/ReflexToQ3/includes/oopless-parser.cpp @@ -6,6 +6,7 @@ using namespace std; bool is_ebrush(const std::vector &input) { +using namespace std; #define KEYWORD_ENTBRUSH1 "type Teleporter" #define KEYWORD_ENTBRUSH2 "type JumpPad" #define KEYWORD_ENTBRUSH3 "type RaceStart" @@ -21,6 +22,25 @@ bool is_ebrush(const std::vector &input) { return false; } + +vector extract_brush(const vector &entity) { + bool brushent = false; + unsigned int i; + for (i = 0; i < entity.size(); ++i) { + if (entity[i].find(KEYWORD_BRUSH) != string::npos) { + brushent = true; + break; + } + } + vector output(entity.size() - i); + if (brushent) { + // entity.begin() + i = vector that begins with KEYWORD_BRUSH + // parsing functions will expect only the contents underneath the keyword. + move((entity.begin() + i), entity.end(), output.begin()); + } + return output; +} + // place holding functions to test proper control flow. bool write(const struct TBrush &geometry, ofstream &fout, @@ -62,20 +82,7 @@ void write(const vector &entity, 2. Searches through and splits the vector. 3. Recombines the brush vector. (4. Sends the vector to parse the brush through again.)*/ - bool brushent = false; - unsigned int i; - for (i = 0; i < entity.size(); ++i) { - if (entity[i].find(KEYWORD_BRUSH) != string::npos) { - brushent = true; - break; - } - } - vector brush(entity.size() - i); - if (brushent) { - // entity.begin() + i = vector that begins with KEYWORD_BRUSH - // parsing functions will expect only the contents underneath the keyword. - move((entity.begin() + i), entity.end(), brush.begin()); - } + vector brush = extract_brush(entity); vector converted = e.convert(entity); if (converted.empty()) { return; @@ -84,7 +91,7 @@ void write(const vector &entity, for (const string &s : converted) { fo << s; } - if (brushent) { + if (brush.size() > 0) { stringstream ss; string line; copy(brush.begin(), brush.end(), ostream_iterator(ss, "\n")); @@ -94,28 +101,6 @@ void write(const vector &entity, fo << "}" << endl; } -// entities will vary widely depending on their type and available flags. -// for now, acquire entity data, and handle their conversion in the write methods. -vector get_entity(ifstream &f) { - vector output; - string line; - unsigned int pos = f.tellg(); - while (getline(f, line)) { - // stop when - // entity or a prefab keyword - // brush keyword and the entity is not of the type teleporter or jumppad. - if ((line.find(KEYWORD_ENTITY) != string::npos || - line.find(KEYWORD_PREFAB) != string::npos) || ( - line.find(KEYWORD_BRUSH) != string::npos && !is_ebrush(output))) { - f.seekg(pos); - return output; - } else { - output.push_back(line); - } - pos = f.tellg(); - } -} - void parse_prefab(ifstream &f, ofstream &out, void (*b) (stringstream &, const vector &), @@ -125,7 +110,7 @@ void parse_prefab(ifstream &f, 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)); + entities.push_back(get_entity(f)); } } @@ -153,7 +138,7 @@ bool convertmap(const char * const infile, line.find(KEYWORD_GLOBAL) != string::npos) { parse_prefab(fin, fout, brushdef, entities); } else if (line.find(KEYWORD_ENTITY) != string::npos) { - entities.push_back(get_entity(fin)); + entities.push_back(get_entity(fin)); } else if (line.find(KEYWORD_BRUSH) != string::npos) { write(parse_brush(fin), fout, brushdef); } diff --git a/ReflexToQ3/includes/oopless-parser.hpp b/ReflexToQ3/includes/oopless-parser.hpp index 92ccbae..c93856d 100644 --- a/ReflexToQ3/includes/oopless-parser.hpp +++ b/ReflexToQ3/includes/oopless-parser.hpp @@ -10,7 +10,9 @@ #include #include "EntityConverter.hpp" -bool is_ebrush(const std::vector&); +bool is_ebrush(const std::vector &); + +std::vector extract_brush(const std::vector &); struct TBrush override_textures(struct TBrush, const char*); @@ -23,8 +25,6 @@ void write(const std::vector &, void (*f) (std::stringstream &, const std::vector &), EntityConverter &); -std::vector get_entity(std::ifstream &); - void parse_prefab(std::ifstream &, std::ofstream &, void (*f) (std::stringstream &, const std::vector &), @@ -35,6 +35,7 @@ bool convertmap(const char * const, void (*f) (std::stringstream &, const std::vector &), std::vector > &); + #define KEYWORD_ENTITY "entity" #define KEYWORD_BRUSH "brush" #define KEYWORD_VERTICES "vertices" @@ -42,6 +43,28 @@ bool convertmap(const char * const, #define KEYWORD_GLOBAL "global" #define KEYWORD_PREFAB "prefab" +template +std::vector get_entity(STREAMOBJ &f) { +using namespace std; + vector output; + string line; + unsigned int pos = f.tellg(); + while (getline(f, line)) { + // stop when + // entity or a prefab keyword + // brush keyword and the entity is not of the type teleporter or jumppad. + if ((line.find(KEYWORD_ENTITY) != string::npos || + line.find(KEYWORD_PREFAB) != string::npos) || ( + line.find(KEYWORD_BRUSH) != string::npos && !is_ebrush(output))) { + f.seekg(pos); + return output; + } else { + output.push_back(line); + } + pos = f.tellg(); + } +} + template std::vector parse_vertices(STREAMOBJ &f) { using namespace std;