Map parser compiles outside of Visual Studio

This commit is contained in:
2017-03-30 22:34:19 -07:00
parent 3845ede3b9
commit 39d66cdd28
2 changed files with 294 additions and 279 deletions

View File

@ -1,236 +1,251 @@
// //
// Author: Michael Cameron // Author: Michael Cameron
// Email: chronokun@hotmail.com // Email: chronokun@hotmail.com
// //
// Libraries Include // Libraries Include
#include "libraries.h" #include "libraries.h"
// This Include // This Include
#include "mapparser.h" #include "mapparser.h"
const bool CMapParser::LoadMap(const char* _kpcFileName) #ifndef _MSC_VER
{ /*
std::ifstream InFile; str*_s functions are microsoft only extensions of C string manip.
EParserState eState = PARSERSTATE_UNKNOWN; mingw is ok with strtok_r, but is not ok with strcpy_r
additionally, old versions of mingw may not be ok with strtok_r at all.
InFile.open(_kpcFileName, std::ios::in); */
if(InFile.is_open()) inline char *strtok_s(char* s, const char* delm, char** context) {
{ return strtok_r(s, delm, context);
std::string Line; }
bool bAdvance = true;
bool bGoing = true; inline char *strcpy_s(char *dest, const char *src) {
while(bGoing) return strcpy(dest, src);
{ }
if(bAdvance) #endif
{
if(!std::getline(InFile, Line)) const bool CMapParser::LoadMap(const char* _kpcFileName)
{ {
bGoing = false; std::ifstream InFile;
} EParserState eState = PARSERSTATE_UNKNOWN;
}
bAdvance = true; InFile.open(_kpcFileName, std::ios::in);
if(eState == PARSERSTATE_UNKNOWN) if(InFile.is_open())
{ {
if(strcmp(Line.c_str(), "entity") == 0) std::string Line;
{ bool bAdvance = true;
eState = PARSERSTATE_ENTITY; bool bGoing = true;
continue; while(bGoing)
} {
} if(bAdvance)
else if(eState == PARSERSTATE_ENTITY) {
{ if(!std::getline(InFile, Line))
eState = this->ParseEntity(Line); {
} bGoing = false;
else if(eState == PARSERSTATE_WORLDSPAWN) }
{ }
eState = this->ParseWorldSpawn(Line); bAdvance = true;
} if(eState == PARSERSTATE_UNKNOWN)
else if(eState == PARSERSTATE_BRUSH) {
{ if(strcmp(Line.c_str(), "entity") == 0)
eState = this->ParseBrush(Line); {
//bAdvance = false; eState = PARSERSTATE_ENTITY;
} continue;
else if(eState == PARSERSTATE_VERTEX) }
{ }
eState = this->ParseVertex(Line); else if(eState == PARSERSTATE_ENTITY)
if(eState != PARSERSTATE_VERTEX) {
{ eState = this->ParseEntity(Line);
bAdvance = false; }
} else if(eState == PARSERSTATE_WORLDSPAWN)
} {
else if(eState == PARSERSTATE_FACE) eState = this->ParseWorldSpawn(Line);
{ }
eState = this->ParseFace(Line); else if(eState == PARSERSTATE_BRUSH)
if(eState != PARSERSTATE_FACE) {
{ eState = this->ParseBrush(Line);
bAdvance = false; //bAdvance = false;
} }
} else if(eState == PARSERSTATE_VERTEX)
} {
InFile.close(); eState = this->ParseVertex(Line);
} if(eState != PARSERSTATE_VERTEX)
else {
{ bAdvance = false;
return(false); }
} }
else if(eState == PARSERSTATE_FACE)
return(true); {
} eState = this->ParseFace(Line);
if(eState != PARSERSTATE_FACE)
EParserState CMapParser::ParseEntity(const std::string _Line) {
{ bAdvance = false;
const size_t kszLineSize = 256; }
char pcLine[kszLineSize]; }
const char* kpcDelim = " "; }
char* pcToken; InFile.close();
char* pcContext; }
else
strcpy_s(pcLine, _Line.c_str()); {
pcToken = strtok_s(pcLine, kpcDelim, &pcContext); return(false);
}
while(pcToken != nullptr)
{ return(true);
if(strcmp(pcToken, "\ttype") == 0) }
{
pcToken = strtok_s(nullptr, kpcDelim, &pcContext); EParserState CMapParser::ParseEntity(const std::string _Line)
if(strcmp(pcToken, "WorldSpawn") == 0) {
{ const size_t kszLineSize = 256;
return(PARSERSTATE_WORLDSPAWN); char pcLine[kszLineSize];
} const char* kpcDelim = " ";
else char* pcToken;
{ char* pcContext;
return(PARSERSTATE_UNKNOWN);
} strcpy_s(pcLine, _Line.c_str());
} pcToken = strtok_s(pcLine, kpcDelim, &pcContext);
else
{ while(pcToken != nullptr)
return(PARSERSTATE_ENTITY); {
} if(strcmp(pcToken, "\ttype") == 0)
} {
return(PARSERSTATE_UNKNOWN); pcToken = strtok_s(nullptr, kpcDelim, &pcContext);
} if(strcmp(pcToken, "WorldSpawn") == 0)
{
EParserState CMapParser::ParseWorldSpawn(const std::string _Line) return(PARSERSTATE_WORLDSPAWN);
{ }
if(strcmp(_Line.c_str(), "brush") == 0) else
{ {
this->m_WorldSpawn.m_Brushes.push_back(TBrush()); return(PARSERSTATE_UNKNOWN);
return(PARSERSTATE_BRUSH); }
} }
return(PARSERSTATE_WORLDSPAWN); else
} {
return(PARSERSTATE_ENTITY);
EParserState CMapParser::ParseBrush(const std::string _Line) }
{ }
if(strcmp(_Line.c_str(), "brush") == 0) return(PARSERSTATE_UNKNOWN);
{ }
this->m_WorldSpawn.m_Brushes.push_back(TBrush());
return(PARSERSTATE_BRUSH); EParserState CMapParser::ParseWorldSpawn(const std::string _Line)
} {
if(strcmp(_Line.c_str(), "\tvertices") == 0) if(strcmp(_Line.c_str(), "brush") == 0)
{ {
return(PARSERSTATE_VERTEX); this->m_WorldSpawn.m_Brushes.push_back(TBrush());
} return(PARSERSTATE_BRUSH);
else if(strcmp(_Line.c_str(), "\tfaces") == 0) }
{ return(PARSERSTATE_WORLDSPAWN);
return(PARSERSTATE_FACE); }
}
return(PARSERSTATE_BRUSH); EParserState CMapParser::ParseBrush(const std::string _Line)
} {
if(strcmp(_Line.c_str(), "brush") == 0)
EParserState CMapParser::ParseVertex(const std::string _Line) {
{ this->m_WorldSpawn.m_Brushes.push_back(TBrush());
const size_t kszLineSize = 2048; return(PARSERSTATE_BRUSH);
char pcLine[kszLineSize]; }
const char* kpcDelim = " \t"; if(strcmp(_Line.c_str(), "\tvertices") == 0)
char* pcToken; {
char* pcContext; return(PARSERSTATE_VERTEX);
}
strcpy_s(pcLine, _Line.c_str()); else if(strcmp(_Line.c_str(), "\tfaces") == 0)
pcToken = strtok_s(pcLine, kpcDelim, &pcContext); {
return(PARSERSTATE_FACE);
TVector3f Vert; }
int iTokenNum = 0; return(PARSERSTATE_BRUSH);
while(pcToken != nullptr) }
{
if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') EParserState CMapParser::ParseVertex(const std::string _Line)
{ {
double dValue = std::stod(pcToken); const size_t kszLineSize = 2048;
if(iTokenNum == 0) char pcLine[kszLineSize];
{ const char* kpcDelim = " \t";
Vert.m_fX = (float)dValue; char* pcToken;
} char* pcContext;
else if(iTokenNum == 1)
{ strcpy_s(pcLine, _Line.c_str());
Vert.m_fY = (float)dValue; pcToken = strtok_s(pcLine, kpcDelim, &pcContext);
}
else if(iTokenNum == 2) Eigen::Vector3f Vert;
{ int iTokenNum = 0;
Vert.m_fZ = (float)dValue; while(pcToken != nullptr)
} {
iTokenNum++; if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-')
} {
else double dValue = std::stod(pcToken);
{ if(iTokenNum == 0)
return(PARSERSTATE_BRUSH); {
} Vert[0] = (float)dValue;
}
pcToken = strtok_s(nullptr, kpcDelim, &pcContext); else if(iTokenNum == 1)
} {
Vert[1] = (float)dValue;
this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Vertices.push_back(Vert); }
return(PARSERSTATE_VERTEX); else if(iTokenNum == 2)
} {
Vert[2] = (float)dValue;
EParserState CMapParser::ParseFace(const std::string _Line) }
{ iTokenNum++;
const size_t kszLineSize = 2048; }
char pcLine[kszLineSize]; else
const char* kpcDelim = " \t"; {
char* pcToken; return(PARSERSTATE_BRUSH);
char* pcContext; }
strcpy_s(pcLine, _Line.c_str()); pcToken = strtok_s(nullptr, kpcDelim, &pcContext);
pcToken = strtok_s(pcLine, kpcDelim, &pcContext); }
int iTokenNum = 0; this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Vertices.push_back(Vert);
return(PARSERSTATE_VERTEX);
std::string material; }
std::vector<int> Indices; EParserState CMapParser::ParseFace(const std::string _Line)
while(pcToken != nullptr) {
{ const size_t kszLineSize = 2048;
if(iTokenNum < 5) char pcLine[kszLineSize];
{ const char* kpcDelim = " \t";
if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') char* pcToken;
{ char* pcContext;
double dValue = std::stod(pcToken);
} strcpy_s(pcLine, _Line.c_str());
else pcToken = strtok_s(pcLine, kpcDelim, &pcContext);
{
return(PARSERSTATE_BRUSH); int iTokenNum = 0;
}
} std::string material;
else
{ std::vector<int> Indices;
if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-') while(pcToken != nullptr)
{ {
int iValue = std::stoi(pcToken); if(iTokenNum < 5)
Indices.push_back(iValue); {
} if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-')
else {
{ double dValue = std::stod(pcToken);
material = pcToken; }
} else
} {
return(PARSERSTATE_BRUSH);
iTokenNum++; }
pcToken = strtok_s(nullptr, kpcDelim, &pcContext); }
} else
{
TFace Face; if(std::isdigit(pcToken[0], std::locale()) || pcToken[0] == '-')
Face.m_Indices = Indices; {
Face.m_Material = material; int iValue = std::stoi(pcToken);
this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Faces.push_back(Face); Indices.push_back(iValue);
return(PARSERSTATE_FACE); }
} else
{
material = pcToken;
}
}
iTokenNum++;
pcToken = strtok_s(nullptr, kpcDelim, &pcContext);
}
TFace Face;
Face.m_Indices = Indices;
Face.m_Material = material;
this->m_WorldSpawn.m_Brushes[this->m_WorldSpawn.m_Brushes.size()-1].m_Faces.push_back(Face);
return(PARSERSTATE_FACE);
}

