I need to parse an input file that contains functions and comment lines.
But the functions are case insensitive so I need to figure out a way to find the functions being lowercase uppercase or a mix.
I thought I could use toupper but it only accepts char.
the 2 functions(in bold) in file are size=x,y and searchFor=word Commented lines can be blank or start with '#'.
Once the size is known we can parse a puzzle, which will be a char[][] type based on the size of the array. (Although this may be also invalid input).
I'm starting to think perhaps string class is not the best way to go about this, perhaps I should write a pile of functions to parse char* for example this could be a valid input file:
1 2 3 4 5 6 7 8 9
siZe=6,6 # this is a comment
SeArChFoR=bum # because we can
bumubx #
skwium
nvbsme # I like pie
puuuuw
mamxbl
# could be that the whole puzzle is on one line?
void processFile(string infile)
{
string line,word;
size_t found;
string size="SIZE=";
string search="SEARCHFOR=";
ifstream ifs(infile.c_str());
if (! ifs.is_open())
{
...
}
// else process as normal
int line_count=0;
while (! ifs.eof())
{
getline(ifs, line);
cout << ++line_count << "\n";
if (line == "" || line[0] == '#')
{
; // line is comment and will be ignored.
}
elseif (line[0] == 's' || line[0] == 'S')
{
//== function line or part of puzzle ==//
found= line.find(size); // needs to be case insensitive
if (found != std::npos)
{
// validate row and cols
bool gotSize=false;
gotSize = parseSize(line,0,0);
}
found = line.find(search);
if (found != std::npos)
{
// figure out what to do here... (substring to space perhaps)
}
}
else
{
//== Puzzle line ==//
/*
need to check if we have valid size value.
check substring to ' ' and see if size == row
*/
}
}
}
bool parseSize(string line, int row, int col)
{
int size_pos[3] = { 0 };
string strRow, strCol;
size_pos[0] = line.find('=');
size_pos[1] = line.find(',');
size_pos[2] = line.find(' ');
strRow = line.substr(size_pos[0],size_pos[1]);
strCol = line.substr(size_pos[1],size_pos[2]);
// validate the above, return false if invalid.
// parse the strings to integers row, col using stringstream
returntrue;
}