1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile prfh.c:
|
---|
4 | * contains those Presentation Manager helper functions
|
---|
5 | * which deal with Profile (Prf*) functions.
|
---|
6 | * This file is new with V0.82.
|
---|
7 | *
|
---|
8 | * Usage: All OS/2 programs.
|
---|
9 | *
|
---|
10 | * Function prefixes:
|
---|
11 | * -- prfh* Prf (profile, INI) helper functions
|
---|
12 | *
|
---|
13 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
14 | * numbering.
|
---|
15 | *
|
---|
16 | *@@header "helpers\prfh.h"
|
---|
17 | */
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * Copyright (C) 1997-2000 Ulrich Mller.
|
---|
21 | * This file is part of the XWorkplace source package.
|
---|
22 | * XWorkplace is free software; you can redistribute it and/or modify
|
---|
23 | * it under the terms of the GNU General Public License as published
|
---|
24 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
25 | * "COPYING" file of the XWorkplace main distribution.
|
---|
26 | * This program is distributed in the hope that it will be useful,
|
---|
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
29 | * GNU General Public License for more details.
|
---|
30 | */
|
---|
31 |
|
---|
32 | #define OS2EMX_PLAIN_CHAR
|
---|
33 | // this is needed for "os2emx.h"; if this is defined,
|
---|
34 | // emx will define PSZ as _signed_ char, otherwise
|
---|
35 | // as unsigned char
|
---|
36 |
|
---|
37 | #define INCL_DOS
|
---|
38 | #define INCL_DOSERRORS
|
---|
39 | #define INCL_WIN
|
---|
40 | #include <os2.h>
|
---|
41 |
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <string.h>
|
---|
45 |
|
---|
46 | #include "setup.h" // code generation and debugging options
|
---|
47 |
|
---|
48 | #include "helpers\prfh.h"
|
---|
49 |
|
---|
50 | #pragma hdrstop
|
---|
51 |
|
---|
52 | /*
|
---|
53 | *@@category: Helpers\Profile (INI) helpers
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | *@@ prfhQueryKeysForApp:
|
---|
58 | * returns the keys list for an INI application. This
|
---|
59 | * list is copied into a newly allocated buffer, of which
|
---|
60 | * the address is returned.
|
---|
61 | *
|
---|
62 | * Returns NULL upon errors.
|
---|
63 | *
|
---|
64 | * If the return value is != NULL, the PSZ points to a new
|
---|
65 | * buffer which contains all the keys within the pszApp
|
---|
66 | * application. Each key name in the list is terminated with
|
---|
67 | * a null character. The last string in the list is terminated
|
---|
68 | * with two null characters.
|
---|
69 | *
|
---|
70 | * The returned buffer should be freed later using free().
|
---|
71 | *
|
---|
72 | * <B>Example</B> for iterating over a keys list:
|
---|
73 | + PSZ pszKeysList = prfhQueryKeysForApp(...);
|
---|
74 | + if (pszKeysList)
|
---|
75 | + {
|
---|
76 | + PSZ pKey2 = pszKeysList;
|
---|
77 | +
|
---|
78 | + while (*pKey2 != 0)
|
---|
79 | + {
|
---|
80 | + ... // pKey2 has the current key now
|
---|
81 | + pKey2 += strlen(pKey2)+1; // next key
|
---|
82 | + }
|
---|
83 | + free(pszKeysList);
|
---|
84 | + }
|
---|
85 | *
|
---|
86 | * You can also use this function to query the applications
|
---|
87 | * list for hIni, if you specifiy pszApp as NULL.
|
---|
88 | */
|
---|
89 |
|
---|
90 | PSZ prfhQueryKeysForApp(HINI hIni, // in: INI handle (can be HINI_USER or HINI_SYSTEM)
|
---|
91 | PSZ pszApp) // in: application to query list for (or NULL for applications list)
|
---|
92 | {
|
---|
93 | PSZ pKeys = NULL;
|
---|
94 | ULONG ulSizeOfKeysList = 0;
|
---|
95 |
|
---|
96 | // get size of keys list for pszApp
|
---|
97 | if (PrfQueryProfileSize(hIni, pszApp, NULL, &ulSizeOfKeysList))
|
---|
98 | {
|
---|
99 | pKeys = (PSZ)malloc(ulSizeOfKeysList);
|
---|
100 | if (!PrfQueryProfileData(hIni, pszApp, NULL, pKeys, &ulSizeOfKeysList))
|
---|
101 | {
|
---|
102 | free(pKeys);
|
---|
103 | pKeys = NULL;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return (pKeys);
|
---|
107 | }
|
---|
108 |
|
---|
109 | #ifdef __XWPMEMDEBUG__ // setup.h, helpers\memdebug.c
|
---|
110 |
|
---|
111 | /*
|
---|
112 | *@@ prfhQueryProfileDataDebug:
|
---|
113 | * debug version of prfhQueryProfileData, which is
|
---|
114 | * automatically mapped to if __XWPMEMDEBUG__ is defined.
|
---|
115 | *
|
---|
116 | *@@added V0.9.1 (99-12-20) [umoeller]
|
---|
117 | *@@changed V0.9.3 (2000-04-20) [umoeller]: this called malloc(0) if the key existed, but was empty. Fixed.
|
---|
118 | */
|
---|
119 |
|
---|
120 | PSZ prfhQueryProfileDataDebug(HINI hIni, // in: INI handle (can be HINI_USER or HINI_SYSTEM)
|
---|
121 | PSZ pszApp, // in: application to query
|
---|
122 | PSZ pszKey, // in: key to query
|
---|
123 | PULONG pcbBuf, // out: size of the returned buffer
|
---|
124 | const char *file,
|
---|
125 | unsigned long line,
|
---|
126 | const char *function)
|
---|
127 | {
|
---|
128 | PSZ pData = NULL;
|
---|
129 | ULONG ulSizeOfData = 0;
|
---|
130 |
|
---|
131 | // get size of data for pszApp/pszKey
|
---|
132 | if (PrfQueryProfileSize(hIni, pszApp, pszKey, &ulSizeOfData))
|
---|
133 | {
|
---|
134 | if (ulSizeOfData)
|
---|
135 | {
|
---|
136 | pData = (PSZ)memdMalloc(ulSizeOfData, file, line, function);
|
---|
137 | if (!PrfQueryProfileData(hIni, pszApp, pszKey, pData, &ulSizeOfData))
|
---|
138 | {
|
---|
139 | free(pData);
|
---|
140 | pData = NULL;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (pcbBuf)
|
---|
146 | *pcbBuf = ulSizeOfData;
|
---|
147 |
|
---|
148 | return (pData);
|
---|
149 | }
|
---|
150 |
|
---|
151 | #else
|
---|
152 |
|
---|
153 | /*
|
---|
154 | *@@ prfhQueryProfileData:
|
---|
155 | * similar to PrfQueryProfileData, but this one copies
|
---|
156 | * the data into a newly allocated buffer, of which the
|
---|
157 | * address is returned.
|
---|
158 | *
|
---|
159 | * Returns NULL upon errors, for example if the specified
|
---|
160 | * key doesn't exist or doesn't contain any data.
|
---|
161 | *
|
---|
162 | * If pcbBuf != NULL, this func will write the size of
|
---|
163 | * the allocated buffer into *pcbBuf.
|
---|
164 | *
|
---|
165 | * The returned buffer should be freed later using free().
|
---|
166 | *
|
---|
167 | *@@added V0.9.0 [umoeller]
|
---|
168 | *@@changed V0.9.3 (2000-04-20) [umoeller]: this called malloc(0) if the key existed, but was empty. Fixed.
|
---|
169 | */
|
---|
170 |
|
---|
171 | PSZ prfhQueryProfileData(HINI hIni, // in: INI handle (can be HINI_USER or HINI_SYSTEM)
|
---|
172 | PSZ pszApp, // in: application to query
|
---|
173 | PSZ pszKey, // in: key to query
|
---|
174 | PULONG pcbBuf) // out: size of the returned buffer
|
---|
175 | {
|
---|
176 | PSZ pData = NULL;
|
---|
177 | ULONG ulSizeOfData = 0;
|
---|
178 |
|
---|
179 | // get size of data for pszApp/pszKey
|
---|
180 | if (PrfQueryProfileSize(hIni, pszApp, pszKey, &ulSizeOfData))
|
---|
181 | {
|
---|
182 | if (ulSizeOfData)
|
---|
183 | {
|
---|
184 | pData = (PSZ)malloc(ulSizeOfData);
|
---|
185 | if (!PrfQueryProfileData(hIni, pszApp, pszKey, pData, &ulSizeOfData))
|
---|
186 | {
|
---|
187 | free(pData);
|
---|
188 | pData = NULL;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (pcbBuf)
|
---|
194 | *pcbBuf = ulSizeOfData;
|
---|
195 |
|
---|
196 | return (pData);
|
---|
197 | }
|
---|
198 |
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | /*
|
---|
202 | *@@ prfhQueryProfileChar:
|
---|
203 | * this query the first character of profile data.
|
---|
204 | * This is mostly useful with the PM country settings
|
---|
205 | * in OS2.INI:
|
---|
206 | * -- date separator: "PM_National", "sDate"
|
---|
207 | * -- time separator: "PM_National", "sTime"
|
---|
208 | */
|
---|
209 |
|
---|
210 | CHAR prfhQueryProfileChar(HINI hini, // in: INI handle (can be HINI_USER or HINI_SYSTEM)
|
---|
211 | PSZ pszApp, // in: application to query
|
---|
212 | PSZ pszKey, // in: key to query
|
---|
213 | CHAR cDefault) // in: default to return if not found
|
---|
214 | {
|
---|
215 | // CHAR crc = 0;
|
---|
216 | CHAR szTemp[5],
|
---|
217 | szDefault[5];
|
---|
218 | szDefault[0] = cDefault;
|
---|
219 | szDefault[1] = 0;
|
---|
220 | PrfQueryProfileString(HINI_USER, pszApp, pszKey,
|
---|
221 | szDefault,
|
---|
222 | szTemp, sizeof(szTemp)-1);
|
---|
223 | return (szTemp[0]);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | *@@ prfhQueryCountrySettings:
|
---|
228 | * this returns the most frequently used country settings
|
---|
229 | * all at once into a COUNTRYSETTINGS structure (prfh.h).
|
---|
230 | * This data corresponds to the user settings in the
|
---|
231 | * WPS "Country" object (which writes the data in "PM_National"
|
---|
232 | * in OS2.INI).
|
---|
233 | *
|
---|
234 | * In case a key cannot be found, the following (English)
|
---|
235 | * default values are set:
|
---|
236 | * -- ulDateFormat = 0 (English date format, mm.dd.yyyy);
|
---|
237 | * -- ulTimeFormat = 0 (12-hour clock);
|
---|
238 | * -- cDateSep = '/' (date separator);
|
---|
239 | * -- cTimeSep = ':' (time separator);
|
---|
240 | * -- cThousands = ',' (thousands separator).
|
---|
241 | *
|
---|
242 | *@@added V0.9.0 [umoeller]
|
---|
243 | */
|
---|
244 |
|
---|
245 | VOID prfhQueryCountrySettings(PCOUNTRYSETTINGS pcs)
|
---|
246 | {
|
---|
247 | if (pcs)
|
---|
248 | {
|
---|
249 | pcs->ulDateFormat = PrfQueryProfileInt(HINI_USER, "PM_National", "iDate", 0);
|
---|
250 | pcs->ulTimeFormat = PrfQueryProfileInt(HINI_USER, "PM_National", "iTime", 0);
|
---|
251 | pcs->cDateSep = prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');
|
---|
252 | pcs->cTimeSep = prfhQueryProfileChar(HINI_USER, "PM_National", "sTime", ':');
|
---|
253 | pcs->cThousands = prfhQueryProfileChar(HINI_USER, "PM_National", "sThousand", ',');
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | *@@ prfhQueryColor:
|
---|
259 | * returns a system color in OS2.INI's PM_Colors as a LONG.
|
---|
260 | */
|
---|
261 |
|
---|
262 | LONG prfhQueryColor(PSZ pszKeyName, PSZ pszDefault)
|
---|
263 | {
|
---|
264 | CHAR szColor[30];
|
---|
265 | ULONG r, g, b;
|
---|
266 | PrfQueryProfileString(
|
---|
267 | HINI_USER,
|
---|
268 | "PM_Colors",
|
---|
269 | pszKeyName,
|
---|
270 | pszDefault,
|
---|
271 | szColor,
|
---|
272 | sizeof(szColor)-1);
|
---|
273 | sscanf(szColor, "%lu %lu %lu ", &r, &g, &b);
|
---|
274 | return (LONG)(r*0x10000 + g*0x100 + b);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /*
|
---|
278 | *@@ prfhCopyKey:
|
---|
279 | * this copies one key from the given profile and application
|
---|
280 | * to another one.
|
---|
281 | *
|
---|
282 | * pszTargetApp may be in the same profile (and must be
|
---|
283 | * different from pszSourceApp then) or in a different
|
---|
284 | * profile (and can be the same then).
|
---|
285 | *
|
---|
286 | * You must specify all parameters. You cannot specify pszKey
|
---|
287 | * as NULL to have a whole application copied. Use prfhCopyApp
|
---|
288 | * for that.
|
---|
289 | * No check is made for this.
|
---|
290 | *
|
---|
291 | * Returns:
|
---|
292 | * -- 0: no error
|
---|
293 | * -- PRFERR_DATASIZE: couldn't query data size for key
|
---|
294 | * -- PRFERR_MEMORY: couldn't allocate memory
|
---|
295 | * -- PRFERR_READ: couldn't read data from source (PrfQueryProfileData error)
|
---|
296 | * -- PRFERR_WRITE: couldn't write data to target (PrfWriteProfileData error)
|
---|
297 | *
|
---|
298 | *@@added V0.9.0 [umoeller]
|
---|
299 | */
|
---|
300 |
|
---|
301 | ULONG prfhCopyKey(HINI hiniSource, // in: source profile (can be HINI_USER or HINI_SYSTEM)
|
---|
302 | PSZ pszSourceApp, // in: source application
|
---|
303 | PSZ pszKey, // in: source/target key
|
---|
304 | HINI hiniTarget, // in: target profile (can be HINI_USER or HINI_SYSTEM)
|
---|
305 | PSZ pszTargetApp) // in: target app
|
---|
306 | {
|
---|
307 | ULONG ulSizeOfData = 0,
|
---|
308 | ulrc = 0; // return: no error
|
---|
309 |
|
---|
310 | if (PrfQueryProfileSize(hiniSource, pszSourceApp, pszKey, &ulSizeOfData))
|
---|
311 | {
|
---|
312 | PSZ pData = 0;
|
---|
313 |
|
---|
314 | // copy data
|
---|
315 | if (ulSizeOfData == 0)
|
---|
316 | {
|
---|
317 | // data size == 0: this shouldn't really happen,
|
---|
318 | // but if it does, we'll just create a NULL string.
|
---|
319 | // Users have reported that some INI files seem to
|
---|
320 | // contain those "empty" keys. I don't see how these
|
---|
321 | // can exist, but they seem to...
|
---|
322 | pData = (PSZ)malloc(1);
|
---|
323 | *pData = 0;
|
---|
324 | }
|
---|
325 | else
|
---|
326 | pData = (PSZ)malloc(ulSizeOfData);
|
---|
327 |
|
---|
328 | if (pData)
|
---|
329 | {
|
---|
330 | if (PrfQueryProfileData(hiniSource,
|
---|
331 | pszSourceApp,
|
---|
332 | pszKey,
|
---|
333 | pData,
|
---|
334 | &ulSizeOfData))
|
---|
335 | {
|
---|
336 | if (!PrfWriteProfileData(hiniTarget,
|
---|
337 | pszTargetApp,
|
---|
338 | pszKey,
|
---|
339 | pData,
|
---|
340 | ulSizeOfData))
|
---|
341 | ulrc = PRFERR_WRITE;
|
---|
342 | }
|
---|
343 | else
|
---|
344 | ulrc = PRFERR_READ;
|
---|
345 |
|
---|
346 | free(pData);
|
---|
347 | }
|
---|
348 | else
|
---|
349 | ulrc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
350 | }
|
---|
351 | else
|
---|
352 | ulrc = PRFERR_DATASIZE;
|
---|
353 |
|
---|
354 | return (ulrc);
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*
|
---|
358 | *@@ prfhCopyApp:
|
---|
359 | * this copies one key from the given profile and application
|
---|
360 | * to another one.
|
---|
361 | *
|
---|
362 | * You can use this function in several contexts:
|
---|
363 | * -- copy one application within the same profile
|
---|
364 | * (i.e. hiniSource == hiniTarget);
|
---|
365 | * in this case, pszSourceApp must be != pszTargetApp;
|
---|
366 | * -- copy an application from one profile to another
|
---|
367 | * (i.e. hiniSource != hiniTarget);
|
---|
368 | * in this case, pszSourceApp can be == pszTargetApp
|
---|
369 | * (but can be different also).
|
---|
370 | *
|
---|
371 | * WARNING: This does _not_ check for whether the target
|
---|
372 | * application exists already. This has two consequences:
|
---|
373 | * -- existing data will be overwritten without warning;
|
---|
374 | * -- if the existing target application has keys that are
|
---|
375 | * not in the source application, they are not deleted.
|
---|
376 | * As a result, you might end up with more keys than
|
---|
377 | * in the source application.
|
---|
378 | *
|
---|
379 | * So you should delete the target application before
|
---|
380 | * calling this function, like this:
|
---|
381 | + PrfWriteProfileString(hiniTarget, pszTargetApp, NULL, NULL);
|
---|
382 | *
|
---|
383 | * You must specify all parameters. You cannot specify pszApp
|
---|
384 | * as NULL to have a whole profile copied. Use prfhCopyProfile
|
---|
385 | * for that.
|
---|
386 | * No check is made for this.
|
---|
387 | *
|
---|
388 | * Returns:
|
---|
389 | * -- 0: no error
|
---|
390 | * -- PRFERR_KEYSLIST: couldn't query keys for pszSourceApp
|
---|
391 | * -- PRFERR_DATASIZE: couldn't query data size for key
|
---|
392 | * -- PRFERR_MEMORY: couldn't allocate memory
|
---|
393 | * -- PRFERR_READ: couldn't read data from source (PrfQueryProfileData error)
|
---|
394 | * -- PRFERR_WRITE: couldn't write data to target (PrfWriteProfileData error)
|
---|
395 | *
|
---|
396 | *@@added V0.9.0 [umoeller]
|
---|
397 | */
|
---|
398 |
|
---|
399 | ULONG prfhCopyApp(HINI hiniSource, // in: source profile (can be HINI_USER or HINI_SYSTEM)
|
---|
400 | PSZ pszSourceApp, // in: source application
|
---|
401 | HINI hiniTarget, // in: target profile (can be HINI_USER or HINI_SYSTEM)
|
---|
402 | PSZ pszTargetApp, // in: name of pszSourceApp in hiniTarget
|
---|
403 | PSZ pszErrorKey) // out: failing key in case of error; ptr can be NULL
|
---|
404 | {
|
---|
405 | ULONG ulrc;
|
---|
406 | PSZ pszKeysList;
|
---|
407 |
|
---|
408 | if (pszErrorKey)
|
---|
409 | *pszErrorKey = 0;
|
---|
410 |
|
---|
411 | pszKeysList = prfhQueryKeysForApp(hiniSource, pszSourceApp);
|
---|
412 | if (pszKeysList)
|
---|
413 | {
|
---|
414 | PSZ pKey2 = pszKeysList;
|
---|
415 |
|
---|
416 | while (*pKey2 != 0)
|
---|
417 | {
|
---|
418 | // copy this key
|
---|
419 | ulrc = prfhCopyKey(hiniSource,
|
---|
420 | pszSourceApp,
|
---|
421 | pKey2,
|
---|
422 | hiniTarget,
|
---|
423 | pszTargetApp);
|
---|
424 | if (ulrc)
|
---|
425 | {
|
---|
426 | // error: copy failing key to buffer
|
---|
427 | if (pszErrorKey)
|
---|
428 | strcpy(pszErrorKey, pKey2);
|
---|
429 | break;
|
---|
430 | }
|
---|
431 | pKey2 += strlen(pKey2)+1;
|
---|
432 | } // end while (*pKey2 != 0)
|
---|
433 |
|
---|
434 | free (pszKeysList);
|
---|
435 | }
|
---|
436 | else
|
---|
437 | ulrc = PRFERR_KEYSLIST;
|
---|
438 |
|
---|
439 | return (ulrc);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*
|
---|
443 | *@@ prfhSetUserProfile:
|
---|
444 | * calls PrfReset to change the current user
|
---|
445 | * profile (normally OS2.INI) to the specified
|
---|
446 | * INI file.
|
---|
447 | *
|
---|
448 | *@@added V0.9.4 (2000-07-19) [umoeller]
|
---|
449 | */
|
---|
450 |
|
---|
451 | BOOL prfhSetUserProfile(HAB hab,
|
---|
452 | PSZ pszUserProfile) // in: new user profile (.INI)
|
---|
453 | {
|
---|
454 | BOOL brc = FALSE;
|
---|
455 | // find out current profile names
|
---|
456 | PRFPROFILE Profiles;
|
---|
457 | Profiles.cchUserName = Profiles.cchSysName = 0;
|
---|
458 | // first query their file name lengths
|
---|
459 | if (PrfQueryProfile(hab, &Profiles))
|
---|
460 | {
|
---|
461 | // allocate memory for filenames
|
---|
462 | Profiles.pszUserName = (PSZ)malloc(Profiles.cchUserName);
|
---|
463 | Profiles.pszSysName = (PSZ)malloc(Profiles.cchSysName);
|
---|
464 |
|
---|
465 | if ((Profiles.pszSysName) && (Profiles.pszUserName))
|
---|
466 | {
|
---|
467 | // get filenames
|
---|
468 | if (PrfQueryProfile(hab, &Profiles))
|
---|
469 | {
|
---|
470 | // _Pmpf(("Old user profile: %s", Profiles.pszUserName));
|
---|
471 |
|
---|
472 | // change INIs
|
---|
473 | free(Profiles.pszUserName);
|
---|
474 | Profiles.pszUserName = pszUserProfile;
|
---|
475 | Profiles.cchUserName = strlen(pszUserProfile) + 1;
|
---|
476 | brc = PrfReset(hab, &Profiles);
|
---|
477 | free(Profiles.pszSysName);
|
---|
478 | }
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 | return (brc);
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|