-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMLite.h
240 lines (199 loc) · 7.45 KB
/
XMLite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// XMLite.h: interface for the XMLite class.
//
// XMLite : XML Lite Parser Library
// by bro ( Cho,Kyung Min: [email protected] ) 2002-10-30
// Microsoft MVP (Visual C++) [email protected]
//
// 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)
// 2003-10-24 : bugfix) attribute parsing <tag a='1' \r\n/> is now ok
// 2004-03-05 : add branch copy functions
// 2004-06-14 : add _tcseistr/_tcsenistr/_tcsenicmp functions
// 2004-06-14 : now support, XML Document and PI, Comment, CDATA node
// 2004-06-15 : add GetText()/ Find() functions
// 2004-06-15 : add force_parse : now can parse HTML (not-welformed xml)
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_)
#define AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vector>
#include <deque>
struct _tagXMLAttr;
typedef _tagXMLAttr XAttr, *LPXAttr;
typedef std::vector<LPXAttr> XAttrs;
struct _tagXMLNode;
typedef _tagXMLNode XNode, *LPXNode;
typedef std::vector<LPXNode> XNodes, *LPXNodes;
struct _tagXMLDocument;
typedef struct _tagXMLDocument XDoc, *LPXDoc;
// Entity Encode/Decode Support
typedef struct _tagXmlEntity
{
TCHAR entity; // entity ( & " ' < > )
TCHAR ref[10]; // entity reference ( & " etc )
int ref_len; // entity reference length
}XENTITY,*LPXENTITY;
typedef struct _tagXMLEntitys : public std::vector<XENTITY>
{
LPXENTITY GetEntity( int entity );
LPXENTITY GetEntity( LPTSTR entity );
int GetEntityCount( LPCTSTR str );
int Ref2Entity( LPCTSTR estr, LPTSTR str, int strlen );
int Entity2Ref( LPCTSTR str, LPTSTR estr, int estrlen );
CString Ref2Entity( LPCTSTR estr );
CString Entity2Ref( LPCTSTR str );
_tagXMLEntitys(){};
_tagXMLEntitys( LPXENTITY entities, int count );
}XENTITYS,*LPXENTITYS;
extern XENTITYS entityDefault;
CString XRef2Entity( LPCTSTR estr );
CString XEntity2Ref( LPCTSTR str );
typedef enum
{
PIE_PARSE_WELFORMED = 0,
PIE_ALONE_NOT_CLOSED,
PIE_NOT_CLOSED,
PIE_NOT_NESTED,
PIE_ATTR_NO_VALUE
}PCODE;
// Parse info.
typedef struct _tagParseInfo
{
bool trim_value; // [set] do trim when parse?
bool entity_value; // [set] do convert from reference to entity? ( < -> < )
LPXENTITYS entitys; // [set] entity table for entity decode
TCHAR escape_value; // [set] escape value (default '\\')
bool force_parse; // [set] force parse even if xml is not welformed
LPTSTR xml; // [get] xml source
bool erorr_occur; // [get] is occurance of error?
LPTSTR error_pointer; // [get] error position of xml source
PCODE error_code; // [get] error code
CString error_string; // [get] error string
LPXDoc doc;
_tagParseInfo() { trim_value = false; entity_value = true; force_parse = false; entitys = &entityDefault; xml = NULL; erorr_occur = false; error_pointer = NULL; error_code = PIE_PARSE_WELFORMED; escape_value = '\\'; }
}PARSEINFO,*LPPARSEINFO;
extern PARSEINFO piDefault;
// display optional environment
typedef struct _tagDispOption
{
bool newline; // newline when new tag
bool reference_value; // do convert from entity to reference ( < -> < )
char value_quotation_mark; // val="" (default value quotation mark "
LPXENTITYS entitys; // entity table for entity encode
int tab_base; // internal usage
_tagDispOption() { newline = true; reference_value = true; entitys = &entityDefault; tab_base = 0; value_quotation_mark = '"'; }
}DISP_OPT, *LPDISP_OPT;
extern DISP_OPT optDefault;
// XAttr : Attribute Implementation
typedef struct _tagXMLAttr
{
CString name;
CString value;
_tagXMLNode* parent;
CString GetXML( LPDISP_OPT opt = &optDefault );
}XAttr, *LPXAttr;
typedef enum
{
XNODE_ELEMENT, // general node '<element>...</element>' or <element/>
XNODE_PI, // <?xml version="1.0" ?>
XNODE_COMMENT, // <!-- comment -->
XNODE_CDATA, // <![CDATA[ cdata ]]>
XNODE_DOC, // internal virtual root
}NODE_TYPE;
// XMLNode structure
typedef struct _tagXMLNode
{
// name and value
CString name;
CString value;
// internal variables
LPXNode parent; // parent node
XNodes childs; // child node
XAttrs attrs; // attributes
NODE_TYPE type; // node type
LPXDoc doc; // document
// Load/Save XML
LPTSTR Load( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
CString GetXML( LPDISP_OPT opt = &optDefault );
CString GetText( LPDISP_OPT opt = &optDefault );
// internal load functions
LPTSTR LoadAttributes( LPCTSTR pszAttrs, LPPARSEINFO pi = &piDefault );
LPTSTR LoadAttributes( LPCTSTR pszAttrs, LPCTSTR pszEnd, LPPARSEINFO pi = &piDefault );
LPTSTR LoadProcessingInstrunction( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
LPTSTR LoadComment( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
LPTSTR LoadCDATA( LPCTSTR pszXml, LPPARSEINFO pi = &piDefault );
// in own attribute list
LPXAttr GetAttr( LPCTSTR attrname );
LPCTSTR GetAttrValue( LPCTSTR attrname );
XAttrs GetAttrs( LPCTSTR name );
// in one level child nodes
LPXNode GetChild( LPCTSTR name );
LPCTSTR GetChildValue( LPCTSTR name );
CString GetChildText( LPCTSTR name, LPDISP_OPT opt = &optDefault );
XNodes GetChilds( LPCTSTR name );
XNodes GetChilds();
LPXAttr GetChildAttr( LPCTSTR name, LPCTSTR attrname );
LPCTSTR GetChildAttrValue( LPCTSTR name, LPCTSTR attrname );
// search node
LPXNode Find( LPCTSTR name );
// modify DOM
int GetChildCount();
LPXNode GetChild( int i );
XNodes::iterator GetChildIterator( LPXNode node );
LPXNode CreateNode( LPCTSTR name = NULL, LPCTSTR value = NULL );
LPXNode AppendChild( LPCTSTR name = NULL, LPCTSTR value = NULL );
LPXNode AppendChild( LPXNode node );
bool RemoveChild( LPXNode node );
LPXNode DetachChild( LPXNode node );
// node/branch copy
void CopyNode( LPXNode node );
void CopyBranch( LPXNode branch );
void _CopyBranch( LPXNode node );
LPXNode AppendChildBranch( LPXNode node );
// modify attribute
LPXAttr GetAttr( int i );
XAttrs::iterator GetAttrIterator( LPXAttr node );
LPXAttr CreateAttr( LPCTSTR anem = NULL, LPCTSTR value = NULL );
LPXAttr AppendAttr( LPCTSTR name = NULL, LPCTSTR value = NULL );
LPXAttr AppendAttr( LPXAttr attr );
bool RemoveAttr( LPXAttr attr );
LPXAttr DetachAttr( LPXAttr attr );
// operator overloads
LPXNode operator [] ( int i ) { return GetChild(i); }
XNode& operator = ( XNode& node ) { CopyBranch(&node); return *this; }
_tagXMLNode() { parent = NULL; doc = NULL; type = XNODE_ELEMENT; }
~_tagXMLNode();
void Close();
}XNode, *LPXNode;
// XMLDocument structure
typedef struct _tagXMLDocument : public XNode
{
PARSEINFO parse_info;
_tagXMLDocument() { parent = NULL; doc = this; type = XNODE_DOC; }
LPTSTR Load( LPCTSTR pszXml, LPPARSEINFO pi = NULL );
LPTSTR LoadFile( LPCTSTR pszFilename, LPPARSEINFO pi = NULL );
LPXNode GetRoot();
BOOL SaveFile(LPCTSTR pszFilename, LPDISP_OPT opt = &optDefault );
}XDoc, *LPXDoc;
// Helper Funtion
inline long XStr2Int( LPCTSTR str, long default_value = 0 )
{
return ( str && *str ) ? _ttol(str) : default_value;
}
inline bool XIsEmptyString( LPCTSTR str )
{
CString s(str);
s.TrimLeft();
s.TrimRight();
return ( s.IsEmpty() || s == _T("") );
}
#endif // !defined(AFX_XMLITE_H__786258A5_8360_4AE4_BDAF_2A52F8E1B877__INCLUDED_)