Skip to content

Commit

Permalink
Comments are now parsed in XMLReader::GetChar (this was to avoid a pr…
Browse files Browse the repository at this point in the history
…oblem when a tag's value contains a comment)
  • Loading branch information
SteveMaas1978 committed Apr 25, 2024
1 parent eadc291 commit fc62846
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions XML/XMLReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,53 +901,7 @@ void XMLReader::ReadTag(XMLTag& tag)
}

ch = GetChar();
if (ch == '!')
{
m_comment.clear();

// parse the comment
ch = GetChar(); if (ch != '-') throw XMLSyntaxError(m_nline);
ch = GetChar(); if (ch != '-') throw XMLSyntaxError(m_nline);

// find the end of the comment
int n = 0;
do
{
ch = GetChar();
if (ch == '-') n++;
else if ((ch == '>') && (n >= 2)) break;
else
{
if (n > 0) m_comment += '-';
if (n > 1) m_comment += '-';
if (ch != '\r') m_comment += ch; // don't append \r
n = 0;
}
}
while (1);

// eat whitespace at the start and end of the comment
if (m_comment.empty() == false)
{
int n = m_comment.size();
char* tmp = new char[n + 1];
strncpy(tmp, m_comment.c_str(), n);
tmp[n] = 0;

char* cl = tmp;
while (*cl && isspace(*cl)) cl++;
n = strlen(cl);
if (n > 0)
{
char* cr = &cl[n - 1];
while ((cr > cl) && isspace(*cr)) *cr-- = 0;
}

m_comment = cl;
delete[] tmp;
}
}
else if (ch == '?')
if (ch == '?')
{
// parse the xml header tag
while ((ch = GetChar()) != '?');
Expand Down Expand Up @@ -1119,7 +1073,9 @@ char XMLReader::readNextChar()
m_eof = (m_bufSize != BUF_SIZE);
}
m_currentPos++;
return m_buf[m_bufIndex++];
char ch = m_buf[m_bufIndex++];
if (ch == '\n') m_nline++;
return ch;
}

//-----------------------------------------------------------------------------
Expand All @@ -1132,23 +1088,82 @@ int64_t XMLReader::currentPos()
//! move the file pointer
void XMLReader::rewind(int64_t nstep)
{
// NOTE: What if we rewind past a newline? Won't that mess up the line index?
m_bufIndex -= nstep;
m_currentPos -= nstep;

if (m_bufIndex < 0)
{
m_stream->seekg(m_bufIndex - m_bufSize, ios_base::cur);
m_stream->seekg(m_bufIndex - m_bufSize, ios_base::cur);
m_bufIndex = m_bufSize = 0;
m_eof = false;
}
}

// clean the string by removing whitespace at the front and back
void clean_string(string& s)
{
if (s.empty()) return;

size_t n = s.size();
char* tmp = new char[n + 1];
strncpy(tmp, s.c_str(), n);
tmp[n] = 0;

char* cl = tmp;
while (*cl && isspace(*cl)) cl++;
n = strlen(cl);
if (n > 0)
{
char* cr = &cl[n - 1];
while ((cr > cl) && isspace(*cr)) *cr-- = 0;
}

s = cl;
delete[] tmp;
}

//-----------------------------------------------------------------------------
//! Read the next character in the file.
//! Read the next character in the file (skipping over comments).
char XMLReader::GetChar()
{
char ch;
if ((ch=readNextChar())=='\n') ++m_nline;
char ch = readNextChar();

// check for comments
if (ch == '<')
{
char tmp = readNextChar();
if (tmp == '!')
{
m_comment.clear();

// parse the comment
ch = readNextChar(); if (ch != '-') throw XMLSyntaxError(m_nline);
ch = readNextChar(); if (ch != '-') throw XMLSyntaxError(m_nline);

// find the end of the comment
int n = 0;
do
{
ch = readNextChar();
if (ch == '-') n++;
else if ((ch == '>') && (n >= 2)) break;
else
{
if (n > 0) m_comment += '-';
if (n > 1) m_comment += '-';
if (ch != '\r') m_comment += ch; // don't append \r
n = 0;
}
} while (1);

// eat whitespace at the start and end of the comment
clean_string(m_comment);

ch = readNextChar();
}
else rewind(1);
}

// read entity references
if (ch=='&')
Expand Down

0 comments on commit fc62846

Please sign in to comment.