Ok, here is the problem code:
Code:
if (containsString(filestreamInBuffer, ";")) { //line which contains 'enum' contains the terminating semicolon also
cout<< "This statement has been entered anyway. WTF???" <<endl;
cout<< "filestreamInBuffer contains: " << filestreamInBuffer <<endl;
currentEnumName = stripString(filestreamInBuffer, 'm', '{'); //we now have the name of the current enum
//continue doing stuff inside this if statement...
}
Ok, now my output statements demonstrate that that if statement is being entered (even though it is clear that the return value of the containsString() function here is false, but the if statement is still entered).
My debugger shows that the line which produces the segfault is the bolded line of code, so the problem appears to be inside stripString().
But the output shows this:
This statement has been entered anyway. WTF???
filestreamInBuffer contains: enum DAY {
stripString() has been entered.
sizeOfStr is: 10
str[3] == start
str[4] != end
str[5] != end
str[6] != end
str[7] != end
str[8] != end
str[9] == end
strippedString is: DAY
Segmentation fault
And here is the definition of the stripString() function (note that I narrowed the last output line before the segfault I posted to the else statement at the end of the function):
Code:
const string& CenumOperations::stripString(const char str[], const char start, const char end, ET_strippingMode strippingMode) {
cout<< "stripString() has been entered." <<endl;
unsigned int sizeOfStr = 0;
while (str[sizeOfStr] != '\0') {
sizeOfStr++; //increment sizeOfStr
}
cout<< "sizeOfStr is: " << sizeOfStr <<endl;
bool startHasNotBeenReachedYet = true;
bool endHasNotBeenReachedYet = true;
unsigned int i2 = 0;
string strippedString = ""; //create an empty string
for (unsigned int i = 0; i < sizeOfStr; i++) {
//loop without doing anything until 'start' character is found or until the end of the string has been reached...
if (str[i] == start) {
cout<< "str[" << i << "] == start" <<endl;
startHasNotBeenReachedYet = false; //since 'start' has now been found
if (strippingMode == exclusive) {
i2 = i+1; //begin the inner do-while loop counter at the index of the next character after the 'begin' character
}
else if (strippingMode == inclusive) {
i2 = i; //begin the inner do-while loop counter at the index of the 'begin' character
}
do {
if (i2 < sizeOfStr - 1) { //we're not at the last character of the string yet
if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the current character matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
else if (str[i2] != end) { //the current character does not match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the current character of the string to the stripped string
}
}
}
else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the current character matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the 'end' character to the stripped string
}
endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
}
else if (str[i2] != end) { //the current character does NOT match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the remaining character we want to add
}
}
}
}
else if (i2 == sizeOfStr - 1) { //we're at the last character of 'str'...
if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the last character of the string matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
else if (str[i2] != end) { //the current character does not match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the last character of the string to the stripped string
}
break; //out of the loop, since we just added the last character (if it wasn't a space)
}
}
else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the last character of the string matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
strippedString += str[i2]; //add the 'end' character to the stripped string
endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
}
else if (str[i2] != end) { //the last character of the string does NOT match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the last character of the string to the stripped string
}
break; //out of the loop, since we just added the last character
}
}
}
i2++; //increment the inner-do while loop iterator
} while(endHasNotBeenReachedYet);
break; //out of the for loop since we already scanned through the rest of the string after 'start'
}
}
if (startHasNotBeenReachedYet) { //meaning the 'start' character was not found after iterating through the whole string
return "";
}
else {
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
}
And here is the content of the file I'm passing to the program:
enum DAY {
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday;
enum Work {
hard = 0,
lazy = -1
};
enum weirdenum //this is a weird enum...
{
weirdenum1,
weirdenum2 = 2,
weirdenum3 = 0
};
EDIT: Oh, and my program has not got as far as outputting to file yet (even though it created the output file, like intended). But here is the command I used to run ConvertEnumToStrings:
Code:
./ConvertEnumToStrings "/home/gorilla/Documents/test.cpp" "results"
The result file was "results.h" (created in the same directory as the inputted file), but as already mentioned, it has not got as far as outputting anything to it since the segfault stops the program before it gets a chance to.