rewrite xml stuff #2 - will be testing the monsters xml code soon

This commit is contained in:
niczkx
2012-07-18 08:04:27 +02:00
committed by Eduardo Bart
parent b7b88dd117
commit c8d1d5ecf2
9 changed files with 117 additions and 355 deletions

View File

@@ -433,15 +433,9 @@ const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const
return 0;
}
void TiXmlElement::RemoveAttribute( const char * name )
void TiXmlElement::RemoveAttribute(const std::string &name)
{
#ifdef TIXML_USE_STL
TIXML_STRING str( name );
TiXmlAttribute* node = attributeSet.Find( str );
#else
TiXmlAttribute* node = attributeSet.Find( name );
#endif
TiXmlAttribute* node = attributeSet.Find(name);
if ( node )
{
attributeSet.Remove( node );
@@ -521,24 +515,12 @@ const TiXmlDocument* TiXmlNode::GetDocument() const
return 0;
}
TiXmlElement::TiXmlElement (const char * _value)
: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
{
firstChild = lastChild = 0;
value = _value;
}
#ifdef TIXML_USE_STL
TiXmlElement::TiXmlElement( const std::string& _value )
: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
{
firstChild = lastChild = 0;
value = _value;
}
#endif
TiXmlElement::TiXmlElement( const TiXmlElement& copy)
: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
@@ -573,207 +555,42 @@ void TiXmlElement::ClearThis()
}
}
const char* TiXmlElement::Attribute( const char* name ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( node )
return node->Value();
return 0;
}
#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name ) const
std::string TiXmlElement::Attribute( const std::string& name ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
if ( attrib )
return &attrib->ValueStr();
return 0;
}
#endif
const char* TiXmlElement::Attribute( const char* name, int* i ) const
{
int p = readType<int>(name);
if(i)
*i = p;
return Attribute(name);
return attrib->ValueStr();
return std::string();
}
#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
std::string TiXmlElement::Attribute( const std::string& name, int* i ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
const std::string* result = 0;
std::string result;
if ( attrib ) {
result = &attrib->ValueStr();
result = attrib->ValueStr();
if ( i ) {
attrib->QueryIntValue( i );
}
}
return result;
}
#endif
const char* TiXmlElement::Attribute(const char *name, double *d) const
{
double p = readType<double>(name);
if(d)
*d = p;
return Attribute(name);
}
#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const
std::string TiXmlElement::Attribute( const std::string& name, double* d ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
const std::string* result = 0;
std::string result = 0;
if ( attrib ) {
result = &attrib->ValueStr();
result = attrib->ValueStr();
if ( d ) {
attrib->QueryDoubleValue( d );
}
}
return result;
}
#endif
int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
if ( !attrib )
return TIXML_NO_ATTRIBUTE;
return attrib->QueryIntValue( ival );
}
int TiXmlElement::QueryUnsignedAttribute( const char* name, unsigned* value ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
int ival = 0;
int result = node->QueryIntValue( &ival );
*value = (unsigned)ival;
return result;
}
int TiXmlElement::QueryBoolAttribute( const char* name, bool* bval ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
int result = TIXML_WRONG_TYPE;
if ( StringEqual( node->Value(), "true", true, TIXML_ENCODING_UNKNOWN )
|| StringEqual( node->Value(), "yes", true, TIXML_ENCODING_UNKNOWN )
|| StringEqual( node->Value(), "1", true, TIXML_ENCODING_UNKNOWN ) )
{
*bval = true;
result = TIXML_SUCCESS;
}
else if ( StringEqual( node->Value(), "false", true, TIXML_ENCODING_UNKNOWN )
|| StringEqual( node->Value(), "no", true, TIXML_ENCODING_UNKNOWN )
|| StringEqual( node->Value(), "0", true, TIXML_ENCODING_UNKNOWN ) )
{
*bval = false;
result = TIXML_SUCCESS;
}
return result;
}
#ifdef TIXML_USE_STL
int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
if ( !attrib )
return TIXML_NO_ATTRIBUTE;
return attrib->QueryIntValue( ival );
}
#endif
int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
if ( !attrib )
return TIXML_NO_ATTRIBUTE;
return attrib->QueryDoubleValue( dval );
}
#ifdef TIXML_USE_STL
int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const
{
const TiXmlAttribute* attrib = attributeSet.Find( name );
if ( !attrib )
return TIXML_NO_ATTRIBUTE;
return attrib->QueryDoubleValue( dval );
}
#endif
void TiXmlElement::SetAttribute( const char * name, int val )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
if ( attrib ) {
attrib->SetIntValue( val );
}
}
#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& name, int val )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
if ( attrib ) {
attrib->SetIntValue( val );
}
}
#endif
void TiXmlElement::SetDoubleAttribute( const char * name, double val )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
if ( attrib ) {
attrib->SetDoubleValue( val );
}
}
#ifdef TIXML_USE_STL
void TiXmlElement::SetDoubleAttribute( const std::string& name, double val )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
if ( attrib ) {
attrib->SetDoubleValue( val );
}
}
#endif
void TiXmlElement::SetAttribute( const char * cname, const char * cvalue )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( cname );
if ( attrib ) {
attrib->SetValue( cvalue );
}
}
#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& _name, const std::string& _value )
{
TiXmlAttribute* attrib = attributeSet.FindOrCreate( _name );
@@ -781,7 +598,6 @@ void TiXmlElement::SetAttribute( const std::string& _name, const std::string& _v
attrib->SetValue( _value );
}
}
#endif
void TiXmlElement::Print( FILE* cfile, int depth ) const

