Download this file

picoxml.h    293 lines (269 with data), 9.9 kB

  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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Copyright (C) 2016 J.F.Dockes
* 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.
*/
#ifndef _PICOXML_H_INCLUDED_
#define _PICOXML_H_INCLUDED_
/**
* PicoXMLParser: a single include file parser for an XML-like, but
* restricted language, adequate for config files, not for arbitrary
* externally generated data.
*
* - The code depends on nothing but the "classical" C++ standard
* library (c++11 not necessary).
* - The input to the parser is a single c++ string. Does not deal with
* input in several pieces or files.
* - SAX mode only. You have access to the tag stack. I've always
* found DOM mode less usable.
* - Checks for proper tag nesting and not much else.
* - ! No CDATA
* - ! Attributes should really really not contain XML special chars.
* - Entity decoding is left as an exercise to the user.
*
* A typical input would be like the following (you can add XML
* declarations, whitespace and newlines to taste).
*
* <top>top chrs1<sub attr="attrval">sub chrs</sub>top chrs2 <emptyelt /></top>
*
* Usage: subclass PicoXMLParser, overriding the methods in the
* "protected:" section (look there for more details), call the
* constructor with your input, then call parse().
*/
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <iostream>
class PicoXMLParser {
public:
PicoXMLParser(const std::string& input)
: m_in(input), m_pos(0) {
}
virtual ~PicoXMLParser() { }
virtual bool parse() {
// skip initial whitespace and XML decl. On success, returns with
// current pos on first tag '<'
if (!skipDecl()) {
return false;
}
if (nomore()) {
// empty file
return true;
}
for (;;) {
// Current char is '<' and the next char is not '?'
//std::cerr<< "m_pos "<< m_pos<<" char "<< m_in[m_pos] << std::endl;
m_pos++;
if (nomore()) {
m_reason << "EOF within tag";
return false;
}
std::string::size_type spos = m_pos;
int isendtag = m_in[m_pos] == '/' ? 1 : 0;
skipStr(">");
if (m_pos == std::string::npos || m_pos <= spos + 1) {
m_reason << "Empty tag or EOF inside tag. pos " << spos;
return false;
}
int emptyel = m_in[m_pos-2] == '/' ? 1 : 0;
if (emptyel && isendtag) {
m_reason << "Bad tag </xx/> at cpos " << spos;
return false;
}
std::string tag =
m_in.substr(spos + isendtag,
m_pos - (spos + 1 + isendtag + emptyel));
//std::cerr << "TAG NAME [" << tag << "]\n";
trimtag(tag);
std::map<std::string, std::string> attrs;
if (!parseattrs(tag, attrs)) {
return false;
}
if (isendtag) {
if (m_tagstack.empty() || tag.compare(m_tagstack.back())) {
m_reason << "Closing not open tag " << tag <<
" at cpos " << m_pos;
return false;
}
m_tagstack.pop_back();
endElement(tag);
} else {
startElement(tag, attrs);
m_tagstack.push_back(tag);
if (emptyel) {
m_tagstack.pop_back();
endElement(tag);
}
}
spos = m_pos;
m_pos = m_in.find("<", m_pos);
if (nomore()) {
if (!m_tagstack.empty()) {
m_reason << "EOF hit inside open element";
return false;
}
return true;
}
if (m_pos != spos) {
characterData(m_in.substr(spos, m_pos - spos));
}
}
return false;
}
virtual std::string getReason() {
return m_reason.str();
}
protected:
/* Methods to be overriden */
/**
* Called when seeing an opening tag.
* @param tagname the tag name
* @param attrs a map of attribute name/value pairs
*/
virtual void startElement(const std::string& /*tagname*/,
const std::map<std::string, std::string>&
/* attrs */) {
}
/**
* Called when closing a tag. You should probably have been
* accumulating text and stuff since the tag opening.
* @param tagname the tag name.
*/
virtual void endElement(const std::string& /*tagname*/) {}
/*
* Called when we see non-tag data.
* @param data the data.
*/
virtual void characterData(const std::string& /*data*/) {}
/*
* Gives access to the current path in the tree. Attributes are
* not kept in there though, you'll have to do this yourself.
* @return a const ref to a vector of tag names.
*/
virtual const std::vector<std::string>& tagStack() {
return m_tagstack;
}
private:
const std::string& m_in;
std::string::size_type m_pos;
std::stringstream m_reason;
std::vector<std::string> m_tagstack;
bool nomore() const {
return m_pos == std::string::npos || m_pos == m_in.size();
}
bool skipWS(const std::string& in, std::string::size_type& pos) {
if (pos == std::string::npos)
return false;
pos = in.find_first_not_of(" \t\n\r", pos);
return pos != std::string::npos;
}
bool skipStr(const std::string& str) {
if (m_pos == std::string::npos)
return false;
m_pos = m_in.find(str, m_pos);
if (m_pos != std::string::npos)
m_pos += str.size();
return m_pos != std::string::npos;
}
int peek() const {
if (nomore())
return -1;
return m_in[m_pos + 1];
}
void trimtag(std::string& tagname) {
std::string::size_type trimpos = tagname.find_last_not_of(" \t\n\r");
if (trimpos != std::string::npos) {
tagname = tagname.substr(0, trimpos+1);
}
}
bool skipDecl() {
for (;;) {
if (!skipWS(m_in, m_pos)) {
m_reason << "EOF during initial ws skip";
return true;
}
if (m_in[m_pos] != '<') {
m_reason << "EOF file does not begin with decl/tag";
return false;
}
if (peek() == '?') {
if (!skipStr("?>")) {
m_reason << "EOF while looking for end of xml decl";
return false;
}
} else {
break;
}
}
return true;
}
bool parseattrs(std::string& tag,
std::map<std::string, std::string>& attrs) {
//std::cerr << "parseattrs: [" << tag << "]\n";
attrs.clear();
std::string::size_type spos = tag.find_first_of(" \t\n\r");
if (spos == std::string::npos)
return true;
std::string tagname = tag.substr(0, spos);
//std::cerr << "tag name [" << tagname << "] pos " << spos << "\n";
skipWS(tag, spos);
for (;;) {
//std::cerr << "top of loop [" << tag.substr(spos) << "]\n";
std::string::size_type epos = tag.find_first_of(" \t\n\r=", spos);
if (epos == std::string::npos) {
m_reason << "Bad attributes syntax at cpos " << m_pos + epos;
return false;
}
std::string attrnm = tag.substr(spos, epos - spos);
if (attrnm.empty()) {
m_reason << "Empty attribute name ?? at cpos " << m_pos + epos;
return false;
}
//std::cerr << "attr name [" << attrnm << "]\n";
skipWS(tag, epos);
if (epos == std::string::npos || epos == tag.size() - 1 ||
tag[epos] != '=') {
m_reason <<"Missing equal sign or value at cpos " << m_pos+epos;
return false;
}
epos++;
skipWS(tag, epos);
if (tag[epos] != '"' || epos == tag.size() - 1) {
m_reason << "Missing dquote or value at cpos " << m_pos+epos;
return false;
}
spos = epos + 1;
epos = tag.find_first_of("\"", spos);
if (epos == std::string::npos) {
m_reason << "Missing closing dquote at cpos " << m_pos+spos;
return false;
}
attrs[attrnm] = tag.substr(spos, epos - spos);
//std::cerr << "attr value [" << attrs[attrnm] << "]\n";
if (epos == tag.size() - 1) {
break;
}
epos++;
skipWS(tag, epos);
if (epos == tag.size() - 1) {
break;
}
spos = epos;
}
tag = tagname;
return true;
}
};
#endif /* _PICOXML_H_INCLUDED_ */