Files
itgmania212121/stepmania/src/XmlFile.h
T

182 lines
7.0 KiB
C++
Raw Normal View History

2005-10-11 09:37:18 +00:00
/* XmlFile - Simple XML reading and writing. */
2004-02-10 09:42:01 +00:00
#ifndef XmlFile_H
#define XmlFile_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
struct XAttr;
typedef map<CString,XAttr*> XAttrs;
struct XNode;
2005-01-07 09:09:23 +00:00
typedef multimap<CString,XNode*> XNodes;
#define FOREACH_Attr( pNode, Var ) \
XAttrs::iterator Var##Iter; \
XAttr *Var = NULL; \
2005-01-08 02:06:22 +00:00
for( Var##Iter = (pNode)->m_attrs.begin(), Var = Var##Iter->second; \
Var##Iter != (pNode)->m_attrs.end(); \
++Var##Iter, Var = Var##Iter->second )
2005-01-07 09:09:23 +00:00
#define FOREACH_CONST_Attr( pNode, Var ) \
XAttrs::const_iterator Var##Iter; \
const XAttr *Var = NULL; \
2005-01-08 02:06:22 +00:00
for( Var##Iter = (pNode)->m_attrs.begin(), Var = Var##Iter->second; \
Var##Iter != (pNode)->m_attrs.end(); \
++Var##Iter, Var = Var##Iter->second )
2005-01-07 09:09:23 +00:00
#define FOREACH_Child( pNode, Var ) \
XNodes::iterator Var##Iter; \
XNode *Var = NULL; \
2005-01-08 02:06:22 +00:00
for( Var##Iter = (pNode)->m_childs.begin(), Var = Var##Iter->second; \
Var##Iter != (pNode)->m_childs.end(); \
++Var##Iter, Var = Var##Iter->second )
2005-01-07 09:09:23 +00:00
#define FOREACH_CONST_Child( pNode, Var ) \
XNodes::const_iterator Var##Iter; \
const XNode *Var = NULL; \
2005-01-08 02:06:22 +00:00
for( Var##Iter = (pNode)->m_childs.begin(), Var = Var##Iter->second; \
Var##Iter != (pNode)->m_childs.end(); \
++Var##Iter, Var = Var##Iter->second )
2005-01-07 09:09:23 +00:00
2004-02-09 08:10:01 +00:00
enum PCODE
2004-02-09 08:10:01 +00:00
{
2005-09-04 21:59:00 +00:00
PIE_PARSE_WELL_FORMED = 0,
2004-02-09 08:10:01 +00:00
PIE_ALONE_NOT_CLOSED,
PIE_NOT_CLOSED,
PIE_NOT_NESTED,
2004-06-06 20:47:48 +00:00
PIE_ATTR_NO_VALUE,
PIE_READ_ERROR
};
2004-02-09 08:10:01 +00:00
// Parse info.
struct PARSEINFO
2004-02-09 08:10:01 +00:00
{
bool trim_value; // [set] do trim when parse?
bool entity_value; // [set] do convert from reference to entity? ( &lt; -> < )
2004-02-25 01:09:17 +00:00
char* xml; // [get] xml source
2005-02-18 11:55:34 +00:00
bool error_occur; // [get] is occurance of error?
2005-09-03 08:28:43 +00:00
const char* error_pointer; // [get] error position of xml source
2004-02-09 08:10:01 +00:00
PCODE error_code; // [get] error code
CString error_string; // [get] error string
PARSEINFO() { trim_value = true; entity_value = true; xml = NULL; error_occur = false; error_pointer = NULL; error_code = PIE_PARSE_WELL_FORMED; }
};
2004-02-09 08:10:01 +00:00
// display optional environment
struct DISP_OPT
2004-02-09 08:10:01 +00:00
{
bool newline; // newline when new tag
bool reference_value; // do convert from entity to reference ( < -> &lt; )
CString stylesheet; // empty string = no stylesheet
bool write_tabs; // if false, don't write tab indent characters
2004-02-09 08:10:01 +00:00
int tab_base; // internal usage
DISP_OPT()
{
newline = true;
reference_value = true;
stylesheet = "";
write_tabs = true;
tab_base = 0;
}
};
2004-02-09 08:10:01 +00:00
// XAttr : Attribute Implementation
struct XAttr
2004-02-09 08:10:01 +00:00
{
2005-01-07 14:28:00 +00:00
CString m_sName; // a duplicate of the m_sName in the parent's map
CString m_sValue;
2005-09-03 08:28:43 +00:00
void GetValue( CString &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;
2004-02-09 08:10:01 +00:00
2005-02-18 13:05:17 +00:00
bool GetXML( RageFileBasic &f, DISP_OPT *opt ) const;
};
2004-02-09 08:10:01 +00:00
// XMLNode structure
struct XNode
2004-02-09 08:10:01 +00:00
{
2005-01-07 14:28:00 +00:00
CString m_sName; // a duplicate of the m_sName in the parent's map
CString m_sValue;
XNodes m_childs; // child node
XAttrs m_attrs; // attributes
2004-02-09 08:10:01 +00:00
2005-09-03 08:28:43 +00:00
void GetValue( CString &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;
void SetValue( int v );
void SetValue( float v );
void SetValue( bool v );
void SetValue( unsigned v );
void SetValue( const DateTime &v );
2004-02-09 08:10:01 +00:00
// Load/Save XML
2005-09-06 18:25:16 +00:00
unsigned Load( const CString &sXml, PARSEINFO *pi, unsigned iOffset = 0 );
unsigned LoadAttributes( const CString &sAttrs, PARSEINFO *pi, unsigned iOffset );
2005-02-18 13:05:17 +00:00
bool GetXML( RageFileBasic &f, DISP_OPT *opt ) const;
2005-06-16 03:08:45 +00:00
CString GetXML() const;
2004-02-09 08:10:01 +00:00
bool LoadFromFile( const CString &sFile );
bool LoadFromFile( RageFileBasic &f );
2005-02-18 13:05:17 +00:00
bool SaveToFile( const CString &sFile, DISP_OPT *opt ) const;
bool SaveToFile( RageFileBasic &f, DISP_OPT *opt ) const;
2004-02-10 09:42:01 +00:00
2004-02-09 08:10:01 +00:00
// in own attribute list
const XAttr *GetAttr( const CString &sAttrName ) const;
XAttr *GetAttr( const CString &sAttrName );
bool GetAttrValue( const CString &sName, CString &out ) const { const XAttr* pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const CString &sName, int &out ) const { const XAttr* pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const CString &sName, float &out ) const { const XAttr* pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const CString &sName, bool &out ) const { const XAttr* pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const CString &sName, unsigned &out ) const { const XAttr* pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue( const CString &sName, DateTime &out ) const { const XAttr* 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
const XNode *GetChild( const CString &sName ) const;
XNode *GetChild( const CString &sName );
bool GetChildValue( const CString &sName, CString &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const CString &sName, int &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const CString &sName, float &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const CString &sName, bool &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const CString &sName, unsigned &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const CString &sName, DateTime &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
// modify DOM
2005-09-04 21:59:00 +00:00
XNode *AppendChild( const CString &sName = CString(), const CString &value = CString() );
XNode *AppendChild( const CString &sName, float value );
XNode *AppendChild( const CString &sName, int value );
XNode *AppendChild( const CString &sName, unsigned value );
XNode *AppendChild( const CString &sName, const DateTime &value );
2005-09-03 08:28:43 +00:00
XNode *AppendChild( XNode *node );
bool RemoveChild( XNode *node );
2004-02-09 08:10:01 +00:00
2005-09-04 21:59:00 +00:00
XAttr *AppendAttr( const CString &sName = CString(), const CString &sValue = CString() );
XAttr *AppendAttr( const CString &sName, float value );
XAttr *AppendAttr( const CString &sName, int value );
XAttr *AppendAttr( const CString &sName, unsigned value );
XAttr *AppendAttr( const CString &sName, const DateTime &value );
XAttr *AppendAttr( XAttr *pAttr );
bool RemoveAttr( XAttr *pAttr );
2004-02-09 08:10:01 +00:00
2005-01-07 14:28:00 +00:00
// creates the attribute if it doesn't already exist
void SetAttrValue( const CString &sName, const CString &sValue );
2005-01-07 14:28:00 +00:00
2005-01-07 06:51:53 +00:00
XNode() { }
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