struct stringdata
{
string tag, value;
}
void readLine(ifstream &inFile, stringdata &data)
{
string dataLine;
int start, readLength;
bool validData = false;
data.value = "";
while (!validData && inFile && !inFile.eof()) //Checks if valid data has been read, the file is still good, and not at the end of the file.
{
getline(inFile, dataLine);
if (dataLine.find('=') != string::npos && dataLine != "")
{
while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
{
dataLine = dataLine.substr(1, dataLine.length());
}
if (dataLine.at(0) != '#') // Leading comment, ignore line
{
start = dataLine.find('=');
char positionChar;
do //Finds a non whitespace character to the left of the equal sign, giving us the tag
{
start--;
positionChar = dataLine.at(start);
}while (positionChar == ' ');
data.tag = dataLine.substr(0, start + 1);
start = dataLine.find('=');
do //Finds the start of the value
{
start++;
positionChar = dataLine.at(start);
}while (positionChar == ' ');
while (dataLine.at(dataLine.length() - 1) == ' ') //Checks for and cleans up trailing whitespace
{
dataLine = dataLine.substr(0, dataLine.length() - 1);
}
if (dataLine.find('#') != string::npos) //If a comment tag is found somewhere in the string
{
readLength = dataLine.find('#'); //Find the comment
do //and read backwards from the comment to the value
{
readLength--;
positionChar = dataLine.at(readLength);
}while (positionChar == ' ');
readLength = (readLength - start) + 1; //Calculate the length of the value for substringing
}
else
readLength = dataLine.length() - start + 1; //Calculate the length of the value for substringing
data.value = dataLine.substr(start, readLength);
validData = true;
}
}
else if (dataLine.find(ECHO) != string::npos)
{
while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
{
dataLine = dataLine.substr(1, dataLine.length());
}
if (dataLine.at(0) != '#')
echoDebug = true;
}
else if (dataLine.find('<') != string::npos && dataLine.find('>') != string::npos ) //Header tag
{
while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
{
dataLine = dataLine.substr(1, dataLine.length());
}
if (dataLine.at(0) != '#')
{
data.tag = dataLine.substr(dataLine.find('<'), dataLine.find('>') - dataLine.find('<') + 1);
data.value = "";
validData = true;
}
// if (echoDebug)
// cout << "Tag: " << data.tag << endl;
}
else if (dataLine.find('=') == string::npos && dataLine != "") //Missing a "=", blank lines are ignored
{
while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
{
dataLine = dataLine.substr(1, dataLine.length());
}
if (dataLine.at(0) != '#')
cout << "Line \"" << dataLine << "\" is missing something important. (Make sure you have an '=' somewhere)" << endl;
}
else if (!inFile && !inFile.eof()) //File is bad and not just at the end of the file
{
cout << "File read error. Please make sure you are using a valid file" << endl
<< "Last line read: \"" << dataLine << "\"." << endl
<< "Now terminating" << endl;
exit(1); //Terminate program as invalid data is crashable
}
}
}