C++[类设计] ini配置文件读写类config

//in Config.h
#pragma once
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib,"shlwapi")
#include <Tchar.h>
class CConfig
{
public:
	CConfig(LPTSTR strFileName=NULL,LPTSTR strFilePath=NULL);
	virtual ~CConfig(void);
private:
	TCHAR m_szFileName[MAX_PATH];
	TCHAR m_szFilePath[MAX_PATH];
	TCHAR m_szAppName[MAX_PATH];
	const TCHAR *m_pszFileExt;
	const TCHAR *m_pszFileDir;
public:
	bool AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
	bool DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
	bool DeleteSection(LPCTSTR strDelSectionName=NULL,LPCTSTR strFilePath=NULL);
	bool ReadKeyValue(LPCTSTR strKeyName,OUT LPTSTR strKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
	bool ReadKeyValue(LPCTSTR strKeyName, OUT int &nKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
	bool ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
	int GetSectionCount(LPCTSTR strFilePath=NULL);
	int GetKeyCount(LPCTSTR strSectionName=NULL, LPCTSTR strFilePath=NULL);
};
//in Config.cpp
#include "Config.h"
/************************************************************************************************************/
/*																											*/
/* Function name : CConfig																				*/		
/* Description   : Create and initialize a CConfig Object.The parameter strFileName indicated the name 
					of the ini file,it must not contain extension .ini.And strFilePath indicated the path
					of the ini file which to be	created and stored.If strFileName or strFilePath is NULL,then 
					use the app's name or current directory as default ini file name or path.If application
					calls this member function to create it's ini file at default path,then the ini file will
					be stored unifily in the subdirectory "...\config\".
					Attention:This function	does not create an actual ini file.																		*/
/*																											*/
/************************************************************************************************************/
CConfig::CConfig(LPTSTR strFileName,LPTSTR strFilePath)
: m_pszFileExt(_T(".ini")),m_pszFileDir(_T("\\config"))
{
	memset(m_szFileName,0,MAX_PATH);
	memset(m_szFilePath,0,MAX_PATH);
	memset(m_szAppName,0,MAX_PATH);

	::GetModuleFileName(NULL,m_szFilePath,MAX_PATH);
	::GetFileTitle(m_szFilePath,m_szFileName,MAX_PATH);
	::PathRemoveExtension(m_szFileName);
	_tcscpy_s(m_szAppName,MAX_PATH,m_szFileName);
	if( strFilePath!=NULL)
	{
		/*if strFilePath is valid,copy it to m_szFilePath and handle it to a directory*/
		if(::PathIsDirectory(strFilePath))
		{
			_tcscpy_s(m_szFilePath,MAX_PATH,strFilePath);
			::PathRemoveBackslash(m_szFilePath);
			::PathAddBackslash(m_szFilePath);
		}
		else//use a default directory
		{
			::PathRemoveFileSpec(m_szFilePath);
			::PathAddBackslash(m_szFilePath);
		}
	}
	else
	{
		::PathRemoveFileSpec(m_szFilePath);
		_tcscat_s(m_szFilePath,MAX_PATH,m_pszFileDir);
		if(!::PathFileExists(m_szFilePath))
		{
			::CreateDirectory(m_szFilePath,NULL);
		}
		::PathAddBackslash(m_szFilePath);

		if(strFileName !=NULL)
		{
			_tcscpy_s(m_szFileName,MAX_PATH,strFileName);
		}
	}
	_tcscat_s(m_szFileName,MAX_PATH,m_pszFileExt);
	_tcscat_s(m_szFilePath,MAX_PATH,m_szFileName);
}

CConfig::~CConfig(void)
{
}
/************************************************************************************************************/
/*																											*/
/* Function name : AddKey																				*/		
/* Description   : Create a key-value pair with format "strKeyName=strKeyValue" in the specified section by
					strSectionName.If strSectionName is NULL,then use the app's name as default section 
					name to be added into. If the section specified by strSectionName does not exist, it is 
					created. The strKeyValue will be modified if strKeyName already exists.This function 
					creates an actual ini file.
					Return true if the function succeed,otherwise false.
																											*/
/*																											*/
/************************************************************************************************************/
bool CConfig::AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
		return true;
	else
		return false;
}
/************************************************************************************************************/
/*																											*/
/* Function name : DeleteKey																				*/		
/* Description   : Delete a key and it's value from the specified section.If the parameter strSectionName is
					NULL,then delete the section with app's name.
					Return true if the function succeed,otherwise false.									 									*/
/*																											*/
/************************************************************************************************************/
bool CConfig::DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	if(::WritePrivateProfileString(szSectionName,strDelKeyName,NULL,szFilePath))
		return true;
	else
		return false;
}
/************************************************************************************************************/
/*																											*/
/* Function name : DeleteSection																			*/		
/* Description   : Delete a specified section and all it's associated contents from the initialization file. 
					If the parameter strDelSectionName is no offered,then delete the section with app's name.
					Return true if the function succeed,otherwise false.									 
/*																											*/
/************************************************************************************************************/
bool CConfig::DeleteSection(LPCTSTR strDelSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strDelSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strDelSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	if(::WritePrivateProfileString(szSectionName,NULL,NULL,szFilePath))
		return true;
	else
		return false;
}

