⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txml.cpp

📁 j2me is based on j2mepolish, client & server for mobile application.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)

this software is provided 'as-is', without any express or implied
warranty. In no_ event will the authors be held liable for any
damages arising from the use of this software.

Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it_ and
redistribute it_ freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3. this notice may not be removed or altered from any source
distribution.
*/

#include <ctype.h>

#ifdef TIXML_USE_STL
#include <sstream>
#include <iostream>
#endif

#include "txml.hpp"

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

namespace aux { namespace xml
{

bool base::condenseWhiteSpace = true;

// Microsoft compiler security
FILE* f_open( const char* filename, const char* mode )
{
	#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
		FILE* fp = 0;
		errno_t err = fopen_s( &fp, filename, mode );
		if ( !err && fp )
			return fp;
		return 0;
	#else
		return fopen( filename, mode );
	#endif
}

void base::encode_string( const TIXML_STRING& str, TIXML_STRING* outString )
{
	int i=0;

	while( i<(int)str.length() )
	{
		unsigned char c = (unsigned char) str[i];

		if (    c == '&' 
		     && i < ( (int)str.length() - 2 )
			 && str[i+1] == '#'
			 && str[i+2] == 'x' )
		{
			// Hexadecimal character reference.
			// Pass through unchanged.
			// &#xA9;	-- copyright symbol, for example.
			//
			// The -1 is a bug_ fix from Rob Laveaux. it keeps
			// an overflow from happening if there is no_ ';'.
			// There are_ actually 2 ways to exit this loop -
			// while fails (error_ case) and break (semicolon found).
			// However, there is no_ mechanism (currently) for
			// this function to return an error_.
			while ( i<(int)str.length()-1 )
			{
				outString->append( str.c_str() + i, 1 );
				++i;
				if ( str[i] == ';' )
					break;
			}
		}
		else if ( c == '&' )
		{
			outString->append( entity_[0].str, entity_[0].strLength );
			++i;
		}
		else if ( c == '<' )
		{
			outString->append( entity_[1].str, entity_[1].strLength );
			++i;
		}
		else if ( c == '>' )
		{
			outString->append( entity_[2].str, entity_[2].strLength );
			++i;
		}
		else if ( c == '\"' )
		{
			outString->append( entity_[3].str, entity_[3].strLength );
			++i;
		}
		else if ( c == '\'' )
		{
			outString->append( entity_[4].str, entity_[4].strLength );
			++i;
		}
		else if ( c < 32 )
		{
			// Easy pass at non-alpha/numeric/symbol
			// Below 32 is symbolic.
			char buf[ 32 ];
			
			#if defined(TIXML_SNPRINTF)		
				TIXML_SNPRINTF( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) );
			#else
				sprintf( buf, "&#x%02X;", (unsigned) ( c & 0xff ) );
			#endif		

			//*ME:	warning C4267: convert 'size_t' to 'int'
			//*ME:	Int-Cast to make compiler happy ...
			outString->append( buf, (int)strlen( buf ) );
			++i;
		}
		else
		{
			//char realc = (char) c;
			//outString->append( &realc, 1 );
			*outString += (char) c;	// somewhat_ more efficient function call.
			++i;
		}
	}
}


node::node( node_type _type ) : base()
{
	parent_ = 0;
	type_ = _type;
	firstChild = 0;
	lastChild = 0;
	prev = 0;
	next_ = 0;
}


node::~node()
{
	node* node_ = firstChild;
	node* temp = 0;

	while ( node_ )
	{
		temp = node_;
		node_ = node_->next_;
		delete temp;
	}	
}


void node::copy_to( node* target ) const
{
	target->set_value (value_.c_str() );
	target->userData = userData; 
}


void node::clear()
{
	node* node_ = firstChild;
	node* temp = 0;

	while ( node_ )
	{
		temp = node_;
		node_ = node_->next_;
		delete temp;
	}	

	firstChild = 0;
	lastChild = 0;
}


node* node::link_end_child( node* node_ )
{
	assert( node_->parent_ == 0 || node_->parent_ == this );
	assert( node_->get_document() == 0 || node_->get_document() == this->get_document() );

	if ( node_->type() == node::DOCUMENT )
	{
		delete node_;
		if ( get_document() ) get_document()->set_error( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}

	node_->parent_ = this;

	node_->prev = lastChild;
	node_->next_ = 0;

	if ( lastChild )
		lastChild->next_ = node_;
	else
		firstChild = node_;			// it_ was an empty list.

	lastChild = node_;
	return node_;
}


node* node::insert_end_child( const node& addThis )
{
	if ( addThis.type() == node::DOCUMENT )
	{
		if ( get_document() ) get_document()->set_error( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}
	node* node_ = addThis.clone();
	if ( !node_ )
		return 0;

	return link_end_child( node_ );
}


node* node::insert_before_child( node* beforeThis, const node& addThis )
{	
	if ( !beforeThis || beforeThis->parent_ != this ) {
		return 0;
	}
	if ( addThis.type() == node::DOCUMENT )
	{
		if ( get_document() ) get_document()->set_error( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}

	node* node_ = addThis.clone();
	if ( !node_ )
		return 0;
	node_->parent_ = this;

	node_->next_ = beforeThis;
	node_->prev = beforeThis->prev;
	if ( beforeThis->prev )
	{
		beforeThis->prev->next_ = node_;
	}
	else
	{
		assert( firstChild == beforeThis );
		firstChild = node_;
	}
	beforeThis->prev = node_;
	return node_;
}


node* node::insert_after_child( node* afterThis, const node& addThis )
{
	if ( !afterThis || afterThis->parent_ != this ) {
		return 0;
	}
	if ( addThis.type() == node::DOCUMENT )
	{
		if ( get_document() ) get_document()->set_error( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}

	node* node_ = addThis.clone();
	if ( !node_ )
		return 0;
	node_->parent_ = this;

	node_->prev = afterThis;
	node_->next_ = afterThis->next_;
	if ( afterThis->next_ )
	{
		afterThis->next_->prev = node_;
	}
	else
	{
		assert( lastChild == afterThis );
		lastChild = node_;
	}
	afterThis->next_ = node_;
	return node_;
}


node* node::replace_child( node* replaceThis, const node& withThis )
{
	if ( replaceThis->parent_ != this )
		return 0;

	node* node_ = withThis.clone();
	if ( !node_ )
		return 0;

	node_->next_ = replaceThis->next_;
	node_->prev = replaceThis->prev;

	if ( replaceThis->next_ )
		replaceThis->next_->prev = node_;
	else
		lastChild = node_;

	if ( replaceThis->prev )
		replaceThis->prev->next_ = node_;
	else
		firstChild = node_;

	delete replaceThis;
	node_->parent_ = this;
	return node_;
}


bool node::remove_child( node* removeThis )
{
	if ( removeThis->parent_ != this )
	{	
		assert( 0 );
		return false;
	}

	if ( removeThis->next_ )
		removeThis->next_->prev = removeThis->prev;
	else
		lastChild = removeThis->prev;

	if ( removeThis->prev )
		removeThis->prev->next_ = removeThis->next_;
	else
		firstChild = removeThis->next_;

	delete removeThis;
	return true;
}

const node* node::first_child( const char * _value ) const
{
	const node* node_;
	for ( node_ = firstChild; node_; node_ = node_->next_ )
	{
		if ( strcmp( node_->value(), _value ) == 0 )
			return node_;
	}
	return 0;
}


const node* node::last_child( const char * _value ) const
{
	const node* node_;
	for ( node_ = lastChild; node_; node_ = node_->prev )
	{
		if ( strcmp( node_->value(), _value ) == 0 )
			return node_;
	}
	return 0;
}


const node* node::iterate_children( const node* previous_ ) const
{
	if ( !previous_ )
	{
		return first_child();
	}
	else
	{
		assert( previous_->parent_ == this );
		return previous_->next_sibling();
	}
}


const node* node::iterate_children( const char * val, const node* previous_ ) const
{
	if ( !previous_ )
	{
		return first_child( val );
	}
	else
	{
		assert( previous_->parent_ == this );
		return previous_->next_sibling( val );
	}
}


const node* node::next_sibling( const char * _value ) const 
{
	const node* node_;
	for ( node_ = next_; node_; node_ = node_->next_ )
	{
		if ( strcmp( node_->value(), _value ) == 0 )
			return node_;
	}
	return 0;
}


const node* node::previous_sibling( const char * _value ) const
{
	const node* node_;
	for ( node_ = prev; node_; node_ = node_->prev )
	{
		if ( strcmp( node_->value(), _value ) == 0 )
			return node_;
	}
	return 0;
}


void element::remove_attribute( const char * name_ )
{
    #ifdef TIXML_USE_STL
	TIXML_STRING str( name_ );
	attribute* node_ = attributeSet.find( str );
	#else
	attribute* node_ = attributeSet.find( name_ );
	#endif
	if ( node_ )
	{
		attributeSet.remove( node_ );
		delete node_;
	}
}

const element* node::first_child_element() const
{
	const node* node_;

	for (	node_ = first_child();
			node_;
			node_ = node_->next_sibling() )
	{
		if ( node_->to_element() )
			return node_->to_element();
	}
	return 0;
}


const element* node::first_child_element( const char * _value ) const
{
	const node* node_;

	for (	node_ = first_child( _value );
			node_;
			node_ = node_->next_sibling( _value ) )
	{
		if ( node_->to_element() )
			return node_->to_element();
	}
	return 0;
}


const element* node::next_sibling_element() const
{
	const node* node_;

	for (	node_ = next_sibling();
			node_;
			node_ = node_->next_sibling() )
	{
		if ( node_->to_element() )
			return node_->to_element();
	}
	return 0;
}


const element* node::next_sibling_element( const char * _value ) const
{
	const node* node_;

	for (	node_ = next_sibling( _value );
			node_;
			node_ = node_->next_sibling( _value ) )
	{
		if ( node_->to_element() )
			return node_->to_element();
	}
	return 0;
}


const document* node::get_document() const
{
	const node* node_;

	for( node_ = this; node_; node_ = node_->parent_ )
	{
		if ( node_->to_document() )
			return node_->to_document();
	}
	return 0;
}


element::element (const char * _value)
	: node( node::ELEMENT )
{
	firstChild = lastChild = 0;
	value_ = _value;
}


#ifdef TIXML_USE_STL
element::element( const std::string& _value ) 
	: node( node::ELEMENT )
{
	firstChild = lastChild = 0;
	value_ = _value;
}
#endif


element::element( const element& copy_)
	: node( node::ELEMENT )
{
	firstChild = lastChild = 0;
	copy_.copy_to( this );	
}


void element::operator=( const element& base_ )
{
	clear_this();
	base_.copy_to( this );
}


element::~element()
{
	clear_this();
}


void element::clear_this()
{
	clear();
	while( attributeSet.first() )
	{
		attribute* node_ = attributeSet.first();
		attributeSet.remove( node_ );
		delete node_;
	}
}


const char* element::get_attribute( const char* name_ ) const
{
	const attribute* node_ = attributeSet.find( name_ );
	if ( node_ )
		return node_->value();
	return 0;
}


#ifdef TIXML_USE_STL
const std::string* element::get_attribute( const std::string& name_ ) const
{
	const attribute* node_ = attributeSet.find( name_ );
	if ( node_ )
		return &node_->value_str();
	return 0;
}
#endif


const char* element::get_attribute( const char* name_, int* i ) const
{
	const char* s = get_attribute( name_ );
	if ( i )
	{
		if ( s ) {
			*i = atoi( s );
		}
		else {
			*i = 0;
		}
	}
	return s;
}


#ifdef TIXML_USE_STL
const std::string* element::get_attribute( const std::string& name_, int* i ) const
{
	const std::string* s = get_attribute( name_ );
	if ( i )
	{
		if ( s ) {
			*i = atoi( s->c_str() );
		}
		else {
			*i = 0;
		}
	}
	return s;
}
#endif


const char* element::get_attribute( const char* name_, double* d ) const
{
	const char* s = get_attribute( name_ );
	if ( d )
	{
		if ( s ) {
			*d = atof( s );
		}
		else {
			*d = 0;
		}
	}
	return s;
}


#ifdef TIXML_USE_STL
const std::string* element::get_attribute( const std::string& name_, double* d ) const
{
	const std::string* s = get_attribute( name_ );
	if ( d )
	{
		if ( s ) {
			*d = atof( s->c_str() );
		}
		else {
			*d = 0;
		}
	}
	return s;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -