1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #pragma once
#ifndef IniHelper_H #define IniHelper_H
#include <Windows.h> #include <string> #include <atlstr.h> #include <io.h>
class IniHelper { private: static LPCTSTR CharToLPCTSTR(const char* value); static std::string GetStringA(const char* key, std::string def_val = ""); static void WriteStringA(const char* key, std::string value);
public: static void SetFileName(const char* fileName); static void SetSection(const char* section);
static int GetInt(const char* key, int def_val = 0); static void WriteInt(const char* key, int value);
static bool GetBool(const char* key, bool def_val = false); static void WriteBool(const char* key, bool value);
static double GetDouble(const char* key, double def_val = 0.0); static void WriteDouble(const char* key, double value);
static CString GetString(const char* key, std::string def_val = ""); static void WriteString(const char* key, CString value); };
CHAR FileName[MAXBYTE]; CHAR Section[MAXBYTE];
inline LPCTSTR IniHelper::CharToLPCTSTR(const char* value) { int num = MultiByteToWideChar(0, 0, value, -1, NULL, 0); wchar_t* wide = new wchar_t[num]; MultiByteToWideChar(0, 0, value, -1, wide, num); return wide; } inline std::string IniHelper::GetStringA(const char* key, std::string def_val) { CHAR cTmp[MAXBYTE]; memset(cTmp, 0, sizeof(cTmp)); GetPrivateProfileStringA(Section, key, def_val.c_str(), cTmp, MAXBYTE, FileName); return cTmp; } inline void IniHelper::WriteStringA(const char* key, std::string value) { WritePrivateProfileStringA(Section, key, value.c_str(), FileName); }
inline void IniHelper::SetFileName(const char* fileName) { memset(FileName, 0, sizeof(FileName)); memcpy(FileName, fileName, strlen(fileName) + 1); } inline void IniHelper::SetSection(const char* section) { memset(Section, 0, sizeof(Section)); memcpy(Section, section, strlen(section) + 1); }
inline int IniHelper::GetInt(const char* key, int def_val) { return GetPrivateProfileInt(CharToLPCTSTR(Section), CharToLPCTSTR(key), def_val, CharToLPCTSTR(FileName)); } inline void IniHelper::WriteInt(const char* key, int value) { WriteStringA(key, std::to_string(value).c_str()); }
inline bool IniHelper::GetBool(const char* key, bool def_val) { return GetStringA(key, def_val ? "True" : "False") == "True"; } inline void IniHelper::WriteBool(const char* key, bool value) { WriteStringA(key, value ? "True" : "False"); }
inline double IniHelper::GetDouble(const char* key, double def_val) { return atof(GetStringA(key, std::to_string(def_val).c_str()).c_str()); } inline void IniHelper::WriteDouble(const char* key, double value) { WriteStringA(key, std::to_string(value).c_str()); }
inline CString IniHelper::GetString(const char* key, std::string def_val) { TCHAR cTmp[MAXBYTE]; memset(cTmp, 0, sizeof(cTmp)); GetPrivateProfileString(CharToLPCTSTR(Section), CharToLPCTSTR(key), CharToLPCTSTR(def_val.c_str()), cTmp, MAXBYTE, CharToLPCTSTR(FileName)); return cTmp; } inline void IniHelper::WriteString(const char* key, CString value) { if (_access(FileName, 0)) { FILE* pFile(NULL); if (_wfopen_s(&pFile, CharToLPCTSTR(FileName), L"wt, ccs=UNICODE") == 0) fclose(pFile); }
WritePrivateProfileString(CharToLPCTSTR(Section), CharToLPCTSTR(key), value, CharToLPCTSTR(FileName)); }
#endif
|