2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_XML_PARSE_HPP
|
|
|
|
#define TORRENT_XML_PARSE_HPP
|
|
|
|
|
2007-08-07 08:48:47 +02:00
|
|
|
#include <cctype>
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2007-08-07 03:32:38 +02:00
|
|
|
enum
|
|
|
|
{
|
2007-08-07 05:27:08 +02:00
|
|
|
xml_start_tag,
|
|
|
|
xml_end_tag,
|
|
|
|
xml_empty_tag,
|
|
|
|
xml_declaration_tag,
|
|
|
|
xml_string,
|
|
|
|
xml_attribute,
|
|
|
|
xml_comment,
|
|
|
|
xml_parse_error
|
2007-08-07 03:32:38 +02:00
|
|
|
};
|
|
|
|
|
2007-08-07 08:48:47 +02:00
|
|
|
// callback(int type, char const* name, char const* val)
|
|
|
|
// str2 is only used for attributes. name is element or attribute
|
|
|
|
// name and val is attribute value
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
template <class CallbackType>
|
|
|
|
void xml_parse(char* p, char* end, CallbackType callback)
|
|
|
|
{
|
|
|
|
for(;p != end; ++p)
|
|
|
|
{
|
|
|
|
char const* start = p;
|
2007-08-07 03:32:38 +02:00
|
|
|
char const* val_start = 0;
|
|
|
|
int token;
|
2007-03-27 09:04:31 +02:00
|
|
|
// look for tag start
|
|
|
|
for(; *p != '<' && p != end; ++p);
|
|
|
|
|
|
|
|
if (p != start)
|
|
|
|
{
|
|
|
|
if (p != end)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*p == '<');
|
2007-03-27 09:04:31 +02:00
|
|
|
*p = 0;
|
|
|
|
}
|
2007-08-07 03:32:38 +02:00
|
|
|
token = xml_string;
|
|
|
|
callback(token, start, val_start);
|
2007-03-27 09:04:31 +02:00
|
|
|
if (p != end) *p = '<';
|
|
|
|
}
|
2007-08-07 03:32:38 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (p == end) break;
|
|
|
|
|
|
|
|
// skip '<'
|
|
|
|
++p;
|
|
|
|
|
2007-08-07 03:32:38 +02:00
|
|
|
// parse the name of the tag.
|
2007-08-07 08:48:47 +02:00
|
|
|
for (start = p; p != end && *p != '>' && !std::isspace(*p); ++p);
|
2007-08-07 03:32:38 +02:00
|
|
|
|
|
|
|
char* tag_name_end = p;
|
|
|
|
|
|
|
|
// skip the attributes for now
|
|
|
|
for (; p != end && *p != '>'; ++p);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
// parse error
|
2007-08-07 05:27:08 +02:00
|
|
|
if (p == end)
|
|
|
|
{
|
|
|
|
token = xml_parse_error;
|
|
|
|
start = "unexpected end of file";
|
|
|
|
callback(token, start, val_start);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*p == '>');
|
2007-08-07 03:32:38 +02:00
|
|
|
// save the character that terminated the tag name
|
|
|
|
// it could be both '>' and ' '.
|
|
|
|
char save = *tag_name_end;
|
|
|
|
*tag_name_end = 0;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-08-07 03:32:38 +02:00
|
|
|
char* tag_end = p;
|
2007-03-27 09:04:31 +02:00
|
|
|
if (*start == '/')
|
|
|
|
{
|
|
|
|
++start;
|
2007-08-07 03:32:38 +02:00
|
|
|
token = xml_end_tag;
|
|
|
|
callback(token, start, val_start);
|
|
|
|
}
|
|
|
|
else if (*(p-1) == '/')
|
|
|
|
{
|
|
|
|
*(p-1) = 0;
|
|
|
|
token = xml_empty_tag;
|
|
|
|
callback(token, start, val_start);
|
|
|
|
*(p-1) = '/';
|
|
|
|
tag_end = p - 1;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2007-08-07 05:27:08 +02:00
|
|
|
else if (*start == '?' && *(p-1) == '?')
|
|
|
|
{
|
|
|
|
*(p-1) = 0;
|
|
|
|
++start;
|
|
|
|
token = xml_declaration_tag;
|
|
|
|
callback(token, start, val_start);
|
|
|
|
*(p-1) = '?';
|
|
|
|
tag_end = p - 1;
|
|
|
|
}
|
|
|
|
else if (start + 5 < p && memcmp(start, "!--", 3) == 0 && memcmp(p-2, "--", 2) == 0)
|
|
|
|
{
|
|
|
|
start += 3;
|
|
|
|
*(p-2) = 0;
|
|
|
|
token = xml_comment;
|
|
|
|
callback(token, start, val_start);
|
|
|
|
*(p-2) = '-';
|
|
|
|
tag_end = p - 2;
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
else
|
|
|
|
{
|
2007-08-07 03:32:38 +02:00
|
|
|
token = xml_start_tag;
|
|
|
|
callback(token, start, val_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
*tag_name_end = save;
|
|
|
|
|
|
|
|
// parse attributes
|
|
|
|
for (char* i = tag_name_end; i < tag_end; ++i)
|
|
|
|
{
|
2007-08-07 05:27:08 +02:00
|
|
|
// find start of attribute name
|
2007-08-07 08:48:47 +02:00
|
|
|
for (; i != tag_end && std::isspace(*i); ++i);
|
2007-08-07 05:27:08 +02:00
|
|
|
if (i == tag_end) break;
|
|
|
|
start = i;
|
|
|
|
// find end of attribute name
|
2007-08-07 08:48:47 +02:00
|
|
|
for (; i != tag_end && *i != '=' && !std::isspace(*i); ++i);
|
2007-08-07 05:27:08 +02:00
|
|
|
char* name_end = i;
|
|
|
|
|
|
|
|
// look for equality sign
|
|
|
|
for (; i != tag_end && *i != '='; ++i);
|
|
|
|
|
|
|
|
if (i == tag_end)
|
|
|
|
{
|
|
|
|
token = xml_parse_error;
|
|
|
|
val_start = 0;
|
|
|
|
start = "garbage inside element brackets";
|
|
|
|
callback(token, start, val_start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
2007-08-07 08:48:47 +02:00
|
|
|
for (; i != tag_end && std::isspace(*i); ++i);
|
2007-08-07 05:27:08 +02:00
|
|
|
// check for parse error (values must be quoted)
|
|
|
|
if (i == tag_end || (*i != '\'' && *i != '\"'))
|
|
|
|
{
|
|
|
|
token = xml_parse_error;
|
|
|
|
val_start = 0;
|
|
|
|
start = "unquoted attribute value";
|
|
|
|
callback(token, start, val_start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char quote = *i;
|
|
|
|
++i;
|
2007-08-07 03:32:38 +02:00
|
|
|
val_start = i;
|
2007-08-07 05:27:08 +02:00
|
|
|
for (; i != tag_end && *i != quote; ++i);
|
|
|
|
// parse error (missing end quote)
|
|
|
|
if (i == tag_end)
|
|
|
|
{
|
|
|
|
token = xml_parse_error;
|
|
|
|
val_start = 0;
|
|
|
|
start = "missing end quote on attribute";
|
|
|
|
callback(token, start, val_start);
|
|
|
|
break;
|
|
|
|
}
|
2007-08-07 03:32:38 +02:00
|
|
|
save = *i;
|
|
|
|
*i = 0;
|
2007-08-07 05:27:08 +02:00
|
|
|
*name_end = 0;
|
2007-08-07 03:32:38 +02:00
|
|
|
token = xml_attribute;
|
|
|
|
callback(token, start, val_start);
|
2007-08-07 05:27:08 +02:00
|
|
|
*name_end = '=';
|
2007-08-07 03:32:38 +02:00
|
|
|
*i = save;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|