📄 txml.cpp
字号:
char buf [64];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
#else
sprintf (buf, "%d", _value);
#endif
set_value (buf);
}
void attribute::set_double_value( double _value )
{
char buf [256];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF( buf, sizeof(buf), "%lf", _value);
#else
sprintf (buf, "%lf", _value);
#endif
set_value (buf);
}
int attribute::int_value() const
{
return atoi (value_.c_str ());
}
double attribute::double_value() const
{
return atof (value_.c_str ());
}
comment::comment( const comment& copy_ ) : node( node::COMMENT )
{
copy_.copy_to( this );
}
void comment::operator=( const comment& base_ )
{
clear();
base_.copy_to( this );
}
void comment::print( FILE* cfile, int depth ) const
{
assert( cfile );
for ( int i=0; i<depth; i++ )
{
fprintf( cfile, " " );
}
fprintf( cfile, "<!--%s-->", value_.c_str() );
}
void comment::copy_to( comment* target ) const
{
node::copy_to( target );
}
bool comment::accept( visitor* visitor_ ) const
{
return visitor_->visit( *this );
}
node* comment::clone() const
{
comment* clone_ = new comment();
if ( !clone_ )
return 0;
copy_to( clone_ );
return clone_;
}
void text::print( FILE* cfile, int depth ) const
{
assert( cfile );
if ( cdata )
{
int i;
fprintf( cfile, "\n" );
for ( i=0; i<depth; i++ ) {
fprintf( cfile, " " );
}
fprintf( cfile, "<![CDATA[%s]]>\n", value_.c_str() ); // unformatted output_
}
else
{
TIXML_STRING buffer;
encode_string( value_, &buffer );
fprintf( cfile, "%s", buffer.c_str() );
}
}
void text::copy_to( text* target ) const
{
node::copy_to( target );
target->cdata = cdata;
}
bool text::accept( visitor* visitor_ ) const
{
return visitor_->visit( *this );
}
node* text::clone() const
{
text* clone_ = 0;
clone_ = new text( "" );
if ( !clone_ )
return 0;
copy_to( clone_ );
return clone_;
}
declaration::declaration( const char * _version,
const char * _encoding,
const char * _standalone )
: node( node::DECLARATION )
{
version_ = _version;
encoding_ = _encoding;
standalone_ = _standalone;
}
#ifdef TIXML_USE_STL
declaration::declaration( const std::string& _version,
const std::string& _encoding,
const std::string& _standalone )
: node( node::DECLARATION )
{
version_ = _version;
encoding_ = _encoding;
standalone_ = _standalone;
}
#endif
declaration::declaration( const declaration& copy_ )
: node( node::DECLARATION )
{
copy_.copy_to( this );
}
void declaration::operator=( const declaration& copy_ )
{
clear();
copy_.copy_to( this );
}
void declaration::print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
{
if ( cfile ) fprintf( cfile, "<?xml " );
if ( str ) (*str) += "<?xml ";
if ( !version_.empty() ) {
if ( cfile ) fprintf (cfile, "version=\"%s\" ", version_.c_str ());
if ( str ) { (*str) += "version=\""; (*str) += version_; (*str) += "\" "; }
}
if ( !encoding_.empty() ) {
if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding_.c_str ());
if ( str ) { (*str) += "encoding=\""; (*str) += encoding_; (*str) += "\" "; }
}
if ( !standalone_.empty() ) {
if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone_.c_str ());
if ( str ) { (*str) += "standalone=\""; (*str) += standalone_; (*str) += "\" "; }
}
if ( cfile ) fprintf( cfile, "?>" );
if ( str ) (*str) += "?>";
}
void declaration::copy_to( declaration* target ) const
{
node::copy_to( target );
target->version_ = version_;
target->encoding_ = encoding_;
target->standalone_ = standalone_;
}
bool declaration::accept( visitor* visitor_ ) const
{
return visitor_->visit( *this );
}
node* declaration::clone() const
{
declaration* clone_ = new declaration();
if ( !clone_ )
return 0;
copy_to( clone_ );
return clone_;
}
void unknown::print( FILE* cfile, int depth ) const
{
for ( int i=0; i<depth; i++ )
fprintf( cfile, " " );
fprintf( cfile, "<%s>", value_.c_str() );
}
void unknown::copy_to( unknown* target ) const
{
node::copy_to( target );
}
bool unknown::accept( visitor* visitor_ ) const
{
return visitor_->visit( *this );
}
node* unknown::clone() const
{
unknown* clone_ = new unknown();
if ( !clone_ )
return 0;
copy_to( clone_ );
return clone_;
}
attribute_set::attribute_set()
{
sentinel.next_ = &sentinel;
sentinel.prev = &sentinel;
}
attribute_set::~attribute_set()
{
assert( sentinel.next_ == &sentinel );
assert( sentinel.prev == &sentinel );
}
void attribute_set::add( attribute* addMe )
{
#ifdef TIXML_USE_STL
assert( !find( TIXML_STRING( addMe->name() ) ) ); // Shouldn't be multiply adding to the set.
#else
assert( !find( addMe->name() ) ); // Shouldn't be multiply adding to the set.
#endif
addMe->next_ = &sentinel;
addMe->prev = sentinel.prev;
sentinel.prev->next_ = addMe;
sentinel.prev = addMe;
}
void attribute_set::remove( attribute* removeMe )
{
attribute* 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_.
}
#ifdef TIXML_USE_STL
const attribute* attribute_set::find( const std::string& name_ ) const
{
for( const attribute* node_ = sentinel.next_; node_ != &sentinel; node_ = node_->next_ )
{
if ( node_->name_ == name_ )
return node_;
}
return 0;
}
/*
attribute* attribute_set::find( const std::string& name_ )
{
for( attribute* node_ = sentinel.next_; node_ != &sentinel; node_ = node_->next_ )
{
if ( node_->name_ == name_ )
return node_;
}
return 0;
}
*/
#endif
const attribute* attribute_set::find( const char* name_ ) const
{
for( const attribute* node_ = sentinel.next_; node_ != &sentinel; node_ = node_->next_ )
{
if ( strcmp( node_->name_.c_str(), name_ ) == 0 )
return node_;
}
return 0;
}
/*
attribute* attribute_set::find( const char* name_ )
{
for( attribute* node_ = sentinel.next_; node_ != &sentinel; node_ = node_->next_ )
{
if ( strcmp( node_->name_.c_str(), name_ ) == 0 )
return node_;
}
return 0;
}
*/
#ifdef TIXML_USE_STL
std::istream& operator>> (std::istream & in, node & base_)
{
TIXML_STRING tag;
tag.reserve( 8 * 1000 );
base_.stream_in( &in, &tag );
base_.parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
return in;
}
#endif
#ifdef TIXML_USE_STL
std::ostream& operator<< (std::ostream & out, const node & base_)
{
printer printer_;
printer_.set_stream_printing();
base_.accept( &printer_ );
out << printer_.str();
return out;
}
std::string& operator<< (std::string& out, const node& base_ )
{
printer printer_;
printer_.set_stream_printing();
base_.accept( &printer_ );
out.append( printer_.str() );
return out;
}
#endif
handle handle::first_child() const
{
if ( node_ )
{
node* child_ = node_->first_child();
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::first_child( const char * value_ ) const
{
if ( node_ )
{
node* child_ = node_->first_child( value_ );
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::first_child_element() const
{
if ( node_ )
{
element* child_ = node_->first_child_element();
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::first_child_element( const char * value_ ) const
{
if ( node_ )
{
element* child_ = node_->first_child_element( value_ );
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::child( int count ) const
{
if ( node_ )
{
int i;
node* child_ = node_->first_child();
for ( i=0;
child_ && i<count;
child_ = child_->next_sibling(), ++i )
{
// nothing
}
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::child( const char* value_, int count ) const
{
if ( node_ )
{
int i;
node* child_ = node_->first_child( value_ );
for ( i=0;
child_ && i<count;
child_ = child_->next_sibling( value_ ), ++i )
{
// nothing
}
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::child_element( int count ) const
{
if ( node_ )
{
int i;
element* child_ = node_->first_child_element();
for ( i=0;
child_ && i<count;
child_ = child_->next_sibling_element(), ++i )
{
// nothing
}
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
handle handle::child_element( const char* value_, int count ) const
{
if ( node_ )
{
int i;
element* child_ = node_->first_child_element( value_ );
for ( i=0;
child_ && i<count;
child_ = child_->next_sibling_element( value_ ), ++i )
{
// nothing
}
if ( child_ )
return handle( child_ );
}
return handle( 0 );
}
bool printer::visit_enter( const document& )
{
return true;
}
bool printer::visit_exit( const document& )
{
return true;
}
bool printer::visit_enter( const element& element_, const attribute* firstAttribute )
{
do_indent();
buffer += "<";
buffer += element_.value();
for( const attribute* attrib = firstAttribute; attrib; attrib = attrib->next() )
{
buffer += " ";
attrib->print( 0, 0, &buffer );
}
if ( !element_.first_child() )
{
#if defined (HAL_BOOST_SERIALIZATION_COMPAT)
buffer += ">";
#else
buffer += " />";
do_line_break();
#endif
}
else
{
buffer += ">";
if ( element_.first_child()->to_text()
&& element_.last_child() == element_.first_child()
&& element_.first_child()->to_text()->CDATA() == false )
{
simpleTextPrint = true;
// no_ do_line_break()!
}
else
{
do_line_break();
}
}
++depth;
return true;
}
bool printer::visit_exit( const element& element_ )
{
--depth;
if ( !element_.first_child() )
{
#if defined (HAL_BOOST_SERIALIZATION_COMPAT)
buffer += "</";
buffer += element_.value();
buffer += ">";
do_line_break();
#else
// nothing
#endif
}
else
{
if ( simpleTextPrint )
{
simpleTextPrint = false;
}
else
{
do_indent();
}
buffer += "</";
buffer += element_.value();
buffer += ">";
do_line_break();
}
return true;
}
bool printer::visit( const text& text_ )
{
if ( text_.CDATA() )
{
do_indent();
buffer += "<![CDATA[";
buffer += text_.value();
buffer += "]]>";
do_line_break();
}
else if ( simpleTextPrint )
{
TIXML_STRING str;
base::encode_string( text_.value_t_str(), &str );
buffer += str;
}
else
{
do_indent();
TIXML_STRING str;
base::encode_string( text_.value_t_str(), &str );
buffer += str;
do_line_break();
}
return true;
}
bool printer::visit( const declaration& declaration_ )
{
do_indent();
declaration_.print( 0, 0, &buffer );
do_line_break();
return true;
}
bool printer::visit( const comment& comment_ )
{
do_indent();
buffer += "<!--";
buffer += comment_.value();
buffer += "-->";
do_line_break();
return true;
}
bool printer::visit( const unknown& unknown_ )
{
do_indent();
buffer += "<";
buffer += unknown_.value();
buffer += ">";
do_line_break();
return true;
}
} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -