I'm trying to parse a text file written in a certain format to be used in a game. I felt C++11/14's new regex library would be suitable.
I did a minimal test program. I've not done the functionality in the switch-case clause yet, but rather focused on getting the totals of various elements (monsters/spells/items...) first to see if that works before moving on.
The code below compiles and works as I intended using GCC 6.1.1 on Linux. However, compiling the same code unaltered with my MinGW cross compiler and running it on WINE, it prints nothing but a line break. I tried compiling the code again in MSVC 2015 with the same result.
I have no platform specific code, so I'm stumped as to why it behaves differently on MinGW/MSVC compiled EXEs.
The syntax of the text file goes like this (eg.):
Code:
LANGUAGE {English/ANSI}
ITEMTOT {32}
INAM1 {sword}
INAM2 {shield}
Code:
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
bool parseScript(const char * filename)
{
std::ifstream scriptFile(filename);
std::regex labelSyntax("([A-Z]+)([0-9]*)[ \t]+\\{(.*)\\}[\r\n]");
std::string line;
while (std::getline(scriptFile, line))
{
std::smatch m;
if (std::regex_match(line, m, labelSyntax))
{
if (m[1] == "LANGUAGE")
std::cout << "Locale set to " << m[3] << std::endl;
else if (m[1] == "ITEMTOT")
std::cout << "There are " << m[3] << " total items." << std::endl;
else if (m[1] == "SPLTOT")
std::cout << "There are " << m[3] << " total spells." << std::endl;
else if (m[1] == "MOBTOT")
std::cout << "There are " << m[3] << " total monsters." << std::endl;
else if (m[1] == "WEPTOT")
std::cout << "There are " << m[3] << " total weapons." << std::endl;
else if (m[1] == "BARTOT")
std::cout << "There are " << m[3] << " total barriers." << std::endl;
else if (m[1] == "TRPTOT")
std::cout << "There are " << m[3] << " total traps." << std::endl;
else if (m[1] == "TRETOT")
std::cout << "There are " << m[3] << " total treasures." << std::endl;
else if (m[1] == "TELTOT")
std::cout << "There are " << m[3] << " total teleports." << std::endl;
else
{
switch (m[1].str()[0])
{
case 'I': // item
break;
case 'S': // spell
break;
case 'M': // monster
break;
case 'W': // weapon
break;
case 'B': // barrier
break;
case 'X': // trap
break;
case 'T': // treasure
break;
case 'P': // teleport
break;
case 'L': // level
break;
default:
std::cerr << "Error: Unknown label" << std::endl;
return false;
}
}
}
}
scriptFile.close();
return true;
}
int main(int argc, char ** argv)
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <input file>" << std::endl;
return 1;
}
parseScript(argv[1]);
return 0;
}