xmlstorage.cpp
字号:
//
// XML storage classes
//
// xmlstorage.cpp
//
// Copyright (c) 2004, 2005, 2006 Martin Fuchs <martin-fuchs@gmx.net>
//
/*
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.
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 _NO_COMMENT
#define _NO_COMMENT // no #pragma comment(lib, ...) statements in .lib files
#endif
//#include "xmlstorage.h"
#include <precomp.h>
// work around GCC's wide string constant bug
#ifdef __GNUC__
const LPCXSSTR XMLStorage::XS_TRUE = XS_TRUE_STR;
const LPCXSSTR XMLStorage::XS_FALSE = XS_FALSE_STR;
const LPCXSSTR XMLStorage::XS_INTFMT = XS_INTFMT_STR;
const LPCXSSTR XMLStorage::XS_FLOATFMT = XS_FLOATFMT_STR;
#endif
namespace XMLStorage {
/// remove escape characters from zero terminated string
static std::string unescape(const char* s, char b='"', char e='"')
{
const char* end = s + strlen(s);
// if (*s == b)
// ++s;
//
// if (end>s && end[-1]==e)
// --end;
if (*s == b)
if (end>s && end[-1]==e)
++s, --end;
return std::string(s, end-s);
}
/// remove escape characters from string with specified length
static std::string unescape(const char* s, int l, char b='"', char e='"')
{
const char* end = s + l;
// if (*s == b)
// ++s;
//
// if (end>s && end[-1]==e)
// --end;
if (*s == b)
if (end>s && end[-1]==e)
++s, --end;
return std::string(s, end-s);
}
/// move XPath like to position in XML tree
bool XMLPos::go(const char* path)
{
XMLNode* node = _cur;
// Is this an absolute path?
if (*path == '/') {
node = _root;
++path;
}
node = node->find_relative(path);
if (node) {
go_to(node);
return true;
} else
return false;
}
/// move XPath like to position in XML tree
bool const_XMLPos::go(const char* path)
{
const XMLNode* node = _cur;
// Is this an absolute path?
if (*path == '/') {
node = _root;
++path;
}
node = node->find_relative(path);
if (node) {
go_to(node);
return true;
} else
return false;
}
const XMLNode* XMLNode::find_relative(const char* path) const
{
const XMLNode* node = this;
// parse relative path
while(*path) {
const char* slash = strchr(path, '/');
if (slash == path)
return NULL;
size_t l = slash? slash-path: strlen(path);
std::string comp(path, l);
path += l;
// look for [n] and [@attr_name="attr_value"] expressions in path components
const char* bracket = strchr(comp.c_str(), '[');
l = bracket? bracket-comp.c_str(): comp.length();
std::string child_name(comp.c_str(), l);
std::string attr_name, attr_value;
int n = 0;
if (bracket) {
std::string expr = unescape(bracket, '[', ']');
const char* p = expr.c_str();
n = atoi(p); // read index number
if (n)
n = n - 1; // convert into zero based index
const char* at = strchr(p, '@');
if (at) {
p = at + 1;
const char* equal = strchr(p, '=');
// read attribute name and value
if (equal) {
attr_name = unescape(p, equal-p);
attr_value = unescape(equal+1);
}
}
}
if (attr_name.empty())
// search n.th child node with specified name
node = node->find(child_name, n);
else
// search n.th child node with specified name and matching attribute value
node = node->find(child_name, attr_name, attr_value, n);
if (!node)
return NULL;
if (*path == '/')
++path;
}
return node;
}
XMLNode* XMLNode::create_relative(const char* path)
{
XMLNode* node = this;
// parse relative path
while(*path) {
const char* slash = strchr(path, '/');
if (slash == path)
return NULL;
int l = slash? slash-path: strlen(path);
std::string comp(path, l);
path += l;
// look for [n] and [@attr_name="attr_value"] expressions in path components
const char* bracket = strchr(comp.c_str(), '[');
l = bracket? bracket-comp.c_str(): comp.length();
std::string child_name(comp.c_str(), l);
std::string attr_name, attr_value;
int n = 0;
if (bracket) {
std::string expr = unescape(bracket, '[', ']');
const char* p = expr.c_str();
n = atoi(p); // read index number
if (n)
n = n - 1; // convert into zero based index
const char* at = strchr(p, '@');
if (at) {
p = at + 1;
const char* equal = strchr(p, '=');
// read attribute name and value
if (equal) {
attr_name = unescape(p, equal-p);
attr_value = unescape(equal+1);
}
}
}
XMLNode* child;
if (attr_name.empty())
// search n.th child node with specified name
child = node->find(child_name, n);
else
// search n.th child node with specified name and matching attribute value
child = node->find(child_name, attr_name, attr_value, n);
if (!child) {
child = new XMLNode(child_name);
node->add_child(child);
if (!attr_name.empty())
(*node)[attr_name] = attr_value;
}
node = child;
if (*path == '/')
++path;
}
return node;
}
/// encode XML string literals
std::string EncodeXMLString(const XS_String& str)
{
LPCXSSTR s = str.c_str();
LPXSSTR buffer = (LPXSSTR)alloca(6*sizeof(XS_CHAR)*XS_len(s)); // worst case """ / "'"
LPXSSTR o = buffer;
for(LPCXSSTR p=s; *p; ++p)
switch(*p) {
case '&':
*o++ = '&'; *o++ = 'a'; *o++ = 'm'; *o++ = 'p'; *o++ = ';';
break;
case '<':
*o++ = '&'; *o++ = 'l'; *o++ = 't'; *o++ = ';';
break;
case '>':
*o++ = '&'; *o++ = 'g'; *o++ = 't'; *o++ = ';';
break;
case '"':
*o++ = '&'; *o++ = 'q'; *o++ = 'u'; *o++ = 'o'; *o++ = 't'; *o++ = ';';
break;
case '\'':
*o++ = '&'; *o++ = 'a'; *o++ = 'p'; *o++ = 'o'; *o++ = 's'; *o++ = ';';
break;
default:
if ((unsigned)*p<20 && *p!='\t' && *p!='\r' && *p!='\n') {
char b[16];
sprintf(b, "&%d;", (unsigned)*p);
for(const char*q=b; *q; )
*o++ = *q++;
} else
*o++ = *p;
}
#ifdef XS_STRING_UTF8
return XS_String(buffer, o-buffer);
#else
return get_utf8(buffer, o-buffer);
#endif
}
/// decode XML string literals
XS_String DecodeXMLString(const XS_String& str)
{
LPCXSSTR s = str.c_str();
LPXSSTR buffer = (LPXSSTR)alloca(sizeof(XS_CHAR)*XS_len(s));
LPXSSTR o = buffer;
for(LPCXSSTR p=s; *p; ++p)
if (*p == '&') {
if (!XS_nicmp(p+1, XS_TEXT("lt;"), 3)) {
*o++ = '<';
p += 3;
} else if (!XS_nicmp(p+1, XS_TEXT("gt;"), 3)) {
*o++ = '>';
p += 3;
} else if (!XS_nicmp(p+1, XS_TEXT("amp;"), 4)) {
*o++ = '&';
p += 4;
} else if (!XS_nicmp(p+1, XS_TEXT("quot;"), 5)) {
*o++ = '"';
p += 5;
} else if (!XS_nicmp(p+1, XS_TEXT("apos;"), 5)) {
*o++ = '\'';
p += 5;
} else
*o++ = *p;
} else
*o++ = *p;
return XS_String(buffer, o-buffer);
}
/// write node with children tree to output stream using original white space
void XMLNode::write_worker(std::ostream& out, int indent) const
{
out << _leading << '<' << EncodeXMLString(*this);
for(AttributeMap::const_iterator it=_attributes.begin(); it!=_attributes.end(); ++it)
out << ' ' << EncodeXMLString(it->first) << "=\"" << EncodeXMLString(it->second) << "\"";
if (!_children.empty() || !_content.empty()) {
out << '>' << _content;
for(Children::const_iterator it=_children.begin(); it!=_children.end(); ++it)
(*it)->write_worker(out, indent+1);
out << _end_leading << "</" << EncodeXMLString(*this) << '>';
} else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -