Originally Posted by
grumpy
You have provided "abbreviated code". Usually, when people do that, they leave out code that is relevant to the problem, but they (incorrectly) deem irrelevant. Try posting a small but complete example of code that illustrates your problem.
In particular, the code you have given does not account for the output "Not yet at end of file" which, apparently, is occurring after the first if() block but before the second. I assume the line "Function is about to return true" line is output by the obj.FindScriptTag() call. In which case, the "Not yet at end of file" line should should be the first line output, not the third.
Actually, the bolded statement is not quite true. If you re-look at the code I posted above, you'll see that outputting "Not yet at end of file." is actually the FIRST output line of the first if statement. And, you are correct in that the line "Function is about to return true" is outputted in obj.findScriptTag(). And yes, it should be the first output line, not the third, which is why I said the code is not doing what I told it to do.
The most likely explanation is that script_found at the two points in code are at different scopes (i.e. despite having the same name, they are not the same variable). That would occur, for example, if your function is recursive - there can be more than one instance of the script_found variable in scope, each corresponding to a call of your function.
And yet it is not...
Complete relevant code example:
Code:
//.....................
//obj.findScriptTag() definition:
bool C_script_operations::findScriptTag(string& storage_str) {
if (!storage_str.empty())
return false;
string buffer_str;
string search_str;
do {
getline(input_stream, buffer_str);
if (input_stream.good()) {
cout<< "storage_str is: " << storage_str <<endl;
search_str = "<script";
if (containsStr(buffer_str, search_str)) {
cout<< "containsStr returned true for script start tag." <<endl;
storage_str += buffer_str;
storage_str += '\n';
search_str = "</script>";
do {
getline(input_stream, buffer_str);
storage_str += buffer_str;
storage_str += '\n';
if (!input_stream.bad()) {
if (containsStr(buffer_str, search_str)) {
cout<< "containsStr returned true for script end tag." <<endl;
storage_str = stripStr(storage_str, "<script", search_str.c_str());
cout<< "storage_str is: " << storage_str <<endl;
num_of_js_found++;
cout<< "Function is about to return true." <<endl;
return true;
}
}
else {
cout<< "input_stream is bad in inner loop." <<endl;
break;
}
} while (!input_stream.eof());
}
}
else {
cout<< "input_stream is bad in outer loop." <<endl;
break;
}
} while(!input_stream.eof()); //end of file has not been reached
cout<< "input_stream is about to return false...";
return false;
}
//int main()
//...do while loop definition:
string storage_str1 = "";
string storage_str2 = "";
int num_of_js_found;
bool script_found = false; //this is the only place that script_found is declared/defined
do {
if (!obj.atEndOfFile()) {
cout<< "Not yet at end of file." <<endl;
script_found = obj.findScriptTag(storage_str1);
cout<< "script_found: " << script_found <<endl;
}
if (script_found == false) {
cerr<< "Error! File did not contain any Javascripts!\n"
"Please pass a file with a javascript in it!\n\n"
"Exiting out...";
return 1;
}
else {
bool script_type_Javascript = obj.scriptTypeIsJavascript(storage_str1);
if (!script_type_Javascript) {
cerr<< "Error! The file you passed did not contain any Javascripts!\n"
"Please pass a file that contains at least one Javascript!\n\n"
"Exiting out...";
return 1;
}
num_of_js_found = obj.getNumOfJSFound();
output_filepath1 += "ExtractedJS";
output_filepath2 += "ExtractedJS";
if (num_of_js_found < 10) {
output_filepath1 += '0';
output_filepath2 += '0';
}
output_filepath1 += num_of_js_found;
output_filepath1 += ".html";
output_filepath2 += num_of_js_found;
output_filepath2 += ".js";
ofstream output_stream1(output_filepath1.c_str());
ofstream output_stream2(output_filepath2.c_str());
output_stream1<< output_stream1, "<-------------------------------------------------------------\n"
"This file has been generated by ExtractJavascriptFromHtmlPage.\n"
"Original HTML file this code was generated from: ";
output_stream1<< output_stream1, input_filepath.c_str();
output_stream1<< output_stream1, "\nHave a nice day.\n-->\n\n";
output_stream1<< output_stream1, storage_str1.c_str();
output_stream1.close();
storage_str2 = obj.stripStr(storage_str1, "<script", "</script>", C_script_operations::exclusive);
for (unsigned int i = 0; i < storage_str2.size(); i++) {
if (storage_str2.at(i) == '>') {
char* p1 = &storage_str2.at(i);
char* p2 = &storage_str2.at(storage_str2.size() - 1);
storage_str2 = obj.stripStr(storage_str2, p1, p2);
storage_str2.erase(storage_str2.begin()); //erase the '>' character of string
break;
}
}
output_stream2<< output_filepath2, "/*************************************************************\n"
"This file has been generated by ExtractJavascriptFromHtmlPage.\n"
"Original HTML file this code was generated from: ";
output_stream2<< output_filepath2, input_filepath.c_str();
output_stream2<< output_filepath2, "\nHave a nice day.\n"
"*************************************************************/\n\n";
output_stream2<< output_stream2, storage_str2;
output_stream2.close();
}
} while (!obj.atEndOfFile());