#pragma once
#include <string>
#include <sstream>
typedef std::basic_string<TCHAR> tstring;
class CIniCfg
{
public:
CIniCfg()
{
TCHAR szTemp[MAX_PATH];
GetModuleFileName(NULL, szTemp, sizeof(szTemp));
LPTSTR p = _tcsrchr(szTemp, _T('.'));
if (p != NULL)
_tcscpy(p, _T(".ini"));
else
_tcscat(szTemp, _T(".ini"));
m_strFilePath = szTemp;
}
void SetName(LPCTSTR szName)
{
TCHAR szTemp[MAX_PATH];
GetModuleFileName(NULL, szTemp, sizeof(szTemp));
LPTSTR p = _tcsrchr(szTemp, _T('\\'));
if (p != NULL)
{
_tcscpy(++p, szName);
}
m_strFilePath = szTemp;
}
void SetPath(LPCTSTR szPath)
{
m_strFilePath = szPath;
}
BOOL SetInt(LPCTSTR lpAppName, // pointer to section name
LPCTSTR lpKeyName, // pointer to key name
int nValue)
{
std::basic_stringstream<TCHAR> ss;
ss << nValue;
return WritePrivateProfileString(
lpAppName,
lpKeyName,
ss.str().c_str(), //strValue,
m_strFilePath.c_str());
}
BOOL SetString(LPCTSTR lpAppName, // pointer to section name
LPCTSTR lpKeyName, // pointer to key name
LPCTSTR lpString) // pointer to string to add
{
return WritePrivateProfileString(
lpAppName,
lpKeyName,
lpString,
m_strFilePath.c_str());
}
int GetInt(LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
int nDefault) // return value if key name is not found
{
return GetPrivateProfileInt(
lpAppName,
lpKeyName,
nDefault,
m_strFilePath.c_str());
}
tstring GetString(LPCTSTR lpAppName, // points to section name
LPCTSTR lpKeyName, // points to key name
LPCTSTR lpDefault) // points to default string
{
TCHAR szRet[MAX_PATH] = {};
DWORD dwSize = MAX_PATH;
GetPrivateProfileString(
lpAppName, // points to section name
lpKeyName, // points to key name
lpDefault, // points to default string
szRet, // points to destination buffer
dwSize, // size of destination buffer
m_strFilePath.c_str());
return szRet;
}
protected:
tstring m_strFilePath;
};