3
Copyright (c) 2007, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_XML_PARSE_HPP
34
#define TORRENT_XML_PARSE_HPP
52
// callback(int type, char const* name, char const* val)
53
// str2 is only used for attributes. name is element or attribute
54
// name and val is attribute value
56
template <class CallbackType>
57
void xml_parse(char* p, char* end, CallbackType callback)
61
char const* start = p;
62
char const* val_start = 0;
65
for(; *p != '<' && p != end; ++p);
71
TORRENT_ASSERT(*p == '<');
75
callback(token, start, val_start);
76
if (p != end) *p = '<';
84
// parse the name of the tag.
85
for (start = p; p != end && *p != '>' && !std::isspace(*p); ++p);
87
char* tag_name_end = p;
89
// skip the attributes for now
90
for (; p != end && *p != '>'; ++p);
95
token = xml_parse_error;
96
start = "unexpected end of file";
97
callback(token, start, val_start);
101
TORRENT_ASSERT(*p == '>');
102
// save the character that terminated the tag name
103
// it could be both '>' and ' '.
104
char save = *tag_name_end;
112
callback(token, start, val_start);
114
else if (*(p-1) == '/')
117
token = xml_empty_tag;
118
callback(token, start, val_start);
122
else if (*start == '?' && *(p-1) == '?')
126
token = xml_declaration_tag;
127
callback(token, start, val_start);
131
else if (start + 5 < p && memcmp(start, "!--", 3) == 0 && memcmp(p-2, "--", 2) == 0)
136
callback(token, start, val_start);
142
token = xml_start_tag;
143
callback(token, start, val_start);
146
*tag_name_end = save;
149
for (char* i = tag_name_end; i < tag_end; ++i)
151
// find start of attribute name
152
for (; i != tag_end && std::isspace(*i); ++i);
153
if (i == tag_end) break;
155
// find end of attribute name
156
for (; i != tag_end && *i != '=' && !std::isspace(*i); ++i);
159
// look for equality sign
160
for (; i != tag_end && *i != '='; ++i);
164
token = xml_parse_error;
166
start = "garbage inside element brackets";
167
callback(token, start, val_start);
172
for (; i != tag_end && std::isspace(*i); ++i);
173
// check for parse error (values must be quoted)
174
if (i == tag_end || (*i != '\'' && *i != '\"'))
176
token = xml_parse_error;
178
start = "unquoted attribute value";
179
callback(token, start, val_start);
185
for (; i != tag_end && *i != quote; ++i);
186
// parse error (missing end quote)
189
token = xml_parse_error;
191
start = "missing end quote on attribute";
192
callback(token, start, val_start);
198
token = xml_attribute;
199
callback(token, start, val_start);