Following my last post about the regex library of C++11/14, I did a bit of MSVC debugging and discovered regex wasn't the issue at all, but instead it was getline() from the string library. I thoroughly debugged my code and I placed breakpoints on 2 consecutive lines. No matter what I try, execution exits without error when it reaches getline()'s call.
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;
if (!scriptFile) return false; // breakpoint 1, this line was added during debugging with no change in behavior
std::getline(scriptFile, line); // breakpoint 2 is never reached
std::cout << line << std::endl;
while (std::getline(scriptFile, line))
{
std::smatch m;
if (std::regex_match(line, m, labelSyntax))
{
if (m[1].str() == "LANGUAGE")
std::cout << "Locale set to " << m[3].str() << std::endl;
else if (m[1].str() == "ITEMTOT")
std::cout << "There are " << m[3].str() << " total items." << std::endl;
else if (m[1].str() == "SPLTOT")
std::cout << "There are " << m[3].str() << " total spells." << std::endl;
else if (m[1].str() == "MOBTOT")
std::cout << "There are " << m[3].str() << " total monsters." << std::endl;
else if (m[1].str() == "WEPTOT")
std::cout << "There are " << m[3].str() << " total weapons." << std::endl;
else if (m[1].str() == "BARTOT")
std::cout << "There are " << m[3].str() << " total barriers." << std::endl;
else if (m[1].str() == "TRPTOT")
std::cout << "There are " << m[3].str() << " total traps." << std::endl;
else if (m[1].str() == "TRETOT")
std::cout << "There are " << m[3].str() << " total treasures." << std::endl;
else if (m[1].str() == "TELTOT")
std::cout << "There are " << m[3].str() << " 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;
}
if (!parseScript(argv[1]))
std::cerr << "Oops!" << std::endl;
return 0;
}
As you can see, if there was an error with the file stream itself, the function would've returned 'false' and "Oops!" would be output, but I get no output whatsoever.