mirror of https://github.com/AxioDL/tinyxml2.git
Error string re-work
This commit is contained in:
parent
3a37e64113
commit
aa1883928b
82
tinyxml2.cpp
82
tinyxml2.cpp
|
@ -165,6 +165,7 @@ void StrPair::TransferTo( StrPair* other )
|
||||||
_end = 0;
|
_end = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StrPair::Reset()
|
void StrPair::Reset()
|
||||||
{
|
{
|
||||||
if ( _flags & NEEDS_DELETE ) {
|
if ( _flags & NEEDS_DELETE ) {
|
||||||
|
@ -1018,7 +1019,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
|
||||||
if ( !p ) {
|
if ( !p ) {
|
||||||
DeleteNode( node );
|
DeleteNode( node );
|
||||||
if ( !_document->Error() ) {
|
if ( !_document->Error() ) {
|
||||||
_document->SetError( XML_ERROR_PARSING, 0, 0, initialLineNum);
|
_document->SetError( XML_ERROR_PARSING, initialLineNum, 0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1038,7 +1039,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( !wellLocated ) {
|
if ( !wellLocated ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0, initialLineNum);
|
_document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "value: %s", decl->Value());
|
||||||
DeleteNode( node );
|
DeleteNode( node );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1073,7 +1074,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( mismatch ) {
|
if ( mismatch ) {
|
||||||
_document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0, initialLineNum);
|
_document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "name=%s", ele->Name());
|
||||||
DeleteNode( node );
|
DeleteNode( node );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1134,7 +1135,7 @@ char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
|
||||||
if ( this->CData() ) {
|
if ( this->CData() ) {
|
||||||
p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
||||||
if ( !p ) {
|
if ( !p ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_CDATA, start, 0, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 );
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -1149,7 +1150,7 @@ char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
|
||||||
return p-1;
|
return p-1;
|
||||||
}
|
}
|
||||||
if ( !p ) {
|
if ( !p ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_TEXT, start, 0, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1200,7 +1201,7 @@ char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
|
||||||
const char* start = p;
|
const char* start = p;
|
||||||
p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr );
|
p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr );
|
||||||
if ( p == 0 ) {
|
if ( p == 0 ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_COMMENT, start, 0, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 );
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -1250,7 +1251,7 @@ char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
|
||||||
const char* start = p;
|
const char* start = p;
|
||||||
p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
||||||
if ( p == 0 ) {
|
if ( p == 0 ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 );
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -1300,7 +1301,7 @@ char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
|
||||||
|
|
||||||
p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
|
||||||
if ( !p ) {
|
if ( !p ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 );
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -1810,7 +1811,7 @@ char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
|
||||||
while( p ) {
|
while( p ) {
|
||||||
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
|
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
|
||||||
if ( !(*p) ) {
|
if ( !(*p) ) {
|
||||||
_document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name(), _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "element name=%s", Name() );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1825,7 +1826,7 @@ char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
|
||||||
p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr );
|
p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr );
|
||||||
if ( !p || Attribute( attrib->Name() ) ) {
|
if ( !p || Attribute( attrib->Name() ) ) {
|
||||||
DeleteAttribute( attrib );
|
DeleteAttribute( attrib );
|
||||||
_document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p, attrLineNum );
|
_document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "element name=%s", Name() );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// There is a minor bug here: if the attribute in the source xml
|
// There is a minor bug here: if the attribute in the source xml
|
||||||
|
@ -1854,7 +1855,7 @@ char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
|
||||||
return p+2; // done; sealed element.
|
return p+2; // done; sealed element.
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_document->SetError( XML_ERROR_PARSING_ELEMENT, start, p, _parseLineNum );
|
_document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2152,7 +2153,7 @@ XMLError XMLDocument::LoadFile( const char* filename )
|
||||||
Clear();
|
Clear();
|
||||||
FILE* fp = callfopen( filename, "rb" );
|
FILE* fp = callfopen( filename, "rb" );
|
||||||
if ( !fp ) {
|
if ( !fp ) {
|
||||||
SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0, 0 );
|
SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
LoadFile( fp );
|
LoadFile( fp );
|
||||||
|
@ -2189,7 +2190,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
|
||||||
|
|
||||||
fseek( fp, 0, SEEK_SET );
|
fseek( fp, 0, SEEK_SET );
|
||||||
if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
|
if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
|
||||||
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
|
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2197,19 +2198,19 @@ XMLError XMLDocument::LoadFile( FILE* fp )
|
||||||
const long filelength = ftell( fp );
|
const long filelength = ftell( fp );
|
||||||
fseek( fp, 0, SEEK_SET );
|
fseek( fp, 0, SEEK_SET );
|
||||||
if ( filelength == -1L ) {
|
if ( filelength == -1L ) {
|
||||||
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
|
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
TIXMLASSERT( filelength >= 0 );
|
TIXMLASSERT( filelength >= 0 );
|
||||||
|
|
||||||
if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
|
if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
|
||||||
// Cannot handle files which won't fit in buffer together with null terminator
|
// Cannot handle files which won't fit in buffer together with null terminator
|
||||||
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
|
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( filelength == 0 ) {
|
if ( filelength == 0 ) {
|
||||||
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
|
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2218,7 +2219,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
|
||||||
_charBuffer = new char[size+1];
|
_charBuffer = new char[size+1];
|
||||||
size_t read = fread( _charBuffer, 1, size, fp );
|
size_t read = fread( _charBuffer, 1, size, fp );
|
||||||
if ( read != size ) {
|
if ( read != size ) {
|
||||||
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 );
|
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2233,7 +2234,7 @@ XMLError XMLDocument::SaveFile( const char* filename, bool compact )
|
||||||
{
|
{
|
||||||
FILE* fp = callfopen( filename, "w" );
|
FILE* fp = callfopen( filename, "w" );
|
||||||
if ( !fp ) {
|
if ( !fp ) {
|
||||||
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0, 0 );
|
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
SaveFile(fp, compact);
|
SaveFile(fp, compact);
|
||||||
|
@ -2258,7 +2259,7 @@ XMLError XMLDocument::Parse( const char* p, size_t len )
|
||||||
Clear();
|
Clear();
|
||||||
|
|
||||||
if ( len == 0 || !p || !*p ) {
|
if ( len == 0 || !p || !*p ) {
|
||||||
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
|
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
||||||
return _errorID;
|
return _errorID;
|
||||||
}
|
}
|
||||||
if ( len == (size_t)(-1) ) {
|
if ( len == (size_t)(-1) ) {
|
||||||
|
@ -2296,21 +2297,29 @@ void XMLDocument::Print( XMLPrinter* streamer ) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void XMLDocument::SetError( XMLError error, const char* str1, const char* str2, int lineNum )
|
void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... )
|
||||||
{
|
{
|
||||||
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
|
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
|
||||||
_errorID = error;
|
_errorID = error;
|
||||||
|
_errorStr.Reset();
|
||||||
|
|
||||||
_errorStr1.Reset();
|
if (format) {
|
||||||
_errorStr2.Reset();
|
size_t BUFFER_SIZE = 1000;
|
||||||
_errorLineNum = lineNum;
|
char* buffer = new char[BUFFER_SIZE];
|
||||||
|
TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d line=%d ", ErrorIDToName(error), int(error), lineNum);
|
||||||
|
size_t len = strlen(buffer);
|
||||||
|
|
||||||
if (str1)
|
va_list va;
|
||||||
_errorStr1.SetStr(str1);
|
va_start( va, format );
|
||||||
if (str2)
|
int result = TIXML_VSNPRINTF( buffer + len, BUFFER_SIZE - len, format, va );
|
||||||
_errorStr2.SetStr(str2);
|
va_end( va );
|
||||||
|
|
||||||
|
_errorStr.SetStr(buffer);
|
||||||
|
delete [] buffer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID)
|
/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID)
|
||||||
{
|
{
|
||||||
TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
|
TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
|
||||||
|
@ -2319,14 +2328,9 @@ void XMLDocument::SetError( XMLError error, const char* str1, const char* str2,
|
||||||
return errorName;
|
return errorName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* XMLDocument::GetErrorStr1() const
|
const char* XMLDocument::GetErrorStr() const
|
||||||
{
|
{
|
||||||
return _errorStr1.GetStr();
|
return _errorStr.Empty() ? "" : _errorStr.GetStr();
|
||||||
}
|
|
||||||
|
|
||||||
const char* XMLDocument::GetErrorStr2() const
|
|
||||||
{
|
|
||||||
return _errorStr2.GetStr();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* XMLDocument::ErrorName() const
|
const char* XMLDocument::ErrorName() const
|
||||||
|
@ -2341,13 +2345,9 @@ void XMLDocument::PrintError() const
|
||||||
char buf1[LEN] = { 0 };
|
char buf1[LEN] = { 0 };
|
||||||
char buf2[LEN] = { 0 };
|
char buf2[LEN] = { 0 };
|
||||||
|
|
||||||
if ( !_errorStr1.Empty() ) {
|
if ( !_errorStr.Empty() ) {
|
||||||
TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1.GetStr() );
|
TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr.GetStr() );
|
||||||
}
|
}
|
||||||
if ( !_errorStr2.Empty() ) {
|
|
||||||
TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2.GetStr() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
|
// Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
|
||||||
// causes a clang "always true" -Wtautological-constant-out-of-range-compare warning
|
// causes a clang "always true" -Wtautological-constant-out-of-range-compare warning
|
||||||
TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );
|
TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );
|
||||||
|
@ -2366,7 +2366,7 @@ void XMLDocument::Parse()
|
||||||
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
|
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
|
||||||
p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
|
p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
|
||||||
if ( !*p ) {
|
if ( !*p ) {
|
||||||
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 );
|
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ParseDeep(p, 0, &_parseCurLineNum );
|
ParseDeep(p, 0, &_parseCurLineNum );
|
||||||
|
|
12
tinyxml2.h
12
tinyxml2.h
|
@ -1785,10 +1785,10 @@ public:
|
||||||
*/
|
*/
|
||||||
void DeleteNode( XMLNode* node );
|
void DeleteNode( XMLNode* node );
|
||||||
|
|
||||||
void SetError( XMLError error, const char* str1, const char* str2, int lineNum );
|
void SetError( XMLError error, int lineNum, const char* format, ... );
|
||||||
|
|
||||||
void ClearError() {
|
void ClearError() {
|
||||||
SetError(XML_SUCCESS, 0, 0, 0);
|
SetError(XML_SUCCESS, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return true if there was an error parsing the document.
|
/// Return true if there was an error parsing the document.
|
||||||
|
@ -1803,10 +1803,7 @@ public:
|
||||||
static const char* ErrorIDToName(XMLError errorID);
|
static const char* ErrorIDToName(XMLError errorID);
|
||||||
|
|
||||||
/// Return a possibly helpful diagnostic location or string.
|
/// Return a possibly helpful diagnostic location or string.
|
||||||
const char* GetErrorStr1() const;
|
const char* GetErrorStr() const;
|
||||||
|
|
||||||
/// Return a possibly helpful secondary diagnostic location or string.
|
|
||||||
const char* GetErrorStr2() const;
|
|
||||||
|
|
||||||
/// Return the line where the error occured, or zero if unknown.
|
/// Return the line where the error occured, or zero if unknown.
|
||||||
int GetErrorLineNum() const
|
int GetErrorLineNum() const
|
||||||
|
@ -1849,8 +1846,7 @@ private:
|
||||||
bool _processEntities;
|
bool _processEntities;
|
||||||
XMLError _errorID;
|
XMLError _errorID;
|
||||||
Whitespace _whitespaceMode;
|
Whitespace _whitespaceMode;
|
||||||
mutable StrPair _errorStr1;
|
mutable StrPair _errorStr;
|
||||||
mutable StrPair _errorStr2;
|
|
||||||
int _errorLineNum;
|
int _errorLineNum;
|
||||||
char* _charBuffer;
|
char* _charBuffer;
|
||||||
int _parseCurLineNum;
|
int _parseCurLineNum;
|
||||||
|
|
Loading…
Reference in New Issue