summaryrefslogtreecommitdiffstats
path: root/Src/nu/Config.h
blob: ddc992ded8a1041778929c2932774977f8ba48bf (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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