-
Notifications
You must be signed in to change notification settings - Fork 3
/
XMLParser.cpp
222 lines (180 loc) · 4.39 KB
/
XMLParser.cpp
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
/// \file XMLParser.cpp
/// \brief class file for xml parsing
// XMLParser.cpp -- class file for xml parsing
//
// Copyright 2003, Quarterflash Design Group
//
// Licensed under GPL version 2 - see COPYING for details
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "XMLParser.h"
#include <iostream>
using namespace std;
static void
wrapper_Characters (void *data, const XML_Char * _chars, int len)
{
XMLParser *xp = (XMLParser *) data;
if (!xp)
return;
wxString chars (wxConvertMB2WX (_chars), len);
xp->Characters (chars);
}
static void
wrapper_TagStart (void *data, const XML_Char * name,
const XML_Char ** attributes)
{
// if data is null we probably should report this somehow to the user
XMLParser *xp = (XMLParser *) data;
if (!xp)
return;
wxString el (wxConvertMB2WX (name));
wxArrayString attr;
unsigned int i = 0;
while (attributes[i]) {
wxString currentAttr (wxConvertMB2WX (attributes[i]));
attr.Add (currentAttr);
i++;
};
xp->TagStart (el, attr);
}
static void
wrapper_TagEnd (void *data, const XML_Char * name)
{
// if data is null we probably should report this somehow to the user
XMLParser *xp = (XMLParser *) data;
wxString el (wxConvertMB2WX (name));
if (xp)
xp->TagEnd (el);
}
XMLParser::XMLParser ()
{
m_parser = XML_ParserCreate (NULL);
m_depth = 0;
// set userdate to this parser, so we can retrieve a pointer to this
// class from the wrapper functions
XML_SetUserData (m_parser, this);
XML_SetElementHandler (m_parser, wrapper_TagStart, wrapper_TagEnd);
XML_SetCharacterDataHandler (m_parser, wrapper_Characters);
m_handler = NULL;
};
XMLParser::~XMLParser ()
{
XML_ParserFree (m_parser);
};
void
XMLParser::TagStart (wxString & el, wxArrayString & attr)
{
m_depth++;
if (m_handler)
m_handler->TagStart (el, attr);
} /* End of start handler */
void
XMLParser::TagEnd (wxString & el)
{
m_depth--;
if (m_handler)
m_handler->TagEnd (el);
} /* End of end handler */
void
XMLParser::Characters (wxString & chars)
{
if (m_handler)
m_handler->Characters (chars);
} /* End of character handler */
int
XMLParser::CurrentLineNumber ()
{
return m_lineNbr;
}
wxString XMLParser::ErrorString ()
{
return wxString (wxConvertMB2WX (XML_ErrorString (m_errorNbr)));
}
int
XMLParser::ErrorCode ()
{
return m_errorNbr;
}
bool XMLParser::Parse (wxString buffer, int &value)
{
int
retVal =
XML_Parse (m_parser, wxConvertWX2MB (buffer.c_str ()),
buffer.length (),
0);
if (retVal == 0)
return false;
return true;
}
#if defined(XMLParser_MAIN_NEEDED)
#include "wx/ffile.h"
#if defined(WIN32) && defined(_DEBUG)
#include <crtdbg.h>
#endif
int
main (int argc, char *argv[])
{
#if defined(WIN32) && defined(_DEBUG)
// Set Breakpoint at memory allocation
//_CrtSetBreakAlloc(94);
#endif
XMLParser *
xp =
new
XMLParser ();
int
len;
wxFFile *
fp =
new
wxFFile (argv[1]);
if (!fp->IsOpened ()) {
std::
cerr << "Could not open \"" << argv[1] << "\" for reading" <<
std::endl;
return 1;
}
wxString
contents;
char *
buffer;
buffer = new char[fp->Length () + 1];
fp->Read (buffer, fp->Length ());
buffer[fp->Length () - 1] = '\0';
delete
fp;
contents = buffer;
/*
contents = "<?xml version=\"1.0\"?>\n<outer type=\"classtag\">Outer"
" Tag\n <inner mytype=\"enclosed\">Enclosed tag</inner>\n</outer>\n";
contents = "<?xml version=\"1.0\"?>\n\n<report>\n</report>\n";
*/
//std::cerr << "Contents: " << contents.Mid(contents.Length()-80).c_str() << endl;
//std::cerr << "file contents:" << endl << contents.c_str() << endl;
bool
retCode =
xp->
Parse (contents, len);
if (retCode) {
std::cout << "end of parsing" << std::endl;
}
else {
std::cout << "parse error" << endl;
}
delete
xp;
delete[]buffer;
}
#endif