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

📄 xmlprofile.cpp

📁 基于domino系统邮件数据库编写的邮件助手程序
💻 CPP
字号:
// XMLProfile.cpp: implementation of the CXMLProfile class.
// By Emilio Guijarro Cameros
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DominoMinder.h"
#include "XMLProfile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////

//构造函数
CXMLProfile::CXMLProfile( LPCTSTR lpszProfileName, LPCTSTR lpszProfileFilePath, LPCTSTR lpszXslFilePath ) : pXMLDoc(NULL)
{
	if ( (lpszProfileFilePath==NULL) && (GetVersion() < 0x80000000) )
	{
		//默认情况下,取LOCAL_APPDATA路径下的文件ProfileName.xml
		TCHAR szProfilesDir[MAX_PATH];
		SHGetSpecialFolderPath(NULL, szProfilesDir, CSIDL_LOCAL_APPDATA, TRUE);
		sFileName = _bstr_t(szProfilesDir) + _bstr_t("\\") + _bstr_t(lpszProfileName) + _bstr_t(".xml");
	} else {
		//否则取自定义的配置文件路径
		sFileName = _bstr_t( lpszProfileFilePath );
	}
	//生成最基本的XML文件信息(没有找到文档,创建时需要)
	sXMLBase  = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	sXMLBase += (lpszXslFilePath == NULL) ? 
				"" : "<?xml-stylesheet type=\"text/xsl\" href=\"" + _bstr_t(lpszXslFilePath) + "\"?>\n";
	sXMLBase += "<!-- 请不要擅自修改此文档,否则后果自负 -->\n";
	sXMLBase += "<" + _bstr_t( lpszProfileName ) + "/>";

}
CXMLProfile::~CXMLProfile()
{
	pXMLDoc->Release();
}

// CXMLProfile member functions

bool CXMLProfile::deleteProfileEntry( LPCTSTR lpszSection, LPCTSTR lpszEntry )
{
	IXMLDOMNode* nSection;
	IXMLDOMNode* nEntry;
	IXMLDOMNode* nEntryRemoved;
	
	nSection = getSection( lpszSection );
	nEntry = getEntry(lpszSection, lpszEntry, (IXMLDOMElement*)nSection);

	nSection->removeChild( nEntry, &nEntryRemoved );
	nSection->Release();

	return ( nEntryRemoved != NULL );
}

bool CXMLProfile::writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue) 
{	
	IXMLDOMNode* nEntry;
	WCHAR szTemp[255];

	_itow(nValue, szTemp, 10);
	
	nEntry = getEntry(lpszSection, lpszEntry);
	nEntry->put_text(_bstr_t(szTemp));
	nEntry->Release();
	
	return true;
}

bool CXMLProfile::writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszData) 
{
	IXMLDOMNode* nEntry;

	nEntry = getEntry(lpszSection, lpszEntry);
	nEntry->put_text(_bstr_t(lpszData));
	nEntry->Release();
	
	return true;
}

int CXMLProfile::getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault) 
{
	IXMLDOMNode* nEntry;
	BSTR szContent = NULL;

	nEntry = getEntry(lpszSection, lpszEntry);
	nEntry->get_text(&szContent);
	
	if(szContent == NULL || _bstr_t(szContent) == _bstr_t(""))
	{
		IXMLDOMNode *nParent;
		
		nEntry->get_parentNode(&nParent);
		nParent->removeChild(nEntry, NULL);
		
		nEntry->Release();
		nParent->Release();

		return nDefault;
	}	
	
	nEntry->Release();

	return _wtoi(szContent);
}

LPSTR CXMLProfile::getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize)
{
	IXMLDOMNode* nEntry;
	BSTR szContent = NULL;

	nEntry = getEntry(lpszSection, lpszEntry);
	
	nEntry->get_text(&szContent);

	if(szContent == NULL || _bstr_t(szContent) == _bstr_t("")) {
		IXMLDOMNode *nParent;
		
		nEntry->get_parentNode(&nParent);
		nParent->removeChild(nEntry, NULL);
		
		nEntry->Release();
		nParent->Release();

		return (LPSTR)lpszDefault;
	}
	
	nEntry->Release();

	if(_bstr_t(szContent).length() < nBufferSize)
	{
		//xxli@2004.09.07
		CHAR* szAsciiContent = _com_util::ConvertBSTRToString(szContent);
		strcpy(lpBuffer, szAsciiContent);
		delete [] szAsciiContent;

		//CHAR* szAsciiContent = _com_util::ConvertBSTRToString(szContent);
		//strcpy(lpBuffer, szAsciiContent);
	}
	else
		throw CXMLProfileException("Buffer overflow");

	return lpBuffer;
}

