📄 tinyxmlparser.cpp
字号:
}
TIXML_STRING endTag ("</");
endTag += value;
endTag += ">";
// Check for and read attributes. Also look for an empty
// tag or an end tag.
while ( p && *p )
{
pErr = p;
p = SkipWhiteSpace( p, encoding );
if ( !p || !*p )
{
if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
return 0;
}
if ( *p == '/' )
{
++p;
// Empty tag.
if ( *p != '>' )
{
if ( document ) document->SetError( TIXML_ERROR_PARSING_EMPTY, p, data, encoding );
return 0;
}
return (p+1);
}
else if ( *p == '>' )
{
// Done with attributes (if there were any.)
// Read the value -- which can include other
// elements -- read the end tag, and return.
++p;
p = ReadValue( p, data, encoding ); // Note this is an Element method, and will set the error if one happens.
if ( !p || !*p )
return 0;
// We should find the end tag now
if ( StringEqual( p, endTag.c_str(), false, encoding ) )
{
p += endTag.length();
return p;
}
else
{
if ( document ) document->SetError( TIXML_ERROR_READING_END_TAG, p, data, encoding );
return 0;
}
}
else
{
// Try to read an attribute:
TiXmlAttribute* attrib = new TiXmlAttribute();
if ( !attrib )
{
if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, pErr, data, encoding );
return 0;
}
attrib->SetDocument( document );
const char* pErr = p;
p = attrib->Parse( p, data, encoding );
if ( !p || !*p )
{
if ( document ) document->SetError( TIXML_ERROR_PARSING_ELEMENT, pErr, data, encoding );
delete attrib;
return 0;
}
// Handle the strange case of double attributes:
TiXmlAttribute* node = attributeSet.Find( attrib->NameTStr() );
if ( node )
{
node->SetValue( attrib->Value() );
delete attrib;
return 0;
}
attributeSet.Add( attrib );
}
}
return p;
}
const char* TiXmlElement::ReadValue( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
TiXmlDocument* document = GetDocument();
// Read in text and elements in any order.
const char* pWithWhiteSpace = p;
p = SkipWhiteSpace( p, encoding );
while ( p && *p )
{
if ( *p != '<' )
{
// Take what we have, make a text element.
TiXmlText* textNode = new TiXmlText( "" );
if ( !textNode )
{
if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, encoding );
return 0;
}
if ( TiXmlBase::IsWhiteSpaceCondensed() )
{
p = textNode->Parse( p, data, encoding );
}
else
{
// Special case: we want to keep the white space
// so that leading spaces aren't removed.
p = textNode->Parse( pWithWhiteSpace, data, encoding );
}
if ( !textNode->Blank() )
LinkEndChild( textNode );
else
delete textNode;
}
else
{
// We hit a '<'
// Have we hit a new element or an end tag? This could also be
// a TiXmlText in the "CDATA" style.
if ( StringEqual( p, "</", false, encoding ) )
{
return p;
}
else
{
TiXmlNode* node = Identify( p, encoding );
if ( node )
{
p = node->Parse( p, data, encoding );
LinkEndChild( node );
}
else
{
return 0;
}
}
}
pWithWhiteSpace = p;
p = SkipWhiteSpace( p, encoding );
}
if ( !p )
{
if ( document ) document->SetError( TIXML_ERROR_READING_ELEMENT_VALUE, 0, 0, encoding );
}
return p;
}
#ifdef TIXML_USE_STL
void TiXmlUnknown::StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag )
{
while ( in->good() )
{
int c = in->get();
if ( c <= 0 )
{
TiXmlDocument* document = GetDocument();
if ( document )
document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
return;
}
(*tag) += (char) c;
if ( c == '>' )
{
// All is well.
return;
}
}
}
#endif
const char* TiXmlUnknown::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
TiXmlDocument* document = GetDocument();
p = SkipWhiteSpace( p, encoding );
if ( data )
{
data->Stamp( p, encoding );
location = data->Cursor();
}
if ( !p || !*p || *p != '<' )
{
if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, p, data, encoding );
return 0;
}
++p;
value = "";
while ( p && *p && *p != '>' )
{
value += *p;
++p;
}
if ( !p )
{
if ( document ) document->SetError( TIXML_ERROR_PARSING_UNKNOWN, 0, 0, encoding );
}
if ( *p == '>' )
return p+1;
return p;
}
#ifdef TIXML_USE_STL
void TiXmlComment::StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag )
{
while ( in->good() )
{
int c = in->get();
if ( c <= 0 )
{
TiXmlDocument* document = GetDocument();
if ( document )
document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
return;
}
(*tag) += (char) c;
if ( c == '>'
&& tag->at( tag->length() - 2 ) == '-'
&& tag->at( tag->length() - 3 ) == '-' )
{
// All is well.
return;
}
}
}
#endif
const char* TiXmlComment::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
TiXmlDocument* document = GetDocument();
value = "";
p = SkipWhiteSpace( p, encoding );
if ( data )
{
data->Stamp( p, encoding );
location = data->Cursor();
}
const char* startTag = "<!--";
const char* endTag = "-->";
if ( !StringEqual( p, startTag, false, encoding ) )
{
document->SetError( TIXML_ERROR_PARSING_COMMENT, p, data, encoding );
return 0;
}
p += strlen( startTag );
p = ReadText( p, &value, false, endTag, false, encoding );
return p;
}
const char* TiXmlAttribute::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
p = SkipWhiteSpace( p, encoding );
if ( !p || !*p ) return 0;
int tabsize = 4;
if ( document )
tabsize = document->TabSize();
if ( data )
{
data->Stamp( p, encoding );
location = data->Cursor();
}
// Read the name, the '=' and the value.
const char* pErr = p;
p = ReadName( p, &name, encoding );
if ( !p || !*p )
{
if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding );
return 0;
}
p = SkipWhiteSpace( p, encoding );
if ( !p || !*p || *p != '=' )
{
if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
return 0;
}
++p; // skip '='
p = SkipWhiteSpace( p, encoding );
if ( !p || !*p )
{
if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding );
return 0;
}
const char* end;
if ( *p == '\'' )
{
++p;
end = "\'";
p = ReadText( p, &value, false, end, false, encoding );
}
else if ( *p == '"' )
{
++p;
end = "\"";
p = ReadText( p, &value, false, end, false, encoding );
}
else
{
// All attribute values should be in single or double quotes.
// But this is such a common error that the parser will try
// its best, even without them.
value = "";
while ( p && *p // existence
&& !IsWhiteSpace( *p ) && *p != '\n' && *p != '\r' // whitespace
&& *p != '/' && *p != '>' ) // tag end
{
value += *p;
++p;
}
}
return p;
}
#ifdef TIXML_USE_STL
void TiXmlText::StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag )
{
if ( cdata )
{
int c = in->get();
if ( c <= 0 )
{
TiXmlDocument* document = GetDocument();
if ( document )
document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
return;
}
(*tag) += (char) c;
if ( c == '>'
&& tag->at( tag->length() - 2 ) == ']'
&& tag->at( tag->length() - 3 ) == ']' )
{
// All is well.
return;
}
}
else
{
while ( in->good() )
{
int c = in->peek();
if ( c == '<' )
return;
if ( c <= 0 )
{
TiXmlDocument* document = GetDocument();
if ( document )
document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
return;
}
(*tag) += (char) c;
in->get();
}
}
}
#endif
const char* TiXmlText::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
value = "";
TiXmlDocument* document = GetDocument();
if ( data )
{
data->Stamp( p, encoding );
location = data->Cursor();
}
const char* const startTag = "<![CDATA[";
const char* const endTag = "]]>";
if ( cdata || StringEqual( p, startTag, false, encoding ) )
{
cdata = true;
if ( !StringEqual( p, startTag, false, encoding ) )
{
document->SetError( TIXML_ERROR_PARSING_CDATA, p, data, encoding );
return 0;
}
p += strlen( startTag );
// Keep all the white space, ignore the encoding, etc.
while ( p && *p
&& !StringEqual( p, endTag, false, encoding )
)
{
value += *p;
++p;
}
TIXML_STRING dummy;
p = ReadText( p, &dummy, false, endTag, false, encoding );
return p;
}
else
{
bool ignoreWhite = true;
const char* end = "<";
p = ReadText( p, &value, ignoreWhite, end, false, encoding );
if ( p )
return p-1; // don't truncate the '<'
return 0;
}
}
#ifdef TIXML_USE_STL
void TiXmlDeclaration::StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag )
{
while ( in->good() )
{
int c = in->get();
if ( c <= 0 )
{
TiXmlDocument* document = GetDocument();
if ( document )
document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN );
return;
}
(*tag) += (char) c;
if ( c == '>' )
{
// All is well.
return;
}
}
}
#endif
const char* TiXmlDeclaration::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding _encoding )
{
p = SkipWhiteSpace( p, _encoding );
// Find the beginning, find the end, and look for
// the stuff in-between.
TiXmlDocument* document = GetDocument();
if ( !p || !*p || !StringEqual( p, "<?xml", true, _encoding ) )
{
if ( document ) document->SetError( TIXML_ERROR_PARSING_DECLARATION, 0, 0, _encoding );
return 0;
}
if ( data )
{
data->Stamp( p, _encoding );
location = data->Cursor();
}
p += 5;
version = "";
encoding = "";
standalone = "";
while ( p && *p )
{
if ( *p == '>' )
{
++p;
return p;
}
p = SkipWhiteSpace( p, _encoding );
if ( StringEqual( p, "version", true, _encoding ) )
{
TiXmlAttribute attrib;
p = attrib.Parse( p, data, _encoding );
version = attrib.Value();
}
else if ( StringEqual( p, "encoding", true, _encoding ) )
{
TiXmlAttribute attrib;
p = attrib.Parse( p, data, _encoding );
encoding = attrib.Value();
}
else if ( StringEqual( p, "standalone", true, _encoding ) )
{
TiXmlAttribute attrib;
p = attrib.Parse( p, data, _encoding );
standalone = attrib.Value();
}
else
{
// Read over whatever it is.
while( p && *p && *p != '>' && !IsWhiteSpace( *p ) )
++p;
}
}
return 0;
}
bool TiXmlText::Blank() const
{
for ( unsigned i=0; i<value.length(); i++ )
if ( !IsWhiteSpace( value[i] ) )
return false;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -