| 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 |  | 
|---|
| 32 | typedef USHORT ID; | 
|---|
| 33 |  | 
|---|
| 34 | // ------------------------------------------------------------------------ | 
|---|
| 35 |  | 
|---|
| 36 | typedef class ththSettingsEntry | 
|---|
| 37 | { | 
|---|
| 38 | friend class ththSettings; | 
|---|
| 39 |  | 
|---|
| 40 | protected: | 
|---|
| 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 |  | 
|---|
| 54 | private: | 
|---|
| 55 | ID       id; | 
|---|
| 56 | PSZ      pszGroup; | 
|---|
| 57 | PSZ      pszKey; | 
|---|
| 58 | USHORT   usType; | 
|---|
| 59 | } THTH_SE; | 
|---|
| 60 |  | 
|---|
| 61 | typedef THTH_SE *PTHTH_SE; | 
|---|
| 62 |  | 
|---|
| 63 | inline 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 |  | 
|---|
| 73 | typedef class ththSettingsEntryFlag : public ththSettingsEntry | 
|---|
| 74 | { | 
|---|
| 75 | friend class ththSettings; | 
|---|
| 76 |  | 
|---|
| 77 | public: | 
|---|
| 78 | ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f) | 
|---|
| 79 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG) { | 
|---|
| 80 | fDefault = f; Reset (); } | 
|---|
| 81 |  | 
|---|
| 82 | ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f, HINI hini) | 
|---|
| 83 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG) { | 
|---|
| 84 | fDefault = f; Load (hini); } | 
|---|
| 85 |  | 
|---|
| 86 | VOID   Set (BOOL f) { this->f = f; } | 
|---|
| 87 | BOOL   Query (VOID) { return f; } | 
|---|
| 88 | VOID   Reset (VOID) { f = fDefault; } | 
|---|
| 89 |  | 
|---|
| 90 | VOID Load (HINI hini) | 
|---|
| 91 | { | 
|---|
| 92 | BOOL  f; | 
|---|
| 93 | ULONG ul = sizeof (f); | 
|---|
| 94 | if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (), | 
|---|
| 95 | PVOID (&f), &ul)) | 
|---|
| 96 | Reset (); | 
|---|
| 97 | else | 
|---|
| 98 | Set (f); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | VOID Save (HINI hini) | 
|---|
| 102 | { | 
|---|
| 103 | BOOL f = Query (); | 
|---|
| 104 | PrfWriteProfileData (hini, QueryGroup (), QueryKey (), | 
|---|
| 105 | PVOID (&f), sizeof (f)); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | private: | 
|---|
| 109 | BOOL   f, fDefault; | 
|---|
| 110 | } THTH_SEF; | 
|---|
| 111 |  | 
|---|
| 112 | typedef THTH_SEF *PTHTH_SEF; | 
|---|
| 113 |  | 
|---|
| 114 | // ------------------------------------------------------------------------ | 
|---|
| 115 |  | 
|---|
| 116 | typedef class ththSettingsEntryLong : public ththSettingsEntry | 
|---|
| 117 | { | 
|---|
| 118 | friend class ththSettings; | 
|---|
| 119 |  | 
|---|
| 120 | public: | 
|---|
| 121 | ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l) | 
|---|
| 122 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG) { | 
|---|
| 123 | lDefault = l; Reset (); } | 
|---|
| 124 |  | 
|---|
| 125 | ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l, HINI hini) | 
|---|
| 126 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG) { | 
|---|
| 127 | lDefault = l; Load (hini); } | 
|---|
| 128 |  | 
|---|
| 129 | VOID   Set (LONG l) { this->l = l; } | 
|---|
| 130 | LONG   Query (VOID) { return l; } | 
|---|
| 131 | VOID   Reset (VOID) { l = lDefault; } | 
|---|
| 132 |  | 
|---|
| 133 | VOID Load (HINI hini) | 
|---|
| 134 | { | 
|---|
| 135 | LONG  l; | 
|---|
| 136 | ULONG ul = sizeof (l); | 
|---|
| 137 | if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (), | 
|---|
| 138 | PVOID (&l), &ul)) | 
|---|
| 139 | Reset (); | 
|---|
| 140 | else | 
|---|
| 141 | Set (l); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | VOID Save (HINI hini) | 
|---|
| 145 | { | 
|---|
| 146 | LONG l = Query (); | 
|---|
| 147 | PrfWriteProfileData (hini, QueryGroup (), QueryKey (), | 
|---|
| 148 | PVOID (&l), sizeof (l)); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | private: | 
|---|
| 152 | LONG   l, lDefault; | 
|---|
| 153 | } THTH_SEL; | 
|---|
| 154 |  | 
|---|
| 155 | typedef THTH_SEL *PTHTH_SEL; | 
|---|
| 156 |  | 
|---|
| 157 | // ------------------------------------------------------------------------ | 
|---|
| 158 |  | 
|---|
| 159 | typedef class ththSettingsEntryString : public ththSettingsEntry | 
|---|
| 160 | { | 
|---|
| 161 | friend class ththSettings; | 
|---|
| 162 |  | 
|---|
| 163 | public: | 
|---|
| 164 | ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz) | 
|---|
| 165 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING) { | 
|---|
| 166 | pszDefault = psz; Reset (); } | 
|---|
| 167 |  | 
|---|
| 168 | ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz, HINI hini) | 
|---|
| 169 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING) { | 
|---|
| 170 | pszDefault = psz; Load (hini); } | 
|---|
| 171 |  | 
|---|
| 172 | VOID Set (PSZ psz) | 
|---|
| 173 | { | 
|---|
| 174 | if (this->psz) | 
|---|
| 175 | delete this->psz; | 
|---|
| 176 | this->psz = strdup (psz); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | PSZ Query (VOID) | 
|---|
| 180 | { | 
|---|
| 181 | if (! psz) | 
|---|
| 182 | return ""; | 
|---|
| 183 | return psz; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | VOID   Reset (VOID) { Set (pszDefault); } | 
|---|
| 187 |  | 
|---|
| 188 | VOID Load (HINI hini) | 
|---|
| 189 | { | 
|---|
| 190 | ULONG ul; | 
|---|
| 191 | if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul)) | 
|---|
| 192 | { | 
|---|
| 193 | CHAR *ach = new char[ul]; | 
|---|
| 194 | PrfQueryProfileString (hini, QueryGroup (), QueryKey (), | 
|---|
| 195 | pszDefault, ach, ul); | 
|---|
| 196 | Set (ach); | 
|---|
| 197 | delete ach; | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | VOID Save (HINI hini) | 
|---|
| 202 | { | 
|---|
| 203 | PrfWriteProfileString (hini, QueryGroup (), QueryKey (), Query ()); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | private: | 
|---|
| 207 | PSZ   psz, pszDefault; | 
|---|
| 208 | } THTH_SES; | 
|---|
| 209 |  | 
|---|
| 210 | typedef THTH_SES *PTHTH_SES; | 
|---|
| 211 |  | 
|---|
| 212 | // ------------------------------------------------------------------------ | 
|---|
| 213 |  | 
|---|
| 214 | typedef class ththSettingsEntryBinary : public ththSettingsEntry | 
|---|
| 215 | { | 
|---|
| 216 | friend class ththSettings; | 
|---|
| 217 |  | 
|---|
| 218 | public: | 
|---|
| 219 | ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size) | 
|---|
| 220 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY) { | 
|---|
| 221 | pvDefault = pv; this->sizeDefault = size; Reset (); } | 
|---|
| 222 |  | 
|---|
| 223 | ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size, HINI hini) | 
|---|
| 224 | : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY) { | 
|---|
| 225 | pvDefault = pv; this->sizeDefault = size; Load (hini); } | 
|---|
| 226 |  | 
|---|
| 227 | VOID Set (PVOID pv, size_t size) | 
|---|
| 228 | { | 
|---|
| 229 | if (this->pv) | 
|---|
| 230 | delete this->pv; | 
|---|
| 231 | this->pv = new BYTE[size]; | 
|---|
| 232 | memcpy (this->pv, pv, size); | 
|---|
| 233 | this->size = size; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | PVOID Query (size_t *psize = NULL) | 
|---|
| 237 | { | 
|---|
| 238 | if (! pv) | 
|---|
| 239 | { | 
|---|
| 240 | if (psize) | 
|---|
| 241 | *psize = 0L; | 
|---|
| 242 | return NULL; | 
|---|
| 243 | } | 
|---|
| 244 | if (psize) | 
|---|
| 245 | *psize = size; | 
|---|
| 246 | return pv; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | VOID Reset (VOID) { Set (pvDefault, sizeDefault); } | 
|---|
| 250 |  | 
|---|
| 251 | VOID Load (HINI hini) | 
|---|
| 252 | { | 
|---|
| 253 | ULONG ul; | 
|---|
| 254 | if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul)) | 
|---|
| 255 | { | 
|---|
| 256 | pv = new BYTE[ul]; | 
|---|
| 257 | if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (), | 
|---|
| 258 | pv, &ul)) | 
|---|
| 259 | Reset (); | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | VOID Save (HINI hini) | 
|---|
| 264 | { | 
|---|
| 265 | PrfWriteProfileData (hini, QueryGroup (), QueryKey (), pv, size); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | private: | 
|---|
| 269 | PVOID   pv, pvDefault; | 
|---|
| 270 | size_t  size, sizeDefault; | 
|---|
| 271 | } THTH_SEB; | 
|---|
| 272 |  | 
|---|
| 273 | typedef THTH_SEB *PTHTH_SEB; | 
|---|
| 274 |  | 
|---|
| 275 | // ------------------------------------------------------------------------ | 
|---|
| 276 |  | 
|---|
| 277 | typedef class ththSettings | 
|---|
| 278 | { | 
|---|
| 279 | public: | 
|---|
| 280 | ththSettings (PTHTH_SE *ppse, PSZ pszInifile = NULL, ULONG ulVersion = 0L); | 
|---|
| 281 | virtual ~ththSettings (VOID); | 
|---|
| 282 |  | 
|---|
| 283 | VOID Reset (VOID) | 
|---|
| 284 | { | 
|---|
| 285 | if (ppse) | 
|---|
| 286 | for (LONG i = 0; ppse[i]; i++) | 
|---|
| 287 | ppse[i]->Reset (); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | VOID Load (VOID) | 
|---|
| 291 | { | 
|---|
| 292 | if (ppse && hini) | 
|---|
| 293 | for (LONG i = 0; ppse[i]; i++) | 
|---|
| 294 | ppse[i]->Load (hini); | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | BOOL Save (VOID); | 
|---|
| 298 |  | 
|---|
| 299 | VOID SetFlag (ID id, BOOL f = TRUE) | 
|---|
| 300 | { | 
|---|
| 301 | if ((pse = ID2Entry (id))) | 
|---|
| 302 | if (pse->QueryType () == ththSettingsEntry::SET_FLAG) | 
|---|
| 303 | PTHTH_SEF (pse)->Set  (f); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | VOID   ClearFlag (ID id) { SetFlag (id, FALSE); } | 
|---|
| 307 | VOID   ToggleFlag (ID id) { SetFlag (id, ! FlagSet (id)); } | 
|---|
| 308 |  | 
|---|
| 309 | BOOL FlagSet (ID id) | 
|---|
| 310 | { | 
|---|
| 311 | if ((pse = ID2Entry (id))) | 
|---|
| 312 | if (pse->QueryType () == ththSettingsEntry::SET_FLAG) | 
|---|
| 313 | return PTHTH_SEF (pse)->Query (); | 
|---|
| 314 | return FALSE; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | BOOL   QueryFlag (ID id) { return FlagSet (id); } | 
|---|
| 318 |  | 
|---|
| 319 | VOID SetLong (ID id, LONG l) | 
|---|
| 320 | { | 
|---|
| 321 | if ((pse = ID2Entry (id))) | 
|---|
| 322 | if (pse->QueryType () == ththSettingsEntry::SET_LONG) | 
|---|
| 323 | PTHTH_SEL (pse)->Set (l); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | LONG QueryLong (ID id) | 
|---|
| 327 | { | 
|---|
| 328 | if ((pse = ID2Entry (id))) | 
|---|
| 329 | if (pse->QueryType () == ththSettingsEntry::SET_LONG) | 
|---|
| 330 | return PTHTH_SEL (pse)->Query (); | 
|---|
| 331 | return 0L; | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | VOID SetString (ID id, const PSZ psz) | 
|---|
| 335 | { | 
|---|
| 336 | if ((pse = ID2Entry (id))) | 
|---|
| 337 | if (pse->QueryType () == ththSettingsEntry::SET_STRING) | 
|---|
| 338 | PTHTH_SES (pse)->Set  (psz); | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | PSZ QueryString (ID id) | 
|---|
| 342 | { | 
|---|
| 343 | if ((pse = ID2Entry (id))) | 
|---|
| 344 | if (pse->QueryType () == ththSettingsEntry::SET_STRING) | 
|---|
| 345 | return PTHTH_SES (pse)->Query (); | 
|---|
| 346 | return ""; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | VOID SetBinary (ID id, PVOID pv, size_t size) | 
|---|
| 350 | { | 
|---|
| 351 | if ((pse = ID2Entry (id))) | 
|---|
| 352 | if (pse->QueryType () == ththSettingsEntry::SET_BINARY) | 
|---|
| 353 | PTHTH_SEB (pse)->Set (pv, size); | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | PVOID QueryBinary (ID id, size_t *psize = NULL) | 
|---|
| 357 | { | 
|---|
| 358 | if ((pse = ID2Entry (id))) | 
|---|
| 359 | if (pse->QueryType () == ththSettingsEntry::SET_BINARY) | 
|---|
| 360 | return PTHTH_SEB (pse)->Query (psize); | 
|---|
| 361 | *psize = 0L; | 
|---|
| 362 | return NULL; | 
|---|
| 363 | } | 
|---|
| 364 |  | 
|---|
| 365 | HINI  QueryPrfHandle (VOID) { return hini; } | 
|---|
| 366 |  | 
|---|
| 367 | protected: | 
|---|
| 368 | PSZ   QueryProfileName (VOID) { return psz; } | 
|---|
| 369 | ULONG Error (VOID) { return ulError; } | 
|---|
| 370 |  | 
|---|
| 371 | ULONG QueryVersion (VOID) { return ulVersion; } | 
|---|
| 372 | VOID  SetVersion (ULONG ul) { ulVersion = ul; } | 
|---|
| 373 |  | 
|---|
| 374 | private: | 
|---|
| 375 | PTHTH_SE ID2Entry (ID id) | 
|---|
| 376 | { | 
|---|
| 377 | if (ppse) | 
|---|
| 378 | for (LONG i = 0; ppse[i]; i++) | 
|---|
| 379 | if (ppse[i]->QueryID () == id) | 
|---|
| 380 | return ppse[i]; | 
|---|
| 381 | return NULL; | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | PSZ    psz; | 
|---|
| 385 | HINI   hini; | 
|---|
| 386 | PTHTH_SE    *ppse, pse; | 
|---|
| 387 | ULONG  ulVersion; | 
|---|
| 388 | ULONG  ulError; | 
|---|
| 389 | } THTH_SETTINGS; | 
|---|
| 390 |  | 
|---|
| 391 | typedef THTH_SETTINGS *PTHTH_SETTINGS; | 
|---|
| 392 |  | 
|---|
| 393 | // ------------------------------------------------------------------------ | 
|---|
| 394 | #endif | 
|---|