forked from rampa069/PhnRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsxmlparser.h
183 lines (153 loc) · 5.33 KB
/
sxmlparser.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
#ifndef _SXMLPARSER
#define _SXMLPARSER
#include <stdio.h>
#include <map>
#include <list>
#include <string>
// kinds of text outputs
#ifndef TO_CONSTANTS
#define TO_CONSTANTS
const unsigned int TO_ERROR = 1;
const unsigned int TO_WARNING = 2;
const unsigned int TO_LOG = 3;
#endif
// format of xml output
const unsigned int SXMLT_NONE = 0;
const unsigned int SXMLT_ADDHEADER = 1;
class SXMLDocument;
// XML properties
class SXMLProperties
{
public:
SXMLProperties();
~SXMLProperties();
void Clear();
std::string &GetValue(std::string name);
void Set(std::string name, std::string arg = "");
void FirstName();
std::string GetName();
bool Exists(std::string name);
bool Remove(std::string name);
void GetAsText(std::string &rText);
int GetNumber();
std::string & operator [] (std::string name);
SXMLProperties & operator = (SXMLProperties &rProp);
void Add(SXMLProperties *pProperties);
protected:
std::map<std::string, std::string> mList;
std::map<std::string, std::string>::iterator mListPointer;
std::string mDefaultString;
};
// XML node
class SXMLNode
{
public:
friend class SXMLDocument;
SXMLNode();
~SXMLNode();
std::string &GetName();
std::string &GetText();
SXMLProperties *GetProperties();
void *GetData();
void SetData(void *pdata);
void SetName(std::string name);
void SetText(std::string text);
void SetStartFileLine(int startFileLine);
void SetStopFileLine(int stopFileLine);
int GetStartFileLine();
int GetStopFileLine();
bool GetQuoted();
void SetQuoted(bool quoted);
void SetDocument(SXMLDocument *pDocument);
SXMLDocument *GetDocument();
void SetParent(SXMLNode *pNode);
SXMLNode *GetParent();
void AddChild(SXMLNode *pNode);
void RemoveChild(SXMLNode *pNode);
bool RemoveChild(std::string name, char *pProperties);
void RemoveAllChilds();
void FirstChild();
SXMLNode *GetChild();
SXMLNode *GetChild(std::string name, char *pProperties = "");
void GetAsText(std::string &rText, int spaceLevel = 0);
void GetChildsAsText(std::string &rText, int spaceLevel = 0);
std::string GetPath(SXMLProperties *pIncludeProperties = 0);
bool operator == (std::string &rText) const;
bool operator == (SXMLNode *pNode) const;
void CopyNodeNoChilds(SXMLNode &rNode);
void CopyChilds(SXMLNode &rNode);
SXMLNode & operator = (SXMLNode &rNode);
protected:
std::string mName;
std::string mText;
bool mQuoted;
void *mpData;
int mStartFileLine;
int mStopFileLine;
SXMLDocument *mpDocument;
SXMLNode *mpParentNode;
SXMLProperties mProperties;
std::list<SXMLNode *> mChilds;
std::list<SXMLNode *>::iterator mChildPointer;
};
// XML document
class SXMLDocument
{
public:
friend class SXMLNode; // access to the ParseProperties function
SXMLDocument();
~SXMLDocument();
// set error handler
typedef void (*TextOutFuncPtrType)(unsigned int textType, char *pText, void *pArg);
void SetTextOutFunc(TextOutFuncPtrType pTextOutFunc, void *pArg = 0);
// new document
void New();
// load XML document
bool Load(char *pFile);
// save XML document
bool Save(char *pFile, unsigned int flags = SXMLT_ADDHEADER);
// parsing functions
bool ParseText(char *pText);
bool ParseText(std::string text);
// create XML text
void GetAsText(std::string &rText, unsigned int flags = SXMLT_ADDHEADER);
// node manipulation functions
SXMLNode &GetRootNode();
SXMLNode &GetNode(std::string key, bool insertNodes = true);
void RemoveNode(SXMLNode *pNode);
void RemoveNode(std::string key);
bool Exists(std::string key);
// access operator
std::string &operator [] (std::string key);
SXMLDocument & operator = (SXMLDocument &rXMLDoc);
protected:
// logging of errors
TextOutFuncPtrType mpTextOutFunc;
void *mpTextOutArg;
SXMLNode *mpRootNode;
SXMLNode mDefaultNode;
// document header
std::string mVersion;
std::string mEncoding;
std::string mFile;
int mFileLine;
// help string
std::string mDefaultString;
// error logging functions
void Error(char *pErrorText);
void Warning(char *pWarningText);
void Log(char *pLogText);
static void DefaultErrorHandler(unsigned int textType, char *pText, void *pArg);
// parsing functions
char *SkipSpaces(char *pText, int *pFileLine);
char *GetTag(char *pText);
char *SkipTag(char *pText);
char *GetText(char *pText, char *pSeparators, int maxQuotes, char **ppNextText, int *pFileLine, bool includeQuotes = false);
SXMLNode *ParseNode(char *pText, char **ppNextText);
bool ParseProperties(char *pText, SXMLProperties *pProperties, char *pNodeName, char *pFile, int *pFileLine, char **ppNextText);
bool ParseBody(char *pText, SXMLNode *pNode, char **ppNextText);
void CutOffSpaces(char *pText);
// help functions
int FileLength(FILE *pF);
};
#endif