📄 tinyxml.cpp
字号:
firstChild->Print( cfile, depth + 1 ); fprintf( cfile, "</%s>", value.c_str() ); } else { fprintf( cfile, ">" ); for ( node = firstChild; node; node=node->NextSibling() ) { if ( !node->ToText() ) { fprintf( cfile, "\n" ); } node->Print( cfile, depth+1 ); } fprintf( cfile, "\n" ); for( i=0; i<depth; ++i ) fprintf( cfile, " " ); fprintf( cfile, "</%s>", value.c_str() ); }}void TiXmlElement::StreamOut( std::ostream* stream ) const{ (*stream) << "<" << value; TiXmlAttribute* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { (*stream) << " "; attrib->StreamOut( stream ); } // If this node has children, give it a closing tag. Else // make it an empty tag. TiXmlNode* node; if ( firstChild ) { (*stream) << ">"; for ( node = firstChild; node; node=node->NextSibling() ) { node->StreamOut( stream ); } (*stream) << "</" << value << ">"; } else { (*stream) << " />"; }}TiXmlNode* TiXmlElement::Clone() const{ TiXmlElement* clone = new TiXmlElement( Value() ); if ( !clone ) return 0; CopyToClone( clone ); // Clone the attributes, then clone the children. TiXmlAttribute* attribute = 0; for( attribute = attributeSet.First(); attribute; attribute = attribute->Next() ) { clone->SetAttribute( attribute->Name(), attribute->Value() ); } TiXmlNode* node = 0; for ( node = firstChild; node; node = node->NextSibling() ) { clone->LinkEndChild( node->Clone() ); } return clone;}TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::DOCUMENT ){ error = false;// ignoreWhiteSpace = true;}TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::DOCUMENT ){// ignoreWhiteSpace = true; value = documentName; error = false;}bool TiXmlDocument::LoadFile(){ return LoadFile( value );}bool TiXmlDocument::SaveFile() const{ return SaveFile( value );}bool TiXmlDocument::LoadFile( const std::string& filename ){ // Delete the existing data: Clear(); value = filename; FILE* file = fopen( filename.c_str(), "r" ); if ( file ) { // Get the file size, so we can pre-allocate the string. HUGE speed impact. long length = 0; fseek( file, 0, SEEK_END ); length = ftell( file ); fseek( file, 0, SEEK_SET ); // If we have a file, assume it is all one big XML file, and read it in. // The document parser may decide the document ends sooner than the entire file, however. std::string data; data.reserve( length ); const int BUF_SIZE = 2048; char buf[BUF_SIZE]; while( fgets( buf, BUF_SIZE, file ) ) { data += buf; } fclose( file ); Parse( data.c_str() ); if ( !Error() ) { return true; } } SetError( TIXML_ERROR_OPENING_FILE ); return false;}bool TiXmlDocument::SaveFile( const std::string& filename ) const{ // The old c stuff lives on... FILE* fp = fopen( filename.c_str(), "w" ); if ( fp ) { Print( fp, 0 ); fclose( fp ); return true; } return false;}TiXmlNode* TiXmlDocument::Clone() const{ TiXmlDocument* clone = new TiXmlDocument(); if ( !clone ) return 0; CopyToClone( clone ); clone->error = error; clone->errorDesc = errorDesc; TiXmlNode* node = 0; for ( node = firstChild; node; node = node->NextSibling() ) { clone->LinkEndChild( node->Clone() ); } return clone;}void TiXmlDocument::Print( FILE* cfile, int depth ) const{ TiXmlNode* node; for ( node=FirstChild(); node; node=node->NextSibling() ) { node->Print( cfile, depth ); fprintf( cfile, "\n" ); }}void TiXmlDocument::StreamOut( std::ostream* out ) const{ TiXmlNode* node; for ( node=FirstChild(); node; node=node->NextSibling() ) { node->StreamOut( out ); // Special rule for streams: stop after the root element. // The stream in code will only read one element, so don't // write more than one. if ( node->ToElement() ) break; }}TiXmlAttribute* TiXmlAttribute::Next() const{ // We are using knowledge of the sentinel. The sentinel // have a value or name. if ( next->value.empty() && next->name.empty() ) return 0; return next;}TiXmlAttribute* TiXmlAttribute::Previous() const{ // We are using knowledge of the sentinel. The sentinel // have a value or name. if ( prev->value.empty() && prev->name.empty() ) return 0; return prev;}void TiXmlAttribute::Print( FILE* cfile, int /*depth*/ ) const{ ostringstream stream( ostringstream::out ); stream.str().reserve( 500 ); StreamOut( &stream ); fprintf( cfile, "%s", stream.str().c_str() );}void TiXmlAttribute::StreamOut( std::ostream* stream ) const{ if ( value.find( '\"' ) != std::string::npos ) { PutString( name, stream ); (*stream) << "=" << "'"; PutString( value, stream ); (*stream) << "'"; } else { PutString( name, stream ); (*stream) << "=" << "\""; PutString( value, stream ); (*stream) << "\""; }}void TiXmlAttribute::SetIntValue( int value ){ std::string s; std::ostringstream stream( s ); stream << value; SetValue( stream.str() );}void TiXmlAttribute::SetDoubleValue( double value ){ std::string s; std::ostringstream stream( s ); stream << value; SetValue( stream.str() );}const int TiXmlAttribute::IntValue() const{ int v; std::istringstream string( value ); string >> v; return v;}const double TiXmlAttribute::DoubleValue() const{ double v; std::istringstream string( value ); string >> v; return v;}void TiXmlComment::Print( FILE* cfile, int depth ) const{ ostringstream stream( ostringstream::out ); stream.str().reserve( 1000 ); for ( int i=0; i<depth; i++ ) { fprintf( cfile, " " ); } StreamOut( &stream ); fprintf( cfile, "%s", stream.str().c_str() );}void TiXmlComment::StreamOut( std::ostream* stream ) const{ (*stream) << "<!--"; PutString( value, stream ); (*stream) << "-->";}TiXmlNode* TiXmlComment::Clone() const{ TiXmlComment* clone = new TiXmlComment(); if ( !clone ) return 0; CopyToClone( clone ); return clone;}void TiXmlText::Print( FILE* cfile, int depth ) const{ ostringstream stream( ostringstream::out ); stream.str().reserve( 1000 ); StreamOut( &stream ); fprintf( cfile, "%s", stream.str().c_str() );}void TiXmlText::StreamOut( std::ostream* stream ) const{ PutString( value, stream );}TiXmlNode* TiXmlText::Clone() const{ TiXmlText* clone = 0; clone = new TiXmlText( "" ); if ( !clone ) return 0; CopyToClone( clone ); return clone;}TiXmlDeclaration::TiXmlDeclaration( const std::string& _version, const std::string& _encoding, const std::string& _standalone ) : TiXmlNode( TiXmlNode::DECLARATION ) { version = _version; encoding = _encoding; standalone = _standalone;}void TiXmlDeclaration::Print( FILE* cfile, int depth ) const{ ostringstream stream( ostringstream::out ); stream.str().reserve( 200 ); StreamOut( &stream ); fprintf( cfile, "%s", stream.str().c_str() );}void TiXmlDeclaration::StreamOut( std::ostream* stream ) const{ (*stream) << "<?xml "; if ( !version.empty() ) { (*stream) << "version=\""; PutString( version, stream ); (*stream) << "\" "; } if ( !encoding.empty() ) { (*stream) << "encoding=\""; PutString( encoding, stream ); (*stream ) << "\" "; } if ( !standalone.empty() ) { (*stream) << "standalone=\""; PutString( standalone, stream ); (*stream) << "\" "; } (*stream) << "?>";}TiXmlNode* TiXmlDeclaration::Clone() const{ TiXmlDeclaration* clone = new TiXmlDeclaration(); if ( !clone ) return 0; CopyToClone( clone ); clone->version = version; clone->encoding = encoding; clone->standalone = standalone; return clone;}void TiXmlUnknown::Print( FILE* cfile, int depth ) const{ ostringstream stream( ostringstream::out ); stream.str().reserve( 200 ); StreamOut( &stream ); for ( int i=0; i<depth; i++ ) fprintf( cfile, " " ); fprintf( cfile, "%s", stream.str().c_str() );}void TiXmlUnknown::StreamOut( std::ostream* stream ) const{ (*stream) << "<" << value << ">"; // Don't use entities hear! It is unknown.}TiXmlNode* TiXmlUnknown::Clone() const{ TiXmlUnknown* clone = new TiXmlUnknown(); if ( !clone ) return 0; CopyToClone( clone ); return clone;}TiXmlAttributeSet::TiXmlAttributeSet(){ sentinel.next = &sentinel; sentinel.prev = &sentinel;}TiXmlAttributeSet::~TiXmlAttributeSet(){ assert( sentinel.next == &sentinel ); assert( sentinel.prev == &sentinel );}void TiXmlAttributeSet::Add( TiXmlAttribute* addMe ){ assert( !Find( addMe->Name() ) ); // Shouldn't be multiply adding to the set. addMe->next = &sentinel; addMe->prev = sentinel.prev; sentinel.prev->next = addMe; sentinel.prev = addMe;}void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe ){ TiXmlAttribute* node; for( node = sentinel.next; node != &sentinel; node = node->next ) { if ( node == removeMe ) { node->prev->next = node->next; node->next->prev = node->prev; node->next = 0; node->prev = 0; return; } } assert( 0 ); // we tried to remove a non-linked attribute.}TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const{ TiXmlAttribute* node; for( node = sentinel.next; node != &sentinel; node = node->next ) { if ( node->Name() == name ) return node; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -