Commit [r126] Maximize Restore History

cleaned up xml2sql. case-insensitive searching that is independant of the current locale.

PhrostByte 2006-11-27

changed /gozoku/docs/index.html
changed /gozoku/gozoku/database.cpp
changed /gozoku/xml2sql/xml2sql.cpp
changed /gozoku/xml2sql/xml2sql.vcproj
changed /gozoku/setup/setup.iss
copied /gozoku/xml2sql/xml_reader.hpp -> /gozoku/xml2sql/xmllite.hpp
/gozoku/docs/index.html Diff Switch to side-by-side view
Loading...
/gozoku/gozoku/database.cpp Diff Switch to side-by-side view
Loading...
/gozoku/xml2sql/xml2sql.cpp Diff Switch to side-by-side view
Loading...
/gozoku/xml2sql/xml2sql.vcproj Diff Switch to side-by-side view
Loading...
/gozoku/setup/setup.iss Diff Switch to side-by-side view
Loading...
/gozoku/xml2sql/xml_reader.hpp to /gozoku/xml2sql/xmllite.hpp
--- a/gozoku/xml2sql/xml_reader.hpp
+++ b/gozoku/xml2sql/xmllite.hpp
@@ -29,121 +29,166 @@
 		website: http://www.int64.org
 */
 
-#ifndef __XML_READER_HPP__
-#define __XML_READER_HPP__
+#pragma once
 
 #include <string>
 #include <stdexcept>
-#include <boost/utility.hpp>
-#include <libxml/xmlreader.h>
-#include "../gozoku/mapped_file.hpp"
 
