1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile nls.c:
|
---|
4 | * contains a few helpers for National Language Support (NLS),
|
---|
5 | * such as printing strings with the format specified by
|
---|
6 | * the "Country" object.
|
---|
7 | *
|
---|
8 | * Usage: All OS/2 programs.
|
---|
9 | *
|
---|
10 | * Function prefixes (new with V0.81):
|
---|
11 | * -- nls* NLS helpers
|
---|
12 | *
|
---|
13 | * This file is new with 0.9.16, but contains functions
|
---|
14 | * formerly in stringh.c.
|
---|
15 | *
|
---|
16 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
17 | * numbering.
|
---|
18 | *
|
---|
19 | *@@header "helpers\nls.h"
|
---|
20 | *@@added V0.9.16 (2001-10-11) [umoeller]
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Copyright (C) 1997-2002 Ulrich Mller.
|
---|
25 | * This file is part of the "XWorkplace helpers" source package.
|
---|
26 | * This is free software; you can redistribute it and/or modify
|
---|
27 | * it under the terms of the GNU General Public License as published
|
---|
28 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
29 | * "COPYING" file of the XWorkplace main distribution.
|
---|
30 | * This program is distributed in the hope that it will be useful,
|
---|
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
33 | * GNU General Public License for more details.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define OS2EMX_PLAIN_CHAR
|
---|
37 | // this is needed for "os2emx.h"; if this is defined,
|
---|
38 | // emx will define PSZ as _signed_ char, otherwise
|
---|
39 | // as unsigned char
|
---|
40 |
|
---|
41 | #define INCL_DOSNLS
|
---|
42 | #define INCL_DOSDATETIME
|
---|
43 | #define INCL_DOSERRORS
|
---|
44 | #define INCL_WINSHELLDATA
|
---|
45 | #include <os2.h>
|
---|
46 |
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <string.h>
|
---|
50 | #include <math.h>
|
---|
51 |
|
---|
52 | // XWP's setup.h replaces strchr and the like, and
|
---|
53 | // we want the originals in here
|
---|
54 | #define DONT_REPLACE_FOR_DBCS
|
---|
55 | #include "setup.h" // code generation and debugging options
|
---|
56 |
|
---|
57 | #include "helpers\nls.h"
|
---|
58 | #include "helpers\prfh.h"
|
---|
59 | #include "helpers\standards.h"
|
---|
60 |
|
---|
61 | #pragma hdrstop
|
---|
62 |
|
---|
63 | /*
|
---|
64 | *@@category: Helpers\National Language Support
|
---|
65 | * See nls.c.
|
---|
66 | */
|
---|
67 |
|
---|
68 | /* ******************************************************************
|
---|
69 | *
|
---|
70 | * DBCS support
|
---|
71 | *
|
---|
72 | ********************************************************************/
|
---|
73 |
|
---|
74 | #define MAX_LEADBYTE 256
|
---|
75 |
|
---|
76 | #pragma pack(1)
|
---|
77 |
|
---|
78 | typedef struct _DBCSVECTOR
|
---|
79 | {
|
---|
80 | BYTE bLow;
|
---|
81 | BYTE bHigh;
|
---|
82 | } DBCSVECTOR;
|
---|
83 |
|
---|
84 | #pragma pack()
|
---|
85 |
|
---|
86 | BOOL G_afLeadByte[MAX_LEADBYTE] = {0};
|
---|
87 | ULONG G_fDBCS = 2; // not queried yet
|
---|
88 | COUNTRYCODE G_cc = { 0, 0 };
|
---|
89 | DBCSVECTOR G_aDBCSVector[8];
|
---|
90 |
|
---|
91 | /*
|
---|
92 | *@@ nlsDBCS:
|
---|
93 | * returns TRUE if the system is currently using DBCS.
|
---|
94 | *
|
---|
95 | *@@added V0.9.19 (2002-06-13) [umoeller]
|
---|
96 | *@@changed V0.9.20 (2002-07-03) [umoeller]: fixed, this never worked
|
---|
97 | */
|
---|
98 |
|
---|
99 | BOOL nlsDBCS(VOID)
|
---|
100 | {
|
---|
101 | APIRET arc;
|
---|
102 |
|
---|
103 | if (G_fDBCS != 2)
|
---|
104 | // already queried:
|
---|
105 | return G_fDBCS;
|
---|
106 |
|
---|
107 | // V0.9.20 (2002-07-03) [umoeller]
|
---|
108 | // assume a non-DBCS system UNLESS the below
|
---|
109 | // loop gives us something meaningful; even
|
---|
110 | // on non-DBCS systems like mine, DosQueryDBCSEnv
|
---|
111 | // does not return an error
|
---|
112 | G_fDBCS = FALSE;
|
---|
113 |
|
---|
114 | if (!(arc = DosQueryDBCSEnv(8 * sizeof(DBCSVECTOR),
|
---|
115 | &G_cc,
|
---|
116 | (PCHAR)G_aDBCSVector)))
|
---|
117 | {
|
---|
118 | int i;
|
---|
119 | for (i = 0;
|
---|
120 | i < 8;
|
---|
121 | ++i)
|
---|
122 | {
|
---|
123 | if ( (G_aDBCSVector[i].bLow)
|
---|
124 | && (G_aDBCSVector[i].bHigh)
|
---|
125 | )
|
---|
126 | {
|
---|
127 | int n;
|
---|
128 | for (n = G_aDBCSVector[i].bLow;
|
---|
129 | n <= G_aDBCSVector[i].bHigh;
|
---|
130 | ++n)
|
---|
131 | G_afLeadByte[n] = TRUE;
|
---|
132 | G_fDBCS = TRUE;
|
---|
133 | }
|
---|
134 | else
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | return G_fDBCS;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | *@@ nlsQueryDBCSChar:
|
---|
144 | * returns the type of the DBCS character with
|
---|
145 | * the given index. Note that the index is the
|
---|
146 | * DBCS character index, not just the array
|
---|
147 | * index into the CHAR array.
|
---|
148 | *
|
---|
149 | * Returns:
|
---|
150 | *
|
---|
151 | * -- TYPE_SBCS: ulOfs is single byte.
|
---|
152 | *
|
---|
153 | * -- TYPE_DBCS_1ST: ulOfs is a double-byte lead char.
|
---|
154 | *
|
---|
155 | * -- TYPE_DBCS_2ND: ulOfs is a double-byte trail char.
|
---|
156 | *
|
---|
157 | * Preconditions:
|
---|
158 | *
|
---|
159 | * -- nlsDBCS must have been called to initialize our
|
---|
160 | * globals, and must have returned TRUE.
|
---|
161 | *
|
---|
162 | *@@added V0.9.19 (2002-06-13) [umoeller]
|
---|
163 | */
|
---|
164 |
|
---|
165 | ULONG nlsQueryDBCSChar(PCSZ pcszString,
|
---|
166 | ULONG ulOfs)
|
---|
167 |
|
---|
168 | {
|
---|
169 | ULONG ulDBCSType = TYPE_SBCS;
|
---|
170 | ULONG i;
|
---|
171 |
|
---|
172 | for (i = 0;
|
---|
173 | i <= ulOfs;
|
---|
174 | ++i)
|
---|
175 | {
|
---|
176 | switch (ulDBCSType)
|
---|
177 | {
|
---|
178 | case TYPE_SBCS:
|
---|
179 | case TYPE_DBCS_2ND:
|
---|
180 | ulDBCSType = G_afLeadByte[pcszString[i]];
|
---|
181 | break;
|
---|
182 |
|
---|
183 | case TYPE_DBCS_1ST :
|
---|
184 | ulDBCSType = TYPE_DBCS_2ND;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return ulDBCSType;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | *@@ nlschr:
|
---|
194 | * replacement for strchr with DBCS support.
|
---|
195 | *
|
---|
196 | * If the system is not running with DBCS,
|
---|
197 | * this calls plain strchr automatically.
|
---|
198 | *
|
---|
199 | *@@added V0.9.19 (2002-06-13) [umoeller]
|
---|
200 | *@@changed V0.9.20 (2002-07-22) [umoeller]: optimized
|
---|
201 | *@@changed V0.9.20 (2002-07-22) [lafaix]: optimized
|
---|
202 | */
|
---|
203 |
|
---|
204 | PSZ nlschr(PCSZ p, char c)
|
---|
205 | {
|
---|
206 | PCSZ p2;
|
---|
207 | ULONG ulDBCSType = TYPE_SBCS;
|
---|
208 |
|
---|
209 | if (!nlsDBCS())
|
---|
210 | // not DBCS:
|
---|
211 | return strchr(p, c);
|
---|
212 |
|
---|
213 | // we're on DBCS:
|
---|
214 |
|
---|
215 | // we can't find c if it is a leading byte
|
---|
216 | if (G_afLeadByte[c] != TYPE_SBCS)
|
---|
217 | return NULL;
|
---|
218 |
|
---|
219 | for (p2 = p;
|
---|
220 | *p2;
|
---|
221 | ++p2)
|
---|
222 | {
|
---|
223 | // check _previous_ DBCS type and refresh
|
---|
224 | // DBCS type accordingly
|
---|
225 | switch (ulDBCSType)
|
---|
226 | {
|
---|
227 | case TYPE_SBCS:
|
---|
228 | case TYPE_DBCS_2ND:
|
---|
229 | ulDBCSType = G_afLeadByte[*p2];
|
---|
230 | // we can safely do the test here (and skip rechecking
|
---|
231 | // the type) because c can't possibly be a leading byte
|
---|
232 | // V0.9.20 (2002-07-22) [lafaix]
|
---|
233 | if (*p2 == c)
|
---|
234 | return (PSZ)p2;
|
---|
235 | break;
|
---|
236 |
|
---|
237 | case TYPE_DBCS_1ST :
|
---|
238 | ulDBCSType = TYPE_DBCS_2ND;
|
---|
239 | break;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /* old code V0.9.20 (2002-07-22) [umoeller]
|
---|
244 | // we're on DBCS:
|
---|
245 | for (p2 = p;
|
---|
246 | *p2;
|
---|
247 | ++p2)
|
---|
248 | {
|
---|
249 | if (*p2 == c)
|
---|
250 | {
|
---|
251 | // match: return this only if it's SBCS;
|
---|
252 | // if it's a DBCS lead char, skip it
|
---|
253 | switch (ulDBCS = nlsQueryDBCSChar(p, p2 - p))
|
---|
254 | {
|
---|
255 | case TYPE_SBCS:
|
---|
256 | return (PSZ)p2;
|
---|
257 |
|
---|
258 | case TYPE_DBCS_1ST:
|
---|
259 | ++p2;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | */
|
---|
264 |
|
---|
265 | return NULL;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | *@@ nlsrchr:
|
---|
270 | * replacement for strrchr with DBCS support.
|
---|
271 | *
|
---|
272 | * If the system is not running with DBCS,
|
---|
273 | * this calls plain strrchr automatically.
|
---|
274 | *
|
---|
275 | *@@added V0.9.19 (2002-06-13) [umoeller]
|
---|
276 | *@@changed V0.9.20 (2002-07-22) [lafaix]: optimized
|
---|
277 | */
|
---|
278 |
|
---|
279 | PSZ nlsrchr(PCSZ p, char c)
|
---|
280 | {
|
---|
281 | PCSZ p2,
|
---|
282 | pLast = NULL;
|
---|
283 | ULONG ulDBCSType = TYPE_SBCS;
|
---|
284 |
|
---|
285 | if (!nlsDBCS())
|
---|
286 | // not DBCS:
|
---|
287 | return strrchr(p, c);
|
---|
288 |
|
---|
289 | // we're on DBCS:
|
---|
290 |
|
---|
291 | // we can't find c if it is a leading byte
|
---|
292 | if (G_afLeadByte[c] != TYPE_SBCS)
|
---|
293 | return NULL;
|
---|
294 |
|
---|
295 | for (p2 = p;
|
---|
296 | *p2;
|
---|
297 | ++p2)
|
---|
298 | {
|
---|
299 | // check _previous_ DBCS type and refresh
|
---|
300 | // DBCS type accordingly
|
---|
301 | switch (ulDBCSType)
|
---|
302 | {
|
---|
303 | case TYPE_SBCS:
|
---|
304 | case TYPE_DBCS_2ND:
|
---|
305 | ulDBCSType = G_afLeadByte[*p2];
|
---|
306 | if (*p2 == c)
|
---|
307 | pLast = p2;
|
---|
308 | break;
|
---|
309 |
|
---|
310 | case TYPE_DBCS_1ST :
|
---|
311 | ulDBCSType = TYPE_DBCS_2ND;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | return (PSZ)pLast;
|
---|
317 |
|
---|
318 | // old code V0.9.20 (2002-07-22) [lafaix]
|
---|
319 | /*
|
---|
320 | ulLength = strlen(p);
|
---|
321 | for (p2 = p + ulLength - 1;
|
---|
322 | p2 >= p;
|
---|
323 | --p2)
|
---|
324 | {
|
---|
325 | if (*p2 == c)
|
---|
326 | {
|
---|
327 | // match: return this only if it's SBCS;
|
---|
328 | // if it's a DBCS trail char, skip it
|
---|
329 | switch (ulDBCS = nlsQueryDBCSChar(p, p2 - p))
|
---|
330 | {
|
---|
331 | case TYPE_SBCS:
|
---|
332 | return (PSZ)p2;
|
---|
333 |
|
---|
334 | case TYPE_DBCS_2ND:
|
---|
335 | --p2;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | return NULL;
|
---|
341 | */
|
---|
342 | }
|
---|
343 |
|
---|
344 | /* ******************************************************************
|
---|
345 | *
|
---|
346 | * Country-dependent formatting
|
---|
347 | *
|
---|
348 | ********************************************************************/
|
---|
349 |
|
---|
350 | /*
|
---|
351 | *@@ nlsQueryCountrySettings:
|
---|
352 | * this returns the most frequently used country settings
|
---|
353 | * all at once into a COUNTRYSETTINGS structure (prfh.h).
|
---|
354 | * This data corresponds to the user settings in the
|
---|
355 | * WPS "Country" object (which writes the data in "PM_National"
|
---|
356 | * in OS2.INI).
|
---|
357 | *
|
---|
358 | * In case a key cannot be found, the following (English)
|
---|
359 | * default values are set:
|
---|
360 | * -- ulDateFormat = 0 (English date format, mm.dd.yyyy);
|
---|
361 | * -- ulTimeFormat = 0 (12-hour clock);
|
---|
362 | * -- cDateSep = '/' (date separator);
|
---|
363 | * -- cTimeSep = ':' (time separator);
|
---|
364 | * -- cDecimal = '.' (decimal separator).
|
---|
365 | * -- cThousands = ',' (thousands separator).
|
---|
366 | *
|
---|
367 | *@@added V0.9.0 [umoeller]
|
---|
368 | *@@changed V0.9.7 (2000-12-02) [umoeller]: added cDecimal
|
---|
369 | */
|
---|
370 |
|
---|
371 | VOID nlsQueryCountrySettings(PCOUNTRYSETTINGS pcs)
|
---|
372 | {
|
---|
373 | if (pcs)
|
---|
374 | {
|
---|
375 | pcs->ulDateFormat = PrfQueryProfileInt(HINI_USER,
|
---|
376 | (PSZ)PMINIAPP_NATIONAL,
|
---|
377 | "iDate",
|
---|
378 | 0);
|
---|
379 | pcs->ulTimeFormat = PrfQueryProfileInt(HINI_USER,
|
---|
380 | (PSZ)PMINIAPP_NATIONAL,
|
---|
381 | "iTime",
|
---|
382 | 0);
|
---|
383 | pcs->cDateSep = prfhQueryProfileChar(HINI_USER,
|
---|
384 | (PSZ)PMINIAPP_NATIONAL,
|
---|
385 | "sDate",
|
---|
386 | '/');
|
---|
387 | pcs->cTimeSep = prfhQueryProfileChar(HINI_USER,
|
---|
388 | (PSZ)PMINIAPP_NATIONAL,
|
---|
389 | "sTime",
|
---|
390 | ':');
|
---|
391 | pcs->cDecimal = prfhQueryProfileChar(HINI_USER,
|
---|
392 | (PSZ)PMINIAPP_NATIONAL,
|
---|
393 | "sDecimal",
|
---|
394 | '.');
|
---|
395 | pcs->cThousands = prfhQueryProfileChar(HINI_USER,
|
---|
396 | (PSZ)PMINIAPP_NATIONAL,
|
---|
397 | "sThousand",
|
---|
398 | ',');
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*
|
---|
403 | *@@ nlsThousandsULong:
|
---|
404 | * converts a ULONG into a decimal string, while
|
---|
405 | * inserting thousands separators into it. Specify
|
---|
406 | * the separator character in cThousands.
|
---|
407 | *
|
---|
408 | * Returns pszTarget so you can use it directly
|
---|
409 | * with sprintf and the "%s" flag.
|
---|
410 | *
|
---|
411 | * For cThousands, you should use the data in
|
---|
412 | * OS2.INI ("PM_National" application), which is
|
---|
413 | * always set according to the "Country" object.
|
---|
414 | * You can use nlsQueryCountrySettings to
|
---|
415 | * retrieve this setting.
|
---|
416 | *
|
---|
417 | * Use nlsThousandsDouble for "double" values.
|
---|
418 | *
|
---|
419 | *@@changed V0.9.20 (2002-07-03) [umoeller]: optimized
|
---|
420 | */
|
---|
421 |
|
---|
422 | PSZ nlsThousandsULong(PSZ pszTarget, // out: decimal as string
|
---|
423 | ULONG ul, // in: decimal to convert
|
---|
424 | CHAR cThousands) // in: separator char (e.g. '.')
|
---|
425 | {
|
---|
426 | USHORT ust, uss, usc;
|
---|
427 | CHAR szTemp[40];
|
---|
428 | usc = sprintf(szTemp, "%lu", ul); // V0.9.20 (2002-07-03) [umoeller]
|
---|
429 |
|
---|
430 | ust = 0;
|
---|
431 | // usc = strlen(szTemp);
|
---|
432 | for (uss = 0; uss < usc; uss++)
|
---|
433 | {
|
---|
434 | if (uss)
|
---|
435 | if (((usc - uss) % 3) == 0)
|
---|
436 | {
|
---|
437 | pszTarget[ust] = cThousands;
|
---|
438 | ust++;
|
---|
439 | }
|
---|
440 | pszTarget[ust] = szTemp[uss];
|
---|
441 | ust++;
|
---|
442 | }
|
---|
443 | pszTarget[ust] = '\0';
|
---|
444 |
|
---|
445 | return pszTarget;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * strhThousandsULong:
|
---|
450 | * wrapper around nlsThousandsULong for those
|
---|
451 | * who used the XFLDR.DLL export.
|
---|
452 | *
|
---|
453 | *added V0.9.16 (2001-10-11) [umoeller]
|
---|
454 | */
|
---|
455 |
|
---|
456 | PSZ APIENTRY strhThousandsULong(PSZ pszTarget, // out: decimal as string
|
---|
457 | ULONG ul, // in: decimal to convert
|
---|
458 | CHAR cThousands) // in: separator char (e.g. '.')
|
---|
459 | {
|
---|
460 | return nlsThousandsULong(pszTarget, ul, cThousands);
|
---|
461 | }
|
---|
462 |
|
---|
463 | /*
|
---|
464 | *@@ nlsThousandsDouble:
|
---|
465 | * like nlsThousandsULong, but for a "double"
|
---|
466 | * value. Note that after-comma values are truncated.
|
---|
467 | *
|
---|
468 | *@@changed V0.9.20 (2002-07-03) [umoeller]: optimized
|
---|
469 | */
|
---|
470 |
|
---|
471 | PSZ nlsThousandsDouble(PSZ pszTarget,
|
---|
472 | double dbl,
|
---|
473 | CHAR cThousands)
|
---|
474 | {
|
---|
475 | USHORT ust, uss, usc;
|
---|
476 | CHAR szTemp[40];
|
---|
477 | usc = sprintf(szTemp, "%.0f", floor(dbl)); // V0.9.20 (2002-07-03) [umoeller]
|
---|
478 |
|
---|
479 | ust = 0;
|
---|
480 | // usc = strlen(szTemp);
|
---|
481 | for (uss = 0; uss < usc; uss++)
|
---|
482 | {
|
---|
483 | if (uss)
|
---|
484 | if (((usc - uss) % 3) == 0)
|
---|
485 | {
|
---|
486 | pszTarget[ust] = cThousands;
|
---|
487 | ust++;
|
---|
488 | }
|
---|
489 | pszTarget[ust] = szTemp[uss];
|
---|
490 | ust++;
|
---|
491 | }
|
---|
492 | pszTarget[ust] = '\0';
|
---|
493 |
|
---|
494 | return pszTarget;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*
|
---|
498 | *@@ nlsVariableDouble:
|
---|
499 | * like nlsThousandsULong, but for a "double" value, and
|
---|
500 | * with a variable number of decimal places depending on the
|
---|
501 | * size of the quantity.
|
---|
502 | *
|
---|
503 | *@@added V0.9.6 (2000-11-12) [pr]
|
---|
504 | *@@changed V0.9.20 (2002-07-03) [umoeller]: now using PCSZ pcszUnits
|
---|
505 | */
|
---|
506 |
|
---|
507 | PSZ nlsVariableDouble(PSZ pszTarget,
|
---|
508 | double dbl,
|
---|
509 | PCSZ pcszUnits,
|
---|
510 | CHAR cThousands)
|
---|
511 | {
|
---|
512 | if (dbl < 100.0)
|
---|
513 | sprintf(pszTarget, "%.2f%s", dbl, pcszUnits);
|
---|
514 | else
|
---|
515 | if (dbl < 1000.0)
|
---|
516 | sprintf(pszTarget, "%.1f%s", dbl, pcszUnits);
|
---|
517 | else
|
---|
518 | strcat(nlsThousandsDouble(pszTarget, dbl, cThousands),
|
---|
519 | pcszUnits);
|
---|
520 |
|
---|
521 | return pszTarget;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*
|
---|
525 | *@@ nlsFileDate:
|
---|
526 | * converts file date data to a string (to pszBuf).
|
---|
527 | * You can pass any FDATE structure to this function,
|
---|
528 | * which are returned in those FILEFINDBUF* or
|
---|
529 | * FILESTATUS* structs by the Dos* functions.
|
---|
530 | *
|
---|
531 | * ulDateFormat is the PM setting for the date format,
|
---|
532 | * as set in the "Country" object, and can be queried using
|
---|
533 | + PrfQueryProfileInt(HINI_USER, "PM_National", "iDate", 0);
|
---|
534 | *
|
---|
535 | * meaning:
|
---|
536 | * -- 0 mm.dd.yyyy (English)
|
---|
537 | * -- 1 dd.mm.yyyy (e.g. German)
|
---|
538 | * -- 2 yyyy.mm.dd (Japanese, ISO)
|
---|
539 | * -- 3 yyyy.dd.mm
|
---|
540 | *
|
---|
541 | * cDateSep is used as a date separator (e.g. '.').
|
---|
542 | * This can be queried using:
|
---|
543 | + prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');
|
---|
544 | *
|
---|
545 | * Alternatively, you can query all the country settings
|
---|
546 | * at once using nlsQueryCountrySettings (prfh.c).
|
---|
547 | *
|
---|
548 | *@@changed V0.9.0 (99-11-07) [umoeller]: now calling nlsDateTime
|
---|
549 | */
|
---|
550 |
|
---|
551 | VOID nlsFileDate(PSZ pszBuf, // out: string returned
|
---|
552 | FDATE *pfDate, // in: date information
|
---|
553 | ULONG ulDateFormat, // in: date format (0-3)
|
---|
554 | CHAR cDateSep) // in: date separator (e.g. '.')
|
---|
555 | {
|
---|
556 | DATETIME dt;
|
---|
557 | dt.day = pfDate->day;
|
---|
558 | dt.month = pfDate->month;
|
---|
559 | dt.year = pfDate->year + 1980;
|
---|
560 |
|
---|
561 | nlsDateTime(pszBuf,
|
---|
562 | NULL, // no time
|
---|
563 | &dt,
|
---|
564 | ulDateFormat,
|
---|
565 | cDateSep,
|
---|
566 | 0, 0); // no time
|
---|
567 | }
|
---|
568 |
|
---|
569 | /*
|
---|
570 | *@@ nlsFileTime:
|
---|
571 | * converts file time data to a string (to pszBuf).
|
---|
572 | * You can pass any FTIME structure to this function,
|
---|
573 | * which are returned in those FILEFINDBUF* or
|
---|
574 | * FILESTATUS* structs by the Dos* functions.
|
---|
575 | *
|
---|
576 | * ulTimeFormat is the PM setting for the time format,
|
---|
577 | * as set in the "Country" object, and can be queried using
|
---|
578 | + PrfQueryProfileInt(HINI_USER, "PM_National", "iTime", 0);
|
---|
579 | * meaning:
|
---|
580 | * -- 0 12-hour clock
|
---|
581 | * -- >0 24-hour clock
|
---|
582 | *
|
---|
583 | * cDateSep is used as a time separator (e.g. ':').
|
---|
584 | * This can be queried using:
|
---|
585 | + prfhQueryProfileChar(HINI_USER, "PM_National", "sTime", ':');
|
---|
586 | *
|
---|
587 | * Alternatively, you can query all the country settings
|
---|
588 | * at once using nlsQueryCountrySettings (prfh.c).
|
---|
589 | *
|
---|
590 | *@@changed V0.8.5 (99-03-15) [umoeller]: fixed 12-hour crash
|
---|
591 | *@@changed V0.9.0 (99-11-07) [umoeller]: now calling nlsDateTime
|
---|
592 | */
|
---|
593 |
|
---|
594 | VOID nlsFileTime(PSZ pszBuf, // out: string returned
|
---|
595 | FTIME *pfTime, // in: time information
|
---|
596 | ULONG ulTimeFormat, // in: 24-hour time format (0 or 1)
|
---|
597 | CHAR cTimeSep) // in: time separator (e.g. ':')
|
---|
598 | {
|
---|
599 | DATETIME dt;
|
---|
600 | dt.hours = pfTime->hours;
|
---|
601 | dt.minutes = pfTime->minutes;
|
---|
602 | dt.seconds = pfTime->twosecs * 2;
|
---|
603 |
|
---|
604 | nlsDateTime(NULL, // no date
|
---|
605 | pszBuf,
|
---|
606 | &dt,
|
---|
607 | 0, 0, // no date
|
---|
608 | ulTimeFormat,
|
---|
609 | cTimeSep);
|
---|
610 | }
|
---|
611 |
|
---|
612 | /*
|
---|
613 | *@@ nlsDateTime:
|
---|
614 | * converts Control Program DATETIME info
|
---|
615 | * into two strings. See nlsFileDate and nlsFileTime
|
---|
616 | * for more detailed parameter descriptions.
|
---|
617 | *
|
---|
618 | *@@added V0.9.0 (99-11-07) [umoeller]
|
---|
619 | *@@changed V0.9.16 (2001-12-05) [pr]: fixed AM/PM hour bug
|
---|
620 | *@@changed V0.9.18 (2002-02-13) [umoeller]: fixed AM/PM hour bug fix
|
---|
621 | */
|
---|
622 |
|
---|
623 | VOID nlsDateTime(PSZ pszDate, // out: date string returned (can be NULL)
|
---|
624 | PSZ pszTime, // out: time string returned (can be NULL)
|
---|
625 | DATETIME *pDateTime, // in: date/time information
|
---|
626 | ULONG ulDateFormat, // in: date format (0-3); see nlsFileDate
|
---|
627 | CHAR cDateSep, // in: date separator (e.g. '.')
|
---|
628 | ULONG ulTimeFormat, // in: 24-hour time format (0 or 1); see nlsFileTime
|
---|
629 | CHAR cTimeSep) // in: time separator (e.g. ':')
|
---|
630 | {
|
---|
631 | if (pszDate)
|
---|
632 | {
|
---|
633 | switch (ulDateFormat)
|
---|
634 | {
|
---|
635 | case 0: // mm.dd.yyyy (English)
|
---|
636 | sprintf(pszDate, "%02d%c%02d%c%04d",
|
---|
637 | pDateTime->month,
|
---|
638 | cDateSep,
|
---|
639 | pDateTime->day,
|
---|
640 | cDateSep,
|
---|
641 | pDateTime->year);
|
---|
642 | break;
|
---|
643 |
|
---|
644 | case 1: // dd.mm.yyyy (e.g. German)
|
---|
645 | sprintf(pszDate, "%02d%c%02d%c%04d",
|
---|
646 | pDateTime->day,
|
---|
647 | cDateSep,
|
---|
648 | pDateTime->month,
|
---|
649 | cDateSep,
|
---|
650 | pDateTime->year);
|
---|
651 | break;
|
---|
652 |
|
---|
653 | case 2: // yyyy.mm.dd (Japanese)
|
---|
654 | sprintf(pszDate, "%04d%c%02d%c%02d",
|
---|
655 | pDateTime->year,
|
---|
656 | cDateSep,
|
---|
657 | pDateTime->month,
|
---|
658 | cDateSep,
|
---|
659 | pDateTime->day);
|
---|
660 | break;
|
---|
661 |
|
---|
662 | default: // yyyy.dd.mm
|
---|
663 | sprintf(pszDate, "%04d%c%02d%c%02d",
|
---|
664 | pDateTime->year,
|
---|
665 | cDateSep,
|
---|
666 | pDateTime->day,
|
---|
667 | cDateSep,
|
---|
668 | pDateTime->month);
|
---|
669 | break;
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (pszTime)
|
---|
674 | {
|
---|
675 | if (ulTimeFormat == 0)
|
---|
676 | {
|
---|
677 | // for 12-hour clock, we need additional INI data
|
---|
678 | CHAR szAMPM[10] = "err";
|
---|
679 |
|
---|
680 | if (pDateTime->hours >= 12) // V0.9.16 (2001-12-05) [pr] if (pDateTime->hours > 12)
|
---|
681 | {
|
---|
682 | // yeah cool Paul, now we get 00:20 PM if it's 20 past noon
|
---|
683 | // V0.9.18 (2002-02-13) [umoeller]
|
---|
684 | ULONG ulHours;
|
---|
685 | if (!(ulHours = pDateTime->hours % 12))
|
---|
686 | ulHours = 12;
|
---|
687 |
|
---|
688 | // >= 12h: PM.
|
---|
689 | PrfQueryProfileString(HINI_USER,
|
---|
690 | "PM_National",
|
---|
691 | "s2359", // key
|
---|
692 | "PM", // default
|
---|
693 | szAMPM, sizeof(szAMPM)-1);
|
---|
694 | sprintf(pszTime, "%02d%c%02d%c%02d %s",
|
---|
695 | // leave 12 == 12 (not 0)
|
---|
696 | ulHours,
|
---|
697 | cTimeSep,
|
---|
698 | pDateTime->minutes,
|
---|
699 | cTimeSep,
|
---|
700 | pDateTime->seconds,
|
---|
701 | szAMPM);
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | // < 12h: AM
|
---|
706 | PrfQueryProfileString(HINI_USER,
|
---|
707 | "PM_National",
|
---|
708 | "s1159", // key
|
---|
709 | "AM", // default
|
---|
710 | szAMPM, sizeof(szAMPM)-1);
|
---|
711 | sprintf(pszTime, "%02d%c%02d%c%02d %s",
|
---|
712 | pDateTime->hours,
|
---|
713 | cTimeSep,
|
---|
714 | pDateTime->minutes,
|
---|
715 | cTimeSep,
|
---|
716 | pDateTime->seconds,
|
---|
717 | szAMPM);
|
---|
718 | }
|
---|
719 | }
|
---|
720 | else
|
---|
721 | // 24-hour clock
|
---|
722 | sprintf(pszTime, "%02d%c%02d%c%02d",
|
---|
723 | pDateTime->hours,
|
---|
724 | cTimeSep,
|
---|
725 | pDateTime->minutes,
|
---|
726 | cTimeSep,
|
---|
727 | pDateTime->seconds);
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | /*
|
---|
732 | * strhDateTime:
|
---|
733 | * wrapper around nlsDateTime for those who used
|
---|
734 | * the XFLDR.DLL export.
|
---|
735 | */
|
---|
736 |
|
---|
737 | VOID APIENTRY strhDateTime(PSZ pszDate, // out: date string returned (can be NULL)
|
---|
738 | PSZ pszTime, // out: time string returned (can be NULL)
|
---|
739 | DATETIME *pDateTime, // in: date/time information
|
---|
740 | ULONG ulDateFormat, // in: date format (0-3); see nlsFileDate
|
---|
741 | CHAR cDateSep, // in: date separator (e.g. '.')
|
---|
742 | ULONG ulTimeFormat, // in: 24-hour time format (0 or 1); see nlsFileTime
|
---|
743 | CHAR cTimeSep) // in: time separator (e.g. ':')
|
---|
744 | {
|
---|
745 | nlsDateTime(pszDate,
|
---|
746 | pszTime,
|
---|
747 | pDateTime,
|
---|
748 | ulDateFormat,
|
---|
749 | cDateSep,
|
---|
750 | ulTimeFormat,
|
---|
751 | cTimeSep);
|
---|
752 | }
|
---|
753 |
|
---|
754 | CHAR G_szUpperMap[257];
|
---|
755 | BOOL G_fUpperMapInited = FALSE;
|
---|
756 |
|
---|
757 | /*
|
---|
758 | *@@ InitUpperMap:
|
---|
759 | * initializes the case map for nlsUpper.
|
---|
760 | *
|
---|
761 | *@@added V0.9.20 (2002-07-25) [umoeller]
|
---|
762 | */
|
---|
763 |
|
---|
764 | STATIC VOID InitUpperMap(VOID)
|
---|
765 | {
|
---|
766 | ULONG ul;
|
---|
767 | COUNTRYCODE cc;
|
---|
768 | BOOL fDBCS = nlsDBCS();
|
---|
769 |
|
---|
770 | for (ul = 0;
|
---|
771 | ul < sizeof(G_szUpperMap);
|
---|
772 | ++ul)
|
---|
773 | {
|
---|
774 | G_szUpperMap[ul] = (CHAR)ul;
|
---|
775 |
|
---|
776 | if ( (fDBCS)
|
---|
777 | && (G_afLeadByte[ul] != TYPE_SBCS)
|
---|
778 | )
|
---|
779 | G_szUpperMap[ul] = ' ';
|
---|
780 | }
|
---|
781 |
|
---|
782 | G_szUpperMap[256] = '\0';
|
---|
783 |
|
---|
784 | cc.country = 0; // use system country code
|
---|
785 | cc.codepage = 0; // use process default codepage
|
---|
786 | DosMapCase(255,
|
---|
787 | &cc,
|
---|
788 | G_szUpperMap + 1);
|
---|
789 |
|
---|
790 | G_fUpperMapInited = TRUE;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /*
|
---|
794 | *@@ nlsUpper:
|
---|
795 | * quick hack for upper-casing a string.
|
---|
796 | *
|
---|
797 | * This now returns the length of the given string always
|
---|
798 | * (V0.9.20).
|
---|
799 | *
|
---|
800 | * Remarks:
|
---|
801 | *
|
---|
802 | * -- On the first call, we build a case map table by
|
---|
803 | * calling DosMapCase with the default system country
|
---|
804 | * code and the process's codepage. Since DosMapCase
|
---|
805 | * is terribly slow with all the 16-bit thunking
|
---|
806 | * involved, we can then use our table without having
|
---|
807 | * to use the API ever again.
|
---|
808 | *
|
---|
809 | * -- As a result, if the process codepage changes for
|
---|
810 | * any reason, this function will not pick up the
|
---|
811 | * change.
|
---|
812 | *
|
---|
813 | * -- This has provisions for DBCS, which hopefully
|
---|
814 | * work.
|
---|
815 | *
|
---|
816 | *@@added V0.9.16 (2001-10-25) [umoeller]
|
---|
817 | *@@changed V0.9.20 (2002-07-25) [umoeller]: speedup, changed prototype
|
---|
818 | */
|
---|
819 |
|
---|
820 | ULONG nlsUpper(PSZ psz) // in/out: string
|
---|
821 | {
|
---|
822 | ULONG ul = 0;
|
---|
823 |
|
---|
824 | if (!G_fUpperMapInited)
|
---|
825 | InitUpperMap();
|
---|
826 |
|
---|
827 | if (psz)
|
---|
828 | {
|
---|
829 | PSZ p = psz;
|
---|
830 |
|
---|
831 | while (*p++ = G_szUpperMap[*p])
|
---|
832 | ++ul;
|
---|
833 | }
|
---|
834 |
|
---|
835 | return ul;
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|