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

📄 xml.cpp

📁 功能齐全的XML解析/生成类. 可以在Windows/WindowsCE下使用.亲自调试通过.
💻 CPP
📖 第 1 页 / 共 5 页
字号:

	if (!c)
		return false;
#ifdef _WIN32
#ifndef WINCE
	if (IsBadStringPtrA(c,-1))
		return false;
#endif
#endif

	if (parent)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(parent,sizeof(XMLElement*)))
			return false;
#endif
#endif
		}

	return true;
	}

int XMLContent :: Compare(XMLContent* x)
	{
	// Contents OK <=> Same text
	if (strcmp(c,x->c) != 0)
		return 1;
	return 0;
	}

XMLContent* XMLContent :: Duplicate()
	{
	// returns a copy of myself

	size_t s2 = GetValue(0);
	Z<char> x2(s2 + 100);
	GetValue(x2);

	return new XMLContent(parent,ep,x2);
	}


size_t XMLCData :: MemoryUsage()
	{
	size_t m = 0;

	// Our size
	m += sizeof(*this);

	// Comment size
	if (c)
		m += strlen(c);

	return m;
	}

void XMLCData :: CompressMemory()
	{
	}

bool XMLCData :: IntegrityTest()
	{
	// check parent,c

	if (!c)
		return false;
#ifdef _WIN32
#ifndef WINCE
	if (IsBadStringPtrA(c,-1))
		return false;
#endif
#endif

	if (parent)
		{
		// Check pointer
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(parent,sizeof(XMLElement*)))
			return false;
#endif
#endif
		}

	return true;
	}

int XMLCData :: Compare(XMLCData* x)
	{
	// Compare OK <=> Same Text
	if (strcmp(c,x->c) != 0)
		return 1;


	return 0;
	}


XMLCData* XMLCData :: Duplicate()
	{
	// returns a copy of myself
	return new XMLCData(parent,ep,c);
	}





size_t XMLVariable :: MemoryUsage()
	{
	size_t m = 0;

	// Our size
	m += sizeof(*this);

	// Variable size
	m += GetName(0);
	m += GetValue(0);

	return m;
	}

void XMLVariable :: CompressMemory()
	{
	}

bool XMLVariable :: IntegrityTest()
	{
	// check vv,vn,owner
	if (!vn || !vv)
		return false;
#ifdef _WIN32
#ifndef WINCE
	if (IsBadStringPtrA(vn,-1))
		return false;
	if (IsBadStringPtrA(vv,-1))
		return false;
#endif
#endif
	if (owner)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(owner,sizeof(XMLElement*)))
			return false;
#endif
#endif
		}
	return true;
	}

int XMLVariable :: Compare(XMLVariable* x)
	{
	// Contents OK <=> Same value & name
	if (strcmp(vn,x->vn) != 0)
		return 1;
	if (strcmp(vv,x->vv) != 0)
		return 1;
	return 0;
	}


size_t XMLElement :: MemoryUsage()
	{
	size_t m = 0;
	// returns # of bytes used by this element's data

	// Our size
	m += sizeof(*this);

	// Variables of this
	for(unsigned int i = 0 ; i < variablesnum ; i++)
		{
		m += GetVariables()[i]->MemoryUsage();
		}

	// Comments of this
	for(unsigned int i = 0 ; i < commentsnum ; i++)
		{
		m += GetComments()[i]->MemoryUsage();
		}

	// Contents of this
	for(unsigned int i = 0 ; i < contentsnum ; i++)
		{
		m += GetContents()[i]->MemoryUsage();
		}

	// CDatas of this
	for(unsigned int i = 0 ; i < cdatasnum ; i++)
		{
		m += GetCDatas()[i]->MemoryUsage();
		}

	// Elements of this
	for(unsigned int i = 0 ; i < childrennum ; i++)
		{
		if (GetChildren()[i])
			m += GetChildren()[i]->MemoryUsage();
		}

	// number of children pointers
	m += TotalChildPointersAvailable*4;

	// number of variable pointers
	m += TotalVariablePointersAvailable*4;

	// number of comment pointers
	m += TotalCommentPointersAvailable*4;

	// number of content pointers
	m += TotalContentPointersAvailable*4;

	// number of cdata pointers
	m += TotalCDataPointersAvailable*4;

	// Element name
	m += GetElementName(0);
	return m;
	}

void XMLElement :: CompressMemory()
	{
		{
		// Remove wasted space by comments
		int PC = commentsnum;
		if (PC == 0)
			PC = 1;

		XMLComment** oldpc = new XMLComment*[PC];
		if (commentsnum)
			memcpy(oldpc,comments,commentsnum*sizeof(XMLComment*));

		TotalCommentPointersAvailable = PC;
		delete[] comments;
		comments = oldpc;
		}

		{
		// Remove wasted space by variables
		int PV = variablesnum;
		if (PV == 0)
			PV = 1;

		XMLVariable** oldpv = new XMLVariable*[PV];
		if (variablesnum)
			memcpy(oldpv,variables,variablesnum*sizeof(XMLVariable*));

		TotalVariablePointersAvailable = PV;
		delete[] variables;
		variables = oldpv;
		}

		{
		// Remove wasted space by children
		int PE = childrennum;
		if (PE == 0)
			PE = 1;

		XMLElement** oldpv = new XMLElement*[PE];
		if (childrennum)
			memcpy(oldpv,children,childrennum*sizeof(XMLElement*));

		TotalChildPointersAvailable = PE;
		delete[] children;
		children = oldpv;
		}

		// Do the same for all Contents
		for(unsigned int i = 0 ; i < contentsnum ; i++)
			{
			contents[i]->CompressMemory();
			}

		// Do the same for all Comments
		for(unsigned int i = 0 ; i < commentsnum ; i++)
			{
			comments[i]->CompressMemory();
			}

		// Do the same for all CDatas
		for(unsigned int i = 0 ; i < cdatasnum ; i++)
			{
			cdatas[i]->CompressMemory();
			}

		// Do the same for all Variables
		for(unsigned int i = 0 ; i < variablesnum ; i++)
			{
			variables[i]->CompressMemory();
			}

		// Do the same for all child elements
		for(unsigned int i = 0 ; i < childrennum ; i++)
			{
			if (children[i])
				children[i]->CompressMemory();
			}

	}

bool XMLElement :: IntegrityTest()
	{
	// The main meat IntegrityTest

	/*

	Check

	name
	parent
	childen
	contents
	variables
	comments

	char* el; // element name
	XMLElement* parent; // one
	XMLElement** children; // many
	XMLVariable** variables; // many
	XMLComment** comments; // many
	XMLContent** contents; // many;

	*/

#ifdef _WIN32
#ifndef WINCE
	// parent pointer
	if (parent && IsBadReadPtr(parent,sizeof(XMLElement*)))
		return false;
	if (IsBadStringPtrA(el,-1))
		return false;
#endif
#endif


#ifdef _WIN32
#ifndef WINCE
	// Comments pointer
	if (IsBadReadPtr(comments,sizeof(XMLComment*)*commentsnum))
		return false;
#endif
#endif

	// Check comment
	for(unsigned int i = 0 ; i < commentsnum ; i++)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(GetComments()[i],sizeof(XMLComment*)))
			return false;
#endif
#endif
		if (!GetComments()[i]->IntegrityTest())
			return false;
		}

#ifdef _WIN32
#ifndef WINCE
	// Contents pointer
	if (IsBadReadPtr(contents,sizeof(XMLContent*)*contentsnum))
		return false;
#endif
#endif

	// Check content
	for(unsigned int i = 0 ; i < contentsnum ; i++)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(GetContents()[i],sizeof(XMLContent*)))
			return false;
#endif
#endif
		if (!GetContents()[i]->IntegrityTest())
			return false;
		}


#ifdef _WIN32
#ifndef WINCE
	// Variables pointer
	if (IsBadReadPtr(variables,sizeof(XMLVariable*)*variablesnum))
		return false;
#endif
#endif

	// Check comment
	for(unsigned int i = 0 ; i < variablesnum ; i++)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(GetVariables()[i],sizeof(XMLVariable*)))
			return false;
#endif
#endif
		if (!GetVariables()[i]->IntegrityTest())
			return false;
		}

#ifdef _WIN32
#ifndef WINCE
	// Children pointer
	if (IsBadReadPtr(children,sizeof(XMLElement*)*childrennum))
		return false;
#endif
#endif

	// Check children
	for(unsigned int i = 0 ; i < childrennum ; i++)
		{
#ifdef _WIN32
#ifndef WINCE
		if (IsBadReadPtr(GetChildren()[i],sizeof(XMLElement*)))
			return false;
#endif
#endif
		if (!GetChildren()[i]->IntegrityTest())
			return false;
		}



	return true;
	}


int XMLElement :: Compare(XMLElement* x)
	{
	/*
	XMLElements match if

	Have same element name

	Have same # of variables,and they match
	Have same # of comments, and they match
	Have same # of contents, and they match
	Have same # of children, and they match
	*/

	// Test element name
	if (strcmp(el,x->el) != 0)
		return 1;

	// Test Variables
	unsigned int nV = GetVariableNum();
	if (nV != x->GetVariableNum())
		return 1;
	for(unsigned int i = 0 ; i < nV ; i++)
		{
		if (GetVariables()[i]->Compare(x->GetVariables()[i]) != 0)
			return 1;
		}

	// Test Comments
	unsigned int nC = GetCommentsNum();
	if (nC != x->GetCommentsNum())
		return 1;
	for(unsigned int i = 0 ; i < nC ; i++)
		{
		if (GetComments()[i]->Compare(x->GetComments()[i]) != 0)
			return 1;
		}

	// Test CDatas
	unsigned int nD = GetCDatasNum();
	if (nD != x->GetCDatasNum())
		return 1;
	for(unsigned int i = 0 ; i < nD ; i++)
		{
		if (GetCDatas()[i]->Compare(x->GetCDatas()[i]) != 0)
			return 1;
		}

	// Test Contents
	unsigned int nT = GetContentsNum();
	if (nT != x->GetContentsNum())
		return 1;
	for(unsigned int i = 0 ; i < nT ; i++)
		{
		if (GetContents()[i]->Compare(x->GetContents()[i]) != 0)
			return 1;
		}

	// Test Children Elements
	unsigned int nE = GetChildrenNum();
	if (nE != x->GetChildrenNum())
		return 1;
	for(unsigned int i = 0 ; i < nE ; i++)
		{
		if (!GetChildren()[i] || !x->GetChildren()[i])
			continue;
		if (GetChildren()[i]->Compare(x->GetChildren()[i]) != 0)
			return 1;
		}

	return 0; // MATCH!
	}


