1 Star 1 Fork 0

reesoft/common-cpp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
IniFile.cpp 4.17 KB
一键复制 编辑 原始数据 按行查看 历史
reesoft 提交于 2020-10-11 10:01 +08:00 . V1.0
// Copyright (c) 2020 Lv Decai, Shenzhen, China.
// Filename: IniFile.cpp
// Version: 1.0
// Release date: 2020-10-11
// License: MIT (http://opensource.org/licenses/MIT)
#include "StdAfx.h"
#include "ConvertUtility.h"
#include "IniFile.h"
#include "StringUtility.h"
NAMESPACE_BEGIN;
// 构造函数
CIniFile::CIniFile(PCTSTR sIniFilePath)
: m_sIniFilePath(sIniFilePath)
{
}
// 重新设置 INI 配置文件路径
void CIniFile::SetFilePath(PCTSTR sIniFilePath)
{
m_sIniFilePath = sIniFilePath;
}
// 读取区段列表
void CIniFile::GetSectionList(list<CString>& lstSection)
{
DWORD iBufferSize = 256;
BOOL bContinue = TRUE;
while (bContinue)
{
TCHAR* sBuffer = new TCHAR[iBufferSize];
memset(sBuffer, 0, sizeof(TCHAR) * iBufferSize);
DWORD iReadSize = GetPrivateProfileString(NULL, NULL, NULL, sBuffer, iBufferSize, m_sIniFilePath);
if (iReadSize == iBufferSize - 1
|| iReadSize == iBufferSize - 2)
{
iBufferSize *= 4;
}
else
{
TCHAR* sCategory = sBuffer;
while (*sCategory != _T('\0'))
{
lstSection.push_back(sCategory);
sCategory += lstSection.back().GetLength() + 1;
}
bContinue = FALSE;
}
delete[] sBuffer;
}
}
// 读取指定区段中的键列表
void CIniFile::GetKeyList(PCTSTR sCategory, list<CString>& lstKey)
{
DWORD iBufferSize = 256;
BOOL bContinue = TRUE;
while (bContinue)
{
TCHAR* sBuffer = new TCHAR[iBufferSize];
memset(sBuffer, 0, sizeof(TCHAR) * iBufferSize);
DWORD iReadSize = GetPrivateProfileString(sCategory, NULL, NULL, sBuffer, iBufferSize, m_sIniFilePath);
if (iReadSize == iBufferSize - 1
|| iReadSize == iBufferSize - 2)
{
iBufferSize *= 4;
}
else
{
TCHAR* sKey = sBuffer;
while (*sKey != _T('\0'))
{
lstKey.push_back(sKey);
sKey += lstKey.back().GetLength() + 1;
}
bContinue = FALSE;
}
delete[] sBuffer;
}
}
// 获取整数类型的选项值
// 返回值:如果选项值存在则返回选项值,否则返回缺省值
int CIniFile::GetInt(PCTSTR sCategory, PCTSTR sKey, int iDefaultValue)
{
return GetPrivateProfileInt(sCategory, sKey, iDefaultValue, m_sIniFilePath);
}
// 获取浮点数类型的选项值
// 返回值:如果选项值存在则返回选项值,否则返回缺省值
double CIniFile::GetFloat(PCTSTR sCategory, PCTSTR sKey, double fDefaultValue)
{
CString sValue = GetString(sCategory, sKey);
if (sValue.IsEmpty())
{
return fDefaultValue;
}
else
{
return _ttof(sValue);
}
}
// 获取字符串类型的选项值
// 返回值:如果选项值存在则返回选项值,否则返回缺省值
CString CIniFile::GetString(PCTSTR sCategory, PCTSTR sKey, PCTSTR sDefaultValue)
{
CString sResult;
DWORD iBufferSize = 256;
BOOL bContinue = TRUE;
while (bContinue)
{
TCHAR* sBuffer = new TCHAR[iBufferSize];
memset(sBuffer, 0, sizeof(TCHAR) * iBufferSize);
DWORD iReadSize = GetPrivateProfileString(sCategory, sKey, sDefaultValue, sBuffer, iBufferSize, m_sIniFilePath);
if (iReadSize == iBufferSize - 1
|| iReadSize == iBufferSize - 2)
{
iBufferSize *= 4;
}
else
{
sResult = sBuffer;
bContinue = FALSE;
}
delete[] sBuffer;
}
return sResult;
}
// 获取字符串列表类型的选项值
// 返回值:选项值参数的引用
list<CString>& CIniFile::GetList(PCTSTR sCategory, PCTSTR sKey, list<CString>& lstOption, PCTSTR sElementDivision)
{
CString sValueList = GetString(sCategory, sKey);
StringUtility::Split(sValueList, lstOption, sElementDivision);
return lstOption;
}
// 设置整数类型的选项值
// 返回值:设置是否成功
BOOL CIniFile::SetInt(PCTSTR sCategory, PCTSTR sKey, int iValue)
{
return SetString(sCategory, sKey, ConvertUtility::ToString(iValue));
}
// 设置浮点数类型的选项值
// 返回值:设置是否成功
BOOL CIniFile::SetFloat(PCTSTR sCategory, PCTSTR sKey, double fValue)
{
return SetString(sCategory, sKey, ConvertUtility::ToString(fValue));
}
// 设置字符串列表类型的选项值
// 返回值:设置是否成功
BOOL CIniFile::SetString(PCTSTR sCategory, PCTSTR sKey, PCTSTR sValue)
{
return WritePrivateProfileString(sCategory, sKey, sValue, m_sIniFilePath);
}
// 设置字符串列表类型的选项值
// 返回值:设置是否成功
BOOL CIniFile::SetList(PCTSTR sCategory, PCTSTR sKey, const list<CString>& lstValue, PCTSTR sElementDivision)
{
CString sValueList;
for (auto& sValue : lstValue)
{
sValueList += sValue + sElementDivision;
}
return SetString(sCategory, sKey, sValueList);
}
NAMESPACE_END;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/reesoft/common-cpp.git
git@gitee.com:reesoft/common-cpp.git
reesoft
common-cpp
common-cpp
master

搜索帮助