reflex2q3/ReflexToQ3/includes/v8mapparser.cpp

96 lines
2.4 KiB
C++

#include "v8mapparser.hpp"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
const bool V8MapParser::LoadMap(const char* _kpcFileName) {
std::ifstream InFile;
EParserState eState = PARSERSTATE_UNKNOWN;
InFile.open(_kpcFileName, std::ios::in);
if(InFile.is_open())
{
std::string Line;
bool bAdvance = true;
bool bGoing = true;
while(bGoing)
{
if(bAdvance)
{
if(!std::getline(InFile, Line))
{
bGoing = false;
// without this break, loop control allows the final empty string to get passed into ParseFunctions()
break;
}
}
bAdvance = true;
if(eState == PARSERSTATE_UNKNOWN)
{
if(Line.find(KEYWORD_ENTITY) != string::npos)
{
eState = PARSERSTATE_ENTITY;
continue;
} else if (Line.find(KEYWORD_PREFAB) != string::npos || Line.find(KEYWORD_GLOBAL) != string::npos) {
// prefab and global share an indentation level
// both encapsulate similar objects
eState = PARSERSTATE_PREFAB;
continue;
} else {
cout << "Warning: PARSERSTATE_UNKNOWN and no cases met." << endl;
}
} else if (eState == PARSERSTATE_PREFAB) {
eState = this->ParsePrefab(Line);
}
else if(eState == PARSERSTATE_ENTITY)
{
eState = this->ParseEntity(Line);
}
else if(eState == PARSERSTATE_WORLDSPAWN)
{
eState = this->ParseWorldSpawn(Line);
}
else if(eState == PARSERSTATE_BRUSH)
{
eState = this->ParseBrush(Line);
//bAdvance = false;
}
else if(eState == PARSERSTATE_VERTEX)
{
eState = this->ParseVertex(Line);
if(eState != PARSERSTATE_VERTEX)
{
bAdvance = false;
}
}
else if(eState == PARSERSTATE_FACE)
{
eState = this->ParseFace(Line);
if(eState != PARSERSTATE_FACE)
{
bAdvance = false;
}
}
}
InFile.close();
}
else
{
return(false);
}
return(true);
}
EParserState V8MapParser::ParsePrefab(const std::string _Line) {
// this is going with the idea that its possible to correctly parse the map file
// if prefabs are simply broken and de-indented.
// typically entities are listed first, then brushes, but just in case.
if (_Line.find(KEYWORD_ENTITY) != string::npos) {
return PARSERSTATE_ENTITY;
} else {
return PARSERSTATE_BRUSH;
}
}