void XMLElement :: Copy()
	{
	// Copies this element to clipboard as a text
#ifdef _WIN32
	size_t M = MemoryUsage();
	Z<char> d(M);
	Export((FILE*)d.operator char *(),1,XML_SAVE_MODE_DEFAULT,XML_TARGET_MODE_MEMORY);
	size_t S = strlen(d);

	OpenClipboard(0);
	EmptyClipboard();

	HGLOBAL hG =
		GlobalAlloc(GMEM_MOVEABLE, S + 10);
	void *pp = GlobalLock(hG);
	//lstrcpyA((char *)pp, d.operator char *());
	strcpy((char*)pp,d.operator char *());
	GlobalUnlock(hG);
	SetClipboardData(CF_TEXT, hG);
	CloseClipboard();
#endif
	}

XMLElement* XML :: Paste(char* txt)
	{
	if (txt)
		{
		XML* xm = new XML();
		xm->Load(txt,XML_LOAD_MODE_MEMORY_BUFFER,0,0);
		int K = xm->ParseStatus();
		if (K == 2) // Fatal error
			{
			delete xm;
			return 0;
			}
		XMLElement* r = xm->GetRootElement()->Duplicate(0);
		delete xm;
		return r;
		}

#ifdef _WIN32
	OpenClipboard(0);

	HGLOBAL hG =
		GetClipboardData(CF_TEXT);
	if (!hG)
		{
		CloseClipboard();
		return 0;
		}


	void *pp = GlobalLock(hG);
	size_t S = strlen((char*)pp);
	Z<char> d(S + 100);
	strcpy(d,(char*)pp);

	GlobalUnlock(hG);
	CloseClipboard();

	// d has the data, size S
	XML* xm = new XML();
	xm->Load(d,XML_LOAD_MODE_MEMORY_BUFFER,0,0);
	int K = xm->ParseStatus();
	if (K == 2) // Fatal error
		{
		delete xm;
		return 0;
		}
	XMLElement* r = xm->GetRootElement()->Duplicate(0);
	delete xm;
	return r;
#else
	return 0;
#endif
	}

XMLElement* XMLElement :: Duplicate(XMLElement* par)
	{
	// Creates a new XML element, excact copy of myself
	/*
	Formula
	dup all variables for this element
	dup all contents  for this element
	dup all comments  for this element
	dup all cdatas    for this element
	dup all elements  in a loop

	*/

	ReloadAllElements();

	size_t z1 = GetElementName(0);
	Z<char> en(z1 + 10);
	GetElementName(en);

	XMLElement* nX = new XMLElement(par,en);

	// Add All Variables
	int y = GetVariableNum();
	for(int i = 0 ; i < y ; i++)
		{
		nX->AddVariable(GetVariables()[i]->Duplicate());
		}

	// Add All Contents
	y = GetContentsNum();
	for(int i = 0 ; i < y ; i++)
		{
		nX->AddContent(GetContents()[i]->Duplicate(),GetContents()[i]->GetEP());
		}

	// Add All Comments
	y = GetCommentsNum();
	for(int i = 0 ; i < y ; i++)
		{
		nX->AddComment(GetComments()[i]->Duplicate(),GetComments()[i]->GetEP());
		}

	// Add All Cdatas
	y = GetCDatasNum();
	for(int i = 0 ; i < y ; i++)
		{
		nX->AddCData(GetCDatas()[i]->Duplicate(),GetCDatas()[i]->GetEP());
		}

	// Recurse to add all child elements
	int c = GetChildrenNum();
	for(int i = 0 ; i < c ; i++)
		{
		nX->AddElement(GetChildren()[i]->Duplicate(nX));
		}

	return nX;
	}

void XML :: SaveOnClose(bool S)
	{
	SOnClose = S;
	}   

int XML :: Save(const char* file,XML_SAVE_MODE SaveMode,XML_TARGET_MODE TargetMode,class XMLTransform* eclass,class XMLTransformData* edata)
	{
	if (TargetMode == 1)
		{
		if (!file)
			return 0;

		// TargetMode == 1, save to memory buffer
		Export((FILE*)file,SaveMode,XML_TARGET_MODE_MEMORY,hdr,eclass,edata);
		return 1;
		}
	if (TargetMode == 2)
		{
		return 0; // We can't save to registry from XML :: Save.
		}



⌨️ 快捷键说明

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