Does anyone see the problem with the following code? I've ran it through the debugger, but I'm unable to isolate it completely.
It's somewhere within this loop.. it will run three times, but after the third.. it crashes (assertion error).
Code:
for (int i = 1; i < (tagOffset[1] - tagOffset[0] - 1); i++)
{
CString gotAtOffset = entireLine.GetAt(i + tagOffset[0]);
subString = subString + gotAtOffset;
if (gotAtOffset == '=' && !inAttrib)
{
inAttrib = 1;
} else if (gotAtOffset == '"' && inAttrib && !foundFirstQuote) {
isWhiteSpaceAttrib = 0;
foundFirstQuote = 1;
} else if (gotAtOffset == '"' && inAttrib && foundFirstQuote) {
inAttrib = 0;
isWhiteSpaceAttrib = 0;
foundFirstQuote = 0;
okToModNextChar = 1;
} else if (gotAtOffset == ' ' && inAttrib && isWhiteSpaceAttrib) {
inAttrib = 0;
isWhiteSpaceAttrib = 0;
okToModNextChar = 1;
} else if (okToModNextChar && !inAttrib) {
if (m_bCase)
{
int iLeng = subString.GetLength();
gotAtOffset.MakeUpper();
subString.SetAt(iLeng-1, gotAtOffset.GetAt(0));
} else {
int iLeng = subString.GetLength();
gotAtOffset.MakeLower();
subString.SetAt(iLeng-1, gotAtOffset.GetAt(0));
}
}
}
That being part of the much larger code:
Code:
void CHTMLConvertDlg::OnConvert()
{
UpdateData(TRUE);
m_sStatus = "Converting";
CFile fileHTML;
fileHTML.Open(m_sFileName, CFile::modeReadWrite | CFile::shareDenyWrite, NULL);
BeginWaitCursor();
while (m_dBytesRead < fileHTML.GetLength())
{
OnPaint();
CString entireLine;
//Force tempLine to dissappear;
{
char tempLine[4096];
fileHTML.Read(tempLine, 2048);
m_iLinesRead++;
entireLine = tempLine;
//AfxMessageBox("4");
//Ensure the line doesn't consist of a whitespace character;
m_dBytesRead =+ entireLine.GetLength();
UpdateData(FALSE);
entireLine.TrimRight();
}
if (!entireLine.IsEmpty())
{
//AfxMessageBox("5");
//'<' and '>' tag offsets.
signed int tagOffset[2] = { 0, 0 };
signed int tempOffset[2] = { 0, 0 };
//The following variables ensure no tag attributes are modified;
bool inAttrib = 0; //Are we inside a tags attribute?
bool isWhiteSpaceAttrib = 0; //Is the attribute surrounded by quotes, or is it delimited by a whitespace character?
bool foundFirstQuote = 0; //Did we find the first quote character?
bool okToModNextChar = 0; //OK to change the case of the next character?
// AfxMessageBox("6");
for
(
tempOffset[0] = entireLine.Find('<', tagOffset[0]), //First instance of '<';
tagOffset[0] = tempOffset[0];
tagOffset[0] != -1; //Loop until there is no more '<' tags;
tagOffset[0] = tagOffset[1] //Set the offset of '<' to '>' to force searching beyond the current tag;
)
{
tempOffset[1] = entireLine.Find('>', tagOffset[1]);
tagOffset[1] = tempOffset[1];
CString subString;
//AfxMessageBox("7");
for (int i = 1; i < (tagOffset[1] - tagOffset[0] - 1); i++)
{
CString gotAtOffset = entireLine.GetAt(i + tagOffset[0]);
subString = subString + gotAtOffset;
if (gotAtOffset == '=' && !inAttrib)
{
inAttrib = 1;
} else if (gotAtOffset == '"' && inAttrib && !foundFirstQuote) {
isWhiteSpaceAttrib = 0;
foundFirstQuote = 1;
} else if (gotAtOffset == '"' && inAttrib && foundFirstQuote) {
inAttrib = 0;
isWhiteSpaceAttrib = 0;
foundFirstQuote = 0;
okToModNextChar = 1;
} else if (gotAtOffset == ' ' && inAttrib && isWhiteSpaceAttrib) {
inAttrib = 0;
isWhiteSpaceAttrib = 0;
okToModNextChar = 1;
} else if (okToModNextChar && !inAttrib) {
if (m_bCase)
{
int iLeng = subString.GetLength();
gotAtOffset.MakeUpper();
subString.SetAt(iLeng-1, gotAtOffset.GetAt(0));
} else {
int iLeng = subString.GetLength();
gotAtOffset.MakeLower();
subString.SetAt(iLeng-1, gotAtOffset.GetAt(0));
}
}
}
for (i = 1; i < (tagOffset[1] - tagOffset[0]); i++)
{
entireLine.SetAt(i + tagOffset[0], subString.GetAt(i + tagOffset[0]));
}
tagOffset[1]++;
}
fileHTML.Write(entireLine, entireLine.GetLength());
}
}
EndWaitCursor();
m_sStatus = "Finished!";
UpdateData(FALSE);
}
If anyone needs any more info on the error.. ask me, I'm not too good with the debugger yet so I can't isolate it myself. I've been working for the past hour trying to find this, but it eludes me and I've grown tired.
Thanks.