Attempted to fix partial conversion bug

This commit is contained in:
_ 2017-12-15 23:42:19 -08:00
parent 579b23fccb
commit e6511e3306
3 changed files with 41 additions and 33 deletions

View File

@ -25,12 +25,7 @@ void write(const std::vector<std::string> &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
EntityConverter &); EntityConverter &);
void parse_prefab(std::ifstream &, bool convertmap(std::stringstream &,
std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &);
bool convertmap(const char * const,
const char * const, const char * const,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &); std::vector<std::vector<std::string> > &);
@ -180,4 +175,19 @@ using namespace std;
return output; return output;
} }
template <class ISTR>
void parse_prefab(ISTR &f,
std::ofstream &out,
void (*b) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &entities) {
using namespace std;
string l;
getline(f, l);
if (l.find(KEYWORD_BRUSH) != string::npos) {
write(parse_brush<ISTR>(f), out, b);
} else if (l.find(KEYWORD_ENTITY) != string::npos) {
entities.push_back(get_entity<ISTR>(f));
}
}
#endif #endif

View File

@ -57,6 +57,22 @@ cxxopts::Options arguments(int ac, char ** av) {
return o; return o;
} }
stringstream loadmap(const char *filename) {
ifstream fin;
fin.open(filename);
if (!fin.good()) {
cout << "error: can not open input file." << endl;
}
vector<string> v;
string line;
stringstream output;
while (getline(fin, line)) {
v.push_back(line);
}
move(v.begin(), v.end(), ostream_iterator<string>(output, "\n"));
return output;
}
bool convert_worldspawn(const cxxopts::Options &o, bool convert_worldspawn(const cxxopts::Options &o,
vector<vector<string> > &q) { vector<vector<string> > &q) {
bool is_ok = false; bool is_ok = false;
@ -64,9 +80,11 @@ bool convert_worldspawn(const cxxopts::Options &o,
if (o.count(ARG_BRUSHFORMAT)) { if (o.count(ARG_BRUSHFORMAT)) {
fn = &brushdef_gtk; fn = &brushdef_gtk;
} }
string inputfile = o[ARG_INPUT_SHORTALIAS].as<string>();
stringstream mapdata = loadmap(inputfile.c_str());
try { try {
is_ok = convertmap( is_ok = convertmap(
o[ARG_INPUT_SHORTALIAS].as<string>().c_str(), // in file mapdata, // in file
o[ARG_OUTPUT_SHORTALIAS].as<string>().c_str(), // out file o[ARG_OUTPUT_SHORTALIAS].as<string>().c_str(), // out file
fn, // brush definition fn, // brush definition
q); // queue of entities q); // queue of entities

View File

@ -101,29 +101,10 @@ void write(const vector<string> &entity,
fo << "}" << endl; fo << "}" << endl;
} }
void parse_prefab(ifstream &f, bool convertmap(stringstream &mapdata,
ofstream &out,
void (*b) (stringstream &, const vector<TPlanePoints> &),
vector<vector<string> > &entities) {
string l;
getline(f, l);
if (l.find(KEYWORD_BRUSH) != string::npos) {
write(parse_brush<ifstream>(f), out, b);
} else if (l.find(KEYWORD_ENTITY) != string::npos) {
entities.push_back(get_entity<ifstream>(f));
}
}
bool convertmap(const char * const infile,
const char * const outfile, const char * const outfile,
void (*brushdef) (stringstream &, const vector<TPlanePoints> &), void (*brushdef) (stringstream &, const vector<TPlanePoints> &),
vector<vector<string> > &entities) { vector<vector<string> > &entities) {
ifstream fin;
fin.open(infile);
if (!fin.good()){
cerr << "error: failed to open input file" << endl;
return false;
}
ofstream fout; ofstream fout;
fout.open(outfile); fout.open(outfile);
if (!fout.good()) { if (!fout.good()) {
@ -133,18 +114,17 @@ bool convertmap(const char * const infile,
fout << "{\n" fout << "{\n"
<< "\"classname\" \"worldspawn\"" << endl; << "\"classname\" \"worldspawn\"" << endl;
string line; string line;
while (getline(fin, line)) { while (getline(mapdata, line)) {
if (line.find(KEYWORD_PREFAB) != string::npos || if (line.find(KEYWORD_PREFAB) != string::npos ||
line.find(KEYWORD_GLOBAL) != string::npos) { line.find(KEYWORD_GLOBAL) != string::npos) {
parse_prefab(fin, fout, brushdef, entities); parse_prefab<stringstream>(mapdata, fout, brushdef, entities);
} else if (line.find(KEYWORD_ENTITY) != string::npos) { } else if (line.find(KEYWORD_ENTITY) != string::npos) {
entities.push_back(get_entity<ifstream>(fin)); entities.push_back(get_entity<stringstream>(mapdata));
} else if (line.find(KEYWORD_BRUSH) != string::npos) { } else if (line.find(KEYWORD_BRUSH) != string::npos) {
write(parse_brush<ifstream>(fin), fout, brushdef); write(parse_brush<stringstream>(mapdata), fout, brushdef);
} }
} }
fout << "}" << endl; fout << "}" << endl;
fin.close();
fout.close(); fout.close();
return true; return true;
} }