IXMLDOMElement * CXMLProfile::getSection(LPCTSTR lpszSection, IXMLDOMElement* in_base)
{
	IXMLDOMElement* base, *element;
	IXMLDOMNode* nSec = NULL;

	if ( in_base == NULL ) {
		pXMLDoc->get_documentElement(&base);
	} else {
		base = in_base;
	}

	//拆离“/”号,进行递归
	char szSection[MAX_PATH];
	strcpy( szSection, lpszSection );
	char* pDiv = strrchr( szSection, '/' );
	if ( pDiv != NULL ) {
		sprintf( pDiv, "\0" );
		base = getSection( szSection, base );
		strcpy( szSection, strrchr(lpszSection,'/')+1 );
	}

	//查找节点
	base->selectSingleNode(_bstr_t(szSection), &nSec);
	/*
	wchar_t* szName;	//if "_bstr_t", then &szName -> szName.GetAddress().
	for(base->get_firstChild(&nSec); nSec != NULL; nSec->get_nextSibling(&nSec)) {
		nSec->get_baseName( &szName );

		if(_bstr_t(szName) == _bstr_t(szSection)) {
			break;
		}
	}
	*/

	//节点不存在则创建
	if(nSec == NULL) {
		pXMLDoc->createElement(_bstr_t(szSection), &element);
		base->appendChild(element, &nSec);
		base->Release();
	}
	return (IXMLDOMElement*) nSec;
}

IXMLDOMElement * CXMLProfile::getEntry(LPCTSTR lpszSection, LPCTSTR lpszEntry, IXMLDOMElement* in_base)
{
	_bstr_t strSection = lpszSection;
	strSection +=  "/";
	strSection +=  lpszEntry;
	return getSection( strSection );

/*
	IXMLDOMElement* base, *element;
	IXMLDOMNode* nSec, *nEntry, *nResult;
	wchar_t* szName;	//if "_bstr_t", then &szName -> szName.GetAddress().

	pXMLDoc->get_documentElement(&base);

	for(base->get_firstChild(&nSec); nSec != NULL; nSec->get_nextSibling(&nSec)) {
		nSec->get_baseName( &szName );

		if(_bstr_t(szName) == _bstr_t(lpszSection)) {
			for(nSec->get_firstChild(&nEntry); nEntry != NULL; nEntry->get_nextSibling(&nEntry)) {
				nEntry->get_baseName( &szName );

				if(_bstr_t(szName) == _bstr_t(lpszEntry)) {
					nResult = nEntry;
					break;
				}
			}

			if(nEntry == NULL) {
				pXMLDoc->createElement(_bstr_t(lpszEntry), &element);
				nSec->appendChild(element, &nEntry);
				element->Release();
				nResult = nEntry;
			}
			nSec->Release();
			break;
		}
	}

	if(nSec == NULL) {
		pXMLDoc->createElement(_bstr_t(lpszSection), &element);
		base->appendChild(element, &nSec);
		element->Release();
		pXMLDoc->createElement(_bstr_t(lpszEntry), &element);
		nSec->appendChild(element, &nEntry);
		element->Release();
		nResult = nEntry;
	}
	
	base->Release();

	return nResult;
*/
}

bool CXMLProfile::saveProfile()
{
	if(pXMLDoc != NULL && SUCCEEDED(pXMLDoc->save(_variant_t(sFileName))))
		return true;
	else
		return false;
}

bool CXMLProfile::loadProfile()
{
	if(pXMLDoc != NULL)
		return false;

	VARIANT_BOOL bResult;
	if(SUCCEEDED(CoInitialize(NULL)) && SUCCEEDED(CoCreateInstance(CLSID_DOMDocument, 
		NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXMLDoc)))
	{
		if(FAILED(pXMLDoc->load(_variant_t(sFileName), &bResult)))
			return false;
	}	

	IXMLDOMElement* base;
	pXMLDoc->get_documentElement(&base);

	if ( base == NULL ) {
		//若装载XMLProfile文档失败,则新建
		if ( FAILED(pXMLDoc->loadXML( (wchar_t*)sXMLBase, &bResult )) ) {
			return false;
		}
	}

	return true;
}

⌨️ 快捷键说明

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