Files
itgmania212121/stepmania/src/XmlFile.h
T

157 lines
5.6 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
2005-12-27 23:43:49 +00:00
typedef map<RString,RString> 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 ) \
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
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
2005-12-27 23:43:49 +00:00
RString error_string; // [get] error string
2004-02-09 08:10:01 +00:00
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; )
2005-12-27 23:43:49 +00:00
RString 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
// 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
RString m_sValue;
2005-01-07 14:28:00 +00:00
XNodes m_childs; // child node
XAttrs m_attrs; // attributes
2004-02-09 08:10:01 +00:00
2005-12-27 23:43:49 +00:00
void GetValue( RString &out ) const;
2005-09-03 08:28:43 +00:00
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-12-27 23:43:49 +00:00
unsigned Load( const RString &sXml, PARSEINFO *pi, unsigned iOffset = 0 );
unsigned LoadAttributes( const RString &sAttrs, PARSEINFO *pi, unsigned iOffset );
bool GetXML( RageFileBasic &f, DISP_OPT &opt ) const;
2005-12-27 23:43:49 +00:00
bool GetAttrXML( RageFileBasic &f, DISP_OPT &opt, const RString &sName, const RString &sValue ) const;
RString GetXML() const;
2004-02-09 08:10:01 +00:00
2005-12-27 23:43:49 +00:00
bool SaveToFile( const RString &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
2005-12-27 23:43:49 +00:00
const RString *GetAttr( const RString &sAttrName ) const;
RString *GetAttr( const RString &sAttrName );
bool GetAttrValue( const RString &sName, RString &out ) const;
bool GetAttrValue( const RString &sName, int &out ) const;
bool GetAttrValue( const RString &sName, float &out ) const;
bool GetAttrValue( const RString &sName, bool &out ) const;
bool GetAttrValue( const RString &sName, unsigned &out ) const;
bool GetAttrValue( const RString &sName, DateTime &out ) const;
2004-02-09 08:10:01 +00:00
// in one level child nodes
2005-12-27 23:43:49 +00:00
const XNode *GetChild( const RString &sName ) const;
XNode *GetChild( const RString &sName );
bool GetChildValue( const RString &sName, RString &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const RString &sName, int &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const RString &sName, float &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const RString &sName, bool &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const RString &sName, unsigned &out ) const { const XNode* pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue( const RString &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-12-27 23:43:49 +00:00
XNode *AppendChild( const RString &sName = RString(), const RString &value = RString() );
XNode *AppendChild( const RString &sName, float value );
XNode *AppendChild( const RString &sName, int value );
XNode *AppendChild( const RString &sName, unsigned value );
XNode *AppendChild( const RString &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-12-27 23:43:49 +00:00
void AppendAttr( const RString &sName = RString(), const RString &sValue = RString() );
void AppendAttr( const RString &sName, float value );
void AppendAttr( const RString &sName, int value );
void AppendAttr( const RString &sName, unsigned value );
void AppendAttr( const RString &sName, const DateTime &value );
bool RemoveAttr( const RString &sName );
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