reflex2q3/ReflexToQ3/includes/oopless-parser.cpp

113 lines
3.4 KiB
C++
Raw Normal View History

#include "oopless-parser.hpp"
#include "brushdef.hpp"
#include "EntityConverter.hpp"
#include <sstream>
#include <iostream>
using namespace std;
bool is_ebrush(std::vector<std::string> input) {
// defines are only used in this function.
#define KEYWORD_ENTBRUSH1 "type Teleporter"
#define KEYWORD_ENTBRUSH2 "type JumpPad"
for (const string &str : input) {
2017-06-17 01:38:10 +05:30
if (str.find(KEYWORD_ENTBRUSH1) != string::npos ||
str.find(KEYWORD_ENTBRUSH2) != string::npos) {
return true;
}
}
return false;
}
// place holding functions to test proper control flow.
bool write(const struct TBrush &geometry,
ofstream &fout,
void (*b) (stringstream &, const vector<TPlanePoints> &)) {
bool ok = true;
if (geometry.m_Vertices.size() < 1) {
cerr << "error: geometry has no vertices" << endl;
ok = false;
}
if (geometry.m_Faces.size() < 1) {
cerr << "error: geometry has no faces"<< endl;
ok = false;
}
if (ok) {
fout << GetBrushString(geometry, b);
}
return ok;
}
void write(const vector<string> &entity, ofstream &fo) {
;
}
// 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<string> get_entity(ifstream &f) {
vector<string> 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<TPlanePoints> &)) {
string l;
getline(f, l);
if (l.find(KEYWORD_BRUSH) != string::npos) {
2017-06-17 01:38:10 +05:30
write(parse_brush<ifstream>(f), out, b);
} else if (l.find(KEYWORD_ENTITY) != string::npos) {
write(get_entity(f), out);
}
}
// note: can't use a single function pointer for variadic overloaded write function?
bool convertmap(const char * const infile,
const char * const outfile,
void (*brushdef) (stringstream &, const vector<TPlanePoints> &)) {
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()) {
cerr << "error: failed to open output file" << endl;
return false;
}
fout << "{\n"
<< "\"classname\" \"worldspawn\"" << endl;
string line;
while (getline(fin, line)) {
if (line.find(KEYWORD_PREFAB) != string::npos ||
line.find(KEYWORD_GLOBAL) != string::npos) {
parse_prefab(fin, fout, brushdef);
} else if (line.find(KEYWORD_ENTITY) != string::npos) {
write(get_entity(fin), fout);
} else if (line.find(KEYWORD_BRUSH) != string::npos) {
2017-06-17 01:38:10 +05:30
write(parse_brush<ifstream>(fin), fout, brushdef);
}
}
fout << "}" << endl;
fin.close();
fout.close();
return true;
}