Files
itgmania212121/stepmania/src/XmlFile.h
T

111 lines
3.4 KiB
C++
Raw Normal View History

2005-10-11 09:37:18 +00:00
/* XmlFile - Simple XML reading and writing. */
2006-10-01 12:09:19 +00:00
#ifndef XML_FILE_H
#define XML_FILE_H
2004-02-09 08:10:01 +00:00
2005-01-07 09:09:23 +00:00
#include <map>
2004-07-18 21:55:49 +00:00
struct DateTime;
2004-12-18 05:37:53 +00:00
class RageFileBasic;
2004-02-09 08:10:01 +00:00
class XNodeValue
{
public:
RString m_sValue;
void GetValue( RString &out ) const;
void GetValue( int &out ) const;
void GetValue( float &out ) const;
void GetValue( bool &out ) const;
void GetValue( unsigned &out ) const;
void GetValue( DateTime &out ) const;
template<typename T>
T GetValue() const { T val; GetValue(val); return val; }
void SetValue( const RString &v );
void SetValue( int v );
void SetValue( float v );
void SetValue( unsigned v );
void SetValue( const DateTime &v );
};
typedef map<RString,XNodeValue> XAttrs;
class XNode;
2005-12-27 23:43:49 +00:00
typedef multimap<RString,XNode*> XNodes;
2005-01-07 09:09:23 +00:00
#define FOREACH_Attr( pNode, Var ) \
2005-10-11 11:11:03 +00:00
for( XAttrs::iterator Var = (pNode)->m_attrs.begin(); \
Var != (pNode)->m_attrs.end(); \
++Var )
2005-01-07 09:09:23 +00:00
#define FOREACH_CONST_Attr( pNode, Var ) \
2005-10-11 11:11:03 +00:00
for( XAttrs::const_iterator Var = (pNode)->m_attrs.begin(); \
Var != (pNode)->m_attrs.end(); \
++Var )
2005-01-07 09:09:23 +00:00
#define FOREACH_Child( pNode, Var ) \
XNode *Var = NULL; \
2006-10-01 12:09:19 +00:00
for( XNodes::iterator Var##Iter = (pNode)->m_childs.begin(); \
2006-06-08 06:11:32 +00:00
Var = (Var##Iter != (pNode)->m_childs.end())? Var##Iter->second:NULL, \
2005-01-08 02:06:22 +00:00
Var##Iter != (pNode)->m_childs.end(); \
2006-06-08 06:11:32 +00:00
++Var##Iter )
2005-01-07 09:09:23 +00:00
#define FOREACH_CONST_Child( pNode, Var ) \
const XNode *Var = NULL; \
2006-10-01 12:09:19 +00:00
for( XNodes::const_iterator Var##Iter = (pNode)->m_childs.begin(); \
2006-06-08 06:11:32 +00:00
Var = (Var##Iter != (pNode)->m_childs.end())? Var##Iter->second:NULL, \
2005-01-08 02:06:22 +00:00
Var##Iter != (pNode)->m_childs.end(); \
2006-06-08 06:11:32 +00:00
++Var##Iter )
2005-01-07 09:09:23 +00:00
2004-02-09 08:10:01 +00:00
// XMLNode structure
class XNode
2004-02-09 08:10:01 +00:00
{
public:
2005-12-27 23:43:49 +00:00
RString m_sName; // a duplicate of the m_sName in the parent's map
XNodeValue m_Value;
2006-10-01 12:09:19 +00:00
XNodes m_childs; // child node
XAttrs m_attrs; // attributes
2004-02-09 08:10:01 +00:00
2006-10-02 22:42:24 +00:00
void SetName( const RString &sName ) { m_sName = sName; }
const RString &GetName() const { return m_sName; }
const RString &GetValue() const { return m_Value.m_sValue; }
2006-10-02 06:02:54 +00:00
template <typename T>
void GetValue( T &out ) const { m_Value.GetValue(out); }
template <typename T>
void SetValue( const T val ) { m_Value.SetValue(val); }
2005-09-03 08:28:43 +00:00
2004-02-09 08:10:01 +00:00
// in own attribute list
const XNodeValue *GetAttr( const RString &sAttrName ) const;
XNodeValue *GetAttr( const RString &sAttrName );
template <typename T>
bool GetAttrValue( const RString &sName, T &out ) const { const XNodeValue *pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
2004-02-09 08:10:01 +00:00
// in one level child nodes
2006-10-01 14:24:19 +00:00
const XNode *GetChild( const RString &sName ) const;
XNode *GetChild( const RString &sName );
template <typename T>
bool GetChildValue( const RString &sName, T &out ) const { const XNode *pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
2004-02-09 08:10:01 +00:00
2006-10-01 14:24:19 +00:00
// modify DOM
template <typename T>
2006-10-01 14:46:28 +00:00
XNode *AppendChild( const RString &sName, T value ) { XNode *p=new XNode(sName); p->SetValue(value); return AppendChild(p); }
XNode *AppendChild( const RString &sName ) { XNode *p=new XNode(sName); return AppendChild(p); }
2005-09-03 08:28:43 +00:00
XNode *AppendChild( XNode *node );
2006-10-02 06:36:07 +00:00
bool RemoveChild( XNode *node, bool bDelete = true );
2004-02-09 08:10:01 +00:00
void AppendAttr( const RString &sName, const XNodeValue &val );
template <typename T>
void AppendAttr( const RString &sName, T value ) { XNodeValue val; val.SetValue( value ); AppendAttr( sName, val ); }
2005-12-27 23:43:49 +00:00
bool RemoveAttr( const RString &sName );
2005-01-07 14:28:00 +00:00
2005-01-07 06:51:53 +00:00
XNode() { }
2006-10-01 14:46:28 +00:00
explicit XNode( const RString &sName ) { m_sName = sName; }
XNode( const XNode &cpy );
~XNode();
2004-02-09 08:10:01 +00:00
2005-01-07 14:28:00 +00:00
void Clear();
};
2004-02-09 08:10:01 +00:00
2004-02-10 09:42:01 +00:00
#endif