source: trunk/settings/thth_settings.h@ 137

Last change on this file since 137 was 137, checked in by Gregg Young, 4 years ago

Fix some issues identified by cppcheck; minor code cleanup.

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1/***
2 This file belongs to the Gotcha! distribution.
3 Copyright (C) 1998-2002 Thorsten Thielen <thth@c2226.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***/
19
20#ifndef _THTH_SETTINGS_H_
21#define _THTH_SETTINGS_H_
22
23#define OS2EMX_PLAIN_CHAR
24
25#define INCL_PM
26#define INCL_GPI
27
28#include "os2.h"
29#include "io.h"
30#include "string.h"
31
32typedef USHORT ID;
33
34// ------------------------------------------------------------------------
35
36typedef class ththSettingsEntry
37{
38 friend class ththSettings;
39
40protected:
41 enum { SET_FLAG, SET_LONG, SET_STRING, SET_BINARY };
42
43 ththSettingsEntry (ID id, PSZ pszGroup, PSZ pszKey, USHORT usType);
44
45 ID QueryID (VOID) { return id; }
46 USHORT QueryType (VOID) { return usType; }
47 PSZ QueryGroup (VOID) { return pszGroup; }
48 PSZ QueryKey (VOID) { return pszKey; }
49
50 virtual VOID Reset (VOID) = 0;
51 virtual VOID Load (HINI hini) = 0;
52 virtual VOID Save (HINI hini) = 0;
53
54private:
55 ID id;
56 PSZ pszGroup;
57 PSZ pszKey;
58 USHORT usType;
59} THTH_SE;
60
61typedef THTH_SE *PTHTH_SE;
62
63inline THTH_SE :: ththSettingsEntry (ID id, PSZ pszGroup, PSZ pszKey, USHORT usType)
64{
65 this->id = id;
66 this->pszGroup = pszGroup;
67 this->pszKey = pszKey;
68 this->usType = usType;
69}
70
71// ------------------------------------------------------------------------
72
73typedef class ththSettingsEntryFlag : public ththSettingsEntry
74{
75 friend class ththSettings;
76
77public:
78 ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f)
79 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG), fDefault (f)
80 {
81 Reset ();
82 }
83
84 ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f, HINI hini)
85 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG), fDefault (f)
86 {
87 Load (hini);
88 }
89
90 VOID Set (BOOL f) { this->f = f; }
91 BOOL Query (VOID) { return f; }
92 VOID Reset (VOID) { f = fDefault; }
93
94 VOID Load (HINI hini)
95 {
96 BOOL f;
97 ULONG ul = sizeof (f);
98 if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
99 PVOID (&f), &ul))
100 Reset ();
101 else
102 Set (f);
103 }
104
105 VOID Save (HINI hini)
106 {
107 BOOL f = Query ();
108 PrfWriteProfileData (hini, QueryGroup (), QueryKey (),
109 PVOID (&f), sizeof (f));
110 }
111
112private:
113 BOOL f, fDefault;
114} THTH_SEF;
115
116typedef THTH_SEF *PTHTH_SEF;
117
118// ------------------------------------------------------------------------
119
120typedef class ththSettingsEntryLong : public ththSettingsEntry
121{
122 friend class ththSettings;
123
124public:
125 ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l)
126 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG), lDefault (l)
127 {
128 Reset ();
129 }
130
131 ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l, HINI hini)
132 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG), lDefault (l)
133 {
134 Load (hini);
135 }
136
137 VOID Set (LONG l) { this->l = l; }
138 LONG Query (VOID) { return l; }
139 VOID Reset (VOID) { l = lDefault; }
140
141 VOID Load (HINI hini)
142 {
143 LONG l;
144 ULONG ul = sizeof (l);
145 if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
146 PVOID (&l), &ul))
147 Reset ();
148 else
149 Set (l);
150 }
151
152 VOID Save (HINI hini)
153 {
154 LONG l = Query ();
155 PrfWriteProfileData (hini, QueryGroup (), QueryKey (),
156 PVOID (&l), sizeof (l));
157 }
158
159private:
160 LONG l, lDefault;
161} THTH_SEL;
162
163typedef THTH_SEL *PTHTH_SEL;
164
165// ------------------------------------------------------------------------
166
167typedef class ththSettingsEntryString : public ththSettingsEntry
168{
169 friend class ththSettings;
170
171public:
172 ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz)
173 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING), pszDefault(psz)
174 {
175 Reset ();
176 }
177
178 ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz, HINI hini)
179 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING), pszDefault(psz)
180 {
181 Load (hini);
182 }
183
184 VOID Set (PSZ psz)
185 {
186 if (this->psz)
187 delete this->psz;
188 this->psz = strdup (psz);
189 }
190
191 PSZ Query (VOID)
192 {
193 if (! psz)
194 return "";
195 return psz;
196 }
197
198 VOID Reset (VOID) { Set (pszDefault); }
199
200 VOID Load (HINI hini)
201 {
202 ULONG ul;
203 if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul))
204 {
205 CHAR *ach = new char[ul];
206 PrfQueryProfileString (hini, QueryGroup (), QueryKey (),
207 pszDefault, ach, ul);
208 Set (ach);
209 delete ach;
210 }
211 }
212
213 VOID Save (HINI hini)
214 {
215 PrfWriteProfileString (hini, QueryGroup (), QueryKey (), Query ());
216 }
217
218private:
219 PSZ psz, pszDefault;
220} THTH_SES;
221
222typedef THTH_SES *PTHTH_SES;
223
224// ------------------------------------------------------------------------
225
226typedef class ththSettingsEntryBinary : public ththSettingsEntry
227{
228 friend class ththSettings;
229
230public:
231 ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size)
232 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY), pvDefault (pv)
233 {
234 this->sizeDefault = size; Reset ();
235 }
236
237 ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size, HINI hini)
238 : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY), pvDefault (pv)
239 {
240 this->sizeDefault = size; Load (hini);
241 }
242
243 VOID Set (PVOID pv, size_t size)
244 {
245 if (this->pv)
246 delete this->pv;
247 this->pv = new BYTE[size];
248 memcpy (this->pv, pv, size);
249 this->size = size;
250 }
251
252 PVOID Query (size_t *psize = NULL)
253 {
254 if (! pv)
255 {
256 if (psize)
257 *psize = 0L;
258 return NULL;
259 }
260 if (psize)
261 *psize = size;
262 return pv;
263 }
264
265 VOID Reset (VOID) { Set (pvDefault, sizeDefault); }
266
267 VOID Load (HINI hini)
268 {
269 ULONG ul;
270 if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul))
271 {
272 pv = new BYTE[ul];
273 if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
274 pv, &ul))
275 Reset ();
276 }
277 }
278
279 VOID Save (HINI hini)
280 {
281 PrfWriteProfileData (hini, QueryGroup (), QueryKey (), pv, size);
282 }
283
284private:
285 PVOID pv, pvDefault;
286 size_t size, sizeDefault;
287} THTH_SEB;
288
289typedef THTH_SEB *PTHTH_SEB;
290
291// ------------------------------------------------------------------------
292
293typedef class ththSettings
294{
295public:
296 ththSettings (PTHTH_SE *ppse, PSZ pszInifile = NULL, ULONG ulVersion = 0L);
297 virtual ~ththSettings (VOID);
298
299 VOID Reset (VOID)
300 {
301 if (ppse)
302 for (LONG i = 0; ppse[i]; i++)
303 ppse[i]->Reset ();
304 }
305
306 VOID Load (VOID)
307 {
308 if (ppse && hini)
309 for (LONG i = 0; ppse[i]; i++)
310 ppse[i]->Load (hini);
311 }
312
313 BOOL Save (VOID);
314
315 VOID SetFlag (ID id, BOOL f = TRUE)
316 {
317 if ((pse = ID2Entry (id)))
318 if (pse->QueryType () == ththSettingsEntry::SET_FLAG)
319 PTHTH_SEF (pse)->Set (f);
320 }
321
322 VOID ClearFlag (ID id) { SetFlag (id, FALSE); }
323 VOID ToggleFlag (ID id) { SetFlag (id, ! FlagSet (id)); }
324
325 BOOL FlagSet (ID id)
326 {
327 if ((pse = ID2Entry (id)))
328 if (pse->QueryType () == ththSettingsEntry::SET_FLAG)
329 return PTHTH_SEF (pse)->Query ();
330 return FALSE;
331 }
332
333 BOOL QueryFlag (ID id) { return FlagSet (id); }
334
335 VOID SetLong (ID id, LONG l)
336 {
337 if ((pse = ID2Entry (id)))
338 if (pse->QueryType () == ththSettingsEntry::SET_LONG)
339 PTHTH_SEL (pse)->Set (l);
340 }
341
342 LONG QueryLong (ID id)
343 {
344 if ((pse = ID2Entry (id)))
345 if (pse->QueryType () == ththSettingsEntry::SET_LONG)
346 return PTHTH_SEL (pse)->Query ();
347 return 0L;
348 }
349
350 VOID SetString (ID id, const PSZ psz)
351 {
352 if ((pse = ID2Entry (id)))
353 if (pse->QueryType () == ththSettingsEntry::SET_STRING)
354 PTHTH_SES (pse)->Set (psz);
355 }
356
357 PSZ QueryString (ID id)
358 {
359 if ((pse = ID2Entry (id)))
360 if (pse->QueryType () == ththSettingsEntry::SET_STRING)
361 return PTHTH_SES (pse)->Query ();
362 return "";
363 }
364
365 VOID SetBinary (ID id, PVOID pv, size_t size)
366 {
367 if ((pse = ID2Entry (id)))
368 if (pse->QueryType () == ththSettingsEntry::SET_BINARY)
369 PTHTH_SEB (pse)->Set (pv, size);
370 }
371
372 PVOID QueryBinary (ID id, size_t *psize = NULL)
373 {
374 if ((pse = ID2Entry (id)))
375 if (pse->QueryType () == ththSettingsEntry::SET_BINARY)
376 return PTHTH_SEB (pse)->Query (psize);
377 *psize = 0L;
378 return NULL;
379 }
380
381 HINI QueryPrfHandle (VOID) { return hini; }
382
383protected:
384 PSZ QueryProfileName (VOID) { return psz; }
385 ULONG Error (VOID) { return ulError; }
386
387 ULONG QueryVersion (VOID) { return ulVersion; }
388 VOID SetVersion (ULONG ul) { ulVersion = ul; }
389
390private:
391 PTHTH_SE ID2Entry (ID id)
392 {
393 if (ppse)
394 for (LONG i = 0; ppse[i]; i++)
395 if (ppse[i]->QueryID () == id)
396 return ppse[i];
397 return NULL;
398 }
399
400 PSZ psz;
401 HINI hini;
402 PTHTH_SE *ppse, pse;
403 ULONG ulVersion;
404 ULONG ulError;
405} THTH_SETTINGS;
406
407typedef THTH_SETTINGS *PTHTH_SETTINGS;
408
409// ------------------------------------------------------------------------
410#endif
Note: See TracBrowser for help on using the repository browser.