欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

xmlstorage.cpp

一个类似windows
CPP
第 1 页 / 共 2 页
字号:

 //
 // XML storage classes
 //
 // xmlstorage.cpp
 //
 // Copyright (c) 2004, 2005 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.

*/

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


 // work around GCC's wide string constant bug
#ifdef __GNUC__
const LPCXSSTR XMLStorage::XS_TRUE = XS_TEXT("true");
const LPCXSSTR XMLStorage::XS_FALSE = XS_TEXT("false");
const LPCXSSTR XMLStorage::XS_NUMBERFMT = XS_TEXT("%d");
#endif


namespace XMLStorage {


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

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;

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

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


 /// read XML stream into XML tree below _pos
XML_Status XMLReaderBase::read()
{
	XML_Status status = XML_STATUS_OK;

	while(status == XML_STATUS_OK) {
		char* buffer = (char*) XML_GetBuffer(_parser, BUFFER_LEN);

		int l = read_buffer(buffer, BUFFER_LEN);
		if (l < 0)
			break;

		status = XML_ParseBuffer(_parser, l, false);
	}

	if (status != XML_STATUS_ERROR)
		status = XML_ParseBuffer(_parser, 0, true);

	if (_pos->_children.empty())
		_pos->_trailing.append(_content);
	else
		_pos->_children.back()->_trailing.append(_content);

	_content.erase();

	return status;
}


 /// store XML version and encoding into XML reader
void XMLCALL XMLReaderBase::XML_XmlDeclHandler(void* userData, const XML_Char* version, const XML_Char* encoding, int standalone)
{
	XMLReaderBase* pReader = (XMLReaderBase*) userData;

	if (version)
		pReader->_xml_version = version;

	if (encoding)
		pReader->_encoding = encoding;
}

 /// notifications about XML start tag
void XMLCALL XMLReaderBase::XML_StartElementHandler(void* userData, const XML_Char* name, const XML_Char** atts)
{
	XMLReaderBase* pReader = (XMLReaderBase*) userData;
	XMLPos& pos = pReader->_pos;

	 // search for end of first line
	const char* s = pReader->_content.c_str();
	const char* p = s;
	const char* e = p + pReader->_content.length();

⌨️ 快捷键说明

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