diff options
author | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
---|---|---|
committer | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
commit | 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch) | |
tree | 12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/nu/Config.h | |
parent | 537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff) | |
download | winamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz |
Initial community commit
Diffstat (limited to 'Src/nu/Config.h')
-rw-r--r-- | Src/nu/Config.h | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/Src/nu/Config.h b/Src/nu/Config.h new file mode 100644 index 00000000..ddc992de --- /dev/null +++ b/Src/nu/Config.h @@ -0,0 +1,188 @@ +#ifndef NULLSOFT_UTILITY_CONFIGH +#define NULLSOFT_UTILITY_CONFIGH +#include <string> +#include <map> +#include <windows.h> + +typedef std::wstring tstring; + +namespace Nullsoft +{ + namespace Utility + { + + class ConfigItemBase + { + public: + ConfigItemBase(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) + : appName(nullptr), fileName(nullptr), keyName(nullptr) + { + appName = _appName; + fileName = _fileName; + keyName = _keyName; + } + + ~ConfigItemBase() + { + + } + + const wchar_t *appName; + const wchar_t *fileName; + const wchar_t *keyName; + }; + + template <class config_t> + class ConfigItem : public ConfigItemBase + { + public: + ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName) + { + } + + ~ConfigItem() {} + + void operator =(config_t input) + { + WritePrivateProfileStruct(appName, + keyName, + (void *) & input, + sizeof(input), + fileName); + } + + operator config_t() + { + config_t temp; + + memset(&temp, 0, sizeof(temp)); + GetPrivateProfileStruct(appName, + keyName, + &temp, + sizeof(temp), + fileName); + return temp; + } + }; + + + template <> + class ConfigItem<TCHAR *> : public ConfigItemBase + { + public: + ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName) + { + } + + ~ConfigItem(){} + + void operator =(LPCTSTR input) + { + WritePrivateProfileString(appName, + keyName, + input, + fileName); + } + void GetString(LPTSTR str, size_t len) + { + GetPrivateProfileString(appName, keyName, TEXT(""), str, len, fileName); + } + }; + + template <> + class ConfigItem<int> : public ConfigItemBase + { + public: + ConfigItem(const wchar_t *_appName, const wchar_t * _fileName, LPCTSTR _keyName) : ConfigItemBase(_appName, _fileName, _keyName), def(0) + { + } + + ~ConfigItem() {} + + void operator =(int input) + { + TCHAR tmp[(sizeof(int) / 2) * 5 + 1]; // enough room to hold for 16,32 or 64 bit ints, plus null terminator + wsprintf(tmp, TEXT("%d"), input); + WritePrivateProfileString(appName, + keyName, + tmp, + fileName); + } + + operator int () + { + return GetPrivateProfileInt(appName, keyName, def, fileName); + } + + void SetDefault(int _def) + { + def = _def; + } + int def; + }; + + class Config + { + public: + Config() : appName(nullptr),fileName(nullptr) + { + } + + ~Config() + { + if (appName != nullptr) + { + free(appName); + appName = nullptr; + } + if (fileName != nullptr) + { + free(fileName); + fileName = nullptr; + } + } + + void SetFile(LPCTSTR iniFile, LPCTSTR _appName) + { + if (appName != nullptr) + { + free(appName); + appName = nullptr; + } + if (fileName != nullptr) + { + free(fileName); + fileName = nullptr; + } + appName = _wcsdup(_appName); + fileName = _wcsdup(iniFile); + } + + ConfigItem<int> cfg_int(LPCTSTR keyName, int def) + { + ConfigItem<int> item(appName, fileName, keyName); + item.SetDefault(def); + return item; + } + + ConfigItem<TCHAR *> cfg_str(LPCTSTR keyName) + { + return ConfigItem<TCHAR *>(appName, fileName, keyName); + } + + ConfigItem<GUID> cfg_guid(LPCTSTR keyName) + { + return ConfigItem<GUID>(appName, fileName, keyName); + + } + ConfigItem<__int64> cfg_int64(LPCTSTR keyName) + { + ConfigItem<__int64> item(appName, fileName, keyName); + return item; + } + + wchar_t *appName, *fileName; + }; + } +} +#endif |