XMLParser.h 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Génevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef __XMLPARSER_H__
#define __XMLPARSER_H__




#include <vector>
#include <list>
#include <cassert>


/*
 *
 *	CLASSES DECLARATIONS.
 *
 */


class XMLAttribute;
class XMLNode;
class XMLParser;


class XMLAttribute
{
	/********************\
	| Member variable(s) |
	\********************/
private:
	const char*   m_Name;
	const char*   m_Value;

	/*****************************\
	| Constructor(s) / destructor |
	\*****************************/
public:
	inline XMLAttribute() : m_Name(NULL), m_Value(NULL)                                         {}
    inline XMLAttribute( const char* name, const char* value ) : m_Name(name), m_Value(value)   {}

	/********************\
	| Member function(s) |
	\********************/
public:
	inline void             setName( const char* name )     { m_Name = name; }
	inline const char*      getName() const                 { return m_Name; }
    inline  bool            caseInsensitiveNameCmp( const char *name ) const;

	inline void             setValue( const char* value )   { m_Value = value; }
	inline const char*      getValue() const                { return m_Value; }
	template <typename T>
	inline bool				getValue( T &val ) const;
};


class XMLNode
{
	friend class	XMLParser;

	/******************\
	| Internal type(s) |
	\******************/
private:
	typedef std::vector<XMLAttribute>   AttribVec;
	typedef std::vector<XMLNode>        NodeVec;

public:
	class ChildIterator
	{
		friend class XMLNode;
		friend class ConstChildIterator;

		NodeVec::iterator	m_Ptr;

        inline	ChildIterator( NodeVec::iterator it ) : m_Ptr(it)           {}

	public:
        inline bool			    operator==( const ChildIterator& it ) const { return m_Ptr == it.m_Ptr; }
        inline bool			    operator!=( const ChildIterator& it ) const { return m_Ptr != it.m_Ptr; }
        inline ChildIterator&	operator++()                                { ++m_Ptr; return *this;    }
        inline ChildIterator&	operator--()                                { --m_Ptr; return *this;    }
        inline XMLNode&		    operator*()                                 { return *m_Ptr;            }
        inline XMLNode*		    operator->()                                { return &*m_Ptr;           }
	};

    class ConstChildIterator
	{
		friend class XMLNode;

		NodeVec::const_iterator	m_Ptr;

        inline  ConstChildIterator( NodeVec::const_iterator it ) : m_Ptr(it)            {}

	public:
        inline ConstChildIterator( const ChildIterator& it ) : m_Ptr(it.m_Ptr)          {}
        inline ConstChildIterator&	operator=( const ChildIterator& it )                { m_Ptr = it.m_Ptr; return *this; }
        inline bool                 operator==( const ConstChildIterator& it ) const    { return m_Ptr == it.m_Ptr;       }
        inline bool                 operator!=( const ConstChildIterator& it ) const    { return m_Ptr != it.m_Ptr;       }
        inline bool                 operator==( const ChildIterator& it ) const         { return m_Ptr == it.m_Ptr;       }
        inline bool                 operator!=( const ChildIterator& it ) const         { return m_Ptr != it.m_Ptr;       }
        inline ConstChildIterator&	operator++()                                        { ++m_Ptr; return *this;          }
        inline ConstChildIterator&	operator--()                                        { --m_Ptr; return *this;          }
        inline const XMLNode&		operator*() const                                   { return *m_Ptr;                  }
        inline const XMLNode*		operator->() const                                  { return &*m_Ptr;                 }
	};
    
    class AttrIterator
	{
		friend class XMLNode;

		AttribVec::iterator	m_Ptr;

        inline	AttrIterator( AttribVec::iterator it ) : m_Ptr(it)          {}

	public:
        inline bool			    operator==( const AttrIterator& it ) const  { return m_Ptr == it.m_Ptr; }
        inline bool			    operator!=( const AttrIterator& it ) const  { return m_Ptr != it.m_Ptr; }
        inline AttrIterator&    operator++()                                { ++m_Ptr; return *this;    }
        inline AttrIterator&    operator--()                                { --m_Ptr; return *this;    }
        inline XMLAttribute&    operator*()                                 { return *m_Ptr;            }
        inline XMLAttribute*    operator->()                                { return &*m_Ptr;           }
	};

	class ConstAttrIterator
	{
		friend class XMLNode;

		AttribVec::const_iterator	m_Ptr;

        inline	ConstAttrIterator( AttribVec::const_iterator it ) : m_Ptr(it)       {}

	public:
        inline ConstAttrIterator( const AttrIterator& it ) : m_Ptr(it.m_Ptr)        {}
        inline ConstAttrIterator&   operator=( const AttrIterator& it )             { m_Ptr = it.m_Ptr; return *this; }
        inline bool                 operator==( const ConstAttrIterator& it ) const { return m_Ptr == it.m_Ptr;       }
        inline bool                 operator!=( const ConstAttrIterator& it ) const { return m_Ptr != it.m_Ptr;       }
        inline bool                 operator==( const AttrIterator& it ) const      { return m_Ptr == it.m_Ptr;       }
        inline bool                 operator!=( const AttrIterator& it ) const      { return m_Ptr != it.m_Ptr;       }
        inline ConstAttrIterator&   operator++()                                    { ++m_Ptr; return *this;          }
        inline ConstAttrIterator&   operator--()                                    { --m_Ptr; return *this;          }
        inline const XMLAttribute&  operator*() const                               { return *m_Ptr;                  }
        inline const XMLAttribute*  operator->() const                              { return &*m_Ptr;                 }
	};