View File

@ -1,44 +1,44 @@
// //
// Author: Michael Cameron // Author: Michael Cameron
// Email: chronokun@hotmail.com // Email: chronokun@hotmail.com
// //
#pragma once #pragma once
#ifndef __MAPPARSER_H__ #ifndef __MAPPARSER_H__
#define __MAPPARSER_H__ #define __MAPPARSER_H__
// Includes // Includes
#include "worldspawn.h" #include "worldspawn.h"
// enums // enums
enum EParserState enum EParserState
{ {
PARSERSTATE_UNKNOWN, PARSERSTATE_UNKNOWN,
PARSERSTATE_ENTITY, PARSERSTATE_ENTITY,
PARSERSTATE_WORLDSPAWN, PARSERSTATE_WORLDSPAWN,
PARSERSTATE_BRUSH, PARSERSTATE_BRUSH,
PARSERSTATE_VERTEX, PARSERSTATE_VERTEX,
PARSERSTATE_FACE PARSERSTATE_FACE
}; };
class CMapParser class CMapParser
{ {
// Variables // Variables
public: public:
TWorldSpawn m_WorldSpawn; TWorldSpawn m_WorldSpawn;
// Functions // Functions
public: public:
const bool LoadMap(const char* _kpcFileName); const bool LoadMap(const char* _kpcFileName);
protected: protected:
EParserState ParseEntity(const std::string _Line); EParserState ParseEntity(const std::string _Line);
EParserState ParseWorldSpawn(const std::string _Line); EParserState ParseWorldSpawn(const std::string _Line);
EParserState ParseBrush(const std::string _Line); EParserState ParseBrush(const std::string _Line);
EParserState ParseVertex(const std::string _Line); EParserState ParseVertex(const std::string _Line);
EParserState ParseFace(const std::string _Line); EParserState ParseFace(const std::string _Line);
}; };
#endif #endif