-class xml_error : public std::runtime_error {
+#include <windows.h>
+#include <atlbase.h>
+#include <xmllite.h>
+
+class xml_error : public std::exception {
+private:
+	const char* const m_msg;
+	const HRESULT m_res;
+
 public:
-	xml_error(const char *msg) : runtime_error(msg) {}
+	xml_error(const char *msg, HRESULT res) : m_msg(msg),m_res(res) {}
+
+	const char *what() const { return m_msg; }
+	HRESULT result() const { return m_res; }
 };
 
-class xml_reader : boost::noncopyable {
+class xml_reader {
 public:
 	enum node_type {
-		none,
-		element,
-		attribute,
-		text,
-		cdata,
-		entityreference,
-		entity,
-		processinginstruction,
-		comment,
-		document,
-		documenttype,
-		documentfragment,
-		notation,
-		whitespace,
-		significantwhitespace,
-		endelement,
-		endentity,
-		xmldeclaration
+		none = XmlNodeType_None,
+		element = XmlNodeType_Element,
+		attribute = XmlNodeType_Attribute,
+		text = XmlNodeType_Text,
+		cdata = XmlNodeType_CDATA,
+		procinstruction = XmlNodeType_ProcessingInstruction,
+		comment = XmlNodeType_Comment,
+		doctype = XmlNodeType_DocumentType,
+		whitespace = XmlNodeType_Whitespace,
+		endelement = XmlNodeType_EndElement,
+		xmldecl = XmlNodeType_XmlDeclaration
 	};
 
 private:
-	xmlTextReader *reader;
-	mapped_file file;
+	CComPtr<IStream> m_fs;
+	CComPtr<IXmlReader> m_reader;
 
 public:
-	void open(const char *file) {
-		if(reader!=NULL) throw xml_error("reader is already open");
-		
-		this->file.open(file);
+	xml_reader() {}
+	xml_reader(LPCTSTR file) { open(file); }
+	~xml_reader() { close(); }
 
-		reader = xmlReaderForMemory(this->file.begin(), (int)this->file.size(), NULL, NULL, XML_PARSE_NOENT);
-		if(!reader) throw xml_error("unable to open xml file");
+	void open(LPCTSTR file) {
+		HRESULT hr = SHCreateStreamOnFile(file, STGM_READ, &m_fs);
+		if(FAILED(hr)) throw xml_error("unable to open file", hr);
+
+		hr = CreateXmlReader(__uuidof(IXmlReader), (void**)&m_reader, NULL);
+		if(FAILED(hr)) throw xml_error("unable to create xml reader", hr);
+
+		hr = m_reader->SetInput(m_fs);
+		if(FAILED(hr)) throw xml_error("unable to set reader input", hr);
 	}
 
 	void close() {
-		if(reader!=NULL) {
-			xmlTextReaderClose(reader);
-			reader=NULL;
+		m_reader.Release();
+		m_fs.Release();
+	}
 
-			this->file.close();
+	void set_property(UINT prop, LONG_PTR val) {
+		HRESULT hr = m_reader->SetProperty(prop, val);
+		if(FAILED(hr)) throw xml_error("unable to set property", hr);
+	}
+
+	void process_dtd(bool process = true) {
+		set_property(XmlReaderProperty_DtdProcessing, process ? DtdProcessing_Parse : DtdProcessing_Prohibit);
+	}
+
+	void max_entityexpansion(unsigned int limit = 0) {
+		set_property(XmlReaderProperty_MaxEntityExpansion, limit);
+	}
+
+	node_type read() {
+		XmlNodeType nt;
+
+		HRESULT hr = m_reader->Read(&nt);
+		if(FAILED(hr)) throw xml_error("unable to read", hr);
+
+		return static_cast<node_type>(nt);
+	}
+
+	void skip_element() {
+		if(!m_reader->IsEmptyElement()) {
+			unsigned int depth = 0;
+
+			node_type type;
+			while((type = read()) != xml_reader::none) {
+				if(type == element) {
+					if(!m_reader->IsEmptyElement()) {
+						++depth;
+					}
+				}
+				else if(type == endelement) {
+					if(!depth--) return;
+				}
+			}
 		}
 	}
 
-	bool read() {
-		switch(xmlTextReaderRead(reader)) {
-			case 0: return false;
-			case 1: return true;
-			default: throw xml_error("unable to read");
-		}
+	UINT depth() {
+		UINT depth;
+
+		HRESULT hr = m_reader->GetDepth(&depth);
+		if(FAILED(hr)) throw xml_error("unable to get depth", hr);
+
+		return depth;
 	}
 
-	node_type type() {
-		int i=xmlTextReaderNodeType(reader);
-		if(i==-1) throw xml_error("unable to get node type");
+	UINT line_number() {
+		UINT line;
 
-		return (node_type)i;
+		HRESULT hr = m_reader->GetLineNumber(&line);
+		if(FAILED(hr)) throw xml_error("unable to get line number", hr);
+
+		return line;
 	}
 
-	bool empty() {
-		switch(xmlTextReaderIsEmptyElement(reader)) {
-			case 0: return false;
-			case 1: return true;
-			default: throw xml_error("unable to read next");
-		}
+	UINT column_number() {
+		UINT col;
+
+		HRESULT hr = m_reader->GetLinePosition(&col);
+		if(FAILED(hr)) throw xml_error("unable to get line number", hr);
+
+		return col;
 	}
 
-	bool has_attributes() {
-		switch(xmlTextReaderHasAttributes(reader)) {
-			case 0: return false;
-			case 1: return true;
-			default: throw xml_error("unable to read next");
-		}
+	UINT attribute_count() {
+		UINT count;
+
+		HRESULT hr = m_reader->GetAttributeCount(&count);
+		if(FAILED(hr)) throw xml_error("unable to get line number", hr);
+
+		return count;
 	}
 
-	const char *localname() {
-		return (const char*)xmlTextReaderConstLocalName(reader);
-	}
+	const wchar_t* local_name(UINT *len = NULL) {
+		const wchar_t *str;
 
-	const char *value() {
-		return (const char*)xmlTextReaderConstValue(reader);
-	}
-
-	std::string get_attribute(const char *name) {
-		xmlChar *tmp=xmlTextReaderGetAttribute(reader, (xmlChar*)name);
-		if(tmp==NULL) throw xml_error("invalid attribute");
-
-		std::string str((char*)tmp);
-		xmlFree(tmp);
+		HRESULT hr = m_reader->GetLocalName(&str, len);
+		if(FAILED(hr)) throw xml_error("unable to get value", hr);
 
 		return str;
 	}
 
-	int line() { return xmlTextReaderGetParserLineNumber(reader); }
-	int column() { return xmlTextReaderGetParserColumnNumber(reader); }
+	const wchar_t* value(UINT *len = NULL) {
+		const wchar_t *str;
 
-	xml_reader() : reader(NULL) {}
-	xml_reader(const char *file) : reader(NULL) { open(file); }
-	~xml_reader() { close(); }
+		HRESULT hr = m_reader->GetValue(&str, len);
+		if(FAILED(hr)) throw xml_error("unable to get value", hr);
+
+		return str;
+	}
+
+	bool move_to_attribute(const wchar_t *attr, const wchar_t *ns = NULL) {
+		HRESULT hr = m_reader->MoveToAttributeByName(attr, ns);
+		if(hr == S_OK) return true;
+		else if(hr == S_FALSE) return false;
+		else throw xml_error("unable to move to attribute", hr);
+	}
 };
-
-#endif