随手写的 IniFiles

IniFiles.h

  1 #pragma once
  2 #include <windows.h>
  3 #include <string>
  4 #include <map>
  5 
  6 class IniFiles
  7 {
  8 public:
  9     IniFiles()
 10     {
 11 
 12     }
 13 
 14     IniFiles(std::string file)
 15     {
 16         m_file = file;
 17     }
 18 
 19     virtual ~IniFiles()
 20     {
 21 
 22     }
 23 
 24     void set_file(std::string file)
 25     {
 26         m_file = file;
 27     }
 28 
 29     void set_string(std::string section, std::string key, std::string value)
 30     {
 31         WritePrivateProfileStringA(section.c_str(), key.c_str(), value.c_str(), m_file.c_str());
 32 
 33     }
 34 
 35 
 36     void set_number(std::string section, std::string key, UINT value)
 37     {
 38         set_string(section, key, std::to_string(value));
 39     }
 40 
 41     std::string get_string(std::string section, std::string key)
 42     {
 43         char buf[4096] = { 0 };
 44         DWORD  dwRet = GetPrivateProfileStringA(section.c_str(), key.c_str(), "", buf, 4096, m_file.c_str());
 45         return buf;
 46     }
 47 
 48     UINT get_number(std::string section, std::string key)
 49     {
 50         return GetPrivateProfileIntA(section.c_str(), key.c_str(), 0, m_file.c_str());
 51 
 52     }
 53 
 54     void get_sections(std::string section, std::map<std::string, std::string>& map_list)
 55     {
 56         char szDestBuff[65535] = { 0 };
 57         DWORD dwRet = GetPrivateProfileSectionA(section.c_str(), szDestBuff, sizeof(szDestBuff), m_file.c_str());
 58 
 59         int i = 0, j = 0, pos;
 60         bool endOfBuffer = FALSE;
 61         char szTmpBuff[4096] = { '\0' };
 62         std::string tmpStr;
 63         std::string key;
 64         std::string value;
 65         while (!endOfBuffer) {
 66 
 67             if (szDestBuff[i] == '\0') {
 68                 szTmpBuff[j] = szDestBuff[i];
 69                 tmpStr = szTmpBuff;
 70 
 71                 pos = tmpStr.find('=');
 72                 key = tmpStr.substr(0, pos);
 73                 value = tmpStr.substr(pos+1, tmpStr.size() - pos);
 74                 if (key.size() > 0) {
 75                     map_list[key] = value;
 76                 }
 77 
 78                 j = 0;
 79             }
 80             else {
 81                 szTmpBuff[j] = szDestBuff[i];
 82                 j++;
 83             }
 84 
 85             if (szDestBuff[i] == '\0' && szDestBuff[i + 1] == '\0') {
 86                 endOfBuffer = TRUE;
 87             }
 88             i++;
 89         }
 90     }
 91 
 92     static void to_hex(std::string value, std::string& ret_value)
 93     {
 94         //char *buf = new char[4];
 95 
 96         for (int i = 0; i < value.size(); i++) {
 97             char buf[4];
 98             memset(buf, 0, 4);
 99             sprintf_s(buf, 4, "%02X", (BYTE)value[i]);
100             //_ltoa(value[i], buf, 16);
101             ret_value.append((const char *)buf, 2);
102 
103         }
104         //delete buf;
105     }
106 
107     static void from_hex(std::string& hex_val, std::string& ret_value)
108     {
109         long val;
110         for (int i = 0; i < hex_val.size(); i++) {
111             char buf[4];
112             memset(buf, 0, 4);
113             memcpy(buf, &hex_val[i], 2);
114             val = strtol((char*)buf, NULL, 16);
115             ret_value.append((const char *)&val, 1);
116             i++;
117         }
118     }
119 
120 protected:
121 private:
122     std::string m_file;
123 };

posted on 2020-10-14 17:34  thinkinc999  阅读(145)  评论(0编辑  收藏  举报

导航