	/********************\
	| Member variable(s) |
	\********************/
private:
	XMLNode     *m_Parent;

	const char* m_Name;
	const char* m_Text;

	AttribVec   m_Attrib;
	NodeVec     m_Children;

	/*****************************\
	| Constructor(s) / destructor |
	\*****************************/
public:
    inline	XMLNode() : m_Parent(NULL), m_Name(NULL), m_Text(NULL)  {}

	/********************\
	| Member function(s) |
	\********************/
private:
    unsigned int				parse( XMLParser& parser );
    unsigned int                parseAttributes( XMLParser& parser );
    unsigned int                parseChildren( XMLParser& parser );

public:
    inline	bool				isElement() const                                   { return m_Name && !m_Text; }
    inline	const char*			getElementName() const                              { return m_Name; }
    inline  bool                caseInsensitiveNameCmp( const char *name ) const;
    inline	bool				isText() const                                      { return !m_Name && m_Text; }
    inline	const char*			getText() const                                     { return m_Text; }

	inline	bool				hasParent() const	                                { return ( m_Parent != NULL ); }
	inline	XMLNode*			getParent()			                                { return m_Parent; }
	inline	const XMLNode*		getParent() const	                                { return m_Parent; }
    inline	bool				hasChildren() const                                 { return !m_Children.empty(); }
    inline	unsigned int		getNbChildren() const                               { return (unsigned int)m_Children.size(); }
    inline	ChildIterator		childBegin()                                        { return ChildIterator( m_Children.begin() );      }
    inline	ConstChildIterator	childBegin() const                                  { return ConstChildIterator( m_Children.begin() ); }
    inline	ChildIterator		childEnd()                                          { return ChildIterator( m_Children.end() );        }
    inline	ConstChildIterator	childEnd() const                                    { return ConstChildIterator( m_Children.end() );   }

    inline	unsigned int		getAttrCount() const                                { return (unsigned int)m_Attrib.size(); }
	inline	bool				hasAttribute( const std::string& attribName ) const;
    inline	AttrIterator		attrBegin()                                         { return AttrIterator( m_Attrib.begin() );      }
    inline	ConstAttrIterator	attrBegin() const                                   { return ConstAttrIterator( m_Attrib.begin() ); }
    inline	AttrIterator		attrEnd()                                           { return AttrIterator( m_Attrib.end() );        }
    inline	ConstAttrIterator	attrEnd() const                                     { return ConstAttrIterator( m_Attrib.end() );   }

	inline	XMLAttribute*		operator[]( const std::string& attribName );
	inline	const XMLAttribute*	operator[]( const std::string& attribName ) const;
};


class XMLParser
{
	friend class XMLNode;

	/******************\
	| Internal type(s) |
	\******************/
public:
    enum ErrorCode
    {
        ERR__NO_ERROR               ,
        ERR__PARSE_ERROR            ,
        ERR__ELEMENT_NAME_FORMAT    ,
        ERR__ELEMENT_CLOSURE        ,
        ERR__TAG_CLOSURE            ,
        ERR__ATTRIBUTE_FORMAT       ,
    };

	/********************\
	| Member variable(s) |
	\********************/
private:
    char                *m_FileContent;         ///< Pointer to the beginning of the file stream to be parsed.
    char                *m_CurrentChar;         ///< Character of the given file which is currently parsed.
    char                *m_EOF;                 ///< Pointer to the end-of-file character.
	unsigned int		m_CurrentLine;          ///< Line number of the parsed file which is currently processed.
	std::string			m_ErrMsg;               ///< Error message in case of parse error.
    std::list<char*>    m_StringEndPtr;         ///< Pointers to the end of all strings found in the parsed file.
    XMLNode             *m_RootNode;

	/*****************************\
	| Constructor(s) / destructor |
	\*****************************/
public:
	inline XMLParser() : m_FileContent(NULL), m_RootNode(NULL)          {}
	inline ~XMLParser()	                                                { delete [] m_FileContent; delete m_RootNode; }

	/********************\
	| Member function(s) |
	\********************/
private:
    void                        clear();
    static bool                 mapFileToMemory( const std::string& filename,
                                                 char **buffer,
                                                 unsigned int *bufferSize );
    static bool                 mapSourceToMemory( const char* source,
                                                   char **buffer,
                                                   unsigned int *bufferSize );
    XMLNode*                    parse( char *buffer,
                                       unsigned int bufferSize );

    void                        removeComments();
	inline void                 skipSpaces();

    inline const char&          operator[]( int i ) const               { return m_CurrentChar[i]; }
    inline char&                operator[]( int i )                     { return m_CurrentChar[i]; }

