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
24 #include "wine/port.h"
37 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(nls
);
47 /******************************************************************************
48 * ConvertDefaultLocale (KERNEL32.@)
50 LCID WINAPI
ConvertDefaultLocale (LCID lcid
)
52 { case LOCALE_SYSTEM_DEFAULT
:
53 return GetSystemDefaultLCID();
54 case LOCALE_USER_DEFAULT
:
55 return GetUserDefaultLCID();
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
)))
75 static BOOL CALLBACK
EnumResourceLanguagesProcW(HMODULE hModule
, LPCWSTR type
,
76 LPCWSTR name
, WORD LangID
, LONG lParam
)
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
,
92 TRACE("(%p,%08lx)\n", lpfnLocaleEnum
,flags
);
94 EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW
,
95 (LPCWSTR
)LOCALE_ILANGUAGE
, EnumResourceLanguagesProcW
,
96 (LONG
)lpfnLocaleEnum
);
101 static BOOL CALLBACK
EnumResourceLanguagesProcA(HMODULE hModule
, LPCSTR type
,
102 LPCSTR name
, WORD LangID
, LONG lParam
)
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
,
116 TRACE("(%p,%08lx)\n", lpfnLocaleEnum
,flags
);
118 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA
,
119 (LPCSTR
)LOCALE_ILANGUAGE
, EnumResourceLanguagesProcA
,
120 (LONG
)lpfnLocaleEnum
);
125 /***********************************************************************
126 * VerLanguageNameA [KERNEL32.@]
128 DWORD WINAPI
VerLanguageNameA( UINT wLang
, LPSTR szLang
, UINT nSize
)
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
)
144 return GetLocaleInfoW(MAKELCID(wLang
, SORT_DEFAULT
), LOCALE_SENGLANGUAGE
, szLang
, nSize
);
148 static const unsigned char LCM_Unicode_LUT
[] = {
182 7 , 29, /* " - 34 */ /* " */
187 6 , 128, /* ' - 39 */
193 6 , 130, /* - - 45 */
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 */
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 */
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 */
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
[] = {
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
)
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,...
592 * Success : length of the result string.
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
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 */
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
);
630 srclen
= strlen(srcstr
) + 1 ; /* (include final '\0') */
632 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE | \
636 NORM_IGNORENONSPACE | \
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",
653 mapflags
& ~LCMAPSTRINGA_SUPPORTED_FLAGS
657 if ( !(mapflags
& LCMAP_SORTKEY
) )
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
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
])) )
688 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
692 if (mapflags
& LCMAP_UPPERCASE
)
694 else if (mapflags
& LCMAP_LOWERCASE
)
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
]);
709 /* FIXME: This function completely ignores the "lcid" parameter. */
710 /* else ... (mapflags & LCMAP_SORTKEY) */
715 int delayed_punctuation_len
=0;
716 char *case_component
;
717 char *diacritic_component
;
718 char *delayed_punctuation_component
;
720 int flag_stringsort
= mapflags
& SORT_STRINGSORT
;
722 /* compute how much room we will need */
723 for (i
=0;i
<srclen
;i
++)
726 unsigned char source_char
= srcstr
[i
];
727 if (source_char
!='\0')
729 if (flag_stringsort
|| !OLE2NLS_isPunctuation(source_char
))
732 if ( LCM_Unicode_LUT
[-2+2*source_char
] & ~15 )
733 unicode_len
++; /* double letter */
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
)
751 if (mapflags
& NORM_IGNORENONSPACE
)
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' */
762 else if (dstlen
<room
)
764 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
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
);
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')
792 type
= LCM_Unicode_LUT
[-2+2*source_char
];
793 longcode
= type
>> 4;
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 */
810 dststr
[2*count
] = type
;
811 dststr
[2*count
+1] = LCM_Unicode_LUT
[-1+2*source_char
];
815 case_component
[count
] = ( isupper(source_char
) ? 18 : 2 ) ;
816 if (count
<diacritic_len
)
817 diacritic_component
[count
] = 2; /* assumption: a double letter
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 */
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);
839 dststr
[room
-1] = '\0';
844 /*************************************************************************
845 * LCMapStringW [KERNEL32.@]
847 * Convert a string, or generate a sort key from it.
851 * See LCMapStringA for documentation
853 INT WINAPI
LCMapStringW(
854 LCID lcid
,DWORD mapflags
,LPCWSTR srcstr
,INT srclen
,LPWSTR dststr
,
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
);
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. */
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
);
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,
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
);
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
);
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
);
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
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",
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
);
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
,
986 /* Since this is an internal error I'm not sure what the correct
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
);
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
);
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
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
,
1032 /* Since this is an internal error I'm not sure what the correct
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
);
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
);
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
);
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
,
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
);
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));
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
);
1119 int (*f
)(int)=identity
;
1125 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1129 if (mapflags
& LCMAP_UPPERCASE
)
1131 else if (mapflags
& LCMAP_LOWERCASE
)
1133 for (i
=0; i
< srclen
; i
++)
1134 dststr
[i
] = (WCHAR
) f(srcstr
[i
]);
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
1166 * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
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.
1177 * This implementation ignores the locale
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
;
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
);
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
);
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");
1229 /* strcmp doesn't necessarily return -1, 0, or 1 */
1230 result
= strcmp(sk1
,sk2
);
1232 HeapFree(GetProcessHeap(),0,sk1
);
1239 /* must be greater, if we reach this point */
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
)
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 */
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
);
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
);
1323 return strcmpiW( str1
, str2
);
1327 /******************************************************************************
1328 * OLE_GetFormatW [Internal]
1330 * dateformat is set TRUE if being called for a date, false for a time
1332 This function implements stuff for GetDateFormat() and
1335 d single-digit (no leading zero) day (of month)
1336 dd two-digit day (of month)
1337 ddd short day-of-week name
1338 dddd long day-of-week name
1339 M single-digit month
1341 MMM short month name
1342 MMMM full month name
1343 y two-digit year, no leading 0
1345 yyyy four-digit year
1347 h hours with no leading zero (12-hour)
1348 hh hours with full two digits
1349 H hours with no leading zero (24-hour)
1350 HH hours with full two digits
1351 m minutes with no leading zero
1352 mm minutes with full two digits
1353 s seconds with no leading zero
1354 ss seconds with full two digits
1355 t time marker (A or P)
1356 tt time marker (AM, PM)
1357 '' used to quote literal characters
1358 '' (within a quoted string) indicates a literal '
1360 These functions REQUIRE valid locale, date, and format.
1362 static INT
OLE_GetFormatW(LCID locale
, DWORD flags
, DWORD tflags
,
1363 const SYSTEMTIME
* xtime
,
1365 LPWSTR output
, INT outlen
, int dateformat
)
1369 /* make a debug report */
1370 TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
1371 "%p with max len %d\n",
1372 locale
, flags
, tflags
,
1373 xtime
->wDay
, xtime
->wHour
, xtime
->wMinute
, xtime
->wSecond
,
1374 debugstr_w(format
), format
, output
, outlen
);
1376 /* initialize state variables */
1380 /* Literal string: Maybe terminated early by a \0 */
1381 if (*format
== (WCHAR
) '\'') {
1384 if (*format
== (WCHAR
) '\'') {
1386 if (*format
!= '\'') {
1387 break; /* It was a terminating quote */
1391 /* We are counting */;
1392 else if (outpos
>= outlen
)
1395 output
[outpos
] = *format
;
1399 } else if ( (dateformat
&& (*format
=='d' ||
1403 (!dateformat
&& (*format
=='H' ||
1414 for (count
= 1; *format
== type
; format
++)
1420 GetLocaleInfoW(locale
,
1421 LOCALE_SDAYNAME1
+ (xtime
->wDayOfWeek
+6)%7,
1422 buf
, sizeof(buf
)/sizeof(WCHAR
) );
1423 } else if (count
== 3) {
1424 GetLocaleInfoW(locale
,
1425 LOCALE_SABBREVDAYNAME1
+
1426 (xtime
->wDayOfWeek
+6)%7,
1427 buf
, sizeof(buf
)/sizeof(WCHAR
) );
1429 sprintf( tmp
, "%.*d", count
, xtime
->wDay
);
1430 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1436 GetLocaleInfoW(locale
, LOCALE_SMONTHNAME1
+
1437 xtime
->wMonth
-1, buf
,
1438 sizeof(buf
)/sizeof(WCHAR
) );
1439 } else if (count
== 3) {
1440 GetLocaleInfoW(locale
, LOCALE_SABBREVMONTHNAME1
+
1441 xtime
->wMonth
-1, buf
,
1442 sizeof(buf
)/sizeof(WCHAR
) );
1444 sprintf( tmp
, "%.*d", count
, xtime
->wMonth
);
1445 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1450 sprintf( tmp
, "%d", xtime
->wYear
);
1452 sprintf( tmp
, "%.*d", count
> 2 ? 2 : count
, xtime
->wYear
% 100 );
1454 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1459 FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
1460 strcpy( tmp
, "AD" );
1462 /* Win API sez we copy it verbatim */
1465 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1469 /* hours 1:00-12:00 --- is this right? */
1470 sprintf( tmp
, "%.*d", count
> 2 ? 2 : count
, (xtime
->wHour
-1)%12 +1);
1471 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1475 sprintf( tmp
, "%.*d", count
> 2 ? 2 : count
, xtime
->wHour
);
1476 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1480 sprintf( tmp
, "%.*d", count
> 2 ? 2 : count
, xtime
->wMinute
);
1481 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1485 sprintf( tmp
, "%.*d", count
> 2 ? 2 : count
, xtime
->wSecond
);
1486 MultiByteToWideChar( CP_ACP
, 0, tmp
, -1, buf
, sizeof(buf
)/sizeof(WCHAR
) );
1490 GetLocaleInfoW(locale
, (xtime
->wHour
< 12) ?
1491 LOCALE_S1159
: LOCALE_S2359
,
1499 /* cat buf onto the output */
1500 buflen
= strlenW(buf
);
1502 /* We are counting */;
1503 else if (outpos
+ buflen
< outlen
) {
1504 strcpyW( output
+ outpos
, buf
);
1506 lstrcpynW( output
+ outpos
, buf
, outlen
- outpos
);
1507 /* Is this an undocumented feature we are supporting? */
1512 /* a literal character */
1514 /* We are counting */;
1515 else if (outpos
>= outlen
)
1518 output
[outpos
] = *format
;
1524 /* final string terminator and sanity check */
1526 /* We are counting */;
1527 else if (outpos
>= outlen
)
1530 output
[outpos
] = '\0';
1533 TRACE(" returning %d %s\n", outpos
, debugstr_w(output
));
1537 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1538 WARN(" buffer overflow\n");
1543 /******************************************************************************
1544 * GetDateFormatA [KERNEL32.@]
1545 * Makes an ASCII string of the date
1547 * Acts the same as GetDateFormatW(), except that it's ASCII.
1549 INT WINAPI
GetDateFormatA(LCID locale
,DWORD flags
,
1550 const SYSTEMTIME
* xtime
,
1551 LPCSTR format
, LPSTR date
, INT datelen
)
1554 LPWSTR wformat
= NULL
;
1555 LPWSTR wdate
= NULL
;
1559 wformat
= HeapAlloc(GetProcessHeap(), 0,
1560 (strlen(format
) + 1) * sizeof(wchar_t));
1562 MultiByteToWideChar(CP_ACP
, 0, format
, -1, wformat
, strlen(format
) + 1);
1565 if (date
&& datelen
)
1567 wdate
= HeapAlloc(GetProcessHeap(), 0,
1568 (datelen
+ 1) * sizeof(wchar_t));
1571 ret
= GetDateFormatW(locale
, flags
, xtime
, wformat
, wdate
, datelen
);
1575 WideCharToMultiByte(CP_ACP
, 0, wdate
, ret
, date
, datelen
, NULL
, NULL
);
1576 HeapFree(GetProcessHeap(), 0, wdate
);
1581 HeapFree(GetProcessHeap(), 0, wformat
);
1587 /******************************************************************************
1588 * GetDateFormatW [KERNEL32.@]
1589 * Makes a Unicode string of the date
1591 * This function uses format to format the date, or, if format
1592 * is NULL, uses the default for the locale. format is a string
1593 * of literal fields and characters as follows:
1595 * - d single-digit (no leading zero) day (of month)
1596 * - dd two-digit day (of month)
1597 * - ddd short day-of-week name
1598 * - dddd long day-of-week name
1599 * - M single-digit month
1600 * - MM two-digit month
1601 * - MMM short month name
1602 * - MMMM full month name
1603 * - y two-digit year, no leading 0
1604 * - yy two-digit year
1605 * - yyyy four-digit year
1608 * Accepts & returns sizes as counts of Unicode characters.
1610 INT WINAPI
GetDateFormatW(LCID locale
,DWORD flags
,
1611 const SYSTEMTIME
* xtime
,
1613 LPWSTR date
, INT datelen
)
1615 WCHAR format_buf
[40];
1618 LPSYSTEMTIME thistime
;
1624 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
1625 locale
,flags
,xtime
,debugstr_w(format
),date
,datelen
);
1627 /* Tests (could be left until OLE_GetFormatW) */
1628 if (flags
&& format
)
1630 SetLastError (ERROR_INVALID_FLAGS
);
1633 if (datelen
&& !date
)
1635 SetLastError (ERROR_INVALID_PARAMETER
);
1639 locale
= LOCALE_SYSTEM_DEFAULT
;
1642 if (locale
== LOCALE_SYSTEM_DEFAULT
) {
1643 thislocale
= GetSystemDefaultLCID();
1644 } else if (locale
== LOCALE_USER_DEFAULT
) {
1645 thislocale
= GetUserDefaultLCID();
1647 thislocale
= locale
;
1650 if (xtime
== NULL
) {
1653 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
1654 res
=SystemTimeToFileTime(xtime
,&ft
);
1655 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
1656 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
1658 SetLastError(ERROR_INVALID_PARAMETER
);
1661 FileTimeToSystemTime(&ft
,&t
);
1666 if (format
== NULL
) {
1667 GetLocaleInfoW(thislocale
, ((flags
&DATE_LONGDATE
)
1669 : LOCALE_SSHORTDATE
),
1670 format_buf
, sizeof(format_buf
)/sizeof(*format_buf
));
1671 thisformat
= format_buf
;
1673 thisformat
= format
;
1677 ret
= OLE_GetFormatW(thislocale
, flags
, 0, thistime
, thisformat
,
1681 TRACE("GetDateFormatW() returning %d, with data=%s\n",
1682 ret
, debugstr_w(date
));
1686 /**************************************************************************
1687 * EnumDateFormatsA (KERNEL32.@)
1689 BOOL WINAPI
EnumDateFormatsA(
1690 DATEFMT_ENUMPROCA lpDateFmtEnumProc
, LCID Locale
, DWORD dwFlags
)
1692 LCID Loc
= GetUserDefaultLCID();
1693 if(!lpDateFmtEnumProc
)
1695 SetLastError(ERROR_INVALID_PARAMETER
);
1702 case 0x00000407: /* (Loc,"de_DE") */
1706 case DATE_SHORTDATE
:
1707 if(!(*lpDateFmtEnumProc
)("dd.MM.yy")) return TRUE
;
1708 if(!(*lpDateFmtEnumProc
)("d.M.yyyy")) return TRUE
;
1709 if(!(*lpDateFmtEnumProc
)("d.MM.yy")) return TRUE
;
1710 if(!(*lpDateFmtEnumProc
)("d.M.yy")) return TRUE
;
1713 if(!(*lpDateFmtEnumProc
)("dddd,d. MMMM yyyy")) return TRUE
;
1714 if(!(*lpDateFmtEnumProc
)("d. MMMM yyyy")) return TRUE
;
1715 if(!(*lpDateFmtEnumProc
)("d. MMM yyyy")) return TRUE
;
1718 FIXME("Unknown date format (%ld)\n", dwFlags
);
1719 SetLastError(ERROR_INVALID_PARAMETER
);
1724 case 0x0000040c: /* (Loc,"fr_FR") */
1728 case DATE_SHORTDATE
:
1729 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1730 if(!(*lpDateFmtEnumProc
)("dd.MM.yy")) return TRUE
;
1731 if(!(*lpDateFmtEnumProc
)("dd-MM-yy")) return TRUE
;
1732 if(!(*lpDateFmtEnumProc
)("dd/MM/yyyy")) return TRUE
;
1735 if(!(*lpDateFmtEnumProc
)("dddd d MMMM yyyy")) return TRUE
;
1736 if(!(*lpDateFmtEnumProc
)("d MMM yy")) return TRUE
;
1737 if(!(*lpDateFmtEnumProc
)("d MMMM yyyy")) return TRUE
;
1740 FIXME("Unknown date format (%ld)\n", dwFlags
);
1741 SetLastError(ERROR_INVALID_PARAMETER
);
1746 case 0x00000c0c: /* (Loc,"fr_CA") */
1750 case DATE_SHORTDATE
:
1751 if(!(*lpDateFmtEnumProc
)("yy-MM-dd")) return TRUE
;
1752 if(!(*lpDateFmtEnumProc
)("dd-MM-yy")) return TRUE
;
1753 if(!(*lpDateFmtEnumProc
)("yy MM dd")) return TRUE
;
1754 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1757 if(!(*lpDateFmtEnumProc
)("d MMMM, yyyy")) return TRUE
;
1758 if(!(*lpDateFmtEnumProc
)("d MMM yyyy")) return TRUE
;
1761 FIXME("Unknown date format (%ld)\n", dwFlags
);
1762 SetLastError(ERROR_INVALID_PARAMETER
);
1767 case 0x00000809: /* (Loc,"en_UK") */
1771 case DATE_SHORTDATE
:
1772 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1773 if(!(*lpDateFmtEnumProc
)("dd/MM/yyyy")) return TRUE
;
1774 if(!(*lpDateFmtEnumProc
)("d/M/yy")) return TRUE
;
1775 if(!(*lpDateFmtEnumProc
)("d.M.yy")) return TRUE
;
1778 if(!(*lpDateFmtEnumProc
)("dd MMMM yyyy")) return TRUE
;
1779 if(!(*lpDateFmtEnumProc
)("d MMMM yyyy")) return TRUE
;
1782 FIXME("Unknown date format (%ld)\n", dwFlags
);
1783 SetLastError(ERROR_INVALID_PARAMETER
);
1788 case 0x00000c09: /* (Loc,"en_AU") */
1792 case DATE_SHORTDATE
:
1793 if(!(*lpDateFmtEnumProc
)("d/MM/yy")) return TRUE
;
1794 if(!(*lpDateFmtEnumProc
)("d/M/yy")) return TRUE
;
1795 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1798 if(!(*lpDateFmtEnumProc
)("dddd,d MMMM yyyy")) return TRUE
;
1799 if(!(*lpDateFmtEnumProc
)("d MMMM yyyy")) return TRUE
;
1802 FIXME("Unknown date format (%ld)\n", dwFlags
);
1803 SetLastError(ERROR_INVALID_PARAMETER
);
1808 case 0x00001009: /* (Loc,"en_CA") */
1812 case DATE_SHORTDATE
:
1813 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1814 if(!(*lpDateFmtEnumProc
)("d/M/yy")) return TRUE
;
1815 if(!(*lpDateFmtEnumProc
)("yy-MM-dd")) return TRUE
;
1816 if(!(*lpDateFmtEnumProc
)("M/dd/yy")) return TRUE
;
1819 if(!(*lpDateFmtEnumProc
)("d-MMM-yy")) return TRUE
;
1820 if(!(*lpDateFmtEnumProc
)("MMMM d, yyyy")) return TRUE
;
1823 FIXME("Unknown date format (%ld)\n", dwFlags
);
1824 SetLastError(ERROR_INVALID_PARAMETER
);
1829 case 0x00001409: /* (Loc,"en_NZ") */
1833 case DATE_SHORTDATE
:
1834 if(!(*lpDateFmtEnumProc
)("d/MM/yy")) return TRUE
;
1835 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1836 if(!(*lpDateFmtEnumProc
)("d.MM.yy")) return TRUE
;
1839 if(!(*lpDateFmtEnumProc
)("d MMMM yyyy")) return TRUE
;
1840 if(!(*lpDateFmtEnumProc
)("dddd, d MMMM yyyy")) return TRUE
;
1843 FIXME("Unknown date format (%ld)\n", dwFlags
);
1844 SetLastError(ERROR_INVALID_PARAMETER
);
1849 case 0x00001809: /* (Loc,"en_IE") */
1853 case DATE_SHORTDATE
:
1854 if(!(*lpDateFmtEnumProc
)("dd/MM/yy")) return TRUE
;
1855 if(!(*lpDateFmtEnumProc
)("d/M/yy")) return TRUE
;
1856 if(!(*lpDateFmtEnumProc
)("d.M.yy")) return TRUE
;
1859 if(!(*lpDateFmtEnumProc
)("dd MMMM yyyy")) return TRUE
;
1860 if(!(*lpDateFmtEnumProc
)("d MMMM yyyy")) return TRUE
;
1863 FIXME("Unknown date format (%ld)\n", dwFlags
);
1864 SetLastError(ERROR_INVALID_PARAMETER
);
1869 case 0x00001c09: /* (Loc,"en_ZA") */
1873 case DATE_SHORTDATE
:
1874 if(!(*lpDateFmtEnumProc
)("yy/MM/dd")) return TRUE
;
1877 if(!(*lpDateFmtEnumProc
)("dd MMMM yyyy")) return TRUE
;
1880 FIXME("Unknown date format (%ld)\n", dwFlags
);
1881 SetLastError(ERROR_INVALID_PARAMETER
);
1886 case 0x00002009: /* (Loc,"en_JM") */
1890 case DATE_SHORTDATE
:
1891 if(!(*lpDateFmtEnumProc
)("dd/MM/yyyy")) return TRUE
;
1894 if(!(*lpDateFmtEnumProc
)("dddd,MMMM dd,yyyy")) return TRUE
;
1895 if(!(*lpDateFmtEnumProc
)("MMMM dd,yyyy")) return TRUE
;
1896 if(!(*lpDateFmtEnumProc
)("dddd,dd MMMM,yyyy")) return TRUE
;
1897 if(!(*lpDateFmtEnumProc
)("dd MMMM,yyyy")) return TRUE
;
1900 FIXME("Unknown date format (%ld)\n", dwFlags
);
1901 SetLastError(ERROR_INVALID_PARAMETER
);
1906 case 0x00002809: /* (Loc,"en_BZ") */
1907 case 0x00002c09: /* (Loc,"en_TT") */
1911 case DATE_SHORTDATE
:
1912 if(!(*lpDateFmtEnumProc
)("dd/MM/yyyy")) return TRUE
;
1915 if(!(*lpDateFmtEnumProc
)("dddd,dd MMMM yyyy")) return TRUE
;
1918 FIXME("Unknown date format (%ld)\n", dwFlags
);
1919 SetLastError(ERROR_INVALID_PARAMETER
);
1924 default: /* default to US English "en_US" */
1928 case DATE_SHORTDATE
:
1929 if(!(*lpDateFmtEnumProc
)("M/d/yy")) return TRUE
;
1930 if(!(*lpDateFmtEnumProc
)("M/d/yyyy")) return TRUE
;
1931 if(!(*lpDateFmtEnumProc
)("MM/dd/yy")) return TRUE
;
1932 if(!(*lpDateFmtEnumProc
)("MM/dd/yyyy")) return TRUE
;
1933 if(!(*lpDateFmtEnumProc
)("yy/MM/dd")) return TRUE
;
1934 if(!(*lpDateFmtEnumProc
)("dd-MMM-yy")) return TRUE
;
1937 if(!(*lpDateFmtEnumProc
)("dddd, MMMM dd, yyyy")) return TRUE
;
1938 if(!(*lpDateFmtEnumProc
)("MMMM dd, yyyy")) return TRUE
;
1939 if(!(*lpDateFmtEnumProc
)("dddd, dd MMMM, yyyy")) return TRUE
;
1940 if(!(*lpDateFmtEnumProc
)("dd MMMM, yyyy")) return TRUE
;
1943 FIXME("Unknown date format (%ld)\n", dwFlags
);
1944 SetLastError(ERROR_INVALID_PARAMETER
);
1951 /**************************************************************************
1952 * EnumDateFormatsW (KERNEL32.@)
1954 BOOL WINAPI
EnumDateFormatsW(
1955 DATEFMT_ENUMPROCW lpDateFmtEnumProc
, LCID Locale
, DWORD dwFlags
)
1957 FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc
, Locale
, dwFlags
);
1958 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1962 /**************************************************************************
1963 * EnumTimeFormatsA (KERNEL32.@)
1965 BOOL WINAPI
EnumTimeFormatsA(
1966 TIMEFMT_ENUMPROCA lpTimeFmtEnumProc
, LCID Locale
, DWORD dwFlags
)
1968 LCID Loc
= GetUserDefaultLCID();
1969 if(!lpTimeFmtEnumProc
)
1971 SetLastError(ERROR_INVALID_PARAMETER
);
1976 FIXME("Unknown time format (%ld)\n", dwFlags
);
1981 case 0x00000407: /* (Loc,"de_DE") */
1983 if(!(*lpTimeFmtEnumProc
)("HH.mm")) return TRUE
;
1984 if(!(*lpTimeFmtEnumProc
)("HH:mm:ss")) return TRUE
;
1985 if(!(*lpTimeFmtEnumProc
)("H:mm:ss")) return TRUE
;
1986 if(!(*lpTimeFmtEnumProc
)("H.mm")) return TRUE
;
1987 if(!(*lpTimeFmtEnumProc
)("H.mm'Uhr'")) return TRUE
;
1991 case 0x0000040c: /* (Loc,"fr_FR") */
1992 case 0x00000c0c: /* (Loc,"fr_CA") */
1994 if(!(*lpTimeFmtEnumProc
)("H:mm")) return TRUE
;
1995 if(!(*lpTimeFmtEnumProc
)("HH:mm:ss")) return TRUE
;
1996 if(!(*lpTimeFmtEnumProc
)("H:mm:ss")) return TRUE
;
1997 if(!(*lpTimeFmtEnumProc
)("HH.mm")) return TRUE
;
1998 if(!(*lpTimeFmtEnumProc
)("HH'h'mm")) return TRUE
;
2002 case 0x00000809: /* (Loc,"en_UK") */
2003 case 0x00000c09: /* (Loc,"en_AU") */
2004 case 0x00001409: /* (Loc,"en_NZ") */
2005 case 0x00001809: /* (Loc,"en_IE") */
2007 if(!(*lpTimeFmtEnumProc
)("h:mm:ss tt")) return TRUE
;
2008 if(!(*lpTimeFmtEnumProc
)("HH:mm:ss")) return TRUE
;
2009 if(!(*lpTimeFmtEnumProc
)("H:mm:ss")) return TRUE
;
2013 case 0x00001c09: /* (Loc,"en_ZA") */
2014 case 0x00002809: /* (Loc,"en_BZ") */
2015 case 0x00002c09: /* (Loc,"en_TT") */
2017 if(!(*lpTimeFmtEnumProc
)("h:mm:ss tt")) return TRUE
;
2018 if(!(*lpTimeFmtEnumProc
)("hh:mm:ss tt")) return TRUE
;
2022 default: /* default to US style "en_US" */
2024 if(!(*lpTimeFmtEnumProc
)("h:mm:ss tt")) return TRUE
;
2025 if(!(*lpTimeFmtEnumProc
)("hh:mm:ss tt")) return TRUE
;
2026 if(!(*lpTimeFmtEnumProc
)("H:mm:ss")) return TRUE
;
2027 if(!(*lpTimeFmtEnumProc
)("HH:mm:ss")) return TRUE
;
2033 /**************************************************************************
2034 * EnumTimeFormatsW (KERNEL32.@)
2036 BOOL WINAPI
EnumTimeFormatsW(
2037 TIMEFMT_ENUMPROCW lpTimeFmtEnumProc
, LCID Locale
, DWORD dwFlags
)
2039 FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc
, Locale
, dwFlags
);
2040 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2044 /**************************************************************************
2045 * This function is used just locally !
2046 * Description: Inverts a string.
2048 static void OLE_InvertString(char* string
)
2050 char sTmpArray
[128];
2053 for (counter
= strlen(string
); counter
> 0; counter
--)
2055 memcpy(sTmpArray
+ i
, string
+ counter
-1, 1);
2058 memcpy(sTmpArray
+ i
, "\0", 1);
2059 strcpy(string
, sTmpArray
);
2062 /***************************************************************************************
2063 * This function is used just locally !
2064 * Description: Test if the given string (psNumber) is valid or not.
2065 * The valid characters are the following:
2066 * - Characters '0' through '9'.
2067 * - One decimal point (dot) if the number is a floating-point value.
2068 * - A minus sign in the first character position if the number is
2070 * If the function succeeds, psBefore/psAfter will point to the string
2071 * on the right/left of the decimal symbol. pbNegative indicates if the
2072 * number is negative.
2074 static INT
OLE_GetNumberComponents(char* pInput
, char* psBefore
, char* psAfter
, BOOL
* pbNegative
)
2076 char sNumberSet
[] = "0123456789";
2077 BOOL bInDecimal
= FALSE
;
2079 /* Test if we do have a minus sign */
2080 if ( *pInput
== '-' )
2083 pInput
++; /* Jump to the next character. */
2086 while(*pInput
!= '\0')
2088 /* Do we have a valid numeric character */
2089 if ( strchr(sNumberSet
, *pInput
) != NULL
)
2091 if (bInDecimal
== TRUE
)
2092 *psAfter
++ = *pInput
;
2094 *psBefore
++ = *pInput
;
2098 /* Is this a decimal point (dot) */
2099 if ( *pInput
== '.' )
2101 /* Is it the first time we find it */
2102 if ((bInDecimal
== FALSE
))
2105 return -1; /* ERROR: Invalid parameter */
2109 /* It's neither a numeric character, nor a decimal point.
2110 * Thus, return an error.
2118 /* Add an End of Line character to the output buffers */
2125 /**************************************************************************
2126 * This function is used just locally !
2127 * Description: A number could be formatted using different numbers
2128 * of "digits in group" (example: 4;3;2;0).
2129 * The first parameter of this function is an array
2130 * containing the rule to be used. Its format is the following:
2131 * |NDG|DG1|DG2|...|0|
2132 * where NDG is the number of used "digits in group" and DG1, DG2,
2133 * are the corresponding "digits in group".
2134 * Thus, this function returns the grouping value in the array
2135 * pointed by the second parameter.
2137 static INT
OLE_GetGrouping(char* sRule
, INT index
)
2139 char sData
[2], sRuleSize
[2];
2140 INT nData
, nRuleSize
;
2142 memcpy(sRuleSize
, sRule
, 1);
2143 memcpy(sRuleSize
+1, "\0", 1);
2144 nRuleSize
= atoi(sRuleSize
);
2146 if (index
> 0 && index
< nRuleSize
)
2148 memcpy(sData
, sRule
+index
, 1);
2149 memcpy(sData
+1, "\0", 1);
2150 nData
= atoi(sData
);
2155 memcpy(sData
, sRule
+nRuleSize
-1, 1);
2156 memcpy(sData
+1, "\0", 1);
2157 nData
= atoi(sData
);
2163 /**************************************************************************
2164 * GetNumberFormatA (KERNEL32.@)
2166 INT WINAPI
GetNumberFormatA(LCID locale
, DWORD dwflags
,
2167 LPCSTR lpvalue
, const NUMBERFMTA
* lpFormat
,
2168 LPSTR lpNumberStr
, int cchNumber
)
2170 char sNumberDigits
[3], sDecimalSymbol
[5], sDigitsInGroup
[11], sDigitGroupSymbol
[5], sILZero
[2];
2171 INT nNumberDigits
, nNumberDecimal
, i
, j
, nCounter
, nStep
, nRuleIndex
, nGrouping
, nDigits
, retVal
, nLZ
;
2172 char sNumber
[128], sDestination
[128], sDigitsAfterDecimal
[10], sDigitsBeforeDecimal
[128];
2173 char sRule
[10], sSemiColumn
[]=";", sBuffer
[5], sNegNumber
[2];
2174 char *pStr
= NULL
, *pTmpStr
= NULL
;
2175 LCID systemDefaultLCID
;
2176 BOOL bNegative
= FALSE
;
2185 strncpy(sNumber
, lpvalue
, 128);
2186 sNumber
[127] = '\0';
2188 /* Make sure we have a valid input string, get the number
2189 * of digits before and after the decimal symbol, and check
2190 * if this is a negative number.
2192 if ( OLE_GetNumberComponents(sNumber
, sDigitsBeforeDecimal
, sDigitsAfterDecimal
, &bNegative
) != -1)
2194 nNumberDecimal
= strlen(sDigitsBeforeDecimal
);
2195 nDigits
= strlen(sDigitsAfterDecimal
);
2199 SetLastError(ERROR_INVALID_PARAMETER
);
2203 /* Which source will we use to format the string */
2204 used_operation
= RETURN_ERROR
;
2205 if (lpFormat
!= NULL
)
2208 used_operation
= USE_PARAMETER
;
2212 if (dwflags
& LOCALE_NOUSEROVERRIDE
)
2213 used_operation
= USE_SYSTEMDEFAULT
;
2215 used_operation
= USE_LOCALEINFO
;
2218 /* Load the fields we need */
2219 switch(used_operation
)
2221 case USE_LOCALEINFO
:
2222 GetLocaleInfoA(locale
, LOCALE_IDIGITS
, sNumberDigits
, sizeof(sNumberDigits
));
2223 GetLocaleInfoA(locale
, LOCALE_SDECIMAL
, sDecimalSymbol
, sizeof(sDecimalSymbol
));
2224 GetLocaleInfoA(locale
, LOCALE_SGROUPING
, sDigitsInGroup
, sizeof(sDigitsInGroup
));
2225 GetLocaleInfoA(locale
, LOCALE_STHOUSAND
, sDigitGroupSymbol
, sizeof(sDigitGroupSymbol
));
2226 GetLocaleInfoA(locale
, LOCALE_ILZERO
, sILZero
, sizeof(sILZero
));
2227 GetLocaleInfoA(locale
, LOCALE_INEGNUMBER
, sNegNumber
, sizeof(sNegNumber
));
2230 sprintf(sNumberDigits
, "%d",lpFormat
->NumDigits
);
2231 strcpy(sDecimalSymbol
, lpFormat
->lpDecimalSep
);
2232 sprintf(sDigitsInGroup
, "%d;0",lpFormat
->Grouping
);
2233 /* Win95-WinME only allow 0-9 for grouping, no matter what MSDN says. */
2234 strcpy(sDigitGroupSymbol
, lpFormat
->lpThousandSep
);
2235 sprintf(sILZero
, "%d",lpFormat
->LeadingZero
);
2236 sprintf(sNegNumber
, "%d",lpFormat
->NegativeOrder
);
2238 case USE_SYSTEMDEFAULT
:
2239 systemDefaultLCID
= GetSystemDefaultLCID();
2240 GetLocaleInfoA(systemDefaultLCID
, LOCALE_IDIGITS
, sNumberDigits
, sizeof(sNumberDigits
));
2241 GetLocaleInfoA(systemDefaultLCID
, LOCALE_SDECIMAL
, sDecimalSymbol
, sizeof(sDecimalSymbol
));
2242 GetLocaleInfoA(systemDefaultLCID
, LOCALE_SGROUPING
, sDigitsInGroup
, sizeof(sDigitsInGroup
));
2243 GetLocaleInfoA(systemDefaultLCID
, LOCALE_STHOUSAND
, sDigitGroupSymbol
, sizeof(sDigitGroupSymbol
));
2244 GetLocaleInfoA(systemDefaultLCID
, LOCALE_ILZERO
, sILZero
, sizeof(sILZero
));
2245 GetLocaleInfoA(systemDefaultLCID
, LOCALE_INEGNUMBER
, sNegNumber
, sizeof(sNegNumber
));
2248 SetLastError(ERROR_INVALID_PARAMETER
);
2252 nNumberDigits
= atoi(sNumberDigits
);
2254 /* Remove the ";" */
2257 for (nCounter
=0; nCounter
<strlen(sDigitsInGroup
); nCounter
++)
2259 if ( memcmp(sDigitsInGroup
+ nCounter
, sSemiColumn
, 1) != 0 )
2261 memcpy(sRule
+ j
, sDigitsInGroup
+ nCounter
, 1);
2266 sprintf(sBuffer
, "%d", i
);
2267 memcpy(sRule
, sBuffer
, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
2268 memcpy(sRule
+ j
, "\0", 1);
2270 /* First, format the digits before the decimal. */
2271 if ((nNumberDecimal
>0) && (atoi(sDigitsBeforeDecimal
) != 0))
2273 /* Working on an inverted string is easier ! */
2274 OLE_InvertString(sDigitsBeforeDecimal
);
2276 nStep
= nCounter
= i
= j
= 0;
2278 nGrouping
= OLE_GetGrouping(sRule
, nRuleIndex
);
2279 if (nGrouping
== 0) /* If the first grouping is zero */
2280 nGrouping
= nNumberDecimal
; /* Don't do grouping */
2282 /* Here, we will loop until we reach the end of the string.
2283 * An internal counter (j) is used in order to know when to
2284 * insert the "digit group symbol".
2286 while (nNumberDecimal
> 0)
2288 i
= nCounter
+ nStep
;
2289 memcpy(sDestination
+ i
, sDigitsBeforeDecimal
+ nCounter
, 1);
2295 if (nRuleIndex
< sRule
[0])
2297 nGrouping
= OLE_GetGrouping(sRule
, nRuleIndex
);
2298 memcpy(sDestination
+ i
+1, sDigitGroupSymbol
, strlen(sDigitGroupSymbol
));
2299 nStep
+= strlen(sDigitGroupSymbol
);
2305 memcpy(sDestination
+ i
+1, "\0", 1);
2306 /* Get the string in the right order ! */
2307 OLE_InvertString(sDestination
);
2311 nLZ
= atoi(sILZero
);
2314 /* Use 0.xxx instead of .xxx */
2315 memcpy(sDestination
, "0", 1);
2316 memcpy(sDestination
+1, "\0", 1);
2319 memcpy(sDestination
, "\0", 1);
2323 /* Second, format the digits after the decimal. */
2325 nCounter
= nNumberDigits
;
2326 if ( (nDigits
>0) && (pStr
= strstr (sNumber
, ".")) )
2328 i
= strlen(sNumber
) - strlen(pStr
) + 1;
2329 strncpy ( sDigitsAfterDecimal
, sNumber
+ i
, nNumberDigits
);
2330 j
= strlen(sDigitsAfterDecimal
);
2331 if (j
< nNumberDigits
)
2332 nCounter
= nNumberDigits
-j
;
2334 for (i
=0;i
<nCounter
;i
++)
2335 memcpy(sDigitsAfterDecimal
+i
+j
, "0", 1);
2336 memcpy(sDigitsAfterDecimal
+ nNumberDigits
, "\0", 1);
2338 i
= strlen(sDestination
);
2339 j
= strlen(sDigitsAfterDecimal
);
2340 /* Finally, construct the resulting formatted string. */
2342 for (nCounter
=0; nCounter
<i
; nCounter
++)
2343 memcpy(sNumber
+ nCounter
, sDestination
+ nCounter
, 1);
2345 memcpy(sNumber
+ nCounter
, sDecimalSymbol
, strlen(sDecimalSymbol
));
2348 memcpy(sNumber
+ nCounter
+i
+strlen(sDecimalSymbol
), sDigitsAfterDecimal
+ i
, 1);
2349 memcpy(sNumber
+ nCounter
+i
+ (i
? strlen(sDecimalSymbol
) : 0), "\0", 1);
2351 /* Is it a negative number */
2352 if (bNegative
== TRUE
)
2354 i
= atoi(sNegNumber
);
2355 pStr
= sDestination
;
2361 while (*sNumber
!= '\0')
2362 *pStr
++ = *pTmpStr
++;
2367 while (*pTmpStr
!= '\0')
2368 *pStr
++ = *pTmpStr
++;
2373 while (*pTmpStr
!= '\0')
2374 *pStr
++ = *pTmpStr
++;
2377 while (*pTmpStr
!= '\0')
2378 *pStr
++ = *pTmpStr
++;
2382 while (*pTmpStr
!= '\0')
2383 *pStr
++ = *pTmpStr
++;
2388 while (*pTmpStr
!= '\0')
2389 *pStr
++ = *pTmpStr
++;
2394 strcpy(sDestination
, sNumber
);
2396 /* If cchNumber is zero, then returns the number of bytes or characters
2397 * required to hold the formatted number string
2399 retVal
= strlen(sDestination
) + 1;
2402 memcpy( lpNumberStr
, sDestination
, min(cchNumber
, retVal
) );
2403 if (cchNumber
< retVal
) retVal
= 0;
2408 /**************************************************************************
2409 * GetNumberFormatW (KERNEL32.@)
2411 INT WINAPI
GetNumberFormatW(LCID locale
, DWORD dwflags
,
2412 LPCWSTR lpvalue
, const NUMBERFMTW
* lpFormat
,
2413 LPWSTR lpNumberStr
, int cchNumber
)
2415 FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue
));
2417 lstrcpynW( lpNumberStr
, lpvalue
, cchNumber
);
2418 return cchNumber
? strlenW( lpNumberStr
) : 0;
2421 /**************************************************************************
2422 * GetCurrencyFormatA (KERNEL32.@)
2424 INT WINAPI
GetCurrencyFormatA(LCID locale
, DWORD dwflags
,
2425 LPCSTR lpvalue
, const CURRENCYFMTA
* lpFormat
,
2426 LPSTR lpCurrencyStr
, int cchCurrency
)
2428 UINT nPosOrder
, nNegOrder
;
2430 char sDestination
[128], sNegOrder
[8], sPosOrder
[8], sCurrencySymbol
[8];
2431 char *pDestination
= sDestination
;
2432 char sNumberFormated
[128];
2433 char *pNumberFormated
= sNumberFormated
;
2434 LCID systemDefaultLCID
;
2435 BOOL bIsPositive
= FALSE
, bValidFormat
= FALSE
;
2444 NUMBERFMTA numberFmt
;
2446 /* Which source will we use to format the string */
2447 used_operation
= RETURN_ERROR
;
2448 if (lpFormat
!= NULL
)
2451 used_operation
= USE_PARAMETER
;
2455 if (dwflags
& LOCALE_NOUSEROVERRIDE
)
2456 used_operation
= USE_SYSTEMDEFAULT
;
2458 used_operation
= USE_LOCALEINFO
;
2461 /* Load the fields we need */
2462 switch(used_operation
)
2464 case USE_LOCALEINFO
:
2465 /* Specific to CURRENCYFMTA */
2466 GetLocaleInfoA(locale
, LOCALE_INEGCURR
, sNegOrder
, sizeof(sNegOrder
));
2467 GetLocaleInfoA(locale
, LOCALE_ICURRENCY
, sPosOrder
, sizeof(sPosOrder
));
2468 GetLocaleInfoA(locale
, LOCALE_SCURRENCY
, sCurrencySymbol
, sizeof(sCurrencySymbol
));
2470 nPosOrder
= atoi(sPosOrder
);
2471 nNegOrder
= atoi(sNegOrder
);
2474 /* Specific to CURRENCYFMTA */
2475 nNegOrder
= lpFormat
->NegativeOrder
;
2476 nPosOrder
= lpFormat
->PositiveOrder
;
2477 strcpy(sCurrencySymbol
, lpFormat
->lpCurrencySymbol
);
2479 case USE_SYSTEMDEFAULT
:
2480 systemDefaultLCID
= GetSystemDefaultLCID();
2481 /* Specific to CURRENCYFMTA */
2482 GetLocaleInfoA(systemDefaultLCID
, LOCALE_INEGCURR
, sNegOrder
, sizeof(sNegOrder
));
2483 GetLocaleInfoA(systemDefaultLCID
, LOCALE_ICURRENCY
, sPosOrder
, sizeof(sPosOrder
));
2484 GetLocaleInfoA(systemDefaultLCID
, LOCALE_SCURRENCY
, sCurrencySymbol
, sizeof(sCurrencySymbol
));
2486 nPosOrder
= atoi(sPosOrder
);
2487 nNegOrder
= atoi(sNegOrder
);
2490 SetLastError(ERROR_INVALID_PARAMETER
);
2494 /* Construct a temporary number format structure */
2495 if (lpFormat
!= NULL
)
2497 numberFmt
.NumDigits
= lpFormat
->NumDigits
;
2498 numberFmt
.LeadingZero
= lpFormat
->LeadingZero
;
2499 numberFmt
.Grouping
= lpFormat
->Grouping
;
2500 numberFmt
.NegativeOrder
= 0;
2501 numberFmt
.lpDecimalSep
= lpFormat
->lpDecimalSep
;
2502 numberFmt
.lpThousandSep
= lpFormat
->lpThousandSep
;
2503 bValidFormat
= TRUE
;
2506 /* Make a call to GetNumberFormatA() */
2507 if (*lpvalue
== '-')
2509 bIsPositive
= FALSE
;
2510 retVal
= GetNumberFormatA(locale
,0,lpvalue
+1,(bValidFormat
)?&numberFmt
:NULL
,pNumberFormated
,128);
2515 retVal
= GetNumberFormatA(locale
,0,lpvalue
,(bValidFormat
)?&numberFmt
:NULL
,pNumberFormated
,128);
2521 /* construct the formatted string */
2526 case 0: /* Prefix, no separation */
2527 strcpy (pDestination
, sCurrencySymbol
);
2528 strcat (pDestination
, pNumberFormated
);
2530 case 1: /* Suffix, no separation */
2531 strcpy (pDestination
, pNumberFormated
);
2532 strcat (pDestination
, sCurrencySymbol
);
2534 case 2: /* Prefix, 1 char separation */
2535 strcpy (pDestination
, sCurrencySymbol
);
2536 strcat (pDestination
, " ");
2537 strcat (pDestination
, pNumberFormated
);
2539 case 3: /* Suffix, 1 char separation */
2540 strcpy (pDestination
, pNumberFormated
);
2541 strcat (pDestination
, " ");
2542 strcat (pDestination
, sCurrencySymbol
);
2545 SetLastError(ERROR_INVALID_PARAMETER
);
2549 else /* negative number */
2553 case 0: /* format: ($1.1) */
2554 strcpy (pDestination
, "(");
2555 strcat (pDestination
, sCurrencySymbol
);
2556 strcat (pDestination
, pNumberFormated
);
2557 strcat (pDestination
, ")");
2559 case 1: /* format: -$1.1 */
2560 strcpy (pDestination
, "-");
2561 strcat (pDestination
, sCurrencySymbol
);
2562 strcat (pDestination
, pNumberFormated
);
2564 case 2: /* format: $-1.1 */
2565 strcpy (pDestination
, sCurrencySymbol
);
2566 strcat (pDestination
, "-");
2567 strcat (pDestination
, pNumberFormated
);
2569 case 3: /* format: $1.1- */
2570 strcpy (pDestination
, sCurrencySymbol
);
2571 strcat (pDestination
, pNumberFormated
);
2572 strcat (pDestination
, "-");
2574 case 4: /* format: (1.1$) */
2575 strcpy (pDestination
, "(");
2576 strcat (pDestination
, pNumberFormated
);
2577 strcat (pDestination
, sCurrencySymbol
);
2578 strcat (pDestination
, ")");
2580 case 5: /* format: -1.1$ */
2581 strcpy (pDestination
, "-");
2582 strcat (pDestination
, pNumberFormated
);
2583 strcat (pDestination
, sCurrencySymbol
);
2585 case 6: /* format: 1.1-$ */
2586 strcpy (pDestination
, pNumberFormated
);
2587 strcat (pDestination
, "-");
2588 strcat (pDestination
, sCurrencySymbol
);
2590 case 7: /* format: 1.1$- */
2591 strcpy (pDestination
, pNumberFormated
);
2592 strcat (pDestination
, sCurrencySymbol
);
2593 strcat (pDestination
, "-");
2595 case 8: /* format: -1.1 $ */
2596 strcpy (pDestination
, "-");
2597 strcat (pDestination
, pNumberFormated
);
2598 strcat (pDestination
, " ");
2599 strcat (pDestination
, sCurrencySymbol
);
2601 case 9: /* format: -$ 1.1 */
2602 strcpy (pDestination
, "-");
2603 strcat (pDestination
, sCurrencySymbol
);
2604 strcat (pDestination
, " ");
2605 strcat (pDestination
, pNumberFormated
);
2607 case 10: /* format: 1.1 $- */
2608 strcpy (pDestination
, pNumberFormated
);
2609 strcat (pDestination
, " ");
2610 strcat (pDestination
, sCurrencySymbol
);
2611 strcat (pDestination
, "-");
2613 case 11: /* format: $ 1.1- */
2614 strcpy (pDestination
, sCurrencySymbol
);
2615 strcat (pDestination
, " ");
2616 strcat (pDestination
, pNumberFormated
);
2617 strcat (pDestination
, "-");
2619 case 12: /* format: $ -1.1 */
2620 strcpy (pDestination
, sCurrencySymbol
);
2621 strcat (pDestination
, " ");
2622 strcat (pDestination
, "-");
2623 strcat (pDestination
, pNumberFormated
);
2625 case 13: /* format: 1.1- $ */
2626 strcpy (pDestination
, pNumberFormated
);
2627 strcat (pDestination
, "-");
2628 strcat (pDestination
, " ");
2629 strcat (pDestination
, sCurrencySymbol
);
2631 case 14: /* format: ($ 1.1) */
2632 strcpy (pDestination
, "(");
2633 strcat (pDestination
, sCurrencySymbol
);
2634 strcat (pDestination
, " ");
2635 strcat (pDestination
, pNumberFormated
);
2636 strcat (pDestination
, ")");
2638 case 15: /* format: (1.1 $) */
2639 strcpy (pDestination
, "(");
2640 strcat (pDestination
, pNumberFormated
);
2641 strcat (pDestination
, " ");
2642 strcat (pDestination
, sCurrencySymbol
);
2643 strcat (pDestination
, ")");
2646 SetLastError(ERROR_INVALID_PARAMETER
);
2651 retVal
= strlen(pDestination
) + 1;
2655 memcpy( lpCurrencyStr
, pDestination
, min(cchCurrency
, retVal
) );
2656 if (cchCurrency
< retVal
) retVal
= 0;
2661 /**************************************************************************
2662 * GetCurrencyFormatW (KERNEL32.@)
2664 INT WINAPI
GetCurrencyFormatW(LCID locale
, DWORD dwflags
,
2665 LPCWSTR lpvalue
, const CURRENCYFMTW
* lpFormat
,
2666 LPWSTR lpCurrencyStr
, int cchCurrency
)
2668 FIXME("This API function is NOT implemented !\n");
2672 /******************************************************************************
2673 * OLE2NLS_CheckLocale [intern]
2675 static LCID
OLE2NLS_CheckLocale (LCID locale
)
2678 { locale
= LOCALE_SYSTEM_DEFAULT
;
2681 if (locale
== LOCALE_SYSTEM_DEFAULT
)
2682 { return GetSystemDefaultLCID();
2684 else if (locale
== LOCALE_USER_DEFAULT
)
2685 { return GetUserDefaultLCID();
2691 /******************************************************************************
2692 * GetTimeFormatA [KERNEL32.@]
2693 * Makes an ASCII string of the time
2695 * Formats date according to format, or locale default if format is
2696 * NULL. The format consists of literal characters and fields as follows:
2698 * h hours with no leading zero (12-hour)
2699 * hh hours with full two digits
2700 * H hours with no leading zero (24-hour)
2701 * HH hours with full two digits
2702 * m minutes with no leading zero
2703 * mm minutes with full two digits
2704 * s seconds with no leading zero
2705 * ss seconds with full two digits
2706 * t time marker (A or P)
2707 * tt time marker (AM, PM)
2711 GetTimeFormatA(LCID locale
, /* [in] */
2712 DWORD flags
, /* [in] */
2713 const SYSTEMTIME
* xtime
, /* [in] */
2714 LPCSTR format
, /* [in] */
2715 LPSTR timestr
, /* [out] */
2716 INT timelen
/* [in] */)
2719 LPWSTR wformat
= NULL
;
2720 LPWSTR wtime
= NULL
;
2724 wformat
= HeapAlloc(GetProcessHeap(), 0,
2725 (strlen(format
) + 1) * sizeof(wchar_t));
2726 MultiByteToWideChar(CP_ACP
, 0, format
, -1, wformat
, strlen(format
) + 1);
2729 if (timestr
&& timelen
)
2731 wtime
= HeapAlloc(GetProcessHeap(), 0,
2732 (timelen
+ 1) * sizeof(wchar_t));
2735 ret
= GetTimeFormatW(locale
, flags
, xtime
, wformat
, wtime
, timelen
);
2739 WideCharToMultiByte(CP_ACP
, 0, wtime
, ret
, timestr
, timelen
, NULL
, NULL
);
2740 HeapFree(GetProcessHeap(), 0, wtime
);
2745 HeapFree(GetProcessHeap(), 0, wformat
);
2752 /******************************************************************************
2753 * GetTimeFormatW [KERNEL32.@]
2754 * Makes a Unicode string of the time
2757 GetTimeFormatW(LCID locale
, /* [in] */
2758 DWORD flags
, /* [in] */
2759 const SYSTEMTIME
* xtime
, /* [in] */
2760 LPCWSTR format
, /* [in] */
2761 LPWSTR timestr
, /* [out] */
2762 INT timelen
/* [in] */)
2763 { WCHAR format_buf
[40];
2766 const SYSTEMTIME
* thistime
;
2768 DWORD thisflags
=LOCALE_STIMEFORMAT
; /* standard timeformat */
2771 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale
,flags
,
2772 xtime
,debugstr_w(format
),timestr
,timelen
);
2774 thislocale
= OLE2NLS_CheckLocale ( locale
);
2777 { if (flags
& LOCALE_NOUSEROVERRIDE
) /* use system default */
2778 { thislocale
= GetSystemDefaultLCID();
2780 GetLocaleInfoW(thislocale
, thisflags
, format_buf
, 40);
2781 thisformat
= format_buf
;
2784 { thisformat
= format
;
2787 if (xtime
== NULL
) /* NULL means use the current local time */
2795 ret
= OLE_GetFormatW(thislocale
, thisflags
, flags
, thistime
, thisformat
,
2796 timestr
, timelen
, 0);
2800 /******************************************************************************
2801 * EnumCalendarInfoA [KERNEL32.@]
2803 BOOL WINAPI
EnumCalendarInfoA(
2804 CALINFO_ENUMPROCA calinfoproc
,LCID locale
,CALID calendar
,CALTYPE caltype
2806 FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc
,locale
,calendar
,caltype
);