Files
itgmania212121/stepmania/src/XmlFile.h
T

240 lines
8.9 KiB
C++
Raw Normal View History

2004-02-10 09:42:01 +00:00
#ifndef XmlFile_H
#define XmlFile_H
2004-02-09 08:10:01 +00:00
// XmlFile.h: interface for the XmlFile class.
//
2004-02-10 09:42:01 +00:00
// Adapted from http://www.codeproject.com/cpp/xmlite.asp.
2004-05-23 02:28:36 +00:00
// On 2004-02-09 Cho, Kyung-Min gave us permission to use and modify this
2004-02-10 09:42:01 +00:00
// library.
2004-02-09 08:10:01 +00:00
//
// XmlFile : XML Lite Parser Library
// by bro ( Cho,Kyung Min: [email protected] ) 2002-10-30
// History.
// 2002-10-29 : First Coded. Parsing XMLElelement and Attributes.
// get xml parsed string ( looks good )
// 2002-10-30 : Get Node Functions, error handling ( not completed )
// 2002-12-06 : Helper Funtion string to long
// 2002-12-12 : Entity Helper Support
// 2003-04-08 : Close,
// 2003-07=23 : add property escape_value. (now no escape on default)
// fix escape functions
//////////////////////////////////////////////////////////////////////
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;
2005-01-07 09:09:23 +00:00
typedef multimap<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; \
for( Var##Iter = pNode->attrs.begin(); \
(Var##Iter != pNode->attrs.end() && (Var = Var##Iter->second) ), Var##Iter != pNode->attrs.end(); \
++Var##Iter )
#define FOREACH_CONST_Attr( pNode, Var ) \
XAttrs::const_iterator Var##Iter; \
const XAttr *Var = NULL; \
for( Var##Iter = pNode->attrs.begin(); \
(Var##Iter != pNode->attrs.end() && (Var = Var##Iter->second) ), Var##Iter != pNode->attrs.end(); \
++Var##Iter )
#define FOREACH_Child( pNode, Var ) \
XNodes::iterator Var##Iter; \
XNode *Var = NULL; \
for( Var##Iter = pNode->childs.begin(); \
(Var##Iter != pNode->childs.end() && (Var = Var##Iter->second) ), Var##Iter != pNode->childs.end(); \
++Var##Iter )
#define FOREACH_CONST_Child( pNode, Var ) \
XNodes::const_iterator Var##Iter; \
const XNode *Var = NULL; \
for( Var##Iter = pNode->childs.begin(); \
(Var##Iter != pNode->childs.end() && (Var = Var##Iter->second) ), Var##Iter != pNode->childs.end(); \
++Var##Iter )
2004-02-09 08:10:01 +00:00
// Entity Encode/Decode Support
struct XENTITY
2004-02-09 08:10:01 +00:00
{
2004-08-18 22:09:23 +00:00
char entity; // entity ( & " ' < > )
char ref[10]; // entity reference ( &amp; &quot; etc )
2004-02-09 08:10:01 +00:00
int ref_len; // entity reference length
};
2004-02-09 08:10:01 +00:00
2005-01-07 02:07:10 +00:00
struct XENTITYS : public vector<XENTITY>
2004-02-09 08:10:01 +00:00
{
2004-07-23 02:27:37 +00:00
XENTITY *GetEntity( int entity );
XENTITY *GetEntity( char* entity );
2004-02-25 01:09:17 +00:00
int GetEntityCount( const char* str );
int Ref2Entity( const char* estr, char* str, int strlen );
int Entity2Ref( const char* str, char* estr, int estrlen );
CString Ref2Entity( const char* estr );
CString Entity2Ref( const char* str );
2004-02-09 08:10:01 +00:00
XENTITYS(){};
2004-07-23 02:27:37 +00:00
XENTITYS( XENTITY *entities, int count );
};
2004-02-09 08:10:01 +00:00
extern XENTITYS entityDefault;
2004-02-25 01:09:17 +00:00
CString XRef2Entity( const char* estr );
CString XEntity2Ref( const char* str );
2004-02-09 08:10:01 +00:00
enum PCODE
2004-02-09 08:10:01 +00:00
{
PIE_PARSE_WELFORMED = 0,
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-07-23 02:27:37 +00:00
XENTITYS *entitys; // [set] entity table for entity decode
2004-08-18 22:09:23 +00:00
char escape_value; // [set] escape value (default '\\')
2004-02-09 08:10:01 +00:00
2004-02-25 01:09:17 +00:00
char* xml; // [get] xml source
2004-02-09 08:10:01 +00:00
bool erorr_occur; // [get] is occurance of error?
2004-02-25 01:09:17 +00:00
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; entitys = &entityDefault; xml = NULL; erorr_occur = false; error_pointer = NULL; error_code = PIE_PARSE_WELFORMED; escape_value = 0; }
};
2004-02-09 08:10:01 +00:00
extern PARSEINFO piDefault;
// 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; )
2004-07-23 02:27:37 +00:00
XENTITYS *entitys; // entity table for entity encode
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;
entitys = &entityDefault;
stylesheet = "";
write_tabs = true;
tab_base = 0;
}
};
2004-02-09 08:10:01 +00:00
extern DISP_OPT optDefault;
// XAttr : Attribute Implementation
struct XAttr
2004-02-09 08:10:01 +00:00
{
2005-01-07 09:09:23 +00:00
CString name; // a duplicate of the name in the parent's map
2004-02-09 08:10:01 +00:00
CString value;
2004-07-18 21:55:49 +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
2004-12-18 05:37:53 +00:00
bool GetXML( RageFileBasic &f, DISP_OPT *opt = &optDefault );
};
2004-02-09 08:10:01 +00:00
// XMLNode structure
struct XNode
2004-02-09 08:10:01 +00:00
{
2005-01-07 09:09:23 +00:00
CString name; // a duplicate of the name in the parent's map
2004-02-09 08:10:01 +00:00
CString value;
2004-07-18 21:55:49 +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-07-17 21:49:32 +00:00
void SetValue(int v);
void SetValue(float v);
void SetValue(bool v);
void SetValue(unsigned v);
2004-07-18 21:55:49 +00:00
void SetValue(const DateTime &v);
2004-02-09 08:10:01 +00:00
// internal variables
2005-01-07 03:18:11 +00:00
XNodes childs; // child node
XAttrs attrs; // attributes
2004-02-09 08:10:01 +00:00
// Load/Save XML
2004-07-23 02:27:37 +00:00
char* Load( const char* pszXml, PARSEINFO *pi = &piDefault );
char* LoadAttributes( const char* pszAttrs, PARSEINFO *pi = &piDefault );
2004-12-18 05:37:53 +00:00
bool GetXML( RageFileBasic &f, DISP_OPT *opt = &optDefault );
2004-02-09 08:10:01 +00:00
2005-01-07 02:07:10 +00:00
bool LoadFromFile( const CString &sFile, PARSEINFO *pi = &piDefault );
bool SaveToFile( const CString &sFile, DISP_OPT *opt = &optDefault );
2004-12-18 05:37:53 +00:00
bool SaveToFile( RageFileBasic &f, DISP_OPT *opt = &optDefault );
2004-02-10 09:42:01 +00:00
2004-02-09 08:10:01 +00:00
// in own attribute list
2004-07-23 02:27:37 +00:00
const XAttr *GetAttr( const char* attrname ) const;
XAttr *GetAttr( const char* attrname );
2004-02-25 01:09:17 +00:00
const char* GetAttrValue( const char* attrname );
bool GetAttrValue(const char* name,CString &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue(const char* name,int &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue(const char* name,float &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue(const char* name,bool &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
bool GetAttrValue(const char* name,unsigned &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
2004-07-18 21:55:49 +00:00
bool GetAttrValue(const char* name,DateTime &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
2004-02-09 08:10:01 +00:00
// in one level child nodes
2004-07-23 02:27:37 +00:00
const XNode *GetChild( const char* name ) const;
XNode *GetChild( const char* name );
2004-02-25 01:09:17 +00:00
const char* GetChildValue( const char* name );
bool GetChildValue(const char* name,CString &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue(const char* name,int &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue(const char* name,float &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue(const char* name,bool &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
bool GetChildValue(const char* name,unsigned &out) const{ const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
2004-07-18 21:55:49 +00:00
bool GetChildValue(const char* name,DateTime &out) const{ const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
2004-02-09 08:10:01 +00:00
2004-07-23 02:27:37 +00:00
XAttr *GetChildAttr( const char* name, const char* attrname );
2004-02-25 01:09:17 +00:00
const char* GetChildAttrValue( const char* name, const char* attrname );
2004-02-09 08:10:01 +00:00
// modify DOM
int GetChildCount();
2004-07-23 02:27:37 +00:00
XNode *CreateNode( const char* name = NULL, const char* value = NULL );
XNode *AppendChild( const char* name = NULL, const char* value = NULL );
XNode *AppendChild( const char* name, float value );
XNode *AppendChild( const char* name, int value );
XNode *AppendChild( const char* name, unsigned value );
XNode *AppendChild( const char* name, const DateTime &value );
XNode *AppendChild( XNode *node );
bool RemoveChild( XNode *node );
2004-02-09 08:10:01 +00:00
2004-07-23 02:27:37 +00:00
XAttr *CreateAttr( const char* anem = NULL, const char* value = NULL );
XAttr *AppendAttr( const char* name = NULL, const char* value = NULL );
XAttr *AppendAttr( const char* name, float value );
XAttr *AppendAttr( const char* name, int value );
XAttr *AppendAttr( const char* name, unsigned value );
XAttr *AppendAttr( const char* name, const DateTime &value );
XAttr *AppendAttr( XAttr *attr );
bool RemoveAttr( XAttr *attr );
2004-02-09 08:10:01 +00:00
2005-01-07 06:51:53 +00:00
XNode() { }
~XNode();
2004-02-09 08:10:01 +00:00
void Close();
};
2004-02-09 08:10:01 +00:00
// Helper Funtion
2004-02-25 01:09:17 +00:00
inline long XStr2Int( const char* str, long default_value = 0 )
2004-02-09 08:10:01 +00:00
{
return str ? atol(str) : default_value;
}
2004-07-18 21:55:49 +00:00
bool XIsEmptyString( const char* str );
2004-02-09 08:10:01 +00:00
2004-02-10 09:42:01 +00:00
#endif