    inline XMLParser&           operator++()                            { m_CurrentChar ++; return *this; }
    inline XMLParser&           operator++( int )                       { m_CurrentChar ++; return *this; }
    inline XMLParser&           operator+=( unsigned int i )            { m_CurrentChar += i; return *this; }
    
    inline bool                 operator==( const char* str ) const;    
    inline bool                 operator!=( const char* str ) const     { return !operator==(str); }

    inline bool                 eof() const                             { return m_CurrentChar == m_EOF; }
    inline bool                 eof( int i ) const                      { return m_CurrentChar == m_EOF+i; }

    char*                       identifier( int *length = NULL );
    char*                       quotedString();
    char*                       untaggedText();

    char*                       getCurrentLine();
    void                        manageError( unsigned int errCode );
    inline void			        setError( const std::string& err )      { m_ErrMsg = err; }

public:
	XMLNode*                    parseFile( const char* filename );
	inline XMLNode*             parseFile( const std::string& filename ){ return parseFile( filename.c_str() ); }

    XMLNode*                    parseSource( const char* src );
	inline XMLNode*             parseSource( const std::string& src )	{ return parseSource( src.c_str() ); }

    XMLNode*                    root() const                            { return m_RootNode; }

	inline const std::string&   getError() const	                    { return m_ErrMsg; }
};




/*
 *
 *	INLINE MEMBER FUNCTIONS DECLARATIONS.
 *
 */


inline bool XMLAttribute::caseInsensitiveNameCmp( const char *name ) const
{
    const char *str = m_Name;

    while( *str  &&  *name )
    {
        if( tolower(*str) != tolower(*name) )
            return false;
        ++ str;
        ++ name;
    }

    return (!*str) && (!*name);
}

template <typename T>
inline bool XMLAttribute::getValue( T& val ) const
{
    assert( 0 );
    return false;
}

template <>
inline bool XMLAttribute::getValue<int>( int& val ) const
{
    char *endptr;
    val = (int) strtol( m_Value, &endptr, 10 );
    return !*endptr;
}

template <>
inline bool XMLAttribute::getValue<unsigned int>( unsigned int& val ) const
{
    char *endptr;
    val = (unsigned int) strtoul( m_Value, &endptr, 10 );
    return !*endptr;
}

template <>
inline bool XMLAttribute::getValue<float>( float& val ) const
{
    char *endptr;
    val = (float) strtod( m_Value, &endptr );
    return !*endptr;
}

template <>
inline bool XMLAttribute::getValue<double>( double& val ) const
{
    char *endptr;
    val = (double) strtod( m_Value, &endptr );
    return !*endptr;
}

#if 0
template <>
inline bool XMLAttribute::getValue<stInt64>( stInt64& val ) const
{
    char *endptr;
#ifdef _WIN32
    val = (stInt64) _strtoi64( m_Value, &endptr, 10 );
#else
    val = (stInt64) strtol( m_Value, &endptr, 10 );
#endif
    return !*endptr;
}

template <>
inline bool XMLAttribute::getValue<stUInt64>( stUInt64& val ) const
{
    char *endptr;
#ifdef _WIN32
    val = (stUInt64) _strtoui64( m_Value, &endptr, 10 );
#else
    val = (stUInt64) strtoul( m_Value, &endptr, 10 );
#endif
    return !*endptr;
}
#endif




inline bool XMLNode::caseInsensitiveNameCmp( const char *name ) const
{
    const char *str = m_Name;

    while( *str  &&  *name )
    {
        if( tolower(*str) != tolower(*name) )
            return false;
        ++ str;
        ++ name;
    }

    return (!*str) && (!*name);
}

inline bool XMLNode::hasAttribute( const std::string& attribName ) const
{
    for( AttribVec::const_iterator a=m_Attrib.begin(); a!=m_Attrib.end(); ++a )
        if( a->getName() == attribName )
            return true;
    return false;
}

inline XMLAttribute* XMLNode::operator[]( const std::string& attribName )
{
    for( AttribVec::iterator a=m_Attrib.begin(); a!=m_Attrib.end(); ++a )
        if( a->getName() == attribName )
            return &*a;
    return NULL;
}

inline const XMLAttribute* XMLNode::operator[]( const std::string& attribName ) const
{
    for( AttribVec::const_iterator a=m_Attrib.begin(); a!=m_Attrib.end(); ++a )
        if( a->getName() == attribName )
            return &*a;
    return NULL;
}




inline bool XMLParser::operator==( const char* str ) const
{
    char *c = m_CurrentChar;
    while( *str )
    {
        if( *c != *str )
            return false;
        ++ c;
        ++ str;
    }
    return true;
}

inline void XMLParser::skipSpaces()
{
    bool currentCharIsASpace = true;
	
    do
	{
		switch( *m_CurrentChar )
		{
			case '\n':	m_CurrentLine ++;
			case ' ' :
			case '\r':
			case '\t':	m_CurrentChar ++; break;
			default  :	currentCharIsASpace = false;
		}
	} while( currentCharIsASpace );
}




#endif /*__XMLPARSER_H__*/