I wrote a function for properly formatting a script extracted from an html file (i.e. by inserting new-lines when appropriate), and here is the definition:
This function appears to work in some cases, but other times, it doesn't. I looked at a script outputted to file by my program, after it was done, and it had inserted a new-line character right after one of the semi-colons (like its supposed to do), but with a few semicolons several characters later of the script string, it ended up inserting the new-line before the the semicolons, and I don't know why.Code:void C_script_operations::properlyFormatScript(string& scriptStr) {
cout<< "Just entered properlyFormatScript()" <<endl;
cout<< "scriptStr.size() is: " << scriptStr.size() <<endl;
//cin.get();
string temp_str = scriptStr;
for (size_t i = 0; i < scriptStr.size(); i++) {
cout<< "i is: " << i << endl;
if ((scriptStr.at(i) == '{') && (!charIsInLiteralString(scriptStr, scriptStr.at(i), i))) {
if (i < scriptStr.size() - 2) {
if (scriptStr.at(i+1) != '\n' && scriptStr.at(i+2) != '\n') {
temp_str.insert(i+1, "\n\n");
}
}
else
temp_str.append("\n\n");
}
else if ((scriptStr.at(i) == ';') && (!charIsInLiteralString(scriptStr, scriptStr.at(i), i))) {
if (i < scriptStr.size() - 1) {
if (scriptStr.at(i+1) != '\n') {
temp_str.insert(i + 1, "\n");
}
}
else
temp_str.append("\n\n");
}
else if ((scriptStr.at(i) == '}') && !(charIsInLiteralString(scriptStr, scriptStr.at(i), i))) {
if (scriptStr.at(i-1) != '\n' && scriptStr.at(i-2) != '\n') {
scriptStr.insert(i - 1, "\n\n");
}
}
}
scriptStr = temp_str;
}
Note that I added the output lines only for debugging purposes.
Please look over the code and tell me what you think. Thanks.