#ifndef __INIPARSER__H
#define __INIPARSER__H
#include <Windows.h>
#include <string>
#include <algorithm>
#include <memory>
#include <fstream>
class CIniParser
{
private:
std::wstring m_wstrPath;
public:
CIniParser() { }
CIniParser(std::wstring wstrPath) { SetPath(wstrPath); }
~CIniParser() { }
bool IsOpen() const
{
std::wifstream ini(m_wstrPath);
return ini.good();
}
void SetPath(std::wstring wstrPath)
{
if (wstrPath.empty())
return;
std::replace(wstrPath.begin(), wstrPath.end(), L'/', L'\\');
m_wstrPath = wstrPath;
}
std::wstring ReadString(std::wstring wstrApp, std::wstring wstrKey) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return L"";
WCHAR wcsBuffer[512] = L"";
if (GetPrivateProfileStringW(wstrApp.data(), wstrKey.data(), L"", wcsBuffer, sizeof(wcsBuffer), m_wstrPath.data()) == NULL)
return L"";
return std::wstring(wcsBuffer);
}
bool WriteString(std::wstring wstrApp, std::wstring wstrKey, std::wstring wstrValue) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return false;
return (WritePrivateProfileStringW(wstrApp.data(), wstrKey.data(), wstrValue.data(), m_wstrPath.data()) != NULL);
}
int ReadInt(std::wstring wstrApp, std::wstring wstrKey) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return 0;
return std::stoi(ReadString(wstrApp, wstrKey));
}
bool WriteInt(std::wstring wstrApp, std::wstring wstrKey, int iValue) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return false;
return WriteString(wstrApp, wstrKey, std::to_wstring(iValue));
}
float ReadFloat(std::wstring wstrApp, std::wstring wstrKey) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return 0.0f;
return std::stof(ReadString(wstrApp, wstrKey));
}
bool WriteFloat(std::wstring wstrApp, std::wstring wstrKey, float flValue) const
{
if (m_wstrPath.empty() || wstrApp.empty() || wstrKey.empty())
return false;
return WriteString(wstrApp, wstrKey, std::to_wstring(flValue));
}
};
#endif