📄 cinifile.cpp
字号:
#include "stdafx.h"
#include "CIniFile.h"
#define MAX_SECTION 512 //Section最大长度
#define MAX_KEY 512 //KeyValues最大长度
#define MAX_ALLSECTIONS 65535 //所有Section的最大长度
#define MAX_ALLKEYS 65535 //所有KeyValue的最大长度
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#pragma warning(disable:4786)
CIniFile::CIniFile()
{
}
CIniFile::~CIniFile()
{
}
//////////////////////////////////////////////////////////////////////
// Public Functions
//////////////////////////////////////////////////////////////////////
bool CIniFile::SetPath(string strPath)
{
m_strPath = strPath;
// 检查文件是否存在
DWORD dwFlag = GetFileAttributes((LPCTSTR)m_strPath.c_str());
// 文件或者路径不存在,返回FALSE
if( 0xFFFFFFFF == dwFlag )
return false;
// 路径是目录,返回FALSE
if (FILE_ATTRIBUTE_DIRECTORY & dwFlag)
return false;
return true;
}
// 判断section是否存在
bool CIniFile::SectionExist(string strSection)
{
TCHAR chSection[MAX_SECTION];
DWORD dwRetValue;
dwRetValue = GetPrivateProfileString((LPCTSTR)strSection.c_str(),NULL,_T(""),chSection,
sizeof(chSection)/sizeof(TCHAR),(LPCTSTR)m_strPath.c_str());
return (dwRetValue>0);
}
// 根据key,获取value
string CIniFile::GetKeyValue(string strSection, string strKey)
{
TCHAR chKey[MAX_KEY];
DWORD dwRetValue;
string strKeyValue=_T("");
dwRetValue = GetPrivateProfileString((LPCTSTR)strSection.c_str(),(LPCTSTR)strKey.c_str(),
_T(""),chKey,sizeof(chKey)/sizeof(TCHAR),(LPCTSTR)m_strPath.c_str());
strKeyValue = chKey;
return strKeyValue;
}
void CIniFile::SetKeyValue(string strSection,string strKey,string strKeyValue)
{
WritePrivateProfileString((LPCTSTR)strSection.c_str(), (LPCTSTR)strKey.c_str(),
(LPCTSTR)strKeyValue.c_str(), (LPCTSTR)m_strPath.c_str());
}
void CIniFile::DeleteKey(string strSection, string strKey)
{
WritePrivateProfileString((LPCTSTR)strSection.c_str(), (LPCTSTR)strKey.c_str(), NULL, // 这里写NULL,则删除Key
(LPCTSTR)m_strPath.c_str());
}
void CIniFile::DeleteSection(string strSection)
{
WritePrivateProfileString((LPCTSTR)strSection.c_str(),NULL,NULL, // 这里都写NULL,则删除Section
(LPCTSTR)m_strPath.c_str());
}
int CIniFile::GetAllSections(stringArray& strArrSection)
{
int dwRetValue, i, j, iPos=0;
TCHAR chAllSections[MAX_ALLSECTIONS];
TCHAR chTempSection[MAX_SECTION];
string section_name="";
ZeroMemory(chAllSections, MAX_ALLSECTIONS);
ZeroMemory(chTempSection, MAX_SECTION);
dwRetValue = GetPrivateProfileSectionNames(chAllSections,MAX_ALLSECTIONS,m_strPath.c_str());
// 因为Section在数组中的存放形式为 "Section1",0,"Section2",0,0
// 所以如果检测到连续两个0,则break
for(i=0; i<MAX_ALLSECTIONS; i++)
{
if( chAllSections[i] == NULL )
{
if( NULL == chAllSections[i+1] )
break;
}
}
i++; // 保证数据读完
strArrSection.clear(); // 清空数组
for(j=0; j<i; j++)
{
section_name += chAllSections[j];
if( chAllSections[j] == NULL )
{
strArrSection.insert(strArrSection.end(),section_name);
section_name="";
}
}
return strArrSection.size();
}
int CIniFile::GetAllKeysAndValues(string strSection, stringArray& strArrKey, stringArray& strArrKeyValue)
{
int dwRetValue, i, j, iPos=0;
TCHAR chAllKeysAndValues[MAX_ALLKEYS];
TCHAR chTempkeyAndValue[MAX_KEY];
string strTempKey="";
ZeroMemory(chAllKeysAndValues, MAX_ALLKEYS);
ZeroMemory(chTempkeyAndValue, MAX_KEY);
char* p_strsec = strdup(strSection.c_str());
dwRetValue = GetPrivateProfileSection(p_strsec,chAllKeysAndValues,MAX_ALLKEYS,
m_strPath.c_str());
free(p_strsec);
// 因为Section在数组中的存放形式为 "Key1=KeyValue1",0,"Key2=KeyValue2",0,0
// 所以如果检测到连续两个0,则break
for(i=0; i<MAX_ALLSECTIONS; i++)
{
if( chAllKeysAndValues[i] == NULL )
{
if( NULL == chAllKeysAndValues[i+1] )
break;
}
}
i++;
strArrKey.clear();
strArrKeyValue.clear();
for(j=0; j<i; j++)
{
strTempKey += chAllKeysAndValues[j];
if( chAllKeysAndValues[j] == NULL )
{
if ((strTempKey.at(0) !='#')&&(strcmp(strTempKey.c_str(),"")))
{
strArrKey.insert(strArrKey.end(),strTempKey.substr(0,strTempKey.find("=",0)) );
strArrKeyValue.insert(strArrKeyValue.end(),strTempKey.substr(strTempKey.find("=",0)+1));
}
strTempKey ="";
}
}
return strArrKey.size();
}
void CIniFile::DeleteAllSections()
{
int nSecNum;
stringArray strArrSection;
nSecNum = GetAllSections(strArrSection);
for(int i=0; i<nSecNum; i++)
{
char* p_strsec = strdup(strArrSection[i].c_str());
char* p_strpath = strdup(m_strPath.c_str());
WritePrivateProfileString((LPCTSTR)p_strsec,NULL,NULL,(LPCTSTR)p_strpath );
free(p_strsec);
free(p_strpath);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -