Main Page   Class Hierarchy   Compound List   Header Files   Compound Members  

tinyxml.h

This is the verbatim text of the tinyxml.h include file.
/*
Copyright (c) 2000 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.
*/


#ifndef TINYXML_INCLUDED
#define TINYXML_INCLUDED

#pragma warning( disable : 4530 )
#pragma warning( disable : 4786 )

#include <string>
#include <stdio.h>
#include <assert.h>

class TiXmlDocument;
class TiXmlElement;
class TiXmlComment;
class TiXmlUnknown;
class TiXmlAttribute;
class TiXmlText;
class TiXmlDeclaration;


class TiXmlBase
{
    friend class TiXmlNode;
    friend class TiXmlElement;
    friend class TiXmlDocument;
 
  public:
    TiXmlBase()                             {}  
    virtual ~TiXmlBase()                    {}
    
    /*  All TinyXml classes can print themselves to a filestream.
    */
    virtual void Print( FILE* fp, int depth )   = 0;
  
  protected:
    /*  General parsing helper method. Takes a pointer in,
        skips all the white space it finds, and returns a pointer
        to the first non-whitespace data.
    */
    static const char* SkipWhiteSpace( const char* p );

    /*  Reads an XML name into the string provided. Returns
        a pointer just past the last character of the name, 
        or 0 if the function has an error.
    */
    static const char* ReadName( const char* p, std::string* name );

    enum
    {
        NO_ERROR = 0,
        ERROR_OPENING_FILE,
        ERROR_OUT_OF_MEMORY,
        ERROR_PARSING_ELEMENT,
        ERROR_FAILED_TO_READ_ELEMENT_NAME,
        ERROR_READING_ELEMENT_VALUE,
        ERROR_READING_ATTRIBUTES,
        ERROR_PARSING_EMPTY,
        ERROR_READING_END_TAG,
        ERROR_PARSING_UNKNOWN,
        ERROR_PARSING_COMMENT,
        ERROR_PARSING_DECLARATION,

        ERROR_STRING_COUNT
    };
    static const char* errorString[ ERROR_STRING_COUNT ];
};


class TiXmlNode : public TiXmlBase
{
  public:
    enum NodeType 
    {
        DOCUMENT, ELEMENT, COMMENT, UNKNOWN, TEXT, DECLARATION, TYPECOUNT
    };

    virtual ~TiXmlNode();

    const std::string& Value()  const           { return value; }

    void SetValue( const std::string& _value )      { value = _value; }

    void Clear();

    TiXmlNode* Parent() const                   { return parent; }

    TiXmlNode* FirstChild() const   { return firstChild; }      
    TiXmlNode* FirstChild( const std::string& value ) const;    
    
    TiXmlNode* LastChild() const    { return lastChild; }       
    TiXmlNode* LastChild( const std::string& value ) const;     

    TiXmlNode* IterateChildren( TiXmlNode* previous );

    TiXmlNode* IterateChildren( const std::string& value, TiXmlNode* previous );
        
    TiXmlNode* InsertEndChild( const TiXmlNode& addThis );                  

    TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );

    TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
    
    TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
    
    bool RemoveChild( TiXmlNode* removeThis );

    TiXmlNode* PreviousSibling() const          { return prev; }

    TiXmlNode* PreviousSibling( const std::string& ) const;
    
    TiXmlNode* NextSibling() const              { return next; }

    TiXmlNode* NextSibling( const std::string& ) const;

    TiXmlElement* NextSiblingElement() const;

    TiXmlElement* NextSiblingElement( const std::string& ) const;

    TiXmlElement* FirstChildElement()   const;
    
    TiXmlElement* FirstChildElement( const std::string& value ) const;

    virtual int Type()  { return type; }

    TiXmlDocument* GetDocument() const;

    TiXmlDocument* ToDocument() const   { return ( type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } 
    TiXmlElement*  ToElement() const    { return ( type == ELEMENT  ) ? (TiXmlElement*)  this : 0; } 
    TiXmlComment*  ToComment() const    { return ( type == COMMENT  ) ? (TiXmlComment*)  this : 0; } 
    TiXmlUnknown*  ToUnknown() const    { return ( type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; } 
    TiXmlText*     ToText()    const    { return ( type == TEXT     ) ? (TiXmlText*)     this : 0; } 
    TiXmlDeclaration* ToDeclaration() const { return ( type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } 

    virtual TiXmlNode* Clone() const = 0;

  protected:
    TiXmlNode( NodeType type );
    virtual const char* Parse( const char* ) = 0;

    // The node is passed in by ownership. This object will delete it.
    TiXmlNode* LinkEndChild( TiXmlNode* addThis );

    // Figure out what is at *p, and parse it. Return a node if
    // successful, and update p.
    TiXmlNode* IdentifyAndParse( const char** p );

    void CopyToClone( TiXmlNode* target ) const { target->value = value; }

    TiXmlNode*      parent;     
    NodeType        type;
    
    TiXmlNode*      firstChild;
    TiXmlNode*      lastChild;

    std::string     value;
    
    TiXmlNode*      prev;
    TiXmlNode*      next;
};


class TiXmlAttribute : public TiXmlBase
{
    friend class TiXmlAttributeSet;

  public:
    TiXmlAttribute() : prev( 0 ), next( 0 ) {}

    TiXmlAttribute( const std::string& _name, const std::string& _value )   : name( _name ), value( _value ), prev( 0 ), next( 0 ) {}

    const std::string& Name()  const { return name; }       
    const std::string& Value() const { return value; }      

    void SetName( const std::string& _name )    { name = _name; }       
    void SetValue( const std::string& _value )  { value = _value; }     

    TiXmlAttribute* Next();
    TiXmlAttribute* Previous();

    bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
    bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
    bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }

    /*  [internal use] 
        Attribtue parsing starts: first letter of the name
                         returns: the next char after the value end quote
    */  
    const char* Parse( const char* );

    // [internal use] 
    virtual void Print( FILE* fp, int depth );

    // [internal use]
    // Set the document pointer so the attribute can report errors.
    void SetDocument( TiXmlDocument* doc )  { document = doc; }

  private:
    TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
    std::string     name;
    std::string     value;

    TiXmlAttribute* prev;
    TiXmlAttribute* next;
};


/*  A class used to manage a group of attributes.
    It is only used internally, both by the ELEMENT and the DECLARATION.
    
    The set can be changed transparent to the Element and Declaration
    classes that use it, but NOT transparent to the Attribute 
    which has to implement a next() and previous() method. Which makes
    it a bit problematic and prevents the use of STL.

    This version is implemented with circular lists because:
        - I like circular lists
        - it demonstrates some independence from the (typical) doubly linked list.
*/
class TiXmlAttributeSet
{
  public:
    TiXmlAttributeSet();
    ~TiXmlAttributeSet();

    void Add( TiXmlAttribute* attribute );
    void Remove( TiXmlAttribute* attribute );

    TiXmlAttribute* First() const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
    TiXmlAttribute* Last()  const   { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
    
    TiXmlAttribute* Find( const std::string& name ) const;

  private:
    TiXmlAttribute sentinel;
};


class TiXmlElement : public TiXmlNode
{
  public:
    TiXmlElement( const std::string& value );

    virtual ~TiXmlElement();

    const std::string* Attribute( const std::string& name ) const;

    const std::string* Attribute( const std::string& name, int* i ) const;

    void SetAttribute( const std::string& name, 
                       const std::string& value );

    void SetAttribute( const std::string& name, 
                       int value );

    void RemoveAttribute( const std::string& name );

    TiXmlAttribute* FirstAttribute()    { return attributeSet.First(); }        
    TiXmlAttribute* LastAttribute()     { return attributeSet.Last(); }     

    // [internal use] Creates a new Element and returs it.
    virtual TiXmlNode* Clone() const;
    // [internal use] 
    virtual void Print( FILE* fp, int depth );

  protected:
    /*  [internal use] 
        Attribtue parsing starts: next char past '<'
                         returns: next char past '>'
    */  
    virtual const char* Parse( const char* );
    const char* ReadValue( const char* p );

  private:
    TiXmlAttributeSet attributeSet;
};


class TiXmlComment : public TiXmlNode
{
  public:
    TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
    virtual ~TiXmlComment() {}

    // [internal use] Creates a new Element and returs it.
    virtual TiXmlNode* Clone() const;
    // [internal use] 
    virtual void Print( FILE* fp, int depth );

  protected:
    /*  [internal use] 
        Attribtue parsing starts: at the ! of the !--
                         returns: next char past '>'
    */  
    virtual const char* Parse( const char* );
};


class TiXmlText : public TiXmlNode
{
  public:
    TiXmlText()  : TiXmlNode( TiXmlNode::TEXT ) {}
    virtual ~TiXmlText() {}


    // [internal use] Creates a new Element and returns it.
    virtual TiXmlNode* Clone() const;
    // [internal use] 
    virtual void Print( FILE* fp, int depth );
    // [internal use]   
    bool Blank();   // returns true if all white space and new lines

    /*  [internal use] 
        Attribtue parsing starts: First char of the text
                         returns: next char past '>'
    */  
    virtual const char* Parse( const char* );
};


class TiXmlDeclaration : public TiXmlNode
{
  public:
    TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}

    TiXmlDeclaration( const std::string& version, 
                      const std::string& encoding,
                      const std::string& standalone );

    virtual ~TiXmlDeclaration() {}

    const std::string& Version()        { return version; }
    const std::string& Encoding()       { return encoding; }
    const std::string& Standalone()     { return standalone; }

    // [internal use] Creates a new Element and returs it.
    virtual TiXmlNode* Clone() const;
    // [internal use] 
    virtual void Print( FILE* fp, int depth );

  protected:
    //  [internal use] 
    //  Attribtue parsing starts: next char past '<'
    //                   returns: next char past '>'
    
    virtual const char* Parse( const char* );

  private:
    std::string version;
    std::string encoding;
    std::string standalone;
};


class TiXmlUnknown : public TiXmlNode
{
  public:
    TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
    virtual ~TiXmlUnknown() {}

    // [internal use]   
    virtual TiXmlNode* Clone() const;
    // [internal use]   
    virtual void Print( FILE* fp, int depth );

  protected:
    /*  [internal use] 
        Attribute parsing starts: First char of the text
                         returns: next char past '>'
    */  
    virtual const char* Parse( const char* );
};


class TiXmlDocument : public TiXmlNode
{
  public:
    TiXmlDocument();
    TiXmlDocument( const std::string& documentName );
    
    virtual ~TiXmlDocument() {}

    bool LoadFile();
    bool SaveFile();
    bool LoadFile( const std::string& filename );
    bool SaveFile( const std::string& filename );

    const char* Parse( const char* );
    
    bool Error()                        { return error; }
    const std::string& ErrorDesc()      { return errorDesc; }

    virtual void Print( FILE* fp, int depth = 0 );
    void Print()                                        { Print( stdout, 0 ); }
  
    // [internal use]   
    virtual TiXmlNode* Clone() const;
    // [internal use]   
    void SetError( int err ) {      assert( err > 0 && err < ERROR_STRING_COUNT );
                                    error   = true; 
                                    errorId = err;
                                    errorDesc = errorString[ errorId ]; }

  private:
    bool error;
    int  errorId;   
    std::string errorDesc;
};


#endif


Generated at Thu Jul 19 21:00:18 2001 for TinyXml by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999