⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlstorage.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
📖 第 1 页 / 共 2 页
字号:

 //
 // XML storage classes version 1.2
 //
 // Copyright (c) 2004, 2005, 2006, 2007 Martin Fuchs <martin-fuchs@gmx.net>
 //

 /// \file xmlstorage.cpp
 /// XMLStorage implementation file


/*

  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 XS_NO_COMMENT
#define XS_NO_COMMENT	// no #pragma comment(lib, ...) statements in .lib files
#endif

//#include "xmlstorage.h"
#include <precomp.h>


namespace XMLStorage {


 // work around GCC's wide string constant bug
#ifdef __GNUC__
const LPCXSSTR XS_EMPTY = XS_EMPTY_STR;
const LPCXSSTR XS_TRUE = XS_TRUE_STR;
const LPCXSSTR XS_FALSE = XS_FALSE_STR;
const LPCXSSTR XS_INTFMT = XS_INTFMT_STR;
const LPCXSSTR XS_FLOATFMT = XS_FLOATFMT_STR;
#endif


 /// 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);
}

inline std::string unescape(const char* s)
{
	return unescape(s, '"', '"');
}

 /// remove escape characters from string with specified length
static std::string unescape(const char* s, size_t 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);
}

inline std::string unescape(const char* s, size_t l)
{
	return unescape(s, l, '"', '"');
}


 /// 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;

		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);
				}
			}
		}

		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, bool cdata)
{
	LPCXSSTR s = str.c_str();
	size_t l = XS_len(s);

	if (cdata) {
		 // encode the whole string in a CDATA section
		std::string ret = "<![CDATA[";

#ifdef XS_STRING_UTF8
		ret += str;
#else
		ret += get_utf8(str);
#endif

		ret += "]]>";

		return ret;
	} else if (l <= BUFFER_LEN) {
		LPXSSTR buffer = (LPXSSTR)alloca(6*sizeof(XS_CHAR)*XS_len(s));	// worst case "&quot;" / "&apos;"
		LPXSSTR o = buffer;

		for(LPCXSSTR p=s; *p; ++p)
			switch(*p) {
			  case '&':
				*o++ = '&';	*o++ = 'a';	*o++ = 'm';	*o++ = 'p';	*o++ = ';';				// "&amp;"
				break;

			  case '<':
				*o++ = '&';	*o++ = 'l'; *o++ = 't';	*o++ = ';';							// "&lt;"
				break;

			  case '>':
				*o++ = '&';	*o++ = 'g'; *o++ = 't';	*o++ = ';';							// "&gt;"
				break;

			  case '"':
				*o++ = '&';	*o++ = 'q'; *o++ = 'u'; *o++ = 'o'; *o++ = 't';	*o++ = ';';	// "&quot;"
				break;

			  case '\'':
				*o++ = '&';	*o++ = 'a'; *o++ = 'p'; *o++ = 'o'; *o++ = 's';	*o++ = ';';	// "&apos;"
				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
	} else { // l > BUFFER_LEN
		 // alternative code for larger strings using ostringstream
		 // and avoiding to use alloca() for preallocated memory
		fast_ostringstream out;

		LPCXSSTR s = str.c_str();

		for(LPCXSSTR p=s; *p; ++p)
			switch(*p) {
			  case '&':
				out << "&amp;";
				break;

			  case '<':
				out << "&lt;";
				break;

			  case '>':
				out << "&gt;";
				break;

			  case '"':
				out << "&quot;";
				break;

			  case '\'':
				out << "&apos;";
				break;

			  default:
				if ((unsigned)*p<20 && *p!='\t' && *p!='\r' && *p!='\n')
					out << "&" << (unsigned)*p << ";";
				else
					out << *p;
			}

#ifdef XS_STRING_UTF8
		return XS_String(out.str());
#else
		return get_utf8(out.str());
#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++ = '"';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -