Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

XmlFile.h

Go to the documentation of this file.
00001 #ifndef XmlFile_H 00002 #define XmlFile_H 00003 // XmlFile.h: interface for the XmlFile class. 00004 // 00005 // Adapted from http://www.codeproject.com/cpp/xmlite.asp. 00006 // On 2004-02-09 Cho, Kyung-Min gave us permission to use and modify this 00007 // library. 00008 // 00009 // XmlFile : XML Lite Parser Library 00010 // by bro ( Cho,Kyung Min: bro@shinbiro.com ) 2002-10-30 00011 // History. 00012 // 2002-10-29 : First Coded. Parsing XMLElelement and Attributes. 00013 // get xml parsed string ( looks good ) 00014 // 2002-10-30 : Get Node Functions, error handling ( not completed ) 00015 // 2002-12-06 : Helper Funtion string to long 00016 // 2002-12-12 : Entity Helper Support 00017 // 2003-04-08 : Close, 00018 // 2003-07=23 : add property escape_value. (now no escape on default) 00019 // fix escape functions 00021 00022 #include <map> 00023 struct DateTime; 00024 class RageFileBasic; 00025 00026 struct XAttr; 00027 typedef multimap<CString,XAttr*> XAttrs; 00028 struct XNode; 00029 typedef multimap<CString,XNode*> XNodes; 00030 00031 #define FOREACH_Attr( pNode, Var ) \ 00032 XAttrs::iterator Var##Iter; \ 00033 XAttr *Var = NULL; \ 00034 for( Var##Iter = (pNode)->m_attrs.begin(), Var = Var##Iter->second; \ 00035 Var##Iter != (pNode)->m_attrs.end(); \ 00036 ++Var##Iter, Var = Var##Iter->second ) 00037 00038 #define FOREACH_CONST_Attr( pNode, Var ) \ 00039 XAttrs::const_iterator Var##Iter; \ 00040 const XAttr *Var = NULL; \ 00041 for( Var##Iter = (pNode)->m_attrs.begin(), Var = Var##Iter->second; \ 00042 Var##Iter != (pNode)->m_attrs.end(); \ 00043 ++Var##Iter, Var = Var##Iter->second ) 00044 00045 #define FOREACH_Child( pNode, Var ) \ 00046 XNodes::iterator Var##Iter; \ 00047 XNode *Var = NULL; \ 00048 for( Var##Iter = (pNode)->m_childs.begin(), Var = Var##Iter->second; \ 00049 Var##Iter != (pNode)->m_childs.end(); \ 00050 ++Var##Iter, Var = Var##Iter->second ) 00051 00052 #define FOREACH_CONST_Child( pNode, Var ) \ 00053 XNodes::const_iterator Var##Iter; \ 00054 const XNode *Var = NULL; \ 00055 for( Var##Iter = (pNode)->m_childs.begin(), Var = Var##Iter->second; \ 00056 Var##Iter != (pNode)->m_childs.end(); \ 00057 ++Var##Iter, Var = Var##Iter->second ) 00058 00059 00060 // Entity Encode/Decode Support 00061 struct XENTITY 00062 { 00063 char entity; // entity ( & " ' < > ) 00064 char ref[10]; // entity reference ( &amp; &quot; etc ) 00065 int ref_len; // entity reference length 00066 }; 00067 00068 struct XENTITYS : public vector<XENTITY> 00069 { 00070 XENTITY *GetEntity( int entity ); 00071 XENTITY *GetEntity( char* entity ); 00072 int GetEntityCount( const char* str ); 00073 int Ref2Entity( const char* estr, char* str, int strlen ); 00074 int Entity2Ref( const char* str, char* estr, int estrlen ); 00075 CString Ref2Entity( const char* estr ); 00076 CString Entity2Ref( const char* str ); 00077 00078 XENTITYS(){}; 00079 XENTITYS( XENTITY *entities, int count ); 00080 }; 00081 extern XENTITYS entityDefault; 00082 CString XRef2Entity( const char* estr ); 00083 CString XEntity2Ref( const char* str ); 00084 00085 enum PCODE 00086 { 00087 PIE_PARSE_WELFORMED = 0, 00088 PIE_ALONE_NOT_CLOSED, 00089 PIE_NOT_CLOSED, 00090 PIE_NOT_NESTED, 00091 PIE_ATTR_NO_VALUE, 00092 PIE_READ_ERROR 00093 }; 00094 00095 // Parse info. 00096 struct PARSEINFO 00097 { 00098 bool trim_value; // [set] do trim when parse? 00099 bool entity_value; // [set] do convert from reference to entity? ( &lt; -> < ) 00100 XENTITYS *entitys; // [set] entity table for entity decode 00101 char escape_value; // [set] escape value (default '\\') 00102 00103 char* xml; // [get] xml source 00104 bool erorr_occur; // [get] is occurance of error? 00105 char* error_pointer; // [get] error position of xml source 00106 PCODE error_code; // [get] error code 00107 CString error_string; // [get] error string 00108 00109 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; } 00110 }; 00111 extern PARSEINFO piDefault; 00112 00113 // display optional environment 00114 struct DISP_OPT 00115 { 00116 bool newline; // newline when new tag 00117 bool reference_value; // do convert from entity to reference ( < -> &lt; ) 00118 XENTITYS *entitys; // entity table for entity encode 00119 CString stylesheet; // empty string = no stylesheet 00120 bool write_tabs; // if false, don't write tab indent characters 00121 00122 int tab_base; // internal usage 00123 DISP_OPT() 00124 { 00125 newline = true; 00126 reference_value = true; 00127 entitys = &entityDefault; 00128 stylesheet = ""; 00129 write_tabs = true; 00130 tab_base = 0; 00131 } 00132 }; 00133 extern DISP_OPT optDefault; 00134 00135 // XAttr : Attribute Implementation 00136 struct XAttr 00137 { 00138 CString m_sName; // a duplicate of the m_sName in the parent's map 00139 CString m_sValue; 00140 void GetValue(CString &out) const; 00141 void GetValue(int &out) const; 00142 void GetValue(float &out) const; 00143 void GetValue(bool &out) const; 00144 void GetValue(unsigned &out) const; 00145 void GetValue(DateTime &out) const; 00146 00147 bool GetXML( RageFileBasic &f, DISP_OPT *opt = &optDefault ); 00148 }; 00149 00150 // XMLNode structure 00151 struct XNode 00152 { 00153 CString m_sName; // a duplicate of the m_sName in the parent's map 00154 CString m_sValue; 00155 void GetValue(CString &out) const; 00156 void GetValue(int &out) const; 00157 void GetValue(float &out) const; 00158 void GetValue(bool &out) const; 00159 void GetValue(unsigned &out) const; 00160 void GetValue(DateTime &out) const; 00161 void SetValue(int v); 00162 void SetValue(float v); 00163 void SetValue(bool v); 00164 void SetValue(unsigned v); 00165 void SetValue(const DateTime &v); 00166 00167 // internal variables 00168 XNodes m_childs; // child node 00169 XAttrs m_attrs; // attributes 00170 00171 // Load/Save XML 00172 char* Load( const char* pszXml, PARSEINFO *pi = &piDefault ); 00173 char* LoadAttributes( const char* pszAttrs, PARSEINFO *pi = &piDefault ); 00174 bool GetXML( RageFileBasic &f, DISP_OPT *opt = &optDefault ); 00175 00176 bool LoadFromFile( const CString &sFile, PARSEINFO *pi = &piDefault ); 00177 bool LoadFromFile( RageFileBasic &f, PARSEINFO *pi = &piDefault ); 00178 bool SaveToFile( const CString &sFile, DISP_OPT *opt = &optDefault ); 00179 bool SaveToFile( RageFileBasic &f, DISP_OPT *opt = &optDefault ); 00180 00181 // in own attribute list 00182 const XAttr *GetAttr( const char* attrname ) const; 00183 XAttr *GetAttr( const char* attrname ); 00184 const char* GetAttrValue( const char* attrname ); 00185 bool GetAttrValue(const char* name,CString &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00186 bool GetAttrValue(const char* name,int &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00187 bool GetAttrValue(const char* name,float &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00188 bool GetAttrValue(const char* name,bool &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00189 bool GetAttrValue(const char* name,unsigned &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00190 bool GetAttrValue(const char* name,DateTime &out) const { const XAttr* pAttr=GetAttr(name); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; } 00191 00192 // in one level child nodes 00193 const XNode *GetChild( const char* m_sName ) const; 00194 XNode *GetChild( const char* m_sName ); 00195 const char* GetChildValue( const char* m_sName ); 00196 bool GetChildValue(const char* name,CString &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00197 bool GetChildValue(const char* name,int &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00198 bool GetChildValue(const char* name,float &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00199 bool GetChildValue(const char* name,bool &out) const { const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00200 bool GetChildValue(const char* name,unsigned &out) const{ const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00201 bool GetChildValue(const char* name,DateTime &out) const{ const XNode* pChild=GetChild(name); if(pChild==NULL) return false; pChild->GetValue(out); return true; } 00202 00203 XAttr *GetChildAttr( const char* name, const char* attrname ); 00204 const char* GetChildAttrValue( const char* name, const char* attrname ); 00205 00206 // modify DOM 00207 int GetChildCount(); 00208 XNode *AppendChild( const char* m_sName = NULL, const char* value = NULL ); 00209 XNode *AppendChild( const char* m_sName, float value ); 00210 XNode *AppendChild( const char* m_sName, int value ); 00211 XNode *AppendChild( const char* m_sName, unsigned value ); 00212 XNode *AppendChild( const char* m_sName, const DateTime &value ); 00213 XNode *AppendChild( XNode *node ); 00214 bool RemoveChild( XNode *node ); 00215 00216 XAttr *AppendAttr( const char* m_sName = NULL, const char* value = NULL ); 00217 XAttr *AppendAttr( const char* m_sName, float value ); 00218 XAttr *AppendAttr( const char* m_sName, int value ); 00219 XAttr *AppendAttr( const char* m_sName, unsigned value ); 00220 XAttr *AppendAttr( const char* m_sName, const DateTime &value ); 00221 XAttr *AppendAttr( XAttr *attr ); 00222 bool RemoveAttr( XAttr *attr ); 00223 00224 // creates the attribute if it doesn't already exist 00225 void SetAttrValue( const char* m_sName, const char* value ); 00226 00227 XNode() { } 00228 ~XNode(); 00229 00230 void Clear(); 00231 }; 00232 00233 // Helper Funtion 00234 inline long XStr2Int( const char* str, long default_value = 0 ) 00235 { 00236 return str ? atol(str) : default_value; 00237 } 00238 00239 bool XIsEmptyString( const char* str ); 00240 00241 #endif

Generated on Thu Jan 27 20:57:34 2005 for StepMania by doxygen 1.3.7