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

📄 tools.cc

📁 一个非常好的GIS开源新版本
💻 CC
📖 第 1 页 / 共 2 页
字号:
// Tools Library//// Copyright (C) 2004  Navel Ltd.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA////  Email://    mhadji@gmail.com#include <cstring>#include <limits>#include <cfloat>#include <Tools.h>#include "ExternalSort.h"//#include "SHA1.h"Tools::IndexOutOfBoundsException::IndexOutOfBoundsException(int i){	std::ostringstream s;	s << "Invalid index " << i;	m_error = s.str();}std::string Tools::IndexOutOfBoundsException::what(){	return "IndexOutOfBoundsException: " + m_error;}Tools::IllegalArgumentException::IllegalArgumentException(std::string s) : m_error(s){}std::string Tools::IllegalArgumentException::what(){	return "IllegalArgumentException: " + m_error;}Tools::IllegalStateException::IllegalStateException(std::string s) : m_error(s){}std::string Tools::IllegalStateException::what(){	return "IllegalStateException: " + m_error + "\n"; //Please contact " + PACKAGE_BUGREPORT;}Tools::EndOfStreamException::EndOfStreamException(std::string s) : m_error(s){}std::string Tools::EndOfStreamException::what(){	return "EndOfStreamException: " + m_error;}Tools::ResourceLockedException::ResourceLockedException(std::string s) : m_error(s){}std::string Tools::ResourceLockedException::what(){	return "ResourceLockedException: " + m_error;}Tools::InvalidPageException::InvalidPageException(long id){	std::ostringstream s;	s << "Unknown page id " << id;	m_error = s.str();}std::string Tools::InvalidPageException::what(){	return "InvalidPageException: " + m_error;}Tools::NotSupportedException::NotSupportedException(std::string s) : m_error(s){}std::string Tools::NotSupportedException::what(){	return "NotSupportedException: " + m_error;}Tools::ParseErrorException::ParseErrorException(std::string s) : m_error(s){}std::string Tools::ParseErrorException::what(){	return "ParseErrorException: " + m_error;}Tools::Variant::Variant() : m_varType(VT_EMPTY){}Tools::PropertySet::PropertySet(const byte* data){	loadFromByteArray(data);}void Tools::PropertySet::loadFromByteArray(const byte* ptr){	m_propertySet.clear();	unsigned long numberOfProperties;	memcpy(&numberOfProperties, ptr, sizeof(unsigned long));	ptr += sizeof(unsigned long);	Variant v;	for (unsigned long cIndex = 0; cIndex < numberOfProperties; cIndex++)	{		std::string s(reinterpret_cast<const char*>(ptr));		ptr += s.size() + 1;		memcpy(&(v.m_varType), ptr, sizeof(VariantType));		ptr += sizeof(VariantType);		switch (v.m_varType)		{		case VT_LONG:			long l;			memcpy(&l, ptr, sizeof(long));			ptr += sizeof(long);			v.m_val.lVal = l;			break;		case VT_LONGLONG:			long long ll;			memcpy(&ll, ptr, sizeof(long long));			ptr += sizeof(long long);			v.m_val.llVal = ll;			break;		case VT_BYTE:			byte b;			memcpy(&b, ptr, sizeof(byte));			ptr += sizeof(byte);			v.m_val.bVal = b;			break;		case VT_SHORT:			short s;			memcpy(&s, ptr, sizeof(short));			ptr += sizeof(short);			v.m_val.iVal = s;			break;		case VT_FLOAT:			float f;			memcpy(&f, ptr, sizeof(float));			ptr += sizeof(float);			v.m_val.fltVal = f;			break;		case VT_DOUBLE:			double d;			memcpy(&d, ptr, sizeof(double));			ptr += sizeof(double);			v.m_val.dblVal = d;			break;		case VT_CHAR:			char c;			memcpy(&c, ptr, sizeof(char));			ptr += sizeof(char);			v.m_val.cVal = c;			break;		case VT_USHORT:			unsigned short us;			memcpy(&us, ptr, sizeof(unsigned short));			ptr += sizeof(unsigned short);			v.m_val.uiVal = us;			break;		case VT_ULONG:			unsigned long ul;			memcpy(&ul, ptr, sizeof(unsigned long));			ptr += sizeof(unsigned long);			v.m_val.ulVal = ul;			break;		case VT_ULONGLONG:			unsigned long long ull;			memcpy(&ull, ptr, sizeof(unsigned long long));			ptr += sizeof(unsigned long long);			v.m_val.ullVal = ull;			break;		case VT_INT:			int i;			memcpy(&i, ptr, sizeof(int));			ptr += sizeof(int);			v.m_val.intVal = i;			break;		case VT_UINT:			unsigned int ui;			memcpy(&ui, ptr, sizeof(unsigned int));			ptr += sizeof(unsigned int);			v.m_val.uintVal = ui;			break;		case VT_BOOL:			byte bl;			memcpy(&bl, ptr, sizeof(byte));			ptr += sizeof(byte);			v.m_val.blVal = bl!=0;			break;		default:			throw IllegalStateException(				"Tools::PropertySet::PropertySet: Deserialization problem."			);		}		m_propertySet.insert(std::pair<std::string, Variant>(s, v));	}}unsigned long Tools::PropertySet::getByteArraySize(){	unsigned long size = sizeof(unsigned long);	std::map<std::string, Variant>::iterator it;	for (it = m_propertySet.begin(); it != m_propertySet.end(); it++)	{		switch ((*it).second.m_varType)		{		case VT_LONG:			size += sizeof(long);			break;		case VT_LONGLONG:			size += sizeof(long long);			break;		case VT_BYTE:			size += sizeof(byte);			break;		case VT_SHORT:			size += sizeof(short);			break;		case VT_FLOAT:			size += sizeof(float);			break;		case VT_DOUBLE:			size += sizeof(double);			break;		case VT_CHAR:			size += sizeof(char);			break;		case VT_USHORT:			size += sizeof(unsigned short);			break;		case VT_ULONG:			size += sizeof(unsigned long);			break;		case VT_ULONGLONG:			size += sizeof(unsigned long long);			break;		case VT_INT:			size += sizeof(int);			break;		case VT_UINT:			size += sizeof(unsigned int);			break;		case VT_BOOL:			size += sizeof(byte);			break;		default:			throw NotSupportedException(				"Tools::PropertySet::getSize: Unknown type."			);		}		size += (*it).first.size() + 1 + sizeof(VariantType);	}	return size;}void Tools::PropertySet::storeToByteArray(byte** data, unsigned long& length){	length = getByteArraySize();	*data = new byte[length];	byte* ptr = *data;	unsigned long numberOfProperties = m_propertySet.size();	memcpy(ptr, &numberOfProperties, sizeof(unsigned long));	ptr += sizeof(unsigned long);	std::map<std::string, Variant>::iterator it;	for (it = m_propertySet.begin(); it != m_propertySet.end(); it++)	{		unsigned long strSize = (*it).first.size();		memcpy(ptr, (*it).first.c_str(), strSize);		ptr += strSize;		*ptr = 0;		ptr++;		memcpy(ptr, &((*it).second.m_varType), sizeof(VariantType));		ptr += sizeof(VariantType);		switch ((*it).second.m_varType)		{		case VT_LONG:			memcpy(ptr, &((*it).second.m_val.lVal), sizeof(long));			ptr += sizeof(long);			break;		case VT_LONGLONG:			memcpy(ptr, &((*it).second.m_val.llVal), sizeof(long long));			ptr += sizeof(long long);			break;		case VT_BYTE:			memcpy(ptr, &((*it).second.m_val.bVal), sizeof(byte));			ptr += sizeof(byte);			break;		case VT_SHORT:			memcpy(ptr, &((*it).second.m_val.iVal), sizeof(short));			ptr += sizeof(short);			break;		case VT_FLOAT:			memcpy(ptr, &((*it).second.m_val.fltVal), sizeof(float));			ptr += sizeof(float);			break;		case VT_DOUBLE:			memcpy(ptr, &((*it).second.m_val.dblVal), sizeof(double));			ptr += sizeof(double);			break;		case VT_CHAR:			memcpy(ptr, &((*it).second.m_val.cVal), sizeof(char));			ptr += sizeof(char);			break;		case VT_USHORT:			memcpy(ptr, &((*it).second.m_val.uiVal), sizeof(unsigned short));			ptr += sizeof(unsigned short);			break;		case VT_ULONG:			memcpy(ptr, &((*it).second.m_val.ulVal), sizeof(unsigned long));			ptr += sizeof(unsigned long);			break;		case VT_ULONGLONG:			memcpy(ptr, &((*it).second.m_val.ullVal), sizeof(unsigned long long));			ptr += sizeof(unsigned long long);			break;		case VT_INT:			memcpy(ptr, &((*it).second.m_val.intVal), sizeof(int));			ptr += sizeof(int);			break;		case VT_UINT:			memcpy(ptr, &((*it).second.m_val.uintVal), sizeof(unsigned int));			ptr += sizeof(unsigned int);			break;		case VT_BOOL:			byte bl;			bl = (*it).second.m_val.blVal;			memcpy(ptr, &bl, sizeof(byte));			ptr += sizeof(byte);			break;		default:			throw NotSupportedException(				"Tools::PropertySet::getData: Cannot serialize a variant of this type."			);		}	}	assert(ptr == (*data) + length);}Tools::Variant Tools::PropertySet::getProperty(std::string property){	std::map<std::string, Variant>::iterator it = m_propertySet.find(property);	if (it != m_propertySet.end()) return (*it).second;	else return Variant();}void Tools::PropertySet::setProperty(std::string property, Variant& v){	m_propertySet.insert(std::pair<std::string, Variant>(property, v));}void Tools::PropertySet::removeProperty(std::string property){	std::map<std::string, Variant>::iterator it = m_propertySet.find(property);	if (it != m_propertySet.end()) m_propertySet.erase(it);}Tools::IObjectStream* Tools::externalSort(IObjectStream& source, unsigned long bufferSize){	return new ExternalSort(source, bufferSize);}Tools::IObjectStream* Tools::externalSort(IObjectStream& source, IObjectComparator& comp, unsigned long bufferSize){	return new ExternalSort(source, comp, bufferSize);}Tools::Interval::Interval() : m_type(IT_RIGHTOPEN), m_low(0.0), m_high(0.0){}Tools::Interval::Interval(IntervalType t, double l, double h) : m_type(t), m_low(l), m_high(h){	assert(l < h);}Tools::Interval::Interval(double l, double h) : m_type(IT_RIGHTOPEN), m_low(l), m_high(h){	assert(l < h);}Tools::Interval::Interval(const Interval& iv){	m_low = iv.m_low;	m_high = iv.m_high;	m_type = iv.m_type;}Tools::IInterval& Tools::Interval::operator=(const IInterval& iv){	if (this != &iv)	{		m_low = iv.getLowerBound();		m_high = iv.getUpperBound();		m_type = iv.getIntervalType();	}	return *this;}Tools::Interval& Tools::Interval::operator=(const Interval& iv){	*this = *(static_cast<const IInterval*>(&iv));	return *this;}bool Tools::Interval::operator==(const Interval& iv) const{	if (		m_type == iv.m_type &&		m_low >= iv.m_low - std::numeric_limits<double>::epsilon() &&		m_low <= iv.m_low + std::numeric_limits<double>::epsilon() &&		m_high >= iv.m_high - std::numeric_limits<double>::epsilon() &&		m_high <= iv.m_high + std::numeric_limits<double>::epsilon())		return true;	return false;}bool Tools::Interval::operator!=(const Interval& iv) const{	return ! (*this == iv);}double Tools::Interval::getLowerBound() const{	return m_low;}double Tools::Interval::getUpperBound() const{	return m_high;}void Tools::Interval::setBounds(double l, double h){	assert(l <= h);	m_low = l;	m_high = h;}bool Tools::Interval::intersectsInterval(const IInterval& i) const{	return intersectsInterval(i.getIntervalType(), i.getLowerBound(), i.getUpperBound());}bool Tools::Interval::intersectsInterval(IntervalType type, const double low, const double high) const{	if (m_high < m_low)		throw IllegalStateException(			"Tools::Interval::intersectsInterval: high boundary is smaller than low boundary."		);	if (m_low > high || m_high < low) return false;	if ((m_low > low && m_low < high) || (m_high > low && m_high < high)) return true;	switch (m_type)	{	case IT_CLOSED:		if (m_low == high)		{			if (type == IT_CLOSED || type == IT_LEFTOPEN) return true;			else return false;		}		else if (m_high == low)		{			if (type == IT_CLOSED || type == IT_RIGHTOPEN) return true;			else return false;		}		break;	case IT_OPEN:		if (m_low == high || m_high == low) return false;		break;	case IT_RIGHTOPEN:		if (m_low == high)		{			if (type == IT_CLOSED || type == IT_LEFTOPEN) return true;			else return false;		}		else if (m_high == low)		{			return false;		}		break;	case IT_LEFTOPEN:		if (m_low == high)		{			return false;		}		else if (m_high == low)		{			if (type == IT_CLOSED || type == IT_RIGHTOPEN) return true;			else return false;		}		break;	}	return true;}bool Tools::Interval::containsInterval(const IInterval& i) const{	if (m_high < m_low)		throw IllegalStateException(			"Tools::Interval::containsInterval: high boundary is smaller than low boundary."		);	double low = i.getLowerBound();	double high = i.getUpperBound();	IntervalType type = i.getIntervalType();	if (m_low < low && m_high > high) return true;	if (m_low > low || m_high < high) return false;	switch (m_type)	{	case IT_CLOSED:		break;	case IT_OPEN:		if ((m_low == low && m_high == high && type != IT_OPEN) ||			(m_low == low && (type == IT_CLOSED || type == IT_RIGHTOPEN)) ||			(m_high == high && ( type == IT_CLOSED || type == IT_LEFTOPEN)))			return false;		break;	case IT_RIGHTOPEN:		if (m_high == high && (type == IT_CLOSED || type == IT_LEFTOPEN))			return false;		break;	case IT_LEFTOPEN:		if (m_low == low && (type == IT_CLOSED || type == IT_RIGHTOPEN))			return false;		break;	}	return true;}Tools::IntervalType Tools::Interval::getIntervalType() const{	return m_type;}

⌨️ 快捷键说明

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