Files
envi-code/SourceCode/Code2026/CoreCommand/IniTools.cpp
T
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

110 lines
2.5 KiB
C++

#include "StdAfx.h"
#include "initools.h"
#include "aecdbobjxdatamap.h"
#include "earthwormdef.h"
#define SP_PT TCHAR(0x07) // 响铃 bel 做为分割符
#pragma region IniTools
bool IniTools::getSections(LPCTSTR szPath, std::vector<CString> &sections)
{
if (_taccess(szPath, 0x0) != 0x0) return false;
DWORD dw(0), len(MAX_PATH);
TCHAR *buf = new TCHAR[len];
::memset(buf, 0, sizeof(TCHAR) * len);
dw = ::GetPrivateProfileSectionNames(buf, len, szPath);
while (dw == len - 2)
{
DELETE_ARR_(buf);
len += MAX_PATH;
buf = new TCHAR[len];
::memset(buf, 0, sizeof(TCHAR) * len);
dw = ::GetPrivateProfileSectionNames(buf, len, szPath);
}
TCHAR *psz(buf);
while ((len = (DWORD)_tcslen(psz)))
{
sections.push_back(psz);
psz += len + 1;
}
DELETE_ARR_(buf);
return !sections.empty();
}
bool IniTools::getSectionContent(LPCTSTR szPath, LPCTSTR szSection, std::map<CString, CString> &mapV)
{
SH_BIM_BLOCK bims;
getSectionContent(szPath, szSection, bims);
for (size_t i = 0; i < bims.size(); ++i)
{
mapV.insert(bims[i]);
}
return !mapV.empty();
}
bool IniTools::getSectionContent(LPCTSTR szPath, LPCTSTR szSection, SH_BIM_BLOCK &mapV)
{
if (_taccess(szPath, 0x0) != 0x0) return false;
DWORD dw(0), len(MAX_PATH);
TCHAR *buf = new TCHAR[len];
::memset(buf, 0, sizeof(TCHAR) * len);
dw = ::GetPrivateProfileSection(szSection, buf, len, szPath);
while (dw == len - 2)
{
DELETE_ARR_(buf);
len += MAX_PATH;
buf = new TCHAR[len];
::memset(buf, 0, sizeof(TCHAR) * len);
dw = ::GetPrivateProfileSection(szSection, buf, len, szPath);
}
TCHAR *psz(buf);
mapV.clear();
std::map<CString, CString>::const_iterator it;
int idx(-1);
CString str(_T("")), pre(_T("")), back(_T(""));
while ((len = (DWORD)_tcslen(psz)))
{
str = psz;
idx = str.Find(_T('='));
if (idx != -1)
{
pre = str.Left(idx);
back = str.Right(str.GetLength() - idx - 1);
mapV.push_back(std::make_pair(pre, back));
}
psz += len + 1;
}
DELETE_ARR_(buf);
return !mapV.empty();
}
bool IniTools::setSectionContent(LPCTSTR szPath, LPCTSTR szSection, const std::map<CString, CString> &mapV)
{
if (!szSection) return false;
//if (_taccess(szPath, 0x0) != 0x0) return false;
// 拼字符串
std::map<CString, CString>::const_iterator it = mapV.begin();
std::wstring ss;
for (; it != mapV.end(); ++it)
{
ss += it->first;
ss += _T("=");
ss += it->second;
ss.push_back(_T('\0'));
}
ss.push_back(_T('\0'));
return ::WritePrivateProfileSection(szSection, ss.c_str(), szPath) == FALSE ? false : true;
}
#pragma endregion