/************************************************************************************************************/
/*																											*/
/* Function name : ReadKeyValue																				*/		
/* Description   : Retrieves the value of strKeyName as String into the buffer specified by parameter
					strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the 
					app's name as default section and ini file to be search.Return true if the function 
					succeed,otherwise false,and the parameter strKeyVal will be set to NULL. 
/*																											*/
/************************************************************************************************************/
bool CConfig::ReadKeyValue(LPCTSTR strKeyName, LPTSTR strKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	::GetPrivateProfileString(szSectionName,strKeyName,NULL,strKeyVal,_tcslen(strKeyVal),szFilePath);
	if(_tcscmp(strKeyVal,_T(""))==0)
		return false;
	else
		return true;
}
/************************************************************************************************************/
/*																											*/
/* Function name : ReadKeyValue																				*/		
/* Description   : Retrieves the value of strKeyName as Int into the buffer specified by parameter
					strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the 
					app's name as default section and ini file to be search.Return true if the function 
					succeed,otherwise false,and the parameter strKeyVal will be set to -1.*/
/*																											*/
/************************************************************************************************************/
bool CConfig::ReadKeyValue(LPCTSTR strKeyName, int &nKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	nKeyVal=::GetPrivateProfileInt(szSectionName,strKeyName,-1,szFilePath);
	if(-1 !=nKeyVal)
		return true;
	else
		return false;
}
/************************************************************************************************************/
/*																											*/
/* Function name : ModifyKeyValue																			*/		
/* Description   : Replace the key value of strKeyName with strKeyValue .If the parameter strSectionName 
					and strFilePath is no offered,then use the app's name as default section and ini file to
					be search.Return true if the function succeed,otherwise false.							*/
/*																											*/
/************************************************************************************************************/
bool CConfig::ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
{
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	::WritePrivateProfileString(szSectionName,strKeyName,NULL,szFilePath);
	if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
		return true;
	else
		return false;
}
/************************************************************************************************************/
/*																											*/
/* Function name : GetSectionCount																			*/		
/* Description   : Retrieves the number of all sections in the initialization file specified by strFilePath.
					if strFilePath is NULL,then use the app's name as default ini file to be search.If the 
					function succeed,the return value is not -1.											*/
/*																											*/
/************************************************************************************************************/
int CConfig::GetSectionCount(LPCTSTR strFilePath)
{
	TCHAR szItem[MAX_PATH]={0};
	LPCTSTR szFilePath;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	int nRet=::GetPrivateProfileSectionNames(szItem,MAX_PATH,szFilePath);
	int nSecCount=0;
	if(nRet !=MAX_PATH-2)
	{
		for(int i=0;i<MAX_PATH;i++)
		{
			if(szItem[i]==0 && szItem[i+1]!=0)
			{
				nSecCount++;
			}
			else if(szItem[i]==0 && szItem[i+1]==0)
			{
				nSecCount++;
				break;
			}
		}
	}
	else
		nSecCount=-1;
	return nSecCount;
}
/************************************************************************************************************/
/*																											*/
/* Function name : GetKeyCount																			*/		
/* Description   : Retrieves the number of all key in the section specified by strSectionName in the 
					initialization file specified by strFilePath.If strSectionName and strFilePath is NULL,
					then use the app's name as default section and ini file to be search.If the 
					function succeed,the return value is not -1.											*/
/*																											*/
/************************************************************************************************************/
int CConfig::GetKeyCount(LPCTSTR strSectionName, LPCTSTR strFilePath)
{
	TCHAR szItem[MAX_PATH]={0};
	LPCTSTR szSectionName;
	LPCTSTR szFilePath;
	if(strSectionName==NULL)
		szSectionName=m_szAppName;
	else
		szSectionName=strSectionName;
	if(strFilePath==NULL)
		szFilePath=m_szFilePath;
	else
		szFilePath=strFilePath;
	int nRet=::GetPrivateProfileSection(szSectionName,szItem,MAX_PATH,szFilePath);
	int nSecCount=0;
	if(nRet !=MAX_PATH-2)
	{
		for(int i=0;i<MAX_PATH;i++)
		{
			if(szItem[i]==0 && szItem[i+1]!=0)
			{
				nSecCount++;
			}
			else if(szItem[i]==0 && szItem[i+1]==0)
			{
				nSecCount++;
				break;
			}
		}
	}
	else
		nSecCount=-1;
	return nSecCount;
}
// in main function
#include <iostream>
#include "Config.h"
int main()
{
	CConfig MyConfig;
	
	MyConfig.AddKey(_T("ID"),_T("123456"));
	MyConfig.AddKey(_T("账户"),_T("123456"),_T("MySection"));
	MyConfig.AddKey(_T("余额"),_T("654321"),_T("MySection"));
	//MyConfig.DeleteKey(_T("ID"));
	//MyConfig.DeleteSection();
	LPCTSTR key=_T("ID");
	LPCTSTR key1=_T("账户");
	LPCTSTR key2=_T("余额");
	TCHAR szBuf[MAX_PATH]={0};
	LPTSTR pstrValue=szBuf;
	int nValue=0;
	MyConfig.ReadKeyValue(key,nValue);
	std::cout << "ID=" << nValue << std::endl;
	MyConfig.ReadKeyValue(key1,nValue,_T("MySection"));
	std::cout << "账户=" << nValue << std::endl;
	MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
	std::cout << "余额=" << nValue << std::endl;
	MyConfig.ModifyKeyValue(_T("余额"),_T("923475632"),_T("MySection"));
	MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
	std::cout << "余额=" << nValue << std::endl;
	
	std::cout << MyConfig.GetKeyCount(_T("MySection")) << std::endl;

	/*CConfig MyConfig2;
	MyConfig2.AddKey(_T("新增记录"),_T("4571498"));
	MyConfig2.AddKey(_T("新增记录"),_T("0775-4571498"));*/
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值