Updated Slovenian resources for (new) version of progman.
[wine/multimedia.git] / ole / ole2nls.c
blobb71224362ff6a2caad86da5baecd1d4f2494a819
1 /*
2 * National Language Support library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 David Lee Lambert
6 * Copyright 2000 Julio César Gázquez
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <string.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <locale.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winternl.h"
37 #include "wine/unicode.h"
38 #include "winver.h"
39 #include "winnls.h"
40 #include "winreg.h"
41 #include "winerror.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(nls);
47 /******************************************************************************
48 * ConvertDefaultLocale (KERNEL32.@)
50 LCID WINAPI ConvertDefaultLocale (LCID lcid)
51 { switch (lcid)
52 { case LOCALE_SYSTEM_DEFAULT:
53 return GetSystemDefaultLCID();
54 case LOCALE_USER_DEFAULT:
55 return GetUserDefaultLCID();
56 case LOCALE_NEUTRAL:
57 return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
59 return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
63 /******************************************************************************
64 * IsValidLocale [KERNEL32.@]
66 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
68 /* check if language is registered in the kernel32 resources */
69 if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
70 return FALSE;
71 else
72 return TRUE;
75 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
76 LPCWSTR name, WORD LangID, LONG lParam)
78 CHAR bufA[20];
79 WCHAR bufW[20];
80 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
81 sprintf(bufA, "%08x", (UINT)LangID);
82 MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
83 return lpfnLocaleEnum(bufW);
86 /******************************************************************************
87 * EnumSystemLocalesW [KERNEL32.@]
89 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
90 DWORD flags )
92 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
94 EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
95 (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
96 (LONG)lpfnLocaleEnum);
98 return TRUE;
101 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
102 LPCSTR name, WORD LangID, LONG lParam)
104 CHAR bufA[20];
105 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
106 sprintf(bufA, "%08x", (UINT)LangID);
107 return lpfnLocaleEnum(bufA);
110 /******************************************************************************
111 * EnumSystemLocalesA [KERNEL32.@]
113 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
114 DWORD flags)
116 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
118 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
119 (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
120 (LONG)lpfnLocaleEnum);
122 return TRUE;
125 /***********************************************************************
126 * VerLanguageNameA [KERNEL32.@]
128 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
130 if(!szLang)
131 return 0;
133 return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
136 /***********************************************************************
137 * VerLanguageNameW [KERNEL32.@]
139 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
141 if(!szLang)
142 return 0;
144 return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
148 static const unsigned char LCM_Unicode_LUT[] = {
149 6 , 3, /* - 1 */
150 6 , 4, /* - 2 */
151 6 , 5, /* - 3 */
152 6 , 6, /* - 4 */
153 6 , 7, /* - 5 */
154 6 , 8, /* - 6 */
155 6 , 9, /* - 7 */
156 6 , 10, /* - 8 */
157 7 , 5, /* - 9 */
158 7 , 6, /* - 10 */
159 7 , 7, /* - 11 */
160 7 , 8, /* - 12 */
161 7 , 9, /* - 13 */
162 6 , 11, /* - 14 */
163 6 , 12, /* - 15 */
164 6 , 13, /* - 16 */
165 6 , 14, /* - 17 */
166 6 , 15, /* - 18 */
167 6 , 16, /* - 19 */
168 6 , 17, /* - 20 */
169 6 , 18, /* - 21 */
170 6 , 19, /* - 22 */
171 6 , 20, /* - 23 */
172 6 , 21, /* - 24 */
173 6 , 22, /* - 25 */
174 6 , 23, /* - 26 */
175 6 , 24, /* - 27 */
176 6 , 25, /* - 28 */
177 6 , 26, /* - 29 */
178 6 , 27, /* - 30 */
179 6 , 28, /* - 31 */
180 7 , 2, /* - 32 */
181 7 , 28, /* ! - 33 */
182 7 , 29, /* " - 34 */ /* " */
183 7 , 31, /* # - 35 */
184 7 , 33, /* $ - 36 */
185 7 , 35, /* % - 37 */
186 7 , 37, /* & - 38 */
187 6 , 128, /* ' - 39 */
188 7 , 39, /* ( - 40 */
189 7 , 42, /* ) - 41 */
190 7 , 45, /* * - 42 */
191 8 , 3, /* + - 43 */
192 7 , 47, /* , - 44 */
193 6 , 130, /* - - 45 */
194 7 , 51, /* . - 46 */
195 7 , 53, /* / - 47 */
196 12 , 3, /* 0 - 48 */
197 12 , 33, /* 1 - 49 */
198 12 , 51, /* 2 - 50 */
199 12 , 70, /* 3 - 51 */
200 12 , 88, /* 4 - 52 */
201 12 , 106, /* 5 - 53 */
202 12 , 125, /* 6 - 54 */
203 12 , 144, /* 7 - 55 */
204 12 , 162, /* 8 - 56 */
205 12 , 180, /* 9 - 57 */
206 7 , 55, /* : - 58 */
207 7 , 58, /* ; - 59 */
208 8 , 14, /* < - 60 */
209 8 , 18, /* = - 61 */
210 8 , 20, /* > - 62 */
211 7 , 60, /* ? - 63 */
212 7 , 62, /* @ - 64 */
213 14 , 2, /* A - 65 */
214 14 , 9, /* B - 66 */
215 14 , 10, /* C - 67 */
216 14 , 26, /* D - 68 */
217 14 , 33, /* E - 69 */
218 14 , 35, /* F - 70 */
219 14 , 37, /* G - 71 */
220 14 , 44, /* H - 72 */
221 14 , 50, /* I - 73 */
222 14 , 53, /* J - 74 */
223 14 , 54, /* K - 75 */
224 14 , 72, /* L - 76 */
225 14 , 81, /* M - 77 */
226 14 , 112, /* N - 78 */
227 14 , 124, /* O - 79 */
228 14 , 126, /* P - 80 */
229 14 , 137, /* Q - 81 */
230 14 , 138, /* R - 82 */
231 14 , 145, /* S - 83 */
232 14 , 153, /* T - 84 */
233 14 , 159, /* U - 85 */
234 14 , 162, /* V - 86 */
235 14 , 164, /* W - 87 */
236 14 , 166, /* X - 88 */
237 14 , 167, /* Y - 89 */
238 14 , 169, /* Z - 90 */
239 7 , 63, /* [ - 91 */
240 7 , 65, /* \ - 92 */
241 7 , 66, /* ] - 93 */
242 7 , 67, /* ^ - 94 */
243 7 , 68, /* _ - 95 */
244 7 , 72, /* ` - 96 */
245 14 , 2, /* a - 97 */
246 14 , 9, /* b - 98 */
247 14 , 10, /* c - 99 */
248 14 , 26, /* d - 100 */
249 14 , 33, /* e - 101 */
250 14 , 35, /* f - 102 */
251 14 , 37, /* g - 103 */
252 14 , 44, /* h - 104 */
253 14 , 50, /* i - 105 */
254 14 , 53, /* j - 106 */
255 14 , 54, /* k - 107 */
256 14 , 72, /* l - 108 */
257 14 , 81, /* m - 109 */
258 14 , 112, /* n - 110 */
259 14 , 124, /* o - 111 */
260 14 , 126, /* p - 112 */
261 14 , 137, /* q - 113 */
262 14 , 138, /* r - 114 */
263 14 , 145, /* s - 115 */
264 14 , 153, /* t - 116 */
265 14 , 159, /* u - 117 */
266 14 , 162, /* v - 118 */
267 14 , 164, /* w - 119 */
268 14 , 166, /* x - 120 */
269 14 , 167, /* y - 121 */
270 14 , 169, /* z - 122 */
271 7 , 74, /* { - 123 */
272 7 , 76, /* | - 124 */
273 7 , 78, /* } - 125 */
274 7 , 80, /* ~ - 126 */
275 6 , 29, /* \x7f - 127 */
276 6 , 30, /* € - 128 */
277 6 , 31, /* � - 129 */
278 7 , 123, /* ‚ - 130 */
279 14 , 35, /* ƒ - 131 */
280 7 , 127, /* „ - 132 */
281 10 , 21, /* … - 133 */
282 10 , 15, /* † - 134 */
283 10 , 16, /* ‡ - 135 */
284 7 , 67, /* ˆ - 136 */
285 10 , 22, /* ‰ - 137 */
286 14 , 145, /* Š - 138 */
287 7 , 136, /* ‹ - 139 */
288 14 + 16 , 124, /* Π- 140 */
289 6 , 43, /* � - 141 */
290 6 , 44, /* Ž - 142 */
291 6 , 45, /* � - 143 */
292 6 , 46, /* � - 144 */
293 7 , 121, /* ‘ - 145 */
294 7 , 122, /* ’ - 146 */
295 7 , 125, /* “ - 147 */
296 7 , 126, /* ” - 148 */
297 10 , 17, /* • - 149 */
298 6 , 137, /* – - 150 */
299 6 , 139, /* — - 151 */
300 7 , 93, /* ˜ - 152 */
301 14 , 156, /* ™ - 153 */
302 14 , 145, /* š - 154 */
303 7 , 137, /* › - 155 */
304 14 + 16 , 124, /* œ - 156 */
305 6 , 59, /* � - 157 */
306 6 , 60, /* ž - 158 */
307 14 , 167, /* Ÿ - 159 */
308 7 , 4, /*   - 160 */
309 7 , 81, /* ¡ - 161 */
310 10 , 2, /* ¢ - 162 */
311 10 , 3, /* £ - 163 */
312 10 , 4, /* ¤ - 164 */
313 10 , 5, /* ¥ - 165 */
314 7 , 82, /* ¦ - 166 */
315 10 , 6, /* § - 167 */
316 7 , 83, /* ¨ - 168 */
317 10 , 7, /* © - 169 */
318 14 , 2, /* ª - 170 */
319 8 , 24, /* « - 171 */
320 10 , 8, /* ¬ - 172 */
321 6 , 131, /* ­ - 173 */
322 10 , 9, /* ® - 174 */
323 7 , 84, /* ¯ - 175 */
324 10 , 10, /* ° - 176 */
325 8 , 23, /* ± - 177 */
326 12 , 51, /* ² - 178 */
327 12 , 70, /* ³ - 179 */
328 7 , 85, /* ´ - 180 */
329 10 , 11, /* µ - 181 */
330 10 , 12, /* ¶ - 182 */
331 10 , 13, /* · - 183 */
332 7 , 86, /* ¸ - 184 */
333 12 , 33, /* ¹ - 185 */
334 14 , 124, /* º - 186 */
335 8 , 26, /* » - 187 */
336 12 , 21, /* ¼ - 188 */
337 12 , 25, /* ½ - 189 */
338 12 , 29, /* ¾ - 190 */
339 7 , 87, /* ¿ - 191 */
340 14 , 2, /* À - 192 */
341 14 , 2, /* Á - 193 */
342 14 , 2, /* Â - 194 */
343 14 , 2, /* Ã - 195 */
344 14 , 2, /* Ä - 196 */
345 14 , 2, /* Å - 197 */
346 14 + 16 , 2, /* Æ - 198 */
347 14 , 10, /* Ç - 199 */
348 14 , 33, /* È - 200 */
349 14 , 33, /* É - 201 */
350 14 , 33, /* Ê - 202 */
351 14 , 33, /* Ë - 203 */
352 14 , 50, /* Ì - 204 */
353 14 , 50, /* Í - 205 */
354 14 , 50, /* Î - 206 */
355 14 , 50, /* Ï - 207 */
356 14 , 26, /* Ð - 208 */
357 14 , 112, /* Ñ - 209 */
358 14 , 124, /* Ò - 210 */
359 14 , 124, /* Ó - 211 */
360 14 , 124, /* Ô - 212 */
361 14 , 124, /* Õ - 213 */
362 14 , 124, /* Ö - 214 */
363 8 , 28, /* × - 215 */
364 14 , 124, /* Ø - 216 */
365 14 , 159, /* Ù - 217 */
366 14 , 159, /* Ú - 218 */
367 14 , 159, /* Û - 219 */
368 14 , 159, /* Ü - 220 */
369 14 , 167, /* Ý - 221 */
370 14 + 32 , 153, /* Þ - 222 */
371 14 + 48 , 145, /* ß - 223 */
372 14 , 2, /* à - 224 */
373 14 , 2, /* á - 225 */
374 14 , 2, /* â - 226 */
375 14 , 2, /* ã - 227 */
376 14 , 2, /* ä - 228 */
377 14 , 2, /* å - 229 */
378 14 + 16 , 2, /* æ - 230 */
379 14 , 10, /* ç - 231 */
380 14 , 33, /* è - 232 */
381 14 , 33, /* é - 233 */
382 14 , 33, /* ê - 234 */
383 14 , 33, /* ë - 235 */
384 14 , 50, /* ì - 236 */
385 14 , 50, /* í - 237 */
386 14 , 50, /* î - 238 */
387 14 , 50, /* ï - 239 */
388 14 , 26, /* ð - 240 */
389 14 , 112, /* ñ - 241 */
390 14 , 124, /* ò - 242 */
391 14 , 124, /* ó - 243 */
392 14 , 124, /* ô - 244 */
393 14 , 124, /* õ - 245 */
394 14 , 124, /* ö - 246 */
395 8 , 29, /* ÷ - 247 */
396 14 , 124, /* ø - 248 */
397 14 , 159, /* ù - 249 */
398 14 , 159, /* ú - 250 */
399 14 , 159, /* û - 251 */
400 14 , 159, /* ü - 252 */
401 14 , 167, /* ý - 253 */
402 14 + 32 , 153, /* þ - 254 */
403 14 , 167 /* ÿ - 255 */ };
405 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
407 #define LCM_Diacritic_Start 131
409 static const unsigned char LCM_Diacritic_LUT[] = {
410 123, /* ƒ - 131 */
411 2, /* „ - 132 */
412 2, /* … - 133 */
413 2, /* † - 134 */
414 2, /* ‡ - 135 */
415 3, /* ˆ - 136 */
416 2, /* ‰ - 137 */
417 20, /* Š - 138 */
418 2, /* ‹ - 139 */
419 2, /* Π- 140 */
420 2, /* � - 141 */
421 2, /* Ž - 142 */
422 2, /* � - 143 */
423 2, /* � - 144 */
424 2, /* ‘ - 145 */
425 2, /* ’ - 146 */
426 2, /* “ - 147 */
427 2, /* ” - 148 */
428 2, /* • - 149 */
429 2, /* – - 150 */
430 2, /* — - 151 */
431 2, /* ˜ - 152 */
432 2, /* ™ - 153 */
433 20, /* š - 154 */
434 2, /* › - 155 */
435 2, /* œ - 156 */
436 2, /* � - 157 */
437 2, /* ž - 158 */
438 19, /* Ÿ - 159 */
439 2, /*   - 160 */
440 2, /* ¡ - 161 */
441 2, /* ¢ - 162 */
442 2, /* £ - 163 */
443 2, /* ¤ - 164 */
444 2, /* ¥ - 165 */
445 2, /* ¦ - 166 */
446 2, /* § - 167 */
447 2, /* ¨ - 168 */
448 2, /* © - 169 */
449 3, /* ª - 170 */
450 2, /* « - 171 */
451 2, /* ¬ - 172 */
452 2, /* ­ - 173 */
453 2, /* ® - 174 */
454 2, /* ¯ - 175 */
455 2, /* ° - 176 */
456 2, /* ± - 177 */
457 2, /* ² - 178 */
458 2, /* ³ - 179 */
459 2, /* ´ - 180 */
460 2, /* µ - 181 */
461 2, /* ¶ - 182 */
462 2, /* · - 183 */
463 2, /* ¸ - 184 */
464 2, /* ¹ - 185 */
465 3, /* º - 186 */
466 2, /* » - 187 */
467 2, /* ¼ - 188 */
468 2, /* ½ - 189 */
469 2, /* ¾ - 190 */
470 2, /* ¿ - 191 */
471 15, /* À - 192 */
472 14, /* Á - 193 */
473 18, /* Â - 194 */
474 25, /* Ã - 195 */
475 19, /* Ä - 196 */
476 26, /* Å - 197 */
477 2, /* Æ - 198 */
478 28, /* Ç - 199 */
479 15, /* È - 200 */
480 14, /* É - 201 */
481 18, /* Ê - 202 */
482 19, /* Ë - 203 */
483 15, /* Ì - 204 */
484 14, /* Í - 205 */
485 18, /* Î - 206 */
486 19, /* Ï - 207 */
487 104, /* Ð - 208 */
488 25, /* Ñ - 209 */
489 15, /* Ò - 210 */
490 14, /* Ó - 211 */
491 18, /* Ô - 212 */
492 25, /* Õ - 213 */
493 19, /* Ö - 214 */
494 2, /* × - 215 */
495 33, /* Ø - 216 */
496 15, /* Ù - 217 */
497 14, /* Ú - 218 */
498 18, /* Û - 219 */
499 19, /* Ü - 220 */
500 14, /* Ý - 221 */
501 2, /* Þ - 222 */
502 2, /* ß - 223 */
503 15, /* à - 224 */
504 14, /* á - 225 */
505 18, /* â - 226 */
506 25, /* ã - 227 */
507 19, /* ä - 228 */
508 26, /* å - 229 */
509 2, /* æ - 230 */
510 28, /* ç - 231 */
511 15, /* è - 232 */
512 14, /* é - 233 */
513 18, /* ê - 234 */
514 19, /* ë - 235 */
515 15, /* ì - 236 */
516 14, /* í - 237 */
517 18, /* î - 238 */
518 19, /* ï - 239 */
519 104, /* ð - 240 */
520 25, /* ñ - 241 */
521 15, /* ò - 242 */
522 14, /* ó - 243 */
523 18, /* ô - 244 */
524 25, /* õ - 245 */
525 19, /* ö - 246 */
526 2, /* ÷ - 247 */
527 33, /* ø - 248 */
528 15, /* ù - 249 */
529 14, /* ú - 250 */
530 18, /* û - 251 */
531 19, /* ü - 252 */
532 14, /* ý - 253 */
533 2, /* þ - 254 */
534 19, /* ÿ - 255 */
537 /******************************************************************************
538 * OLE2NLS_isPunctuation [INTERNAL]
540 static int OLE2NLS_isPunctuation(unsigned char c)
542 /* "punctuation character" in this context is a character which is
543 considered "less important" during word sort comparison.
544 See LCMapString implementation for the precise definition
545 of "less important". */
547 return (LCM_Unicode_LUT[-2+2*c]==6);
550 /******************************************************************************
551 * OLE2NLS_isNonSpacing [INTERNAL]
553 static int OLE2NLS_isNonSpacing(unsigned char c)
555 /* This function is used by LCMapStringA. Characters
556 for which it returns true are ignored when mapping a
557 string with NORM_IGNORENONSPACE */
558 return ((c==136) || (c==170) || (c==186));
561 /******************************************************************************
562 * OLE2NLS_isSymbol [INTERNAL]
563 * FIXME: handle current locale
565 static int OLE2NLS_isSymbol(unsigned char c)
567 /* This function is used by LCMapStringA. Characters
568 for which it returns true are ignored when mapping a
569 string with NORM_IGNORESYMBOLS */
570 return ( (c!=0) && !(isalpha(c) || isdigit(c)) );
573 /******************************************************************************
574 * identity [Internal]
576 static int identity(int c)
578 return c;
581 /*************************************************************************
582 * LCMapStringA [KERNEL32.@]
584 * Convert a string, or generate a sort key from it.
586 * If (mapflags & LCMAP_SORTKEY), the function will generate
587 * a sort key for the source string. Else, it will convert it
588 * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
590 * RETURNS
591 * Error : 0.
592 * Success : length of the result string.
594 * NOTES
595 * If called with scrlen = -1, the function will compute the length
596 * of the 0-terminated string strsrc by itself.
598 * If called with dstlen = 0, returns the buffer length that
599 * would be required.
601 * NORM_IGNOREWIDTH means to compare ASCII and wide characters
602 * as if they are equal.
603 * In the only code page implemented so far, there may not be
604 * wide characters in strings passed to LCMapStringA,
605 * so there is nothing to be done for this flag.
607 INT WINAPI LCMapStringA(
608 LCID lcid, /* [in] locale identifier created with MAKELCID;
609 LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are
610 predefined values. */
611 DWORD mapflags, /* [in] flags */
612 LPCSTR srcstr, /* [in] source buffer */
613 INT srclen, /* [in] source length */
614 LPSTR dststr, /* [out] destination buffer */
615 INT dstlen) /* [in] destination buffer length */
617 int i;
619 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
620 lcid,mapflags,debugstr_an(srcstr,srclen),srclen,dststr,dstlen);
622 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
624 ERR("(src=%s,dest=%s): Invalid NULL string\n",
625 debugstr_an(srcstr,srclen), dststr);
626 SetLastError(ERROR_INVALID_PARAMETER);
627 return 0;
629 if (srclen == -1)
630 srclen = strlen(srcstr) + 1 ; /* (include final '\0') */
632 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE | \
633 LCMAP_LOWERCASE | \
634 LCMAP_SORTKEY | \
635 NORM_IGNORECASE | \
636 NORM_IGNORENONSPACE | \
637 SORT_STRINGSORT | \
638 NORM_IGNOREWIDTH | \
639 NORM_IGNOREKANATYPE)
640 /* FIXME: as long as we don't support Katakana nor Hiragana
641 * characters, we can support NORM_IGNOREKANATYPE
643 if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
645 FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
646 "unimplemented flags: 0x%08lx\n",
647 lcid,
648 mapflags,
649 srcstr,
650 srclen,
651 dststr,
652 dstlen,
653 mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
657 if ( !(mapflags & LCMAP_SORTKEY) )
659 int i,j;
660 int (*f)(int) = identity;
661 int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
662 int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
664 if (flag_ignorenonspace || flag_ignoresymbols)
666 /* For some values of mapflags, the length of the resulting
667 string is not known at this point. Windows does map the string
668 and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
669 these cases. */
670 if (dstlen==0)
672 /* Compute required length */
673 for (i=j=0; i < srclen; i++)
675 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
676 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
677 j++;
679 return j;
682 else
684 if (dstlen==0)
685 return srclen;
686 if (dstlen<srclen)
688 SetLastError(ERROR_INSUFFICIENT_BUFFER);
689 return 0;
692 if (mapflags & LCMAP_UPPERCASE)
693 f = toupper;
694 else if (mapflags & LCMAP_LOWERCASE)
695 f = tolower;
696 /* FIXME: NORM_IGNORENONSPACE requires another conversion */
697 for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
699 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
700 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
702 dststr[j] = (CHAR) f(srcstr[i]);
703 j++;
706 return j;
709 /* FIXME: This function completely ignores the "lcid" parameter. */
710 /* else ... (mapflags & LCMAP_SORTKEY) */
712 int unicode_len=0;
713 int case_len=0;
714 int diacritic_len=0;
715 int delayed_punctuation_len=0;
716 char *case_component;
717 char *diacritic_component;
718 char *delayed_punctuation_component;
719 int room,count;
720 int flag_stringsort = mapflags & SORT_STRINGSORT;
722 /* compute how much room we will need */
723 for (i=0;i<srclen;i++)
725 int ofs;
726 unsigned char source_char = srcstr[i];
727 if (source_char!='\0')
729 if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
731 unicode_len++;
732 if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
733 unicode_len++; /* double letter */
735 else
737 delayed_punctuation_len++;
741 if (isupper(source_char))
742 case_len=unicode_len;
744 ofs = source_char - LCM_Diacritic_Start;
745 if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
746 diacritic_len=unicode_len;
749 if (mapflags & NORM_IGNORECASE)
750 case_len=0;
751 if (mapflags & NORM_IGNORENONSPACE)
752 diacritic_len=0;
754 room = 2 * unicode_len /* "unicode" component */
755 + diacritic_len /* "diacritic" component */
756 + case_len /* "case" component */
757 + 4 * delayed_punctuation_len /* punctuation in word sort mode */
758 + 4 /* four '\1' separators */
759 + 1 ; /* terminal '\0' */
760 if (dstlen==0)
761 return room;
762 else if (dstlen<room)
764 SetLastError(ERROR_INSUFFICIENT_BUFFER);
765 return 0;
767 #if 0
768 /*FIXME the Pointercheck should not be nessesary */
769 if (IsBadWritePtr (dststr,room))
770 { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
771 SetLastError(ERROR_INSUFFICIENT_BUFFER);
772 return 0;
774 #endif
775 /* locate each component, write separators */
776 diacritic_component = dststr + 2*unicode_len ;
777 *diacritic_component++ = '\1';
778 case_component = diacritic_component + diacritic_len ;
779 *case_component++ = '\1';
780 delayed_punctuation_component = case_component + case_len ;
781 *delayed_punctuation_component++ = '\1';
782 *delayed_punctuation_component++ = '\1';
784 /* read source string char by char, write
785 corresponding weight in each component. */
786 for (i=0,count=0;i<srclen;i++)
788 unsigned char source_char=srcstr[i];
789 if (source_char!='\0')
791 int type,longcode;
792 type = LCM_Unicode_LUT[-2+2*source_char];
793 longcode = type >> 4;
794 type &= 15;
795 if (!flag_stringsort && OLE2NLS_isPunctuation(source_char))
797 WORD encrypted_location = (1<<15) + 7 + 4*count;
798 *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
799 *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
800 /* big-endian is used here because it lets string comparison be
801 compatible with numerical comparison */
803 *delayed_punctuation_component++ = type;
804 *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];
805 /* assumption : a punctuation character is never a
806 double or accented letter */
808 else
810 dststr[2*count] = type;
811 dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];
812 if (longcode)
814 if (count<case_len)
815 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
816 if (count<diacritic_len)
817 diacritic_component[count] = 2; /* assumption: a double letter
818 is never accented */
819 count++;
821 dststr[2*count] = type;
822 dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode);
823 /* 16 in the first column of LCM_Unicode_LUT --> longcode = 1
824 32 in the first column of LCM_Unicode_LUT --> longcode = 2
825 48 in the first column of LCM_Unicode_LUT --> longcode = 3 */
828 if (count<case_len)
829 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
830 if (count<diacritic_len)
832 int ofs = source_char - LCM_Diacritic_Start;
833 diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
835 count++;
839 dststr[room-1] = '\0';
840 return room;
844 /*************************************************************************
845 * LCMapStringW [KERNEL32.@]
847 * Convert a string, or generate a sort key from it.
849 * NOTE
851 * See LCMapStringA for documentation
853 INT WINAPI LCMapStringW(
854 LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
855 INT dstlen)
857 int i;
859 TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
860 lcid, mapflags, srcstr, srclen, dststr, dstlen);
862 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
864 ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
865 SetLastError(ERROR_INVALID_PARAMETER);
866 return 0;
868 if (srclen==-1)
869 srclen = strlenW(srcstr)+1;
871 /* FIXME: Both this function and it's companion LCMapStringA()
872 * completely ignore the "lcid" parameter. In place of the "lcid"
873 * parameter the application must set the "LC_COLLATE" or "LC_ALL"
874 * environment variable prior to invoking this function. */
875 if (mapflags & LCMAP_SORTKEY)
877 /* Possible values of LC_COLLATE. */
878 char *lc_collate_default = 0; /* value prior to this function */
879 char *lc_collate_env = 0; /* value retrieved from the environment */
881 /* General purpose index into strings of any type. */
882 int str_idx = 0;
884 /* Lengths of various strings where the length is measured in
885 * wide characters for wide character strings and in bytes for
886 * native strings. The lengths include the NULL terminator. */
887 size_t returned_len = 0;
888 size_t src_native_len = 0;
889 size_t dst_native_len = 0;
890 size_t dststr_libc_len = 0;
892 /* Native (character set determined by locale) versions of the
893 * strings source and destination strings. */
894 LPSTR src_native = 0;
895 LPSTR dst_native = 0;
897 /* Version of the source and destination strings using the
898 * "wchar_t" Unicode data type needed by various libc functions. */
899 wchar_t *srcstr_libc = 0;
900 wchar_t *dststr_libc = 0;
902 if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
903 srclen * sizeof(wchar_t))))
905 ERR("Unable to allocate %d bytes for srcstr_libc\n",
906 srclen * sizeof(wchar_t));
907 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
908 return 0;
911 /* Convert source string to a libc Unicode string. */
912 for(str_idx = 0; str_idx < srclen; str_idx++)
914 srcstr_libc[str_idx] = srcstr[str_idx];
917 /* src_native should contain at most 3 bytes for each
918 * multibyte characters in the original srcstr string. */
919 src_native_len = 3 * srclen;
920 if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
921 src_native_len)))
923 ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
924 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
925 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
926 return 0;
929 /* FIXME: Prior to to setting the LC_COLLATE locale category the
930 * current value is backed up so it can be restored after the
931 * last LC_COLLATE sensitive function returns.
933 * Even though the locale is adjusted for a minimum amount of
934 * time a race condition exists where other threads may be
935 * affected if they invoke LC_COLLATE sensitive functions. One
936 * possible solution is to wrap all LC_COLLATE sensitive Wine
937 * functions, like LCMapStringW(), in a mutex.
939 * Another enhancement to the following would be to set the
940 * LC_COLLATE locale category as a function of the "lcid"
941 * parameter instead of the "LC_COLLATE" environment variable. */
942 if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
944 ERR("Unable to query the LC_COLLATE catagory\n");
945 SetLastError(ERROR_INVALID_PARAMETER);
946 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
947 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
948 return 0;
951 if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
953 ERR("Unable to inherit the LC_COLLATE locale category from the "
954 "environment. The \"LC_COLLATE\" environment variable is "
955 "\"%s\".\n", getenv("LC_COLLATE") ?
956 getenv("LC_COLLATE") : "<unset>");
957 SetLastError(ERROR_INVALID_PARAMETER);
958 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
959 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
960 return 0;
963 TRACE("lc_collate_default = %s\n", lc_collate_default);
964 TRACE("lc_collate_env = %s\n", lc_collate_env);
966 /* Convert the libc Unicode string to a native multibyte character
967 * string. */
968 returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
969 if(returned_len == 0)
971 ERR("wcstombs failed. The string specified (%s) may contain an invalid character.\n",
972 debugstr_w(srcstr));
973 SetLastError(ERROR_INVALID_PARAMETER);
974 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
975 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
976 setlocale(LC_COLLATE, lc_collate_default);
977 return 0;
979 else if(returned_len > src_native_len)
981 src_native[src_native_len - 1] = 0;
982 ERR("wcstombs returned a string (%s) that was longer (%d bytes) "
983 "than expected (%d bytes).\n", src_native, returned_len,
984 dst_native_len);
986 /* Since this is an internal error I'm not sure what the correct
987 * error code is. */
988 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
990 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
991 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
992 setlocale(LC_COLLATE, lc_collate_default);
993 return 0;
995 src_native_len = returned_len;
997 TRACE("src_native = %s src_native_len = %d\n",
998 src_native, src_native_len);
1000 /* dst_native seems to contain at most 4 bytes for each byte in
1001 * the original src_native string. Change if need be since this
1002 * isn't documented by the strxfrm() man page. */
1003 dst_native_len = 4 * src_native_len;
1004 if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
1006 ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
1007 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1008 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1009 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1010 setlocale(LC_COLLATE, lc_collate_default);
1011 return 0;
1014 /* The actual translation is done by the following call to
1015 * strxfrm(). The surrounding code could have been simplified
1016 * by calling wcsxfrm() instead except that wcsxfrm() is not
1017 * available on older Linux systems (RedHat 4.1 with
1018 * libc-5.3.12-17).
1020 * Also, it is possible that the translation could be done by
1021 * various tables as it is done in LCMapStringA(). However, I'm
1022 * not sure what those tables are. */
1023 returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
1025 if(returned_len > dst_native_len)
1027 dst_native[dst_native_len - 1] = 0;
1028 ERR("strxfrm returned a string (%s) that was longer (%d bytes) "
1029 "than expected (%d bytes).\n", dst_native, returned_len,
1030 dst_native_len);
1032 /* Since this is an internal error I'm not sure what the correct
1033 * error code is. */
1034 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1036 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1037 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1038 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1039 setlocale(LC_COLLATE, lc_collate_default);
1040 return 0;
1042 dst_native_len = returned_len;
1044 TRACE("dst_native = %s dst_native_len = %d\n",
1045 dst_native, dst_native_len);
1047 dststr_libc_len = dst_native_len;
1048 if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1049 dststr_libc_len * sizeof(wchar_t))))
1051 ERR("Unable to allocate %d bytes for dststr_libc\n",
1052 dststr_libc_len * sizeof(wchar_t));
1053 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1054 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1055 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1056 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1057 setlocale(LC_COLLATE, lc_collate_default);
1058 return 0;
1061 /* Convert the native multibyte string to a libc Unicode string. */
1062 returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
1064 /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
1065 * function has returned. */
1066 setlocale(LC_COLLATE, lc_collate_default);
1068 if(returned_len == 0)
1070 ERR("mbstowcs failed. The native version of the translated string "
1071 "(%s) may contain an invalid character.\n", dst_native);
1072 SetLastError(ERROR_INVALID_PARAMETER);
1073 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1074 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1075 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1076 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1077 return 0;
1079 if(dstlen)
1081 if(returned_len > dstlen)
1083 ERR("mbstowcs returned a string that was longer (%d chars) "
1084 "than the buffer provided (%d chars).\n", returned_len,
1085 dstlen);
1086 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1087 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1088 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1089 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1090 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1091 return 0;
1093 dstlen = returned_len;
1095 /* Convert a libc Unicode string to the destination string. */
1096 for(str_idx = 0; str_idx < dstlen; str_idx++)
1098 dststr[str_idx] = dststr_libc[str_idx];
1100 TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
1101 *(((int *)dststr) + 0),
1102 *(((int *)dststr) + 1),
1103 *(((int *)dststr) + 2),
1104 *(((int *)dststr) + 3));
1106 else
1108 dstlen = returned_len;
1110 TRACE("dstlen (return) = %d\n", dstlen);
1111 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1112 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1113 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1114 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1115 return dstlen;
1117 else
1119 int (*f)(int)=identity;
1121 if (dstlen==0)
1122 return srclen;
1123 if (dstlen<srclen)
1125 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1126 return 0;
1129 if (mapflags & LCMAP_UPPERCASE)
1130 f = toupper;
1131 else if (mapflags & LCMAP_LOWERCASE)
1132 f = tolower;
1133 for (i=0; i < srclen; i++)
1134 dststr[i] = (WCHAR) f(srcstr[i]);
1135 return srclen;
1140 /***********************************************************************
1141 * OLE2NLS_EstimateMappingLength
1143 * Estimates the number of characters required to hold the string
1144 * computed by LCMapStringA.
1146 * The size is always over-estimated, with a fixed limit on the
1147 * amount of estimation error.
1149 * Note that len == -1 is not permitted.
1151 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
1152 LPCSTR str, DWORD len)
1154 /* Estimate only for small strings to keep the estimation error from
1155 * becoming too large. */
1156 if (len < 128) return len * 8 + 5;
1157 else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
1160 /******************************************************************************
1161 * CompareStringA [KERNEL32.@]
1162 * Compares two strings using locale
1164 * RETURNS
1166 * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
1167 * failure: 0
1169 * NOTES
1171 * Defaults to a word sort, but uses a string sort if
1172 * SORT_STRINGSORT is set.
1173 * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
1175 * BUGS
1177 * This implementation ignores the locale
1179 * FIXME
1181 * Quite inefficient.
1183 int WINAPI CompareStringA(
1184 LCID lcid, /* [in] locale ID */
1185 DWORD fdwStyle, /* [in] comparison-style options */
1186 LPCSTR s1, /* [in] first string */
1187 int l1, /* [in] length of first string */
1188 LPCSTR s2, /* [in] second string */
1189 int l2) /* [in] length of second string */
1191 int mapstring_flags;
1192 int len1,len2;
1193 int result;
1194 LPSTR sk1,sk2;
1195 TRACE("%s and %s\n",
1196 debugstr_an (s1,l1), debugstr_an (s2,l2));
1198 if ( (s1==NULL) || (s2==NULL) )
1200 ERR("(s1=%s,s2=%s): Invalid NULL string\n",
1201 debugstr_an(s1,l1), debugstr_an(s2,l2));
1202 SetLastError(ERROR_INVALID_PARAMETER);
1203 return 0;
1206 if(fdwStyle & NORM_IGNORESYMBOLS)
1207 FIXME("IGNORESYMBOLS not supported\n");
1209 if (l1 == -1) l1 = strlen(s1);
1210 if (l2 == -1) l2 = strlen(s2);
1212 mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1213 len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
1214 len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
1216 if ((len1==0)||(len2==0))
1217 return 0; /* something wrong happened */
1219 sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
1220 sk2 = sk1 + len1;
1221 if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
1222 || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1224 ERR("Bug in LCmapStringA.\n");
1225 result = 0;
1227 else
1229 /* strcmp doesn't necessarily return -1, 0, or 1 */
1230 result = strcmp(sk1,sk2);
1232 HeapFree(GetProcessHeap(),0,sk1);
1234 if (result < 0)
1235 return 1;
1236 if (result == 0)
1237 return 2;
1239 /* must be greater, if we reach this point */
1240 return 3;
1243 /******************************************************************************
1244 * CompareStringW [KERNEL32.@]
1245 * This implementation ignores the locale
1246 * FIXME : Does only string sort. Should
1247 * be reimplemented the same way as CompareStringA.
1249 int WINAPI CompareStringW(LCID lcid, DWORD fdwStyle,
1250 LPCWSTR s1, int l1, LPCWSTR s2, int l2)
1252 int len,ret;
1253 if(fdwStyle & NORM_IGNORENONSPACE)
1254 FIXME("IGNORENONSPACE not supported\n");
1255 if(fdwStyle & NORM_IGNORESYMBOLS)
1256 FIXME("IGNORESYMBOLS not supported\n");
1258 /* Is strcmp defaulting to string sort or to word sort?? */
1259 /* FIXME: Handle NORM_STRINGSORT */
1260 l1 = (l1==-1)?strlenW(s1):l1;
1261 l2 = (l2==-1)?strlenW(s2):l2;
1262 len = l1<l2 ? l1:l2;
1263 ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
1264 /* not equal, return 1 or 3 */
1265 if(ret!=0) {
1266 /* need to translate result */
1267 return ((int)ret < 0) ? 1 : 3;
1269 /* same len, return 2 */
1270 if(l1==l2) return 2;
1271 /* the longer one is lexically greater */
1272 return (l1<l2)? 1 : 3;
1275 /***********************************************************************
1276 * lstrcmp (KERNEL32.@)
1277 * lstrcmpA (KERNEL32.@)
1279 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
1281 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
1285 /***********************************************************************
1286 * lstrcmpW (KERNEL32.@)
1287 * FIXME : should call CompareStringW, when it is implemented.
1288 * This implementation is not "word sort", as it should.
1290 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
1292 TRACE("%s and %s\n",
1293 debugstr_w (str1), debugstr_w (str2));
1294 if (!str1 || !str2) {
1295 SetLastError(ERROR_INVALID_PARAMETER);
1296 return 0;
1298 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
1299 return (INT)(*str1 - *str2);
1303 /***********************************************************************
1304 * lstrcmpi (KERNEL32.@)
1305 * lstrcmpiA (KERNEL32.@)
1307 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
1308 { TRACE("strcmpi %s and %s\n",
1309 debugstr_a (str1), debugstr_a (str2));
1310 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
1314 /***********************************************************************
1315 * lstrcmpiW (KERNEL32.@)
1317 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
1319 if (!str1 || !str2) {
1320 SetLastError(ERROR_INVALID_PARAMETER);
1321 return 0;
1323 return strcmpiW( str1, str2 );
1327 /******************************************************************************
1328 * OLE_GetFormatA [Internal]
1330 * FIXME
1331 * If datelen == 0, it should return the reguired string length.
1333 This function implements stuff for GetDateFormat() and
1334 GetTimeFormat().
1336 d single-digit (no leading zero) day (of month)
1337 dd two-digit day (of month)
1338 ddd short day-of-week name
1339 dddd long day-of-week name
1340 M single-digit month
1341 MM two-digit month
1342 MMM short month name
1343 MMMM full month name
1344 y two-digit year, no leading 0
1345 yy two-digit year
1346 yyyy four-digit year
1347 gg era string
1348 h hours with no leading zero (12-hour)
1349 hh hours with full two digits
1350 H hours with no leading zero (24-hour)
1351 HH hours with full two digits
1352 m minutes with no leading zero
1353 mm minutes with full two digits
1354 s seconds with no leading zero
1355 ss seconds with full two digits
1356 t time marker (A or P)
1357 tt time marker (AM, PM)
1358 '' used to quote literal characters
1359 '' (within a quoted string) indicates a literal '
1361 These functions REQUIRE valid locale, date, and format.
1363 static INT OLE_GetFormatA(LCID locale,
1364 DWORD flags,
1365 DWORD tflags,
1366 const SYSTEMTIME* xtime,
1367 LPCSTR _format, /*in*/
1368 LPSTR date, /*out*/
1369 INT datelen)
1371 INT inpos, outpos;
1372 int count, type, inquote, Overflow;
1373 char buf[40];
1374 char format[40];
1375 char * pos;
1376 int buflen;
1378 const char * _dgfmt[] = { "%d", "%02d" };
1379 const char ** dgfmt = _dgfmt - 1;
1381 /* report, for debugging */
1382 TRACE("(0x%lx,0x%lx, 0x%lx, time(y=%d m=%d wd=%d d=%d,h=%d,m=%d,s=%d), fmt=%p \'%s\' , %p, len=%d)\n",
1383 locale, flags, tflags,
1384 xtime->wYear,xtime->wMonth,xtime->wDayOfWeek,xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
1385 _format, _format, date, datelen);
1387 if(datelen == 0) {
1388 FIXME("datelen = 0, returning 255\n");
1389 return 255;
1392 /* initalize state variables and output buffer */
1393 inpos = outpos = 0;
1394 count = 0; inquote = 0; Overflow = 0;
1395 type = '\0';
1396 date[0] = buf[0] = '\0';
1398 strcpy(format,_format);
1400 /* alter the formatstring, while it works for all languages now in wine
1401 its possible that it fails when the time looks like ss:mm:hh as example*/
1402 if (tflags & (TIME_NOMINUTESORSECONDS))
1403 { if ((pos = strstr ( format, ":mm")))
1404 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
1407 if (tflags & (TIME_NOSECONDS))
1408 { if ((pos = strstr ( format, ":ss")))
1409 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
1413 for (inpos = 0;; inpos++) {
1414 /* TRACE("STATE inpos=%2d outpos=%2d count=%d inquote=%d type=%c buf,date = %c,%c\n", inpos, outpos, count, inquote, type, buf[inpos], date[outpos]); */
1415 if (inquote) {
1416 if (format[inpos] == '\'') {
1417 if (format[inpos+1] == '\'') {
1418 inpos += 1;
1419 date[outpos++] = '\'';
1420 } else {
1421 inquote = 0;
1422 continue; /* we did nothing to the output */
1424 } else if (format[inpos] == '\0') {
1425 date[outpos++] = '\0';
1426 if (outpos > datelen) Overflow = 1;
1427 break;
1428 } else {
1429 date[outpos++] = format[inpos];
1430 if (outpos > datelen) {
1431 Overflow = 1;
1432 date[outpos-1] = '\0'; /* this is the last place where
1433 it's safe to write */
1434 break;
1437 } else if ( (count && (format[inpos] != type))
1438 || count == 4
1439 || (count == 2 && strchr("ghHmst", type)) ) {
1440 if (type == 'h' && (tflags & TIME_FORCE24HOURFORMAT)) type= 'H';
1441 if (type == 'd') {
1442 if (count == 4) {
1443 GetLocaleInfoA(locale,
1444 LOCALE_SDAYNAME1
1445 + (xtime->wDayOfWeek+6)%7,
1446 buf, sizeof(buf));
1447 } else if (count == 3) {
1448 GetLocaleInfoA(locale,
1449 LOCALE_SABBREVDAYNAME1
1450 + (xtime->wDayOfWeek+6)%7,
1451 buf, sizeof(buf));
1452 } else {
1453 sprintf(buf, dgfmt[count], xtime->wDay);
1455 } else if (type == 'M') {
1456 if (count == 3) {
1457 GetLocaleInfoA(locale,
1458 LOCALE_SABBREVMONTHNAME1
1459 + xtime->wMonth - 1,
1460 buf, sizeof(buf));
1461 } else if (count == 4) {
1462 GetLocaleInfoA(locale,
1463 LOCALE_SMONTHNAME1
1464 + xtime->wMonth - 1,
1465 buf, sizeof(buf));
1466 } else {
1467 sprintf(buf, dgfmt[count], xtime->wMonth);
1469 } else if (type == 'y') {
1470 if (count == 4) {
1471 sprintf(buf, "%d", xtime->wYear);
1472 } else if (count == 3) {
1473 strcpy(buf, "yyy");
1474 WARN("unknown format, c=%c, n=%d\n", type, count);
1475 } else {
1476 sprintf(buf, dgfmt[count], xtime->wYear % 100);
1478 } else if (type == 'g') {
1479 if (count == 2) {
1480 FIXME("LOCALE_ICALENDARTYPE unimp.\n");
1481 strcpy(buf, "AD");
1482 } else {
1483 strcpy(buf, "g");
1484 WARN("unknown format, c=%c, n=%d\n", type, count);
1486 } else if (type == 'h') {
1487 /* gives us hours 1:00 -- 12:00 */
1488 sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
1489 } else if (type == 'H') {
1490 /* 24-hour time */
1491 sprintf(buf, dgfmt[count], xtime->wHour);
1492 } else if ( type == 'm') {
1493 sprintf(buf, dgfmt[count], xtime->wMinute);
1494 } else if ( type == 's') {
1495 sprintf(buf, dgfmt[count], xtime->wSecond);
1496 } else if (type == 't') {
1497 if ((tflags & TIME_NOTIMEMARKER))
1498 buf[0]='\0';
1499 else if (count == 1) {
1500 sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
1501 } else if (count == 2) {
1502 /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
1503 GetLocaleInfoA(locale,
1504 (xtime->wHour<12)
1505 ? LOCALE_S1159 : LOCALE_S2359,
1506 buf, sizeof(buf));
1510 /* we need to check the next char in the format string
1511 again, no matter what happened */
1512 inpos--;
1514 /* add the contents of buf to the output */
1515 buflen = strlen(buf);
1516 if (outpos + buflen < datelen) {
1517 date[outpos] = '\0'; /* for strcat to hook onto */
1518 strcat(date, buf);
1519 outpos += buflen;
1520 } else {
1521 date[outpos] = '\0';
1522 strncat(date, buf, datelen - outpos);
1523 date[datelen - 1] = '\0';
1524 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1525 WARN("insufficient buffer\n");
1526 return 0;
1529 /* reset the variables we used to keep track of this item */
1530 count = 0;
1531 type = '\0';
1532 } else if (format[inpos] == '\0') {
1533 /* we can't check for this at the loop-head, because
1534 that breaks the printing of the last format-item */
1535 date[outpos] = '\0';
1536 break;
1537 } else if (count) {
1538 /* continuing a code for an item */
1539 count +=1;
1540 continue;
1541 } else if (strchr("hHmstyMdg", format[inpos])) {
1542 type = format[inpos];
1543 count = 1;
1544 continue;
1545 } else if (format[inpos] == '\'') {
1546 inquote = 1;
1547 continue;
1548 } else {
1549 date[outpos++] = format[inpos];
1551 /* now deal with a possible buffer overflow */
1552 if (outpos >= datelen) {
1553 date[datelen - 1] = '\0';
1554 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1555 return 0;
1559 if (Overflow) {
1560 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1563 /* finish it off with a string terminator */
1564 outpos++;
1565 /* sanity check */
1566 if (outpos > datelen-1) outpos = datelen-1;
1567 date[outpos] = '\0';
1569 TRACE("returns string '%s', len %d\n", date, outpos);
1570 return outpos;
1573 /******************************************************************************
1574 * OLE_GetFormatW [INTERNAL]
1576 * dateformat is set TRUE if being called for a date, false for a time
1578 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
1579 const SYSTEMTIME* xtime,
1580 LPCWSTR format,
1581 LPWSTR output, INT outlen, int dateformat)
1583 INT outpos;
1585 /* make a debug report */
1586 TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
1587 "%p with max len %d\n",
1588 locale, flags, tflags,
1589 xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
1590 debugstr_w(format), format, output, outlen);
1592 /* initialize state variables */
1593 outpos = 0;
1595 while (*format) {
1596 /* Literal string: Maybe terminated early by a \0 */
1597 if (*format == (WCHAR) '\'') {
1598 format++;
1599 while (*format) {
1600 if (*format == (WCHAR) '\'') {
1601 format++;
1602 if (*format != '\'') {
1603 break; /* It was a terminating quote */
1606 if (!outlen)
1607 /* We are counting */;
1608 else if (outpos >= outlen)
1609 goto too_short;
1610 else
1611 output[outpos] = *format;
1612 outpos++;
1613 format++;
1615 } else if ( (dateformat && (*format=='d' ||
1616 *format=='M' ||
1617 *format=='y' ||
1618 *format=='g') ) ||
1619 (!dateformat && (*format=='H' ||
1620 *format=='h' ||
1621 *format=='m' ||
1622 *format=='s' ||
1623 *format=='t') ) ) {
1624 int type, count;
1625 char tmp[16];
1626 WCHAR buf[40];
1627 int buflen=0;
1628 type = *format;
1629 format++;
1630 for (count = 1; *format == type; format++)
1631 count++;
1632 switch(type)
1634 case 'd':
1635 if (count >= 4) {
1636 GetLocaleInfoW(locale,
1637 LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7,
1638 buf, sizeof(buf)/sizeof(WCHAR) );
1639 } else if (count == 3) {
1640 GetLocaleInfoW(locale,
1641 LOCALE_SABBREVDAYNAME1 +
1642 (xtime->wDayOfWeek +6)%7,
1643 buf, sizeof(buf)/sizeof(WCHAR) );
1644 } else {
1645 sprintf( tmp, "%.*d", count, xtime->wDay );
1646 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1648 break;
1650 case 'M':
1651 if (count >= 4) {
1652 GetLocaleInfoW(locale, LOCALE_SMONTHNAME1 +
1653 xtime->wMonth -1, buf,
1654 sizeof(buf)/sizeof(WCHAR) );
1655 } else if (count == 3) {
1656 GetLocaleInfoW(locale, LOCALE_SABBREVMONTHNAME1 +
1657 xtime->wMonth -1, buf,
1658 sizeof(buf)/sizeof(WCHAR) );
1659 } else {
1660 sprintf( tmp, "%.*d", count, xtime->wMonth );
1661 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1663 break;
1664 case 'y':
1665 if (count >= 4) {
1666 sprintf( tmp, "%d", xtime->wYear );
1667 } else {
1668 sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wYear % 100 );
1670 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1671 break;
1673 case 'g':
1674 if (count == 2) {
1675 FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
1676 strcpy( tmp, "AD" );
1677 } else {
1678 /* Win API sez we copy it verbatim */
1679 strcpy( tmp, "g" );
1681 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1682 break;
1684 case 'h':
1685 /* hours 1:00-12:00 --- is this right? */
1686 sprintf( tmp, "%.*d", count > 2 ? 2 : count, (xtime->wHour-1)%12 +1);
1687 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1688 break;
1690 case 'H':
1691 sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wHour );
1692 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1693 break;
1695 case 'm':
1696 sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wMinute );
1697 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1698 break;
1700 case 's':
1701 sprintf( tmp, "%.*d", count > 2 ? 2 : count, xtime->wSecond );
1702 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
1703 break;
1705 case 't':
1706 GetLocaleInfoW(locale, (xtime->wHour < 12) ?
1707 LOCALE_S1159 : LOCALE_S2359,
1708 buf, sizeof(buf) );
1709 if (count == 1) {
1710 buf[1] = 0;
1712 break;
1715 /* cat buf onto the output */
1716 buflen = strlenW(buf);
1717 if (!outlen)
1718 /* We are counting */;
1719 else if (outpos + buflen < outlen) {
1720 strcpyW( output + outpos, buf );
1721 } else {
1722 lstrcpynW( output + outpos, buf, outlen - outpos );
1723 /* Is this an undocumented feature we are supporting? */
1724 goto too_short;
1726 outpos += buflen;
1727 } else {
1728 /* a literal character */
1729 if (!outlen)
1730 /* We are counting */;
1731 else if (outpos >= outlen)
1732 goto too_short;
1733 else
1734 output[outpos] = *format;
1735 outpos++;
1736 format++;
1740 /* final string terminator and sanity check */
1741 if (!outlen)
1742 /* We are counting */;
1743 else if (outpos >= outlen)
1744 goto too_short;
1745 else
1746 output[outpos] = '\0';
1747 outpos++;
1749 TRACE(" returning %d %s\n", outpos, debugstr_w(output));
1750 return outpos;
1752 too_short:
1753 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1754 WARN(" buffer overflow\n");
1755 return 0;
1759 /******************************************************************************
1760 * GetDateFormatA [KERNEL32.@]
1761 * Makes an ASCII string of the date
1763 * This function uses format to format the date, or, if format
1764 * is NULL, uses the default for the locale. format is a string
1765 * of literal fields and characters as follows:
1767 * - d single-digit (no leading zero) day (of month)
1768 * - dd two-digit day (of month)
1769 * - ddd short day-of-week name
1770 * - dddd long day-of-week name
1771 * - M single-digit month
1772 * - MM two-digit month
1773 * - MMM short month name
1774 * - MMMM full month name
1775 * - y two-digit year, no leading 0
1776 * - yy two-digit year
1777 * - yyyy four-digit year
1778 * - gg era string
1781 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
1782 const SYSTEMTIME* xtime,
1783 LPCSTR format, LPSTR date,INT datelen)
1786 char format_buf[40];
1787 LPCSTR thisformat;
1788 SYSTEMTIME t;
1789 LPSYSTEMTIME thistime;
1790 LCID thislocale;
1791 INT ret;
1792 FILETIME ft;
1793 BOOL res;
1795 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
1796 locale,flags,xtime,format,date,datelen);
1798 if (!locale) {
1799 locale = LOCALE_SYSTEM_DEFAULT;
1802 if (locale == LOCALE_SYSTEM_DEFAULT) {
1803 thislocale = GetSystemDefaultLCID();
1804 } else if (locale == LOCALE_USER_DEFAULT) {
1805 thislocale = GetUserDefaultLCID();
1806 } else {
1807 thislocale = locale;
1810 if (xtime == NULL) {
1811 GetSystemTime(&t);
1812 } else {
1813 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
1814 res=SystemTimeToFileTime(xtime,&ft);
1815 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
1816 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
1817 if(!res)
1819 SetLastError(ERROR_INVALID_PARAMETER);
1820 return 0;
1822 FileTimeToSystemTime(&ft,&t);
1825 thistime = &t;
1827 if (format == NULL) {
1828 GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE)
1829 ? LOCALE_SLONGDATE
1830 : LOCALE_SSHORTDATE),
1831 format_buf, sizeof(format_buf));
1832 thisformat = format_buf;
1833 } else {
1834 thisformat = format;
1838 ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat,
1839 date, datelen);
1842 TRACE(
1843 "GetDateFormatA() returning %d, with data=%s\n",
1844 ret, date);
1845 return ret;
1848 /******************************************************************************
1849 * GetDateFormatW [KERNEL32.@]
1850 * Makes a Unicode string of the date
1852 * Acts the same as GetDateFormatA(), except that it's Unicode.
1853 * Accepts & returns sizes as counts of Unicode characters.
1856 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
1857 const SYSTEMTIME* xtime,
1858 LPCWSTR format,
1859 LPWSTR date, INT datelen)
1861 WCHAR format_buf[40];
1862 LPCWSTR thisformat;
1863 SYSTEMTIME t;
1864 LPSYSTEMTIME thistime;
1865 LCID thislocale;
1866 INT ret;
1867 FILETIME ft;
1868 BOOL res;
1870 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
1871 locale,flags,xtime,debugstr_w(format),date,datelen);
1873 /* Tests (could be left until OLE_GetFormatW) */
1874 if (flags && format)
1876 SetLastError (ERROR_INVALID_FLAGS);
1877 return 0;
1879 if (datelen && !date)
1881 SetLastError (ERROR_INVALID_PARAMETER);
1882 return 0;
1884 if (!locale) {
1885 locale = LOCALE_SYSTEM_DEFAULT;
1888 if (locale == LOCALE_SYSTEM_DEFAULT) {
1889 thislocale = GetSystemDefaultLCID();
1890 } else if (locale == LOCALE_USER_DEFAULT) {
1891 thislocale = GetUserDefaultLCID();
1892 } else {
1893 thislocale = locale;
1896 if (xtime == NULL) {
1897 GetSystemTime(&t);
1898 } else {
1899 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
1900 res=SystemTimeToFileTime(xtime,&ft);
1901 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
1902 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
1903 if(!res) {
1904 SetLastError(ERROR_INVALID_PARAMETER);
1905 return 0;
1907 FileTimeToSystemTime(&ft,&t);
1910 thistime = &t;
1912 if (format == NULL) {
1913 GetLocaleInfoW(thislocale, ((flags&DATE_LONGDATE)
1914 ? LOCALE_SLONGDATE
1915 : LOCALE_SSHORTDATE),
1916 format_buf, sizeof(format_buf)/sizeof(*format_buf));
1917 thisformat = format_buf;
1918 } else {
1919 thisformat = format;
1923 ret = OLE_GetFormatW(thislocale, flags, 0, thistime, thisformat,
1924 date, datelen, 1);
1927 TRACE("GetDateFormatW() returning %d, with data=%s\n",
1928 ret, debugstr_w(date));
1929 return ret;
1932 /**************************************************************************
1933 * EnumDateFormatsA (KERNEL32.@)
1935 BOOL WINAPI EnumDateFormatsA(
1936 DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
1938 LCID Loc = GetUserDefaultLCID();
1939 if(!lpDateFmtEnumProc)
1941 SetLastError(ERROR_INVALID_PARAMETER);
1942 return FALSE;
1945 switch( Loc )
1948 case 0x00000407: /* (Loc,"de_DE") */
1950 switch(dwFlags)
1952 case DATE_SHORTDATE:
1953 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
1954 if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
1955 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
1956 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
1957 return TRUE;
1958 case DATE_LONGDATE:
1959 if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
1960 if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
1961 if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
1962 return TRUE;
1963 default:
1964 FIXME("Unknown date format (%ld)\n", dwFlags);
1965 SetLastError(ERROR_INVALID_PARAMETER);
1966 return FALSE;
1970 case 0x0000040c: /* (Loc,"fr_FR") */
1972 switch(dwFlags)
1974 case DATE_SHORTDATE:
1975 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
1976 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
1977 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
1978 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
1979 return TRUE;
1980 case DATE_LONGDATE:
1981 if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
1982 if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
1983 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
1984 return TRUE;
1985 default:
1986 FIXME("Unknown date format (%ld)\n", dwFlags);
1987 SetLastError(ERROR_INVALID_PARAMETER);
1988 return FALSE;
1992 case 0x00000c0c: /* (Loc,"fr_CA") */
1994 switch(dwFlags)
1996 case DATE_SHORTDATE:
1997 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
1998 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
1999 if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
2000 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2001 return TRUE;
2002 case DATE_LONGDATE:
2003 if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
2004 if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
2005 return TRUE;
2006 default:
2007 FIXME("Unknown date format (%ld)\n", dwFlags);
2008 SetLastError(ERROR_INVALID_PARAMETER);
2009 return FALSE;
2013 case 0x00000809: /* (Loc,"en_UK") */
2015 switch(dwFlags)
2017 case DATE_SHORTDATE:
2018 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2019 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2020 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2021 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2022 return TRUE;
2023 case DATE_LONGDATE:
2024 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2025 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2026 return TRUE;
2027 default:
2028 FIXME("Unknown date format (%ld)\n", dwFlags);
2029 SetLastError(ERROR_INVALID_PARAMETER);
2030 return FALSE;
2034 case 0x00000c09: /* (Loc,"en_AU") */
2036 switch(dwFlags)
2038 case DATE_SHORTDATE:
2039 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2040 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2041 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2042 return TRUE;
2043 case DATE_LONGDATE:
2044 if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
2045 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2046 return TRUE;
2047 default:
2048 FIXME("Unknown date format (%ld)\n", dwFlags);
2049 SetLastError(ERROR_INVALID_PARAMETER);
2050 return FALSE;
2054 case 0x00001009: /* (Loc,"en_CA") */
2056 switch(dwFlags)
2058 case DATE_SHORTDATE:
2059 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2060 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2061 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2062 if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
2063 return TRUE;
2064 case DATE_LONGDATE:
2065 if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
2066 if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
2067 return TRUE;
2068 default:
2069 FIXME("Unknown date format (%ld)\n", dwFlags);
2070 SetLastError(ERROR_INVALID_PARAMETER);
2071 return FALSE;
2075 case 0x00001409: /* (Loc,"en_NZ") */
2077 switch(dwFlags)
2079 case DATE_SHORTDATE:
2080 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2081 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2082 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2083 return TRUE;
2084 case DATE_LONGDATE:
2085 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2086 if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
2087 return TRUE;
2088 default:
2089 FIXME("Unknown date format (%ld)\n", dwFlags);
2090 SetLastError(ERROR_INVALID_PARAMETER);
2091 return FALSE;
2095 case 0x00001809: /* (Loc,"en_IE") */
2097 switch(dwFlags)
2099 case DATE_SHORTDATE:
2100 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2101 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2102 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2103 return TRUE;
2104 case DATE_LONGDATE:
2105 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2106 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2107 return TRUE;
2108 default:
2109 FIXME("Unknown date format (%ld)\n", dwFlags);
2110 SetLastError(ERROR_INVALID_PARAMETER);
2111 return FALSE;
2115 case 0x00001c09: /* (Loc,"en_ZA") */
2117 switch(dwFlags)
2119 case DATE_SHORTDATE:
2120 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2121 return TRUE;
2122 case DATE_LONGDATE:
2123 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2124 return TRUE;
2125 default:
2126 FIXME("Unknown date format (%ld)\n", dwFlags);
2127 SetLastError(ERROR_INVALID_PARAMETER);
2128 return FALSE;
2132 case 0x00002009: /* (Loc,"en_JM") */
2134 switch(dwFlags)
2136 case DATE_SHORTDATE:
2137 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2138 return TRUE;
2139 case DATE_LONGDATE:
2140 if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
2141 if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
2142 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
2143 if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
2144 return TRUE;
2145 default:
2146 FIXME("Unknown date format (%ld)\n", dwFlags);
2147 SetLastError(ERROR_INVALID_PARAMETER);
2148 return FALSE;
2152 case 0x00002809: /* (Loc,"en_BZ") */
2153 case 0x00002c09: /* (Loc,"en_TT") */
2155 switch(dwFlags)
2157 case DATE_SHORTDATE:
2158 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2159 return TRUE;
2160 case DATE_LONGDATE:
2161 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
2162 return TRUE;
2163 default:
2164 FIXME("Unknown date format (%ld)\n", dwFlags);
2165 SetLastError(ERROR_INVALID_PARAMETER);
2166 return FALSE;
2170 default: /* default to US English "en_US" */
2172 switch(dwFlags)
2174 case DATE_SHORTDATE:
2175 if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
2176 if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
2177 if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
2178 if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
2179 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2180 if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
2181 return TRUE;
2182 case DATE_LONGDATE:
2183 if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
2184 if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
2185 if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
2186 if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
2187 return TRUE;
2188 default:
2189 FIXME("Unknown date format (%ld)\n", dwFlags);
2190 SetLastError(ERROR_INVALID_PARAMETER);
2191 return FALSE;
2197 /**************************************************************************
2198 * EnumDateFormatsW (KERNEL32.@)
2200 BOOL WINAPI EnumDateFormatsW(
2201 DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2203 FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
2204 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2205 return FALSE;
2208 /**************************************************************************
2209 * EnumTimeFormatsA (KERNEL32.@)
2211 BOOL WINAPI EnumTimeFormatsA(
2212 TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2214 LCID Loc = GetUserDefaultLCID();
2215 if(!lpTimeFmtEnumProc)
2217 SetLastError(ERROR_INVALID_PARAMETER);
2218 return FALSE;
2220 if(dwFlags)
2222 FIXME("Unknown time format (%ld)\n", dwFlags);
2225 switch( Loc )
2227 case 0x00000407: /* (Loc,"de_DE") */
2229 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2230 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2231 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2232 if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
2233 if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
2234 return TRUE;
2237 case 0x0000040c: /* (Loc,"fr_FR") */
2238 case 0x00000c0c: /* (Loc,"fr_CA") */
2240 if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
2241 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2242 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2243 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2244 if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
2245 return TRUE;
2248 case 0x00000809: /* (Loc,"en_UK") */
2249 case 0x00000c09: /* (Loc,"en_AU") */
2250 case 0x00001409: /* (Loc,"en_NZ") */
2251 case 0x00001809: /* (Loc,"en_IE") */
2253 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2254 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2255 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2256 return TRUE;
2259 case 0x00001c09: /* (Loc,"en_ZA") */
2260 case 0x00002809: /* (Loc,"en_BZ") */
2261 case 0x00002c09: /* (Loc,"en_TT") */
2263 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2264 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2265 return TRUE;
2268 default: /* default to US style "en_US" */
2270 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2271 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2272 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2273 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2274 return TRUE;
2279 /**************************************************************************
2280 * EnumTimeFormatsW (KERNEL32.@)
2282 BOOL WINAPI EnumTimeFormatsW(
2283 TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2285 FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
2286 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2287 return FALSE;
2290 /**************************************************************************
2291 * This function is used just locally !
2292 * Description: Inverts a string.
2294 static void OLE_InvertString(char* string)
2296 char sTmpArray[128];
2297 INT counter, i = 0;
2299 for (counter = strlen(string); counter > 0; counter--)
2301 memcpy(sTmpArray + i, string + counter-1, 1);
2302 i++;
2304 memcpy(sTmpArray + i, "\0", 1);
2305 strcpy(string, sTmpArray);
2308 /***************************************************************************************
2309 * This function is used just locally !
2310 * Description: Test if the given string (psNumber) is valid or not.
2311 * The valid characters are the following:
2312 * - Characters '0' through '9'.
2313 * - One decimal point (dot) if the number is a floating-point value.
2314 * - A minus sign in the first character position if the number is
2315 * a negative value.
2316 * If the function succeeds, psBefore/psAfter will point to the string
2317 * on the right/left of the decimal symbol. pbNegative indicates if the
2318 * number is negative.
2320 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
2322 char sNumberSet[] = "0123456789";
2323 BOOL bInDecimal = FALSE;
2325 /* Test if we do have a minus sign */
2326 if ( *pInput == '-' )
2328 *pbNegative = TRUE;
2329 pInput++; /* Jump to the next character. */
2332 while(*pInput != '\0')
2334 /* Do we have a valid numeric character */
2335 if ( strchr(sNumberSet, *pInput) != NULL )
2337 if (bInDecimal == TRUE)
2338 *psAfter++ = *pInput;
2339 else
2340 *psBefore++ = *pInput;
2342 else
2344 /* Is this a decimal point (dot) */
2345 if ( *pInput == '.' )
2347 /* Is it the first time we find it */
2348 if ((bInDecimal == FALSE))
2349 bInDecimal = TRUE;
2350 else
2351 return -1; /* ERROR: Invalid parameter */
2353 else
2355 /* It's neither a numeric character, nor a decimal point.
2356 * Thus, return an error.
2358 return -1;
2361 pInput++;
2364 /* Add an End of Line character to the output buffers */
2365 *psBefore = '\0';
2366 *psAfter = '\0';
2368 return 0;
2371 /**************************************************************************
2372 * This function is used just locally !
2373 * Description: A number could be formatted using different numbers
2374 * of "digits in group" (example: 4;3;2;0).
2375 * The first parameter of this function is an array
2376 * containing the rule to be used. Its format is the following:
2377 * |NDG|DG1|DG2|...|0|
2378 * where NDG is the number of used "digits in group" and DG1, DG2,
2379 * are the corresponding "digits in group".
2380 * Thus, this function returns the grouping value in the array
2381 * pointed by the second parameter.
2383 static INT OLE_GetGrouping(char* sRule, INT index)
2385 char sData[2], sRuleSize[2];
2386 INT nData, nRuleSize;
2388 memcpy(sRuleSize, sRule, 1);
2389 memcpy(sRuleSize+1, "\0", 1);
2390 nRuleSize = atoi(sRuleSize);
2392 if (index > 0 && index < nRuleSize)
2394 memcpy(sData, sRule+index, 1);
2395 memcpy(sData+1, "\0", 1);
2396 nData = atoi(sData);
2399 else
2401 memcpy(sData, sRule+nRuleSize-1, 1);
2402 memcpy(sData+1, "\0", 1);
2403 nData = atoi(sData);
2406 return nData;
2409 /**************************************************************************
2410 * GetNumberFormatA (KERNEL32.@)
2412 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
2413 LPCSTR lpvalue, const NUMBERFMTA * lpFormat,
2414 LPSTR lpNumberStr, int cchNumber)
2416 char sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
2417 INT nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
2418 char sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
2419 char sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];
2420 char *pStr = NULL, *pTmpStr = NULL;
2421 LCID systemDefaultLCID;
2422 BOOL bNegative = FALSE;
2423 enum Operations
2425 USE_PARAMETER,
2426 USE_LOCALEINFO,
2427 USE_SYSTEMDEFAULT,
2428 RETURN_ERROR
2429 } used_operation;
2431 strncpy(sNumber, lpvalue, 128);
2432 sNumber[127] = '\0';
2434 /* Make sure we have a valid input string, get the number
2435 * of digits before and after the decimal symbol, and check
2436 * if this is a negative number.
2438 if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
2440 nNumberDecimal = strlen(sDigitsBeforeDecimal);
2441 nDigits = strlen(sDigitsAfterDecimal);
2443 else
2445 SetLastError(ERROR_INVALID_PARAMETER);
2446 return 0;
2449 /* Which source will we use to format the string */
2450 used_operation = RETURN_ERROR;
2451 if (lpFormat != NULL)
2453 if (dwflags == 0)
2454 used_operation = USE_PARAMETER;
2456 else
2458 if (dwflags & LOCALE_NOUSEROVERRIDE)
2459 used_operation = USE_SYSTEMDEFAULT;
2460 else
2461 used_operation = USE_LOCALEINFO;
2464 /* Load the fields we need */
2465 switch(used_operation)
2467 case USE_LOCALEINFO:
2468 GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
2469 GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
2470 GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
2471 GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
2472 GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
2473 GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
2474 break;
2475 case USE_PARAMETER:
2476 sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
2477 strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
2478 sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
2479 strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
2480 sprintf(sILZero, "%d",lpFormat->LeadingZero);
2481 sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
2482 break;
2483 case USE_SYSTEMDEFAULT:
2484 systemDefaultLCID = GetSystemDefaultLCID();
2485 GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
2486 GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
2487 GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
2488 GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
2489 GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
2490 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
2491 break;
2492 default:
2493 SetLastError(ERROR_INVALID_PARAMETER);
2494 return 0;
2497 nNumberDigits = atoi(sNumberDigits);
2499 /* Remove the ";" */
2500 i=0;
2501 j = 1;
2502 for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
2504 if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
2506 memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
2507 i++;
2508 j++;
2511 sprintf(sBuffer, "%d", i);
2512 memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
2513 memcpy(sRule + j, "\0", 1);
2515 /* First, format the digits before the decimal. */
2516 if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0))
2518 /* Working on an inverted string is easier ! */
2519 OLE_InvertString(sDigitsBeforeDecimal);
2521 nStep = nCounter = i = j = 0;
2522 nRuleIndex = 1;
2523 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
2525 /* Here, we will loop until we reach the end of the string.
2526 * An internal counter (j) is used in order to know when to
2527 * insert the "digit group symbol".
2529 while (nNumberDecimal > 0)
2531 i = nCounter + nStep;
2532 memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
2533 nCounter++;
2534 j++;
2535 if (j >= nGrouping)
2537 j = 0;
2538 if (nRuleIndex < sRule[0])
2539 nRuleIndex++;
2540 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
2541 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
2542 nStep+= strlen(sDigitGroupSymbol);
2545 nNumberDecimal--;
2548 memcpy(sDestination + i+1, "\0", 1);
2549 /* Get the string in the right order ! */
2550 OLE_InvertString(sDestination);
2552 else
2554 nLZ = atoi(sILZero);
2555 if (nLZ != 0)
2557 /* Use 0.xxx instead of .xxx */
2558 memcpy(sDestination, "0", 1);
2559 memcpy(sDestination+1, "\0", 1);
2561 else
2562 memcpy(sDestination, "\0", 1);
2566 /* Second, format the digits after the decimal. */
2567 j = 0;
2568 nCounter = nNumberDigits;
2569 if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
2571 i = strlen(sNumber) - strlen(pStr) + 1;
2572 strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
2573 j = strlen(sDigitsAfterDecimal);
2574 if (j < nNumberDigits)
2575 nCounter = nNumberDigits-j;
2577 for (i=0;i<nCounter;i++)
2578 memcpy(sDigitsAfterDecimal+i+j, "0", 1);
2579 memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);
2581 i = strlen(sDestination);
2582 j = strlen(sDigitsAfterDecimal);
2583 /* Finally, construct the resulting formatted string. */
2585 for (nCounter=0; nCounter<i; nCounter++)
2586 memcpy(sNumber + nCounter, sDestination + nCounter, 1);
2588 memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
2590 for (i=0; i<j; i++)
2591 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
2592 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
2594 /* Is it a negative number */
2595 if (bNegative == TRUE)
2597 i = atoi(sNegNumber);
2598 pStr = sDestination;
2599 pTmpStr = sNumber;
2600 switch (i)
2602 case 0:
2603 *pStr++ = '(';
2604 while (*sNumber != '\0')
2605 *pStr++ = *pTmpStr++;
2606 *pStr++ = ')';
2607 break;
2608 case 1:
2609 *pStr++ = '-';
2610 while (*pTmpStr != '\0')
2611 *pStr++ = *pTmpStr++;
2612 break;
2613 case 2:
2614 *pStr++ = '-';
2615 *pStr++ = ' ';
2616 while (*pTmpStr != '\0')
2617 *pStr++ = *pTmpStr++;
2618 break;
2619 case 3:
2620 while (*pTmpStr != '\0')
2621 *pStr++ = *pTmpStr++;
2622 *pStr++ = '-';
2623 break;
2624 case 4:
2625 while (*pTmpStr != '\0')
2626 *pStr++ = *pTmpStr++;
2627 *pStr++ = ' ';
2628 *pStr++ = '-';
2629 break;
2630 default:
2631 while (*pTmpStr != '\0')
2632 *pStr++ = *pTmpStr++;
2633 break;
2636 else
2637 strcpy(sDestination, sNumber);
2639 /* If cchNumber is zero, then returns the number of bytes or characters
2640 * required to hold the formatted number string
2642 retVal = strlen(sDestination) + 1;
2643 if (cchNumber!=0)
2645 memcpy( lpNumberStr, sDestination, min(cchNumber, retVal) );
2646 if (cchNumber < retVal) retVal = 0;
2648 return retVal;
2651 /**************************************************************************
2652 * GetNumberFormatW (KERNEL32.@)
2654 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
2655 LPCWSTR lpvalue, const NUMBERFMTW * lpFormat,
2656 LPWSTR lpNumberStr, int cchNumber)
2658 FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue));
2660 lstrcpynW( lpNumberStr, lpvalue, cchNumber );
2661 return cchNumber? strlenW( lpNumberStr ) : 0;
2664 /**************************************************************************
2665 * GetCurrencyFormatA (KERNEL32.@)
2667 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
2668 LPCSTR lpvalue, const CURRENCYFMTA * lpFormat,
2669 LPSTR lpCurrencyStr, int cchCurrency)
2671 UINT nPosOrder, nNegOrder;
2672 INT retVal;
2673 char sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
2674 char *pDestination = sDestination;
2675 char sNumberFormated[128];
2676 char *pNumberFormated = sNumberFormated;
2677 LCID systemDefaultLCID;
2678 BOOL bIsPositive = FALSE, bValidFormat = FALSE;
2679 enum Operations
2681 USE_PARAMETER,
2682 USE_LOCALEINFO,
2683 USE_SYSTEMDEFAULT,
2684 RETURN_ERROR
2685 } used_operation;
2687 NUMBERFMTA numberFmt;
2689 /* Which source will we use to format the string */
2690 used_operation = RETURN_ERROR;
2691 if (lpFormat != NULL)
2693 if (dwflags == 0)
2694 used_operation = USE_PARAMETER;
2696 else
2698 if (dwflags & LOCALE_NOUSEROVERRIDE)
2699 used_operation = USE_SYSTEMDEFAULT;
2700 else
2701 used_operation = USE_LOCALEINFO;
2704 /* Load the fields we need */
2705 switch(used_operation)
2707 case USE_LOCALEINFO:
2708 /* Specific to CURRENCYFMTA */
2709 GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
2710 GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
2711 GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
2713 nPosOrder = atoi(sPosOrder);
2714 nNegOrder = atoi(sNegOrder);
2715 break;
2716 case USE_PARAMETER:
2717 /* Specific to CURRENCYFMTA */
2718 nNegOrder = lpFormat->NegativeOrder;
2719 nPosOrder = lpFormat->PositiveOrder;
2720 strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
2721 break;
2722 case USE_SYSTEMDEFAULT:
2723 systemDefaultLCID = GetSystemDefaultLCID();
2724 /* Specific to CURRENCYFMTA */
2725 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
2726 GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
2727 GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
2729 nPosOrder = atoi(sPosOrder);
2730 nNegOrder = atoi(sNegOrder);
2731 break;
2732 default:
2733 SetLastError(ERROR_INVALID_PARAMETER);
2734 return 0;
2737 /* Construct a temporary number format structure */
2738 if (lpFormat != NULL)
2740 numberFmt.NumDigits = lpFormat->NumDigits;
2741 numberFmt.LeadingZero = lpFormat->LeadingZero;
2742 numberFmt.Grouping = lpFormat->Grouping;
2743 numberFmt.NegativeOrder = 0;
2744 numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
2745 numberFmt.lpThousandSep = lpFormat->lpThousandSep;
2746 bValidFormat = TRUE;
2749 /* Make a call to GetNumberFormatA() */
2750 if (*lpvalue == '-')
2752 bIsPositive = FALSE;
2753 retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
2755 else
2757 bIsPositive = TRUE;
2758 retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
2761 if (retVal == 0)
2762 return 0;
2764 /* construct the formatted string */
2765 if (bIsPositive)
2767 switch (nPosOrder)
2769 case 0: /* Prefix, no separation */
2770 strcpy (pDestination, sCurrencySymbol);
2771 strcat (pDestination, pNumberFormated);
2772 break;
2773 case 1: /* Suffix, no separation */
2774 strcpy (pDestination, pNumberFormated);
2775 strcat (pDestination, sCurrencySymbol);
2776 break;
2777 case 2: /* Prefix, 1 char separation */
2778 strcpy (pDestination, sCurrencySymbol);
2779 strcat (pDestination, " ");
2780 strcat (pDestination, pNumberFormated);
2781 break;
2782 case 3: /* Suffix, 1 char separation */
2783 strcpy (pDestination, pNumberFormated);
2784 strcat (pDestination, " ");
2785 strcat (pDestination, sCurrencySymbol);
2786 break;
2787 default:
2788 SetLastError(ERROR_INVALID_PARAMETER);
2789 return 0;
2792 else /* negative number */
2794 switch (nNegOrder)
2796 case 0: /* format: ($1.1) */
2797 strcpy (pDestination, "(");
2798 strcat (pDestination, sCurrencySymbol);
2799 strcat (pDestination, pNumberFormated);
2800 strcat (pDestination, ")");
2801 break;
2802 case 1: /* format: -$1.1 */
2803 strcpy (pDestination, "-");
2804 strcat (pDestination, sCurrencySymbol);
2805 strcat (pDestination, pNumberFormated);
2806 break;
2807 case 2: /* format: $-1.1 */
2808 strcpy (pDestination, sCurrencySymbol);
2809 strcat (pDestination, "-");
2810 strcat (pDestination, pNumberFormated);
2811 break;
2812 case 3: /* format: $1.1- */
2813 strcpy (pDestination, sCurrencySymbol);
2814 strcat (pDestination, pNumberFormated);
2815 strcat (pDestination, "-");
2816 break;
2817 case 4: /* format: (1.1$) */
2818 strcpy (pDestination, "(");
2819 strcat (pDestination, pNumberFormated);
2820 strcat (pDestination, sCurrencySymbol);
2821 strcat (pDestination, ")");
2822 break;
2823 case 5: /* format: -1.1$ */
2824 strcpy (pDestination, "-");
2825 strcat (pDestination, pNumberFormated);
2826 strcat (pDestination, sCurrencySymbol);
2827 break;
2828 case 6: /* format: 1.1-$ */
2829 strcpy (pDestination, pNumberFormated);
2830 strcat (pDestination, "-");
2831 strcat (pDestination, sCurrencySymbol);
2832 break;
2833 case 7: /* format: 1.1$- */
2834 strcpy (pDestination, pNumberFormated);
2835 strcat (pDestination, sCurrencySymbol);
2836 strcat (pDestination, "-");
2837 break;
2838 case 8: /* format: -1.1 $ */
2839 strcpy (pDestination, "-");
2840 strcat (pDestination, pNumberFormated);
2841 strcat (pDestination, " ");
2842 strcat (pDestination, sCurrencySymbol);
2843 break;
2844 case 9: /* format: -$ 1.1 */
2845 strcpy (pDestination, "-");
2846 strcat (pDestination, sCurrencySymbol);
2847 strcat (pDestination, " ");
2848 strcat (pDestination, pNumberFormated);
2849 break;
2850 case 10: /* format: 1.1 $- */
2851 strcpy (pDestination, pNumberFormated);
2852 strcat (pDestination, " ");
2853 strcat (pDestination, sCurrencySymbol);
2854 strcat (pDestination, "-");
2855 break;
2856 case 11: /* format: $ 1.1- */
2857 strcpy (pDestination, sCurrencySymbol);
2858 strcat (pDestination, " ");
2859 strcat (pDestination, pNumberFormated);
2860 strcat (pDestination, "-");
2861 break;
2862 case 12: /* format: $ -1.1 */
2863 strcpy (pDestination, sCurrencySymbol);
2864 strcat (pDestination, " ");
2865 strcat (pDestination, "-");
2866 strcat (pDestination, pNumberFormated);
2867 break;
2868 case 13: /* format: 1.1- $ */
2869 strcpy (pDestination, pNumberFormated);
2870 strcat (pDestination, "-");
2871 strcat (pDestination, " ");
2872 strcat (pDestination, sCurrencySymbol);
2873 break;
2874 case 14: /* format: ($ 1.1) */
2875 strcpy (pDestination, "(");
2876 strcat (pDestination, sCurrencySymbol);
2877 strcat (pDestination, " ");
2878 strcat (pDestination, pNumberFormated);
2879 strcat (pDestination, ")");
2880 break;
2881 case 15: /* format: (1.1 $) */
2882 strcpy (pDestination, "(");
2883 strcat (pDestination, pNumberFormated);
2884 strcat (pDestination, " ");
2885 strcat (pDestination, sCurrencySymbol);
2886 strcat (pDestination, ")");
2887 break;
2888 default:
2889 SetLastError(ERROR_INVALID_PARAMETER);
2890 return 0;
2894 retVal = strlen(pDestination) + 1;
2896 if (cchCurrency)
2898 memcpy( lpCurrencyStr, pDestination, min(cchCurrency, retVal) );
2899 if (cchCurrency < retVal) retVal = 0;
2901 return retVal;
2904 /**************************************************************************
2905 * GetCurrencyFormatW (KERNEL32.@)
2907 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
2908 LPCWSTR lpvalue, const CURRENCYFMTW * lpFormat,
2909 LPWSTR lpCurrencyStr, int cchCurrency)
2911 FIXME("This API function is NOT implemented !\n");
2912 return 0;
2915 /******************************************************************************
2916 * OLE2NLS_CheckLocale [intern]
2918 static LCID OLE2NLS_CheckLocale (LCID locale)
2920 if (!locale)
2921 { locale = LOCALE_SYSTEM_DEFAULT;
2924 if (locale == LOCALE_SYSTEM_DEFAULT)
2925 { return GetSystemDefaultLCID();
2927 else if (locale == LOCALE_USER_DEFAULT)
2928 { return GetUserDefaultLCID();
2930 else
2931 { return locale;
2934 /******************************************************************************
2935 * GetTimeFormatA [KERNEL32.@]
2936 * Makes an ASCII string of the time
2938 * Formats date according to format, or locale default if format is
2939 * NULL. The format consists of literal characters and fields as follows:
2941 * h hours with no leading zero (12-hour)
2942 * hh hours with full two digits
2943 * H hours with no leading zero (24-hour)
2944 * HH hours with full two digits
2945 * m minutes with no leading zero
2946 * mm minutes with full two digits
2947 * s seconds with no leading zero
2948 * ss seconds with full two digits
2949 * t time marker (A or P)
2950 * tt time marker (AM, PM)
2953 INT WINAPI
2954 GetTimeFormatA(LCID locale, /* [in] */
2955 DWORD flags, /* [in] */
2956 const SYSTEMTIME* xtime, /* [in] */
2957 LPCSTR format, /* [in] */
2958 LPSTR timestr, /* [out] */
2959 INT timelen /* [in] */)
2960 { char format_buf[40];
2961 LPCSTR thisformat;
2962 SYSTEMTIME t;
2963 const SYSTEMTIME* thistime;
2964 LCID thislocale=0;
2965 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
2966 INT ret;
2968 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
2970 thislocale = OLE2NLS_CheckLocale ( locale );
2972 if (format == NULL)
2973 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
2974 { thislocale = GetSystemDefaultLCID();
2976 GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
2977 thisformat = format_buf;
2979 else
2980 { thisformat = format;
2983 if (xtime == NULL) /* NULL means use the current local time */
2984 { GetLocalTime(&t);
2985 thistime = &t;
2987 else
2988 { thistime = xtime;
2989 /* Check that hour,min and sec is in range */
2991 ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
2992 timestr, timelen);
2993 return ret;
2997 /******************************************************************************
2998 * GetTimeFormatW [KERNEL32.@]
2999 * Makes a Unicode string of the time
3001 INT WINAPI
3002 GetTimeFormatW(LCID locale, /* [in] */
3003 DWORD flags, /* [in] */
3004 const SYSTEMTIME* xtime, /* [in] */
3005 LPCWSTR format, /* [in] */
3006 LPWSTR timestr, /* [out] */
3007 INT timelen /* [in] */)
3008 { WCHAR format_buf[40];
3009 LPCWSTR thisformat;
3010 SYSTEMTIME t;
3011 const SYSTEMTIME* thistime;
3012 LCID thislocale=0;
3013 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3014 INT ret;
3016 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
3017 xtime,debugstr_w(format),timestr,timelen);
3019 thislocale = OLE2NLS_CheckLocale ( locale );
3021 if (format == NULL)
3022 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3023 { thislocale = GetSystemDefaultLCID();
3025 GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
3026 thisformat = format_buf;
3028 else
3029 { thisformat = format;
3032 if (xtime == NULL) /* NULL means use the current local time */
3033 { GetLocalTime(&t);
3034 thistime = &t;
3036 else
3037 { thistime = xtime;
3040 ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
3041 timestr, timelen, 0);
3042 return ret;
3045 /******************************************************************************
3046 * EnumCalendarInfoA [KERNEL32.@]
3048 BOOL WINAPI EnumCalendarInfoA(
3049 CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
3051 FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
3052 return FALSE;