Config.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef NULLSOFT_UTILITY_CONFIGH
  2. #define NULLSOFT_UTILITY_CONFIGH
  3. #include <string>
  4. #include <map>
  5. #include <windows.h>
  6. typedef std::wstring tstring;
  7. namespace Nullsoft
  8. {
  9. namespace Utility
  10. {
  11. class ConfigItemBase
  12. {
  13. public:
  14. ConfigItemBase(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName)
  15. : appName(nullptr), fileName(nullptr), keyName(nullptr)
  16. {
  17. appName = _appName;
  18. fileName = _fileName;
  19. keyName = _keyName;
  20. }
  21. ~ConfigItemBase()
  22. {
  23. }
  24. const wchar_t *appName;
  25. const wchar_t *fileName;
  26. const wchar_t *keyName;
  27. };
  28. template <class config_t>
  29. class ConfigItem : public ConfigItemBase
  30. {
  31. public:
  32. ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName)
  33. {
  34. }
  35. ~ConfigItem() {}
  36. void operator =(config_t input)
  37. {
  38. WritePrivateProfileStruct(appName,
  39. keyName,
  40. (void *) & input,
  41. sizeof(input),
  42. fileName);
  43. }
  44. operator config_t()
  45. {
  46. config_t temp;
  47. memset(&temp, 0, sizeof(temp));
  48. GetPrivateProfileStruct(appName,
  49. keyName,
  50. &temp,
  51. sizeof(temp),
  52. fileName);
  53. return temp;
  54. }
  55. };
  56. template <>
  57. class ConfigItem<TCHAR *> : public ConfigItemBase
  58. {
  59. public:
  60. ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName)
  61. {
  62. }
  63. ~ConfigItem(){}
  64. void operator =(LPCTSTR input)
  65. {
  66. WritePrivateProfileString(appName,
  67. keyName,
  68. input,
  69. fileName);
  70. }
  71. void GetString(LPTSTR str, size_t len)
  72. {
  73. GetPrivateProfileString(appName, keyName, TEXT(""), str, len, fileName);
  74. }
  75. };
  76. template <>
  77. class ConfigItem<int> : public ConfigItemBase
  78. {
  79. public:
  80. ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName), def(0)
  81. {
  82. }
  83. ~ConfigItem() {}
  84. void operator =(int input)
  85. {
  86. TCHAR tmp[(sizeof(int) / 2) * 5 + 1]; // enough room to hold for 16,32 or 64 bit ints, plus null terminator
  87. wsprintf(tmp, TEXT("%d"), input);
  88. WritePrivateProfileString(appName,
  89. keyName,
  90. tmp,
  91. fileName);
  92. }
  93. operator int ()
  94. {
  95. return GetPrivateProfileInt(appName, keyName, def, fileName);
  96. }
  97. void SetDefault(int _def)
  98. {
  99. def = _def;
  100. }
  101. int def;
  102. };
  103. class Config
  104. {
  105. public:
  106. Config() : appName(nullptr),fileName(nullptr)
  107. {
  108. }
  109. ~Config()
  110. {
  111. if (appName != nullptr)
  112. {
  113. free(appName);
  114. appName = nullptr;
  115. }
  116. if (fileName != nullptr)
  117. {
  118. free(fileName);
  119. fileName = nullptr;
  120. }
  121. }
  122. void SetFile(LPCTSTR iniFile, LPCTSTR _appName)
  123. {
  124. if (appName != nullptr)
  125. {
  126. free(appName);
  127. appName = nullptr;
  128. }
  129. if (fileName != nullptr)
  130. {
  131. free(fileName);
  132. fileName = nullptr;
  133. }
  134. appName = _wcsdup(_appName);
  135. fileName = _wcsdup(iniFile);
  136. }
  137. ConfigItem<int> cfg_int(LPCTSTR keyName, int def)
  138. {
  139. ConfigItem<int> item(appName, fileName, keyName);
  140. item.SetDefault(def);
  141. return item;
  142. }
  143. ConfigItem<TCHAR *> cfg_str(LPCTSTR keyName)
  144. {
  145. return ConfigItem<TCHAR *>(appName, fileName, keyName);
  146. }
  147. ConfigItem<GUID> cfg_guid(LPCTSTR keyName)
  148. {
  149. return ConfigItem<GUID>(appName, fileName, keyName);
  150. }
  151. ConfigItem<__int64> cfg_int64(LPCTSTR keyName)
  152. {
  153. ConfigItem<__int64> item(appName, fileName, keyName);
  154. return item;
  155. }
  156. wchar_t *appName, *fileName;
  157. };
  158. }
  159. }
  160. #endif