View File

@@ -40,7 +40,7 @@ distribution.
#include <string.h>
#include <assert.h>
#include <framework/global.h>
#include <framework/const.h>
#include <otclient/position.h>
#include <framework/stdext/cast.h>
@@ -945,43 +945,21 @@ private:
class TiXmlElement : public TiXmlNode
{
public:
/// Construct an element.
TiXmlElement (const char * in_value);
#ifdef TIXML_USE_STL
/// std::string constructor.
TiXmlElement( const std::string& _value );
#endif
TiXmlElement( const TiXmlElement& );
TiXmlElement& operator=( const TiXmlElement& base );
virtual ~TiXmlElement();
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
*/
const char* Attribute( const char* name ) const;
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
If the attribute exists and can be converted to an integer,
the integer value will be put in the return 'i', if 'i'
is non-null.
*/
const char* Attribute( const char* name, int* i ) const;
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none exists.
If the attribute exists and can be converted to an double,
the double value will be put in the return 'd', if 'd'
is non-null.
*/
const char* Attribute( const char* name, double* d ) const;
template<typename T = std::string>
inline T readType(const std::string& str) const { return stdext::unsafe_cast<T>(Attribute(str)); }
inline T readType(const std::string& str) const
{
T ret;
int r = QueryValueAttribute(str, &ret);
if(r == TIXML_NO_ATTRIBUTE || r == TIXML_WRONG_TYPE)
return T();
return ret;
}
Position readPos(const std::string& base = std::string()) const
{
@@ -992,43 +970,6 @@ public:
{
return Point(readType<int>("x"), readType<int>("y"));
}
/** QueryIntAttribute examines the attribute - it is an alternative to the
Attribute() method with richer error checking.
If the attribute is an integer, it is stored in 'value' and
the call returns TIXML_SUCCESS. If it is not
an integer, it returns TIXML_WRONG_TYPE. If the attribute
does not exist, then TIXML_NO_ATTRIBUTE is returned.
*/
int QueryIntAttribute( const char* name, int* _value ) const;
/// QueryUnsignedAttribute examines the attribute - see QueryIntAttribute().
int QueryUnsignedAttribute( const char* name, unsigned* _value ) const;
/** QueryBoolAttribute examines the attribute - see QueryIntAttribute().
Note that '1', 'true', or 'yes' are considered true, while '0', 'false'
and 'no' are considered false.
*/
int QueryBoolAttribute( const char* name, bool* _value ) const;
/// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
int QueryDoubleAttribute( const char* name, double* _value ) const;
/// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
int QueryFloatAttribute( const char* name, float* _value ) const {
double d;
int result = QueryDoubleAttribute( name, &d );
if ( result == TIXML_SUCCESS ) {
*_value = (float)d;
}
return result;
}
#ifdef TIXML_USE_STL
/// QueryStringAttribute examines the attribute - see QueryIntAttribute().
int QueryStringAttribute( const char* name, std::string* _value ) const {
const char* cstr = Attribute( name );
if ( cstr ) {
*_value = std::string( cstr );
return TIXML_SUCCESS;
}
return TIXML_NO_ATTRIBUTE;
}
/** Template form of the attribute query which will try to read the
attribute into the specified type. Very easy, very powerful, but
@@ -1059,44 +1000,14 @@ public:
*outValue = node->ValueStr();
return TIXML_SUCCESS;
}
#endif
/** Sets an attribute of name to a given value. The attribute
will be created if it does not exist, or changed if it does.
*/
void SetAttribute( const char* name, const char * _value );
std::string Attribute( const std::string& name ) const;
std::string Attribute( const std::string& name, int* i ) const;
std::string Attribute( const std::string& name, double* d ) const;
#ifdef TIXML_USE_STL
const std::string* Attribute( const std::string& name ) const;
const std::string* Attribute( const std::string& name, int* i ) const;
const std::string* Attribute( const std::string& name, double* d ) const;
int QueryIntAttribute( const std::string& name, int* _value ) const;
int QueryDoubleAttribute( const std::string& name, double* _value ) const;
/// STL std::string form.
void SetAttribute( const std::string& name, const std::string& _value );
///< STL std::string form.
void SetAttribute( const std::string& name, int _value );
///< STL std::string form.
void SetDoubleAttribute( const std::string& name, double value );
#endif
/** Sets an attribute of name to a given value. The attribute
will be created if it does not exist, or changed if it does.
*/
void SetAttribute( const char * name, int value );
/** Sets an attribute of name to a given value. The attribute
will be created if it does not exist, or changed if it does.
*/
void SetDoubleAttribute( const char * name, double value );
/** Deletes an attribute with the given name.
*/
void RemoveAttribute( const char * name );
#ifdef TIXML_USE_STL
void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
#endif
void RemoveAttribute( const std::string& name );
const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }