diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index cdf8ec7..fb367fc 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -189,7 +189,7 @@ void StrPair::SetStr( const char* str, int flags )
}
-char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
+char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int& curLineNum )
{
TIXMLASSERT( p );
TIXMLASSERT( endTag && *endTag );
@@ -203,6 +203,8 @@ char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Set( start, p, strFlags );
return p + length;
+ } else if (*p == '\n') {
+ ++curLineNum;
}
++p;
TIXMLASSERT( p );
@@ -236,7 +238,8 @@ void StrPair::CollapseWhitespace()
// Adjusting _start would cause undefined behavior on delete[]
TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
// Trim leading space.
- _start = XMLUtil::SkipWhiteSpace( _start );
+ int unusedLineNum(0);
+ _start = XMLUtil::SkipWhiteSpace( _start, unusedLineNum );
if ( *_start ) {
const char* p = _start; // the read pointer
@@ -244,7 +247,7 @@ void StrPair::CollapseWhitespace()
while( *p ) {
if ( XMLUtil::IsWhiteSpace( *p )) {
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, unusedLineNum );
if ( *p == 0 ) {
break; // don't write to q; this trims the trailing space.
}
@@ -637,7 +640,8 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
TIXMLASSERT( node );
TIXMLASSERT( p );
char* const start = p;
- p = XMLUtil::SkipWhiteSpace( p );
+ int const startLine = _parseCurLineNum;
+ p = XMLUtil::SkipWhiteSpace( p, _parseCurLineNum );
if( !*p ) {
*node = 0;
TIXMLASSERT( p );
@@ -663,12 +667,14 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
+ returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += xmlHeaderLen;
}
else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
returnNode = new (_commentPool.Alloc()) XMLComment( this );
+ returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += commentHeaderLen;
}
@@ -676,6 +682,7 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
XMLText* text = new (_textPool.Alloc()) XMLText( this );
returnNode = text;
+ returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_textPool;
p += cdataHeaderLen;
text->SetCData( true );
@@ -683,12 +690,14 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
+ returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += dtdHeaderLen;
}
else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
returnNode = new (_elementPool.Alloc()) XMLElement( this );
+ returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_elementPool;
p += elementHeaderLen;
}
@@ -696,7 +705,9 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
returnNode = new (_textPool.Alloc()) XMLText( this );
returnNode->_memPool = &_textPool;
+ returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character
p = start; // Back it up, all the text counts.
+ _parseCurLineNum = startLine;
}
TIXMLASSERT( returnNode );
@@ -725,6 +736,7 @@ bool XMLDocument::Accept( XMLVisitor* visitor ) const
XMLNode::XMLNode( XMLDocument* doc ) :
_document( doc ),
_parent( 0 ),
+ _parseLineNum( 0 ),
_firstChild( 0 ), _lastChild( 0 ),
_prev( 0 ), _next( 0 ),
_userData( 0 ),
@@ -942,7 +954,7 @@ const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const
}
-char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
+char* XMLNode::ParseDeep( char* p, StrPair* parentEnd, int& curLineNum )
{
// This is a recursive method, but thinking about it "at the current level"
// it is a pretty simple flat list:
@@ -970,12 +982,14 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
break;
}
+ int nodeLineNum = node->_parseLineNum;
+
StrPair endTag;
- p = node->ParseDeep( p, &endTag );
+ p = node->ParseDeep( p, &endTag, curLineNum );
if ( !p ) {
DeleteNode( node );
if ( !_document->Error() ) {
- _document->SetError( XML_ERROR_PARSING, 0, 0 );
+ _document->SetError( XML_ERROR_PARSING, 0, 0, nodeLineNum );
}
break;
}
@@ -995,7 +1009,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
}
}
if ( !wellLocated ) {
- _document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0 );
+ _document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0, nodeLineNum );
DeleteNode( node );
break;
}
@@ -1030,7 +1044,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
}
}
if ( mismatch ) {
- _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0 );
+ _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0, nodeLineNum );
DeleteNode( node );
break;
}
@@ -1077,13 +1091,13 @@ const XMLElement* XMLNode::ToElementWithName( const char* name ) const
}
// --------- XMLText ---------- //
-char* XMLText::ParseDeep( char* p, StrPair* )
+char* XMLText::ParseDeep( char* p, StrPair*, int& curLineNum )
{
const char* start = p;
if ( this->CData() ) {
- p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
+ p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNum );
if ( !p ) {
- _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
+ _document->SetError( XML_ERROR_PARSING_CDATA, start, 0, _parseLineNum );
}
return p;
}
@@ -1093,12 +1107,12 @@ char* XMLText::ParseDeep( char* p, StrPair* )
flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING;
}
- p = _value.ParseText( p, "<", flags );
+ p = _value.ParseText( p, "<", flags, curLineNum );
if ( p && *p ) {
return p-1;
}
if ( !p ) {
- _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
+ _document->SetError( XML_ERROR_PARSING_TEXT, start, 0, _parseLineNum );
}
}
return 0;
@@ -1142,13 +1156,13 @@ XMLComment::~XMLComment()
}
-char* XMLComment::ParseDeep( char* p, StrPair* )
+char* XMLComment::ParseDeep( char* p, StrPair*, int& curLineNum )
{
// Comment parses as text.
const char* start = p;
- p = _value.ParseText( p, "-->", StrPair::COMMENT );
+ p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNum );
if ( p == 0 ) {
- _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
+ _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0, _parseLineNum );
}
return p;
}
@@ -1192,13 +1206,13 @@ XMLDeclaration::~XMLDeclaration()
}
-char* XMLDeclaration::ParseDeep( char* p, StrPair* )
+char* XMLDeclaration::ParseDeep( char* p, StrPair*, int& curLineNum )
{
// Declaration parses as text.
const char* start = p;
- p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
+ p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNum );
if ( p == 0 ) {
- _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
+ _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0, _parseLineNum );
}
return p;
}
@@ -1241,14 +1255,14 @@ XMLUnknown::~XMLUnknown()
}
-char* XMLUnknown::ParseDeep( char* p, StrPair* )
+char* XMLUnknown::ParseDeep( char* p, StrPair*, int& curLineNum )
{
// Unknown parses as text.
const char* start = p;
- p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
+ p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNum );
if ( !p ) {
- _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
+ _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0, _parseLineNum );
}
return p;
}
@@ -1290,7 +1304,7 @@ const char* XMLAttribute::Value() const
return _value.GetStr();
}
-char* XMLAttribute::ParseDeep( char* p, bool processEntities )
+char* XMLAttribute::ParseDeep( char* p, bool processEntities, int& curLineNum )
{
// Parse using the name rules: bug fix, was using ParseText before
p = _name.ParseName( p );
@@ -1299,13 +1313,13 @@ char* XMLAttribute::ParseDeep( char* p, bool processEntities )
}
// Skip white space before =
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, curLineNum );
if ( *p != '=' ) {
return 0;
}
++p; // move up to opening quote
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, curLineNum );
if ( *p != '\"' && *p != '\'' ) {
return 0;
}
@@ -1313,7 +1327,7 @@ char* XMLAttribute::ParseDeep( char* p, bool processEntities )
char endTag[2] = { *p, 0 };
++p; // move past opening quote
- p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
+ p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES, curLineNum );
return p;
}
@@ -1749,16 +1763,16 @@ void XMLElement::DeleteAttribute( const char* name )
}
-char* XMLElement::ParseAttributes( char* p )
+char* XMLElement::ParseAttributes( char* p, int& curLineNum )
{
const char* start = p;
XMLAttribute* prevAttribute = 0;
// Read the attributes.
while( p ) {
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, curLineNum );
if ( !(*p) ) {
- _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
+ _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name(), _parseLineNum );
return 0;
}
@@ -1766,13 +1780,16 @@ char* XMLElement::ParseAttributes( char* p )
if (XMLUtil::IsNameStartChar( *p ) ) {
TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
+ attrib->_parseLineNum = _document->_parseCurLineNum;
attrib->_memPool = &_document->_attributePool;
attrib->_memPool->SetTracked();
- p = attrib->ParseDeep( p, _document->ProcessEntities() );
+ int attrLineNum = attrib->_parseLineNum;
+
+ p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNum );
if ( !p || Attribute( attrib->Name() ) ) {
DeleteAttribute( attrib );
- _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
+ _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p, attrLineNum );
return 0;
}
// There is a minor bug here: if the attribute in the source xml
@@ -1799,7 +1816,7 @@ char* XMLElement::ParseAttributes( char* p )
return p+2; // done; sealed element.
}
else {
- _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
+ _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p, _parseLineNum );
return 0;
}
}
@@ -1820,10 +1837,10 @@ void XMLElement::DeleteAttribute( XMLAttribute* attribute )
//
// foobar
//
-char* XMLElement::ParseDeep( char* p, StrPair* strPair )
+char* XMLElement::ParseDeep( char* p, StrPair* strPair, int& curLineNum )
{
// Read the element name.
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, curLineNum );
// The closing element is the form. It is
// parsed just like a regular element then deleted from
@@ -1838,12 +1855,12 @@ char* XMLElement::ParseDeep( char* p, StrPair* strPair )
return 0;
}
- p = ParseAttributes( p );
+ p = ParseAttributes( p, curLineNum );
if ( !p || !*p || _closingType ) {
return p;
}
- p = XMLNode::ParseDeep( p, strPair );
+ p = XMLNode::ParseDeep( p, strPair, curLineNum );
return p;
}
@@ -1958,6 +1975,7 @@ void XMLDocument::Clear()
_errorID = XML_SUCCESS;
_errorStr1.Reset();
_errorStr2.Reset();
+ _errorLineNum = 0;
delete [] _charBuffer;
_charBuffer = 0;
@@ -2068,7 +2086,7 @@ XMLError XMLDocument::LoadFile( const char* filename )
Clear();
FILE* fp = callfopen( filename, "rb" );
if ( !fp ) {
- SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
+ SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0, 0 );
return _errorID;
}
LoadFile( fp );
@@ -2105,7 +2123,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
fseek( fp, 0, SEEK_SET );
if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
- SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
return _errorID;
}
@@ -2113,19 +2131,19 @@ XMLError XMLDocument::LoadFile( FILE* fp )
const long filelength = ftell( fp );
fseek( fp, 0, SEEK_SET );
if ( filelength == -1L ) {
- SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
return _errorID;
}
TIXMLASSERT( filelength >= 0 );
if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
// Cannot handle files which won't fit in buffer together with null terminator
- SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
return _errorID;
}
if ( filelength == 0 ) {
- SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
return _errorID;
}
@@ -2134,7 +2152,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
_charBuffer = new char[size+1];
size_t read = fread( _charBuffer, 1, size, fp );
if ( read != size ) {
- SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
+ SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
return _errorID;
}
@@ -2149,7 +2167,7 @@ XMLError XMLDocument::SaveFile( const char* filename, bool compact )
{
FILE* fp = callfopen( filename, "w" );
if ( !fp ) {
- SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
+ SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0, 0 );
return _errorID;
}
SaveFile(fp, compact);
@@ -2162,7 +2180,7 @@ XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
{
// Clear any error from the last save, otherwise it will get reported
// for *this* call.
- SetError(XML_SUCCESS, 0, 0);
+ SetError(XML_SUCCESS, 0, 0, 0);
XMLPrinter stream( fp, compact );
Print( &stream );
return _errorID;
@@ -2174,7 +2192,7 @@ XMLError XMLDocument::Parse( const char* p, size_t len )
Clear();
if ( len == 0 || !p || !*p ) {
- SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
return _errorID;
}
if ( len == (size_t)(-1) ) {
@@ -2212,13 +2230,14 @@ void XMLDocument::Print( XMLPrinter* streamer ) const
}
-void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
+void XMLDocument::SetError( XMLError error, const char* str1, const char* str2, int lineNum )
{
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
_errorID = error;
_errorStr1.Reset();
_errorStr2.Reset();
+ _errorLineNum = lineNum;
if (str1)
_errorStr1.SetStr(str1);
@@ -2256,8 +2275,8 @@ void XMLDocument::PrintError() const
// Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
// causes a clang "always true" -Wtautological-constant-out-of-range-compare warning
TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );
- printf( "XMLDocument error id=%d '%s' str1=%s str2=%s\n",
- static_cast( _errorID ), ErrorName(), buf1, buf2 );
+ printf( "XMLDocument error id=%d '%s' str1=%s str2=%s line=%d\n",
+ static_cast( _errorID ), ErrorName(), buf1, buf2, _errorLineNum );
}
}
@@ -2265,14 +2284,16 @@ void XMLDocument::Parse()
{
TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
TIXMLASSERT( _charBuffer );
+ _parseCurLineNum = 1;
+ _parseLineNum = 1;
char* p = _charBuffer;
- p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::SkipWhiteSpace( p, _parseCurLineNum );
p = const_cast( XMLUtil::ReadBOM( p, &_writeBOM ) );
if ( !*p ) {
- SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
return;
}
- ParseDeep(p, 0 );
+ ParseDeep(p, 0, _parseCurLineNum );
}
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
diff --git a/tinyxml2.h b/tinyxml2.h
index b7f822f..b15da9e 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -160,7 +160,7 @@ public:
void SetStr( const char* str, int flags=0 );
- char* ParseText( char* in, const char* endTag, int strFlags );
+ char* ParseText( char* in, const char* endTag, int strFlags, int& curLineNum );
char* ParseName( char* in );
void TransferTo( StrPair* other );
@@ -530,16 +530,19 @@ enum XMLError {
class XMLUtil
{
public:
- static const char* SkipWhiteSpace( const char* p ) {
+ static const char* SkipWhiteSpace( const char* p, int& curLineNum ) {
TIXMLASSERT( p );
while( IsWhiteSpace(*p) ) {
+ if (*p == '\n') {
+ ++curLineNum;
+ }
++p;
}
TIXMLASSERT( p );
return p;
}
- static char* SkipWhiteSpace( char* p ) {
- return const_cast( SkipWhiteSpace( const_cast(p) ) );
+ static char* SkipWhiteSpace( char* p, int& curLineNum ) {
+ return const_cast( SkipWhiteSpace( const_cast(p), curLineNum ) );
}
// Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
@@ -706,6 +709,9 @@ public:
*/
void SetValue( const char* val, bool staticMem=false );
+ /// Gets the line number the node is in, if the document was parsed from a file.
+ int GetParseLineNum() const { return _parseLineNum; }
+
/// Get the parent of this node on the DOM.
const XMLNode* Parent() const {
return _parent;
@@ -889,11 +895,12 @@ protected:
XMLNode( XMLDocument* );
virtual ~XMLNode();
- virtual char* ParseDeep( char*, StrPair* );
+ virtual char* ParseDeep( char*, StrPair*, int& );
XMLDocument* _document;
XMLNode* _parent;
mutable StrPair _value;
+ int _parseLineNum;
XMLNode* _firstChild;
XMLNode* _lastChild;
@@ -956,7 +963,7 @@ protected:
XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
virtual ~XMLText() {}
- char* ParseDeep( char*, StrPair* endTag );
+ char* ParseDeep( char*, StrPair* endTag, int& curLineNum );
private:
bool _isCData;
@@ -987,7 +994,7 @@ protected:
XMLComment( XMLDocument* doc );
virtual ~XMLComment();
- char* ParseDeep( char*, StrPair* endTag );
+ char* ParseDeep( char*, StrPair* endTag, int& curLineNum);
private:
XMLComment( const XMLComment& ); // not supported
@@ -1026,7 +1033,7 @@ protected:
XMLDeclaration( XMLDocument* doc );
virtual ~XMLDeclaration();
- char* ParseDeep( char*, StrPair* endTag );
+ char* ParseDeep( char*, StrPair* endTag, int& curLineNum );
private:
XMLDeclaration( const XMLDeclaration& ); // not supported
@@ -1061,7 +1068,7 @@ protected:
XMLUnknown( XMLDocument* doc );
virtual ~XMLUnknown();
- char* ParseDeep( char*, StrPair* endTag );
+ char* ParseDeep( char*, StrPair* endTag, int& curLineNum );
private:
XMLUnknown( const XMLUnknown& ); // not supported
@@ -1086,6 +1093,9 @@ public:
/// The value of the attribute.
const char* Value() const;
+ /// Gets the line number the attribute is in, if the document was parsed from a file.
+ int GetParseLineNum() const { return _parseLineNum; }
+
/// The next attribute in the list.
const XMLAttribute* Next() const {
return _next;
@@ -1173,10 +1183,11 @@ private:
void operator=( const XMLAttribute& ); // not supported
void SetName( const char* name );
- char* ParseDeep( char* p, bool processEntities );
+ char* ParseDeep( char* p, bool processEntities, int& curLineNum );
mutable StrPair _name;
mutable StrPair _value;
+ int _parseLineNum;
XMLAttribute* _next;
MemPool* _memPool;
};
@@ -1548,7 +1559,7 @@ public:
virtual bool ShallowEqual( const XMLNode* compare ) const;
protected:
- char* ParseDeep( char* p, StrPair* endTag );
+ char* ParseDeep( char* p, StrPair* endTag, int& curLineNum );
private:
XMLElement( XMLDocument* doc );
@@ -1561,7 +1572,7 @@ private:
}
XMLAttribute* FindOrCreateAttribute( const char* name );
//void LinkAttribute( XMLAttribute* attrib );
- char* ParseAttributes( char* p );
+ char* ParseAttributes( char* p, int& curLineNum );
static void DeleteAttribute( XMLAttribute* attribute );
enum { BUF_SIZE = 200 };
@@ -1738,7 +1749,7 @@ public:
*/
void DeleteNode( XMLNode* node );
- void SetError( XMLError error, const char* str1, const char* str2 );
+ void SetError( XMLError error, const char* str1, const char* str2, int lineNum );
/// Return true if there was an error parsing the document.
bool Error() const {
@@ -1759,6 +1770,11 @@ public:
const char* GetErrorStr2() const {
return _errorStr2.GetStr();
}
+ /// Return the line where the error occured, or zero if unknown.
+ int GetErrorLineNum() const
+ {
+ return _errorLineNum;
+ }
/// If there is an error, print it to stdout.
void PrintError() const;
@@ -1785,7 +1801,9 @@ private:
Whitespace _whitespace;
mutable StrPair _errorStr1;
mutable StrPair _errorStr2;
+ int _errorLineNum;
char* _charBuffer;
+ int _parseCurLineNum;
MemPoolT< sizeof(XMLElement) > _elementPool;
MemPoolT< sizeof(XMLAttribute) > _attributePool;
diff --git a/xmltest.cpp b/xmltest.cpp
index 27dd85c..a4fc5c6 100644
--- a/xmltest.cpp
+++ b/xmltest.cpp
@@ -1640,11 +1640,136 @@ int main( int argc, const char ** argv )
{
XMLDocument doc;
for( int i = 0; i < XML_ERROR_COUNT; i++ ) {
- doc.SetError( (XMLError)i, 0, 0 );
+ doc.SetError( (XMLError)i, 0, 0, 0 );
doc.ErrorName();
}
}
+ // ----------- Line Number Tracking --------------
+ {
+ struct Functor: XMLVisitor
+ {
+ void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
+ {
+ XMLDocument doc;
+ XMLError err = doc.Parse(docStr);
+
+ XMLTest(testString, true, doc.Error());
+ XMLTest(testString, expected_error, err);
+ XMLTest(testString, expectedLine, doc.GetErrorLineNum());
+ };
+
+ void TestStringLines(const char *testString, const char *docStr, const char *expectedLines)
+ {
+ XMLDocument doc;
+ doc.Parse(docStr);
+ XMLTest(testString, false, doc.Error());
+ TestDocLines(testString, doc, expectedLines);
+ }
+
+ void TestFileLines(const char *testString, const char *file_name, const char *expectedLines)
+ {
+ XMLDocument doc;
+ doc.LoadFile(file_name);
+ XMLTest(testString, false, doc.Error());
+ TestDocLines(testString, doc, expectedLines);
+ }
+
+ private:
+ DynArray str;
+
+ void Push(char type, int lineNum)
+ {
+ str.Push(type);
+ str.Push(char('0' + (lineNum / 10)));
+ str.Push(char('0' + (lineNum % 10)));
+ }
+
+ bool VisitEnter(const XMLDocument& doc)
+ {
+ Push('D', doc.GetParseLineNum());
+ return true;
+ }
+ bool VisitEnter(const XMLElement& element, const XMLAttribute* firstAttribute)
+ {
+ Push('E', element.GetParseLineNum());
+ for (const XMLAttribute *attr = firstAttribute; attr != 0; attr = attr->Next())
+ Push('A', attr->GetParseLineNum());
+ return true;
+ }
+ bool Visit(const XMLDeclaration& declaration)
+ {
+ Push('L', declaration.GetParseLineNum());
+ return true;
+ }
+ bool Visit(const XMLText& text)
+ {
+ Push('T', text.GetParseLineNum());
+ return true;
+ }
+ bool Visit(const XMLComment& comment)
+ {
+ Push('C', comment.GetParseLineNum());
+ return true;
+ }
+ bool Visit(const XMLUnknown& unknown)
+ {
+ Push('U', unknown.GetParseLineNum());
+ return true;
+ }
+
+ void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines)
+ {
+ str.Clear();
+ doc.Accept(this);
+ str.Push(0);
+ XMLTest(testString, expectedLines, str.Mem());
+ }
+ } T;
+
+ T.TestParseError("ErrorLine-Parsing", "\n\n foo \n", XML_ERROR_PARSING, 2);
+ T.TestParseError("ErrorLine-Declaration", "\n", XML_ERROR_PARSING_DECLARATION, 2);
+ T.TestParseError("ErrorLine-Mismatch", "\n\n", XML_ERROR_MISMATCHED_ELEMENT, 2);
+ T.TestParseError("ErrorLine-CData", "\n\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
+ T.TestParseError("ErrorLine-Comment", "\n\n\n"
+ "",
+ "D01L01E02A02A03T03E03T04E05T05C06U07");
+
+ T.TestStringLines(
+ "LineNumbers-CRLF",
+ "\r\n"
+ "\n"
+ "\r\n"
+ "\n"
+ "text contining new line \n"
+ " and also containing crlf \r\n"
+ "",
+ "D01L02E03T05E07T07E10");
+
+ T.TestFileLines(
+ "LineNumbers-File",
+ "resources/utf8test.xml",
+ "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");
+ }
+
// ----------- Performance tracking --------------
{
#if defined( _MSC_VER )