Run make clean in all directories, even the ones we don't compile in.
[wine/multimedia.git] / ole / ole2nls.c
bloba3320d74088b206e2d3734e7506847b1c9312691
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 "wine/unicode.h"
37 #include "options.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(string);
46 /* Locale name to id map. used by EnumSystemLocales, GetLocaleInfoA
47 * MUST contain all #defines from winnls.h
48 * last entry has NULL name, 0 id.
49 */
50 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
51 static const struct tagLOCALE_NAME2ID {
52 const char *name;
53 LCTYPE id;
54 } locale_name2id[]= {
55 LOCALE_ENTRY(ILANGUAGE),
56 LOCALE_ENTRY(SLANGUAGE),
57 LOCALE_ENTRY(SENGLANGUAGE),
58 LOCALE_ENTRY(SABBREVLANGNAME),
59 LOCALE_ENTRY(SNATIVELANGNAME),
60 LOCALE_ENTRY(ICOUNTRY),
61 LOCALE_ENTRY(SCOUNTRY),
62 LOCALE_ENTRY(SENGCOUNTRY),
63 LOCALE_ENTRY(SABBREVCTRYNAME),
64 LOCALE_ENTRY(SNATIVECTRYNAME),
65 LOCALE_ENTRY(IDEFAULTLANGUAGE),
66 LOCALE_ENTRY(IDEFAULTCOUNTRY),
67 LOCALE_ENTRY(IDEFAULTCODEPAGE),
68 LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
69 LOCALE_ENTRY(IDEFAULTMACCODEPAGE),
70 LOCALE_ENTRY(SLIST),
71 LOCALE_ENTRY(IMEASURE),
72 LOCALE_ENTRY(SDECIMAL),
73 LOCALE_ENTRY(STHOUSAND),
74 LOCALE_ENTRY(SGROUPING),
75 LOCALE_ENTRY(IDIGITS),
76 LOCALE_ENTRY(ILZERO),
77 LOCALE_ENTRY(INEGNUMBER),
78 LOCALE_ENTRY(SNATIVEDIGITS),
79 LOCALE_ENTRY(SCURRENCY),
80 LOCALE_ENTRY(SINTLSYMBOL),
81 LOCALE_ENTRY(SMONDECIMALSEP),
82 LOCALE_ENTRY(SMONTHOUSANDSEP),
83 LOCALE_ENTRY(SMONGROUPING),
84 LOCALE_ENTRY(ICURRDIGITS),
85 LOCALE_ENTRY(IINTLCURRDIGITS),
86 LOCALE_ENTRY(ICURRENCY),
87 LOCALE_ENTRY(INEGCURR),
88 LOCALE_ENTRY(SDATE),
89 LOCALE_ENTRY(STIME),
90 LOCALE_ENTRY(SSHORTDATE),
91 LOCALE_ENTRY(SLONGDATE),
92 LOCALE_ENTRY(STIMEFORMAT),
93 LOCALE_ENTRY(IDATE),
94 LOCALE_ENTRY(ILDATE),
95 LOCALE_ENTRY(ITIME),
96 LOCALE_ENTRY(ITIMEMARKPOSN),
97 LOCALE_ENTRY(ICENTURY),
98 LOCALE_ENTRY(ITLZERO),
99 LOCALE_ENTRY(IDAYLZERO),
100 LOCALE_ENTRY(IMONLZERO),
101 LOCALE_ENTRY(S1159),
102 LOCALE_ENTRY(S2359),
103 LOCALE_ENTRY(ICALENDARTYPE),
104 LOCALE_ENTRY(IOPTIONALCALENDAR),
105 LOCALE_ENTRY(IFIRSTDAYOFWEEK),
106 LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
107 LOCALE_ENTRY(SDAYNAME1),
108 LOCALE_ENTRY(SDAYNAME2),
109 LOCALE_ENTRY(SDAYNAME3),
110 LOCALE_ENTRY(SDAYNAME4),
111 LOCALE_ENTRY(SDAYNAME5),
112 LOCALE_ENTRY(SDAYNAME6),
113 LOCALE_ENTRY(SDAYNAME7),
114 LOCALE_ENTRY(SABBREVDAYNAME1),
115 LOCALE_ENTRY(SABBREVDAYNAME2),
116 LOCALE_ENTRY(SABBREVDAYNAME3),
117 LOCALE_ENTRY(SABBREVDAYNAME4),
118 LOCALE_ENTRY(SABBREVDAYNAME5),
119 LOCALE_ENTRY(SABBREVDAYNAME6),
120 LOCALE_ENTRY(SABBREVDAYNAME7),
121 LOCALE_ENTRY(SMONTHNAME1),
122 LOCALE_ENTRY(SMONTHNAME2),
123 LOCALE_ENTRY(SMONTHNAME3),
124 LOCALE_ENTRY(SMONTHNAME4),
125 LOCALE_ENTRY(SMONTHNAME5),
126 LOCALE_ENTRY(SMONTHNAME6),
127 LOCALE_ENTRY(SMONTHNAME7),
128 LOCALE_ENTRY(SMONTHNAME8),
129 LOCALE_ENTRY(SMONTHNAME9),
130 LOCALE_ENTRY(SMONTHNAME10),
131 LOCALE_ENTRY(SMONTHNAME11),
132 LOCALE_ENTRY(SMONTHNAME12),
133 LOCALE_ENTRY(SMONTHNAME13),
134 LOCALE_ENTRY(SABBREVMONTHNAME1),
135 LOCALE_ENTRY(SABBREVMONTHNAME2),
136 LOCALE_ENTRY(SABBREVMONTHNAME3),
137 LOCALE_ENTRY(SABBREVMONTHNAME4),
138 LOCALE_ENTRY(SABBREVMONTHNAME5),
139 LOCALE_ENTRY(SABBREVMONTHNAME6),
140 LOCALE_ENTRY(SABBREVMONTHNAME7),
141 LOCALE_ENTRY(SABBREVMONTHNAME8),
142 LOCALE_ENTRY(SABBREVMONTHNAME9),
143 LOCALE_ENTRY(SABBREVMONTHNAME10),
144 LOCALE_ENTRY(SABBREVMONTHNAME11),
145 LOCALE_ENTRY(SABBREVMONTHNAME12),
146 LOCALE_ENTRY(SABBREVMONTHNAME13),
147 LOCALE_ENTRY(SPOSITIVESIGN),
148 LOCALE_ENTRY(SNEGATIVESIGN),
149 LOCALE_ENTRY(IPOSSIGNPOSN),
150 LOCALE_ENTRY(INEGSIGNPOSN),
151 LOCALE_ENTRY(IPOSSYMPRECEDES),
152 LOCALE_ENTRY(IPOSSEPBYSPACE),
153 LOCALE_ENTRY(INEGSYMPRECEDES),
154 LOCALE_ENTRY(INEGSEPBYSPACE),
155 LOCALE_ENTRY(FONTSIGNATURE),
156 LOCALE_ENTRY(SISO639LANGNAME),
157 LOCALE_ENTRY(SISO3166CTRYNAME),
158 {NULL,0}
161 static char *GetLocaleSubkeyName( DWORD lctype );
163 /***********************************************************************
164 * GetUserDefaultLCID (KERNEL32.@)
166 LCID WINAPI GetUserDefaultLCID(void)
168 return MAKELCID( GetUserDefaultLangID() , SORT_DEFAULT );
171 /***********************************************************************
172 * GetSystemDefaultLCID (KERNEL32.@)
174 LCID WINAPI GetSystemDefaultLCID(void)
176 return GetUserDefaultLCID();
179 #define NLS_MAX_LANGUAGES 20
180 typedef struct {
181 char lang[128];
182 char country[128];
183 LANGID found_lang_id[NLS_MAX_LANGUAGES];
184 char found_language[NLS_MAX_LANGUAGES][3];
185 char found_country[NLS_MAX_LANGUAGES][3];
186 int n_found;
187 } LANG_FIND_DATA;
189 static BOOL CALLBACK NLS_FindLanguageID_ProcA(HMODULE hModule, LPCSTR type,
190 LPCSTR name, WORD LangID, LONG lParam)
192 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
193 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
194 char buf_language[128];
195 char buf_country[128];
196 char buf_en_language[128];
198 TRACE("%04X\n", (UINT)LangID);
199 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
200 return TRUE; /* continue search */
202 buf_language[0] = 0;
203 buf_country[0] = 0;
205 GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
206 buf_language, sizeof(buf_language));
207 TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
209 GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
210 buf_country, sizeof(buf_country));
211 TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
213 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
215 if(l_data->country && strlen(l_data->country) > 0)
217 if(!strcasecmp(l_data->country, buf_country))
219 l_data->found_lang_id[0] = LangID;
220 l_data->n_found = 1;
221 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
222 return FALSE; /* stop enumeration */
225 else /* l_data->country not specified */
227 if(l_data->n_found < NLS_MAX_LANGUAGES)
229 l_data->found_lang_id[l_data->n_found] = LangID;
230 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
231 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
232 l_data->n_found++;
233 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
234 return TRUE; /* continue search */
239 /* Just in case, check LOCALE_SENGLANGUAGE too,
240 * in hope that possible alias name might have that value.
242 buf_en_language[0] = 0;
243 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
244 buf_en_language, sizeof(buf_en_language));
245 TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
247 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
249 l_data->found_lang_id[l_data->n_found] = LangID;
250 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
251 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
252 l_data->n_found++;
253 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
256 return TRUE; /* continue search */
259 /***********************************************************************
260 * NLS_GetLanguageID
262 * INPUT:
263 * Lang: a string whose two first chars are the iso name of a language.
264 * Country: a string whose two first chars are the iso name of country
265 * Charset: a string defining the chossen charset encoding
266 * Dialect: a string defining a variation of the locale
268 * all those values are from the standardized format of locale
269 * name in unix which is: Lang[_Country][.Charset][@Dialect]
271 * RETURNS:
272 * the numeric code of the language used by Windows
274 * FIXME: Charset and Dialect are not handled
276 static LANGID NLS_GetLanguageID(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
278 LANG_FIND_DATA l_data;
279 char lang_string[256];
281 if(!Lang)
283 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
284 goto END;
287 memset(&l_data, 0, sizeof(LANG_FIND_DATA));
288 strncpy(l_data.lang, Lang, sizeof(l_data.lang));
290 if(Country && strlen(Country) > 0)
291 strncpy(l_data.country, Country, sizeof(l_data.country));
293 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
294 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
296 strcpy(lang_string, l_data.lang);
297 if(l_data.country && strlen(l_data.country) > 0)
299 strcat(lang_string, "_");
300 strcat(lang_string, l_data.country);
303 if(!l_data.n_found)
305 if(l_data.country && strlen(l_data.country) > 0)
307 MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
308 l_data.country[0] = 0;
309 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
310 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
314 /* Re-evaluate lang_string */
315 strcpy(lang_string, l_data.lang);
316 if(l_data.country && strlen(l_data.country) > 0)
318 strcat(lang_string, "_");
319 strcat(lang_string, l_data.country);
322 if(!l_data.n_found)
324 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
325 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
327 else
329 if(l_data.n_found == 1)
330 TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
331 else /* l_data->n_found > 1 */
333 int i;
334 MESSAGE("For language '%s' several language ids were found:\n", lang_string);
335 for(i = 0; i < l_data.n_found; i++)
336 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
338 MESSAGE("\nInstead of using first in the list, suggest to define\n"
339 "your LANG environment variable like this: LANG=%s_%s\n",
340 l_data.found_language[0], l_data.found_country[0]);
343 END:
344 TRACE("Returning %04X\n", l_data.found_lang_id[0]);
345 return l_data.found_lang_id[0];
348 /***********************************************************************
349 * GetUserDefaultLangID (KERNEL32.@)
351 LANGID WINAPI GetUserDefaultLangID(void)
353 /* caching result, if defined from environment, which should (?) not change during a WINE session */
354 static LANGID userLCID = 0;
356 if (userLCID == 0)
358 char buf[256];
359 char *lang,*country,*charset,*dialect,*next;
361 if (GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) )) goto ok;
362 if (GetEnvironmentVariableA( "LANG", buf, sizeof(buf) )) goto ok;
363 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) )) goto ok;
364 if (GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) )) goto ok;
365 if (GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) )) goto ok;
367 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
370 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C"))
371 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
373 lang=buf;
375 do {
376 next=strchr(lang,':'); if (next) *next++='\0';
377 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
378 charset=strchr(lang,'.'); if (charset) *charset++='\0';
379 country=strchr(lang,'_'); if (country) *country++='\0';
381 userLCID = NLS_GetLanguageID(lang, country, charset, dialect);
383 lang=next;
384 } while (lang && !userLCID);
386 if (!userLCID)
388 MESSAGE( "Warning: language '%s' not recognized, defaulting to English\n",
389 buf );
390 userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
393 return userLCID;
396 /***********************************************************************
397 * GetSystemDefaultLangID (KERNEL32.@)
399 LANGID WINAPI GetSystemDefaultLangID(void)
401 return GetUserDefaultLangID();
404 /******************************************************************************
405 * ConvertDefaultLocale (KERNEL32.@)
407 LCID WINAPI ConvertDefaultLocale (LCID lcid)
408 { switch (lcid)
409 { case LOCALE_SYSTEM_DEFAULT:
410 return GetSystemDefaultLCID();
411 case LOCALE_USER_DEFAULT:
412 return GetUserDefaultLCID();
413 case LOCALE_NEUTRAL:
414 return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
416 return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
419 /* Enhanced version of LoadStringW.
420 * It takes LanguageId to find and load language dependant resources.
421 * Resource is copied as is: "binary" strings are not truncated.
422 * Return: length of resource + 1 to distinguish absent resources
423 * from the resources with zero length.
425 static INT NLS_LoadStringExW(HMODULE hModule, LANGID lang_id, UINT res_id, LPWSTR buffer, INT buflen)
427 HRSRC hrsrc;
428 HGLOBAL hmem;
429 WCHAR *p;
430 int string_num;
431 int i;
433 /* Replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
434 if(SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
435 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
437 hrsrc = FindResourceExW(hModule, RT_STRINGW, (LPCWSTR)((res_id >> 4) + 1), lang_id);
439 if(!hrsrc) return 0;
440 hmem = LoadResource(hModule, hrsrc);
441 if(!hmem) return 0;
443 p = LockResource(hmem);
444 string_num = res_id & 0x000f;
445 for(i = 0; i < string_num; i++)
446 p += *p + 1;
448 TRACE("strlen = %d\n", (int)*p );
450 if (buffer == NULL) return *p;
451 i = min(buflen - 1, *p);
452 if (i > 0) {
453 memcpy(buffer, p + 1, i * sizeof (WCHAR));
454 buffer[i] = (WCHAR) 0;
455 } else {
456 if (buflen > 1)
457 buffer[0] = (WCHAR) 0;
460 FreeResource(hmem);
461 TRACE("%s loaded!\n", debugstr_w(buffer));
462 return (i + 1);
465 /******************************************************************************
466 * GetLocaleInfoA (KERNEL32.@)
468 * NOTES
469 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
471 * MS online documentation states that the string returned is NULL terminated
472 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
473 * terminated string".
475 INT WINAPI GetLocaleInfoA(LCID lcid,LCTYPE LCType,LPSTR buf,INT len)
477 LPCSTR retString = NULL;
478 int found = 0, i;
479 char *pacKey;
480 char acBuffer[128];
481 DWORD dwBufferSize=128;
482 BOOL NoUserOverride;
484 TRACE("(lcid=0x%lx,lctype=0x%lx,%p,%x)\n",lcid,LCType,buf,len);
486 if (len && (! buf) ) {
487 SetLastError(ERROR_INSUFFICIENT_BUFFER);
488 return 0;
491 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT)
493 lcid = GetSystemDefaultLCID();
495 else if (lcid == LANG_USER_DEFAULT) /*0x800*/
497 lcid = GetUserDefaultLCID();
500 /* LOCALE_NOUSEROVERRIDE means: do not get user redefined settings
501 from the registry. Instead, use system default values. */
502 NoUserOverride = (LCType & LOCALE_NOUSEROVERRIDE) != 0;
504 LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
506 /* First, check if it's in the registry. */
507 /* All user customized values are stored in the registry by SetLocaleInfo */
508 if ( !NoUserOverride && (pacKey = GetLocaleSubkeyName(LCType)) )
510 char acRealKey[128];
511 HKEY hKey;
513 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
515 if ( RegOpenKeyExA( HKEY_CURRENT_USER, acRealKey,
516 0, KEY_READ, &hKey) == ERROR_SUCCESS )
518 if ( RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)acBuffer,
519 &dwBufferSize ) == ERROR_SUCCESS )
521 retString = acBuffer;
522 found = 1;
524 RegCloseKey(hKey);
528 /* If not in the registry, get it from the NLS entries. */
529 if(!found) {
530 WCHAR wcBuffer[128];
531 int res_size;
533 /* check if language is registered in the kernel32 resources */
534 if((res_size = NLS_LoadStringExW(GetModuleHandleA("KERNEL32"), LANGIDFROMLCID(lcid),
535 LCType, wcBuffer, sizeof(wcBuffer)/sizeof(wcBuffer[0])))) {
536 WideCharToMultiByte(CP_ACP, 0, wcBuffer, res_size, acBuffer, dwBufferSize, NULL, NULL);
537 retString = acBuffer;
538 found = 1;
542 /* if not found report a most descriptive error */
543 if(!found) {
544 retString=0;
545 /* If we are through all of this, retLen should not be zero anymore.
546 If it is, the value is not supported */
547 i=0;
548 while (locale_name2id[i].name!=NULL) {
549 if (LCType == locale_name2id[i].id) {
550 retString = locale_name2id[i].name;
551 break;
553 i++;
555 if(!retString)
556 FIXME("Unkown LC type %lX\n", LCType);
557 else
558 FIXME("'%s' is not defined for your language (%04X).\n"
559 "Please define it in dlls/kernel/nls/YourLanguage.nls\n"
560 "and submit patch for inclusion into the next Wine release.\n",
561 retString, LOWORD(lcid));
562 SetLastError(ERROR_INVALID_PARAMETER);
563 return 0;
566 /* a FONTSIGNATURE is not a string, just 6 DWORDs */
567 if (LCType == LOCALE_FONTSIGNATURE) {
568 if (len) {
569 len = (len < sizeof(FONTSIGNATURE)) ? len : sizeof(FONTSIGNATURE);
570 memcpy(buf, retString, len);
571 return len;
573 return sizeof(FONTSIGNATURE);
575 /* if len=0 return only the length, don't touch the buffer*/
576 if (len) {
577 lstrcpynA(buf,retString,len);
578 return strlen(buf) + 1;
580 return strlen(retString)+1;
583 /******************************************************************************
584 * GetLocaleInfoW (KERNEL32.@)
586 * NOTES
587 * MS documentation states that len "specifies the size, in bytes (ANSI version)
588 * or characters (Unicode version), of" wbuf. Thus the number returned is
589 * the same between GetLocaleInfoW and GetLocaleInfoA.
591 INT WINAPI GetLocaleInfoW(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT len)
592 { WORD wlen;
593 LPSTR abuf;
595 if (len && (! wbuf) )
596 { SetLastError(ERROR_INSUFFICIENT_BUFFER);
597 return 0;
600 abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
601 wlen = GetLocaleInfoA(lcid, LCType, abuf, len);
603 if (wlen && len) /* if len=0 return only the length*/
604 MultiByteToWideChar( CP_ACP, 0, abuf, -1, wbuf, len );
606 HeapFree(GetProcessHeap(),0,abuf);
607 return wlen;
610 /******************************************************************************
612 * GetLocaleSubkeyName [helper function]
614 * - For use with the registry.
615 * - Gets the registry subkey name for a given lctype.
617 static char *GetLocaleSubkeyName( DWORD lctype )
619 char *pacKey=NULL;
621 switch ( lctype )
623 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
624 * the values are stored in the registry, confirmed under Windows,
625 * for the ones that actually assign pacKey. Cases that aren't finished
626 * have not been confirmed, so that must be done before they can be
627 * added.
629 case LOCALE_SDATE : /* The date separator. */
630 pacKey = "sDate";
631 break;
632 case LOCALE_ICURRDIGITS:
633 pacKey = "iCurrDigits";
634 break;
635 case LOCALE_SDECIMAL :
636 pacKey = "sDecimal";
637 break;
638 case LOCALE_ICURRENCY:
639 pacKey = "iCurrency";
640 break;
641 case LOCALE_SGROUPING :
642 pacKey = "sGrouping";
643 break;
644 case LOCALE_IDIGITS:
645 pacKey = "iDigits";
646 break;
647 case LOCALE_SLIST :
648 pacKey = "sList";
649 break;
650 /* case LOCALE_ICALENDARTYPE: */
651 /* case LOCALE_IFIRSTDAYOFWEEK: */
652 /* case LOCALE_IFIRSTWEEKOFYEAR: */
653 /* case LOCALE_SYEARMONTH : */
654 /* case LOCALE_SPOSITIVESIGN : */
655 /* case LOCALE_IPAPERSIZE: */
656 /* break; */
657 case LOCALE_SLONGDATE :
658 pacKey = "sLongDate";
659 break;
660 case LOCALE_SMONDECIMALSEP :
661 pacKey = "sMonDecimalSep";
662 break;
663 case LOCALE_SMONGROUPING:
664 pacKey = "sMonGrouping";
665 break;
666 case LOCALE_IMEASURE:
667 pacKey = "iMeasure";
668 break;
669 case LOCALE_SMONTHOUSANDSEP :
670 pacKey = "sMonThousandSep";
671 break;
672 case LOCALE_INEGCURR:
673 pacKey = "iNegCurr";
674 break;
675 case LOCALE_SNEGATIVESIGN :
676 pacKey = "sNegativeSign";
677 break;
678 case LOCALE_INEGNUMBER:
679 pacKey = "iNegNumber";
680 break;
681 case LOCALE_SSHORTDATE :
682 pacKey = "sShortDate";
683 break;
684 case LOCALE_ILDATE: /* Long Date format ordering specifier. */
685 pacKey = "iLDate";
686 break;
687 case LOCALE_ILZERO:
688 pacKey = "iLZero";
689 break;
690 case LOCALE_ITLZERO:
691 pacKey = "iTLZero";
692 break;
693 case LOCALE_ITIME: /* Time format specifier. */
694 pacKey = "iTime";
695 break;
696 case LOCALE_STHOUSAND :
697 pacKey = "sThousand";
698 break;
699 case LOCALE_S1159: /* AM */
700 pacKey = "s1159";
701 break;
702 case LOCALE_STIME:
703 pacKey = "sTime";
704 break;
705 case LOCALE_S2359: /* PM */
706 pacKey = "s2359";
707 break;
708 case LOCALE_STIMEFORMAT :
709 pacKey = "sTimeFormat";
710 break;
711 case LOCALE_SCURRENCY:
712 pacKey = "sCurrency";
713 break;
715 /* The following are not listed under MSDN as supported,
716 * but seem to be used and also stored in the registry.
719 case LOCALE_IDATE:
720 pacKey = "iDate";
721 break;
722 case LOCALE_SCOUNTRY:
723 pacKey = "sCountry";
724 break;
725 case LOCALE_ICOUNTRY:
726 pacKey = "iCountry";
727 break;
728 case LOCALE_SLANGUAGE:
729 pacKey = "sLanguage";
730 break;
732 default:
733 break;
736 return( pacKey );
739 /******************************************************************************
740 * SetLocaleInfoA [KERNEL32.@]
742 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
744 HKEY hKey;
745 char *pacKey;
746 char acRealKey[128];
748 if ( (pacKey = GetLocaleSubkeyName(lctype)) )
750 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
751 if ( RegCreateKeyA( HKEY_CURRENT_USER, acRealKey,
752 &hKey ) == ERROR_SUCCESS )
754 if ( RegSetValueExA( hKey, NULL, 0, REG_SZ,
755 data, strlen(data)+1 ) != ERROR_SUCCESS )
757 ERR("SetLocaleInfoA: %s did not work\n", pacKey );
759 RegCloseKey( hKey );
762 else
764 FIXME("(%ld,%ld,%s): stub\n",lcid,lctype,data);
766 return TRUE;
769 /******************************************************************************
770 * IsValidLocale [KERNEL32.@]
772 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
774 /* check if language is registered in the kernel32 resources */
775 if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
776 return FALSE;
777 else
778 return TRUE;
781 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
782 LPCWSTR name, WORD LangID, LONG lParam)
784 CHAR bufA[20];
785 WCHAR bufW[20];
786 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
787 sprintf(bufA, "%08X", (UINT)LangID);
788 MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
789 return lpfnLocaleEnum(bufW);
792 /******************************************************************************
793 * EnumSystemLocalesW [KERNEL32.@]
795 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
796 DWORD flags )
798 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
800 EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
801 (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
802 (LONG)lpfnLocaleEnum);
804 return TRUE;
807 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
808 LPCSTR name, WORD LangID, LONG lParam)
810 CHAR bufA[20];
811 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
812 sprintf(bufA, "%08X", (UINT)LangID);
813 return lpfnLocaleEnum(bufA);
816 /******************************************************************************
817 * EnumSystemLocalesA [KERNEL32.@]
819 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
820 DWORD flags)
822 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
824 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
825 (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
826 (LONG)lpfnLocaleEnum);
828 return TRUE;
831 /***********************************************************************
832 * VerLanguageNameA [KERNEL32.@]
834 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
836 if(!szLang)
837 return 0;
839 return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
842 /***********************************************************************
843 * VerLanguageNameW [KERNEL32.@]
845 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
847 if(!szLang)
848 return 0;
850 return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
854 static const unsigned char LCM_Unicode_LUT[] = {
855 6 , 3, /* - 1 */
856 6 , 4, /* - 2 */
857 6 , 5, /* - 3 */
858 6 , 6, /* - 4 */
859 6 , 7, /* - 5 */
860 6 , 8, /* - 6 */
861 6 , 9, /* - 7 */
862 6 , 10, /* - 8 */
863 7 , 5, /* - 9 */
864 7 , 6, /* - 10 */
865 7 , 7, /* - 11 */
866 7 , 8, /* - 12 */
867 7 , 9, /* - 13 */
868 6 , 11, /* - 14 */
869 6 , 12, /* - 15 */
870 6 , 13, /* - 16 */
871 6 , 14, /* - 17 */
872 6 , 15, /* - 18 */
873 6 , 16, /* - 19 */
874 6 , 17, /* - 20 */
875 6 , 18, /* - 21 */
876 6 , 19, /* - 22 */
877 6 , 20, /* - 23 */
878 6 , 21, /* - 24 */
879 6 , 22, /* - 25 */
880 6 , 23, /* - 26 */
881 6 , 24, /* - 27 */
882 6 , 25, /* - 28 */
883 6 , 26, /* - 29 */
884 6 , 27, /* - 30 */
885 6 , 28, /* - 31 */
886 7 , 2, /* - 32 */
887 7 , 28, /* ! - 33 */
888 7 , 29, /* " - 34 */ /* " */
889 7 , 31, /* # - 35 */
890 7 , 33, /* $ - 36 */
891 7 , 35, /* % - 37 */
892 7 , 37, /* & - 38 */
893 6 , 128, /* ' - 39 */
894 7 , 39, /* ( - 40 */
895 7 , 42, /* ) - 41 */
896 7 , 45, /* * - 42 */
897 8 , 3, /* + - 43 */
898 7 , 47, /* , - 44 */
899 6 , 130, /* - - 45 */
900 7 , 51, /* . - 46 */
901 7 , 53, /* / - 47 */
902 12 , 3, /* 0 - 48 */
903 12 , 33, /* 1 - 49 */
904 12 , 51, /* 2 - 50 */
905 12 , 70, /* 3 - 51 */
906 12 , 88, /* 4 - 52 */
907 12 , 106, /* 5 - 53 */
908 12 , 125, /* 6 - 54 */
909 12 , 144, /* 7 - 55 */
910 12 , 162, /* 8 - 56 */
911 12 , 180, /* 9 - 57 */
912 7 , 55, /* : - 58 */
913 7 , 58, /* ; - 59 */
914 8 , 14, /* < - 60 */
915 8 , 18, /* = - 61 */
916 8 , 20, /* > - 62 */
917 7 , 60, /* ? - 63 */
918 7 , 62, /* @ - 64 */
919 14 , 2, /* A - 65 */
920 14 , 9, /* B - 66 */
921 14 , 10, /* C - 67 */
922 14 , 26, /* D - 68 */
923 14 , 33, /* E - 69 */
924 14 , 35, /* F - 70 */
925 14 , 37, /* G - 71 */
926 14 , 44, /* H - 72 */
927 14 , 50, /* I - 73 */
928 14 , 53, /* J - 74 */
929 14 , 54, /* K - 75 */
930 14 , 72, /* L - 76 */
931 14 , 81, /* M - 77 */
932 14 , 112, /* N - 78 */
933 14 , 124, /* O - 79 */
934 14 , 126, /* P - 80 */
935 14 , 137, /* Q - 81 */
936 14 , 138, /* R - 82 */
937 14 , 145, /* S - 83 */
938 14 , 153, /* T - 84 */
939 14 , 159, /* U - 85 */
940 14 , 162, /* V - 86 */
941 14 , 164, /* W - 87 */
942 14 , 166, /* X - 88 */
943 14 , 167, /* Y - 89 */
944 14 , 169, /* Z - 90 */
945 7 , 63, /* [ - 91 */
946 7 , 65, /* \ - 92 */
947 7 , 66, /* ] - 93 */
948 7 , 67, /* ^ - 94 */
949 7 , 68, /* _ - 95 */
950 7 , 72, /* ` - 96 */
951 14 , 2, /* a - 97 */
952 14 , 9, /* b - 98 */
953 14 , 10, /* c - 99 */
954 14 , 26, /* d - 100 */
955 14 , 33, /* e - 101 */
956 14 , 35, /* f - 102 */
957 14 , 37, /* g - 103 */
958 14 , 44, /* h - 104 */
959 14 , 50, /* i - 105 */
960 14 , 53, /* j - 106 */
961 14 , 54, /* k - 107 */
962 14 , 72, /* l - 108 */
963 14 , 81, /* m - 109 */
964 14 , 112, /* n - 110 */
965 14 , 124, /* o - 111 */
966 14 , 126, /* p - 112 */
967 14 , 137, /* q - 113 */
968 14 , 138, /* r - 114 */
969 14 , 145, /* s - 115 */
970 14 , 153, /* t - 116 */
971 14 , 159, /* u - 117 */
972 14 , 162, /* v - 118 */
973 14 , 164, /* w - 119 */
974 14 , 166, /* x - 120 */
975 14 , 167, /* y - 121 */
976 14 , 169, /* z - 122 */
977 7 , 74, /* { - 123 */
978 7 , 76, /* | - 124 */
979 7 , 78, /* } - 125 */
980 7 , 80, /* ~ - 126 */
981 6 , 29, /* \x7f - 127 */
982 6 , 30, /* € - 128 */
983 6 , 31, /* � - 129 */
984 7 , 123, /* ‚ - 130 */
985 14 , 35, /* ƒ - 131 */
986 7 , 127, /* „ - 132 */
987 10 , 21, /* … - 133 */
988 10 , 15, /* † - 134 */
989 10 , 16, /* ‡ - 135 */
990 7 , 67, /* ˆ - 136 */
991 10 , 22, /* ‰ - 137 */
992 14 , 145, /* Š - 138 */
993 7 , 136, /* ‹ - 139 */
994 14 + 16 , 124, /* Œ - 140 */
995 6 , 43, /* � - 141 */
996 6 , 44, /* Ž - 142 */
997 6 , 45, /* � - 143 */
998 6 , 46, /* � - 144 */
999 7 , 121, /* ‘ - 145 */
1000 7 , 122, /* ’ - 146 */
1001 7 , 125, /* “ - 147 */
1002 7 , 126, /* ” - 148 */
1003 10 , 17, /* • - 149 */
1004 6 , 137, /* – - 150 */
1005 6 , 139, /* — - 151 */
1006 7 , 93, /* ˜ - 152 */
1007 14 , 156, /* ™ - 153 */
1008 14 , 145, /* š - 154 */
1009 7 , 137, /* › - 155 */
1010 14 + 16 , 124, /* œ - 156 */
1011 6 , 59, /* � - 157 */
1012 6 , 60, /* ž - 158 */
1013 14 , 167, /* Ÿ - 159 */
1014 7 , 4, /*   - 160 */
1015 7 , 81, /* ¡ - 161 */
1016 10 , 2, /* ¢ - 162 */
1017 10 , 3, /* £ - 163 */
1018 10 , 4, /* ¤ - 164 */
1019 10 , 5, /* ¥ - 165 */
1020 7 , 82, /* ¦ - 166 */
1021 10 , 6, /* § - 167 */
1022 7 , 83, /* ¨ - 168 */
1023 10 , 7, /* © - 169 */
1024 14 , 2, /* ª - 170 */
1025 8 , 24, /* « - 171 */
1026 10 , 8, /* ¬ - 172 */
1027 6 , 131, /* ­ - 173 */
1028 10 , 9, /* ® - 174 */
1029 7 , 84, /* ¯ - 175 */
1030 10 , 10, /* ° - 176 */
1031 8 , 23, /* ± - 177 */
1032 12 , 51, /* ² - 178 */
1033 12 , 70, /* ³ - 179 */
1034 7 , 85, /* ´ - 180 */
1035 10 , 11, /* µ - 181 */
1036 10 , 12, /* ¶ - 182 */
1037 10 , 13, /* · - 183 */
1038 7 , 86, /* ¸ - 184 */
1039 12 , 33, /* ¹ - 185 */
1040 14 , 124, /* º - 186 */
1041 8 , 26, /* » - 187 */
1042 12 , 21, /* ¼ - 188 */
1043 12 , 25, /* ½ - 189 */
1044 12 , 29, /* ¾ - 190 */
1045 7 , 87, /* ¿ - 191 */
1046 14 , 2, /* À - 192 */
1047 14 , 2, /* Á - 193 */
1048 14 , 2, /* Â - 194 */
1049 14 , 2, /* Ã - 195 */
1050 14 , 2, /* Ä - 196 */
1051 14 , 2, /* Å - 197 */
1052 14 + 16 , 2, /* Æ - 198 */
1053 14 , 10, /* Ç - 199 */
1054 14 , 33, /* È - 200 */
1055 14 , 33, /* É - 201 */
1056 14 , 33, /* Ê - 202 */
1057 14 , 33, /* Ë - 203 */
1058 14 , 50, /* Ì - 204 */
1059 14 , 50, /* Í - 205 */
1060 14 , 50, /* Î - 206 */
1061 14 , 50, /* Ï - 207 */
1062 14 , 26, /* Ð - 208 */
1063 14 , 112, /* Ñ - 209 */
1064 14 , 124, /* Ò - 210 */
1065 14 , 124, /* Ó - 211 */
1066 14 , 124, /* Ô - 212 */
1067 14 , 124, /* Õ - 213 */
1068 14 , 124, /* Ö - 214 */
1069 8 , 28, /* × - 215 */
1070 14 , 124, /* Ø - 216 */
1071 14 , 159, /* Ù - 217 */
1072 14 , 159, /* Ú - 218 */
1073 14 , 159, /* Û - 219 */
1074 14 , 159, /* Ü - 220 */
1075 14 , 167, /* Ý - 221 */
1076 14 + 32 , 153, /* Þ - 222 */
1077 14 + 48 , 145, /* ß - 223 */
1078 14 , 2, /* à - 224 */
1079 14 , 2, /* á - 225 */
1080 14 , 2, /* â - 226 */
1081 14 , 2, /* ã - 227 */
1082 14 , 2, /* ä - 228 */
1083 14 , 2, /* å - 229 */
1084 14 + 16 , 2, /* æ - 230 */
1085 14 , 10, /* ç - 231 */
1086 14 , 33, /* è - 232 */
1087 14 , 33, /* é - 233 */
1088 14 , 33, /* ê - 234 */
1089 14 , 33, /* ë - 235 */
1090 14 , 50, /* ì - 236 */
1091 14 , 50, /* í - 237 */
1092 14 , 50, /* î - 238 */
1093 14 , 50, /* ï - 239 */
1094 14 , 26, /* ð - 240 */
1095 14 , 112, /* ñ - 241 */
1096 14 , 124, /* ò - 242 */
1097 14 , 124, /* ó - 243 */
1098 14 , 124, /* ô - 244 */
1099 14 , 124, /* õ - 245 */
1100 14 , 124, /* ö - 246 */
1101 8 , 29, /* ÷ - 247 */
1102 14 , 124, /* ø - 248 */
1103 14 , 159, /* ù - 249 */
1104 14 , 159, /* ú - 250 */
1105 14 , 159, /* û - 251 */
1106 14 , 159, /* ü - 252 */
1107 14 , 167, /* ý - 253 */
1108 14 + 32 , 153, /* þ - 254 */
1109 14 , 167 /* ÿ - 255 */ };
1111 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1113 #define LCM_Diacritic_Start 131
1115 static const unsigned char LCM_Diacritic_LUT[] = {
1116 123, /* ƒ - 131 */
1117 2, /* „ - 132 */
1118 2, /* … - 133 */
1119 2, /* † - 134 */
1120 2, /* ‡ - 135 */
1121 3, /* ˆ - 136 */
1122 2, /* ‰ - 137 */
1123 20, /* Š - 138 */
1124 2, /* ‹ - 139 */
1125 2, /* Œ - 140 */
1126 2, /* � - 141 */
1127 2, /* Ž - 142 */
1128 2, /* � - 143 */
1129 2, /* � - 144 */
1130 2, /* ‘ - 145 */
1131 2, /* ’ - 146 */
1132 2, /* “ - 147 */
1133 2, /* ” - 148 */
1134 2, /* • - 149 */
1135 2, /* – - 150 */
1136 2, /* — - 151 */
1137 2, /* ˜ - 152 */
1138 2, /* ™ - 153 */
1139 20, /* š - 154 */
1140 2, /* › - 155 */
1141 2, /* œ - 156 */
1142 2, /* � - 157 */
1143 2, /* ž - 158 */
1144 19, /* Ÿ - 159 */
1145 2, /*   - 160 */
1146 2, /* ¡ - 161 */
1147 2, /* ¢ - 162 */
1148 2, /* £ - 163 */
1149 2, /* ¤ - 164 */
1150 2, /* ¥ - 165 */
1151 2, /* ¦ - 166 */
1152 2, /* § - 167 */
1153 2, /* ¨ - 168 */
1154 2, /* © - 169 */
1155 3, /* ª - 170 */
1156 2, /* « - 171 */
1157 2, /* ¬ - 172 */
1158 2, /* ­ - 173 */
1159 2, /* ® - 174 */
1160 2, /* ¯ - 175 */
1161 2, /* ° - 176 */
1162 2, /* ± - 177 */
1163 2, /* ² - 178 */
1164 2, /* ³ - 179 */
1165 2, /* ´ - 180 */
1166 2, /* µ - 181 */
1167 2, /* ¶ - 182 */
1168 2, /* · - 183 */
1169 2, /* ¸ - 184 */
1170 2, /* ¹ - 185 */
1171 3, /* º - 186 */
1172 2, /* » - 187 */
1173 2, /* ¼ - 188 */
1174 2, /* ½ - 189 */
1175 2, /* ¾ - 190 */
1176 2, /* ¿ - 191 */
1177 15, /* À - 192 */
1178 14, /* Á - 193 */
1179 18, /* Â - 194 */
1180 25, /* Ã - 195 */
1181 19, /* Ä - 196 */
1182 26, /* Å - 197 */
1183 2, /* Æ - 198 */
1184 28, /* Ç - 199 */
1185 15, /* È - 200 */
1186 14, /* É - 201 */
1187 18, /* Ê - 202 */
1188 19, /* Ë - 203 */
1189 15, /* Ì - 204 */
1190 14, /* Í - 205 */
1191 18, /* Î - 206 */
1192 19, /* Ï - 207 */
1193 104, /* Ð - 208 */
1194 25, /* Ñ - 209 */
1195 15, /* Ò - 210 */
1196 14, /* Ó - 211 */
1197 18, /* Ô - 212 */
1198 25, /* Õ - 213 */
1199 19, /* Ö - 214 */
1200 2, /* × - 215 */
1201 33, /* Ø - 216 */
1202 15, /* Ù - 217 */
1203 14, /* Ú - 218 */
1204 18, /* Û - 219 */
1205 19, /* Ü - 220 */
1206 14, /* Ý - 221 */
1207 2, /* Þ - 222 */
1208 2, /* ß - 223 */
1209 15, /* à - 224 */
1210 14, /* á - 225 */
1211 18, /* â - 226 */
1212 25, /* ã - 227 */
1213 19, /* ä - 228 */
1214 26, /* å - 229 */
1215 2, /* æ - 230 */
1216 28, /* ç - 231 */
1217 15, /* è - 232 */
1218 14, /* é - 233 */
1219 18, /* ê - 234 */
1220 19, /* ë - 235 */
1221 15, /* ì - 236 */
1222 14, /* í - 237 */
1223 18, /* î - 238 */
1224 19, /* ï - 239 */
1225 104, /* ð - 240 */
1226 25, /* ñ - 241 */
1227 15, /* ò - 242 */
1228 14, /* ó - 243 */
1229 18, /* ô - 244 */
1230 25, /* õ - 245 */
1231 19, /* ö - 246 */
1232 2, /* ÷ - 247 */
1233 33, /* ø - 248 */
1234 15, /* ù - 249 */
1235 14, /* ú - 250 */
1236 18, /* û - 251 */
1237 19, /* ü - 252 */
1238 14, /* ý - 253 */
1239 2, /* þ - 254 */
1240 19, /* ÿ - 255 */
1243 /******************************************************************************
1244 * OLE2NLS_isPunctuation [INTERNAL]
1246 static int OLE2NLS_isPunctuation(unsigned char c)
1248 /* "punctuation character" in this context is a character which is
1249 considered "less important" during word sort comparison.
1250 See LCMapString implementation for the precise definition
1251 of "less important". */
1253 return (LCM_Unicode_LUT[-2+2*c]==6);
1256 /******************************************************************************
1257 * OLE2NLS_isNonSpacing [INTERNAL]
1259 static int OLE2NLS_isNonSpacing(unsigned char c)
1261 /* This function is used by LCMapStringA. Characters
1262 for which it returns true are ignored when mapping a
1263 string with NORM_IGNORENONSPACE */
1264 return ((c==136) || (c==170) || (c==186));
1267 /******************************************************************************
1268 * OLE2NLS_isSymbol [INTERNAL]
1269 * FIXME: handle current locale
1271 static int OLE2NLS_isSymbol(unsigned char c)
1273 /* This function is used by LCMapStringA. Characters
1274 for which it returns true are ignored when mapping a
1275 string with NORM_IGNORESYMBOLS */
1276 return ( (c!=0) && !(isalpha(c) || isdigit(c)) );
1279 /******************************************************************************
1280 * identity [Internal]
1282 static int identity(int c)
1284 return c;
1287 /*************************************************************************
1288 * LCMapStringA [KERNEL32.@]
1290 * Convert a string, or generate a sort key from it.
1292 * If (mapflags & LCMAP_SORTKEY), the function will generate
1293 * a sort key for the source string. Else, it will convert it
1294 * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1296 * RETURNS
1297 * Error : 0.
1298 * Success : length of the result string.
1300 * NOTES
1301 * If called with scrlen = -1, the function will compute the length
1302 * of the 0-terminated string strsrc by itself.
1304 * If called with dstlen = 0, returns the buffer length that
1305 * would be required.
1307 * NORM_IGNOREWIDTH means to compare ASCII and wide characters
1308 * as if they are equal.
1309 * In the only code page implemented so far, there may not be
1310 * wide characters in strings passed to LCMapStringA,
1311 * so there is nothing to be done for this flag.
1313 INT WINAPI LCMapStringA(
1314 LCID lcid, /* [in] locale identifier created with MAKELCID;
1315 LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are
1316 predefined values. */
1317 DWORD mapflags, /* [in] flags */
1318 LPCSTR srcstr, /* [in] source buffer */
1319 INT srclen, /* [in] source length */
1320 LPSTR dststr, /* [out] destination buffer */
1321 INT dstlen) /* [in] destination buffer length */
1323 int i;
1325 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1326 lcid,mapflags,debugstr_an(srcstr,srclen),srclen,dststr,dstlen);
1328 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1330 ERR("(src=%s,dest=%s): Invalid NULL string\n",
1331 debugstr_an(srcstr,srclen), dststr);
1332 SetLastError(ERROR_INVALID_PARAMETER);
1333 return 0;
1335 if (srclen == -1)
1336 srclen = strlen(srcstr) + 1 ; /* (include final '\0') */
1338 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE | \
1339 LCMAP_LOWERCASE | \
1340 LCMAP_SORTKEY | \
1341 NORM_IGNORECASE | \
1342 NORM_IGNORENONSPACE | \
1343 SORT_STRINGSORT | \
1344 NORM_IGNOREWIDTH | \
1345 NORM_IGNOREKANATYPE)
1346 /* FIXME: as long as we don't support Katakana nor Hiragana
1347 * characters, we can support NORM_IGNOREKANATYPE
1349 if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
1351 FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1352 "unimplemented flags: 0x%08lx\n",
1353 lcid,
1354 mapflags,
1355 srcstr,
1356 srclen,
1357 dststr,
1358 dstlen,
1359 mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
1363 if ( !(mapflags & LCMAP_SORTKEY) )
1365 int i,j;
1366 int (*f)(int) = identity;
1367 int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
1368 int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
1370 if (flag_ignorenonspace || flag_ignoresymbols)
1372 /* For some values of mapflags, the length of the resulting
1373 string is not known at this point. Windows does map the string
1374 and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
1375 these cases. */
1376 if (dstlen==0)
1378 /* Compute required length */
1379 for (i=j=0; i < srclen; i++)
1381 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1382 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1383 j++;
1385 return j;
1388 else
1390 if (dstlen==0)
1391 return srclen;
1392 if (dstlen<srclen)
1394 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1395 return 0;
1398 if (mapflags & LCMAP_UPPERCASE)
1399 f = toupper;
1400 else if (mapflags & LCMAP_LOWERCASE)
1401 f = tolower;
1402 /* FIXME: NORM_IGNORENONSPACE requires another conversion */
1403 for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
1405 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1406 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1408 dststr[j] = (CHAR) f(srcstr[i]);
1409 j++;
1412 return j;
1415 /* FIXME: This function completely ignores the "lcid" parameter. */
1416 /* else ... (mapflags & LCMAP_SORTKEY) */
1418 int unicode_len=0;
1419 int case_len=0;
1420 int diacritic_len=0;
1421 int delayed_punctuation_len=0;
1422 char *case_component;
1423 char *diacritic_component;
1424 char *delayed_punctuation_component;
1425 int room,count;
1426 int flag_stringsort = mapflags & SORT_STRINGSORT;
1428 /* compute how much room we will need */
1429 for (i=0;i<srclen;i++)
1431 int ofs;
1432 unsigned char source_char = srcstr[i];
1433 if (source_char!='\0')
1435 if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
1437 unicode_len++;
1438 if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1439 unicode_len++; /* double letter */
1441 else
1443 delayed_punctuation_len++;
1447 if (isupper(source_char))
1448 case_len=unicode_len;
1450 ofs = source_char - LCM_Diacritic_Start;
1451 if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1452 diacritic_len=unicode_len;
1455 if (mapflags & NORM_IGNORECASE)
1456 case_len=0;
1457 if (mapflags & NORM_IGNORENONSPACE)
1458 diacritic_len=0;
1460 room = 2 * unicode_len /* "unicode" component */
1461 + diacritic_len /* "diacritic" component */
1462 + case_len /* "case" component */
1463 + 4 * delayed_punctuation_len /* punctuation in word sort mode */
1464 + 4 /* four '\1' separators */
1465 + 1 ; /* terminal '\0' */
1466 if (dstlen==0)
1467 return room;
1468 else if (dstlen<room)
1470 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1471 return 0;
1473 #if 0
1474 /*FIXME the Pointercheck should not be nessesary */
1475 if (IsBadWritePtr (dststr,room))
1476 { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1477 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1478 return 0;
1480 #endif
1481 /* locate each component, write separators */
1482 diacritic_component = dststr + 2*unicode_len ;
1483 *diacritic_component++ = '\1';
1484 case_component = diacritic_component + diacritic_len ;
1485 *case_component++ = '\1';
1486 delayed_punctuation_component = case_component + case_len ;
1487 *delayed_punctuation_component++ = '\1';
1488 *delayed_punctuation_component++ = '\1';
1490 /* read source string char by char, write
1491 corresponding weight in each component. */
1492 for (i=0,count=0;i<srclen;i++)
1494 unsigned char source_char=srcstr[i];
1495 if (source_char!='\0')
1497 int type,longcode;
1498 type = LCM_Unicode_LUT[-2+2*source_char];
1499 longcode = type >> 4;
1500 type &= 15;
1501 if (!flag_stringsort && OLE2NLS_isPunctuation(source_char))
1503 WORD encrypted_location = (1<<15) + 7 + 4*count;
1504 *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1505 *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1506 /* big-endian is used here because it lets string comparison be
1507 compatible with numerical comparison */
1509 *delayed_punctuation_component++ = type;
1510 *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];
1511 /* assumption : a punctuation character is never a
1512 double or accented letter */
1514 else
1516 dststr[2*count] = type;
1517 dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];
1518 if (longcode)
1520 if (count<case_len)
1521 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1522 if (count<diacritic_len)
1523 diacritic_component[count] = 2; /* assumption: a double letter
1524 is never accented */
1525 count++;
1527 dststr[2*count] = type;
1528 dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode);
1529 /* 16 in the first column of LCM_Unicode_LUT --> longcode = 1
1530 32 in the first column of LCM_Unicode_LUT --> longcode = 2
1531 48 in the first column of LCM_Unicode_LUT --> longcode = 3 */
1534 if (count<case_len)
1535 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1536 if (count<diacritic_len)
1538 int ofs = source_char - LCM_Diacritic_Start;
1539 diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1541 count++;
1545 dststr[room-1] = '\0';
1546 return room;
1550 /*************************************************************************
1551 * LCMapStringW [KERNEL32.@]
1553 * Convert a string, or generate a sort key from it.
1555 * NOTE
1557 * See LCMapStringA for documentation
1559 INT WINAPI LCMapStringW(
1560 LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
1561 INT dstlen)
1563 int i;
1565 TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
1566 lcid, mapflags, srcstr, srclen, dststr, dstlen);
1568 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1570 ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
1571 SetLastError(ERROR_INVALID_PARAMETER);
1572 return 0;
1574 if (srclen==-1)
1575 srclen = strlenW(srcstr)+1;
1577 /* FIXME: Both this function and it's companion LCMapStringA()
1578 * completely ignore the "lcid" parameter. In place of the "lcid"
1579 * parameter the application must set the "LC_COLLATE" or "LC_ALL"
1580 * environment variable prior to invoking this function. */
1581 if (mapflags & LCMAP_SORTKEY)
1583 /* Possible values of LC_COLLATE. */
1584 char *lc_collate_default = 0; /* value prior to this function */
1585 char *lc_collate_env = 0; /* value retrieved from the environment */
1587 /* General purpose index into strings of any type. */
1588 int str_idx = 0;
1590 /* Lengths of various strings where the length is measured in
1591 * wide characters for wide character strings and in bytes for
1592 * native strings. The lengths include the NULL terminator. */
1593 size_t returned_len = 0;
1594 size_t src_native_len = 0;
1595 size_t dst_native_len = 0;
1596 size_t dststr_libc_len = 0;
1598 /* Native (character set determined by locale) versions of the
1599 * strings source and destination strings. */
1600 LPSTR src_native = 0;
1601 LPSTR dst_native = 0;
1603 /* Version of the source and destination strings using the
1604 * "wchar_t" Unicode data type needed by various libc functions. */
1605 wchar_t *srcstr_libc = 0;
1606 wchar_t *dststr_libc = 0;
1608 if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1609 srclen * sizeof(wchar_t))))
1611 ERR("Unable to allocate %d bytes for srcstr_libc\n",
1612 srclen * sizeof(wchar_t));
1613 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1614 return 0;
1617 /* Convert source string to a libc Unicode string. */
1618 for(str_idx = 0; str_idx < srclen; str_idx++)
1620 srcstr_libc[str_idx] = srcstr[str_idx];
1623 /* src_native should contain at most 3 bytes for each
1624 * multibyte characters in the original srcstr string. */
1625 src_native_len = 3 * srclen;
1626 if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
1627 src_native_len)))
1629 ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
1630 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1631 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1632 return 0;
1635 /* FIXME: Prior to to setting the LC_COLLATE locale category the
1636 * current value is backed up so it can be restored after the
1637 * last LC_COLLATE sensitive function returns.
1639 * Even though the locale is adjusted for a minimum amount of
1640 * time a race condition exists where other threads may be
1641 * affected if they invoke LC_COLLATE sensitive functions. One
1642 * possible solution is to wrap all LC_COLLATE sensitive Wine
1643 * functions, like LCMapStringW(), in a mutex.
1645 * Another enhancement to the following would be to set the
1646 * LC_COLLATE locale category as a function of the "lcid"
1647 * parameter instead of the "LC_COLLATE" environment variable. */
1648 if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
1650 ERR("Unable to query the LC_COLLATE catagory\n");
1651 SetLastError(ERROR_INVALID_PARAMETER);
1652 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1653 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1654 return 0;
1657 if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
1659 ERR("Unable to inherit the LC_COLLATE locale category from the "
1660 "environment. The \"LC_COLLATE\" environment variable is "
1661 "\"%s\".\n", getenv("LC_COLLATE") ?
1662 getenv("LC_COLLATE") : "<unset>");
1663 SetLastError(ERROR_INVALID_PARAMETER);
1664 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1665 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1666 return 0;
1669 TRACE("lc_collate_default = %s\n", lc_collate_default);
1670 TRACE("lc_collate_env = %s\n", lc_collate_env);
1672 /* Convert the libc Unicode string to a native multibyte character
1673 * string. */
1674 returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
1675 if(returned_len == 0)
1677 ERR("wcstombs failed. The string specified (%s) may contain an invalid character.\n",
1678 debugstr_w(srcstr));
1679 SetLastError(ERROR_INVALID_PARAMETER);
1680 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1681 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1682 setlocale(LC_COLLATE, lc_collate_default);
1683 return 0;
1685 else if(returned_len > src_native_len)
1687 src_native[src_native_len - 1] = 0;
1688 ERR("wcstombs returned a string (%s) that was longer (%d bytes) "
1689 "than expected (%d bytes).\n", src_native, returned_len,
1690 dst_native_len);
1692 /* Since this is an internal error I'm not sure what the correct
1693 * error code is. */
1694 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1696 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1697 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1698 setlocale(LC_COLLATE, lc_collate_default);
1699 return 0;
1701 src_native_len = returned_len;
1703 TRACE("src_native = %s src_native_len = %d\n",
1704 src_native, src_native_len);
1706 /* dst_native seems to contain at most 4 bytes for each byte in
1707 * the original src_native string. Change if need be since this
1708 * isn't documented by the strxfrm() man page. */
1709 dst_native_len = 4 * src_native_len;
1710 if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
1712 ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
1713 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1714 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1715 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1716 setlocale(LC_COLLATE, lc_collate_default);
1717 return 0;
1720 /* The actual translation is done by the following call to
1721 * strxfrm(). The surrounding code could have been simplified
1722 * by calling wcsxfrm() instead except that wcsxfrm() is not
1723 * available on older Linux systems (RedHat 4.1 with
1724 * libc-5.3.12-17).
1726 * Also, it is possible that the translation could be done by
1727 * various tables as it is done in LCMapStringA(). However, I'm
1728 * not sure what those tables are. */
1729 returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
1731 if(returned_len > dst_native_len)
1733 dst_native[dst_native_len - 1] = 0;
1734 ERR("strxfrm returned a string (%s) that was longer (%d bytes) "
1735 "than expected (%d bytes).\n", dst_native, returned_len,
1736 dst_native_len);
1738 /* Since this is an internal error I'm not sure what the correct
1739 * error code is. */
1740 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1742 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1743 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1744 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1745 setlocale(LC_COLLATE, lc_collate_default);
1746 return 0;
1748 dst_native_len = returned_len;
1750 TRACE("dst_native = %s dst_native_len = %d\n",
1751 dst_native, dst_native_len);
1753 dststr_libc_len = dst_native_len;
1754 if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1755 dststr_libc_len * sizeof(wchar_t))))
1757 ERR("Unable to allocate %d bytes for dststr_libc\n",
1758 dststr_libc_len * sizeof(wchar_t));
1759 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1760 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1761 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1762 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1763 setlocale(LC_COLLATE, lc_collate_default);
1764 return 0;
1767 /* Convert the native multibyte string to a libc Unicode string. */
1768 returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
1770 /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
1771 * function has returned. */
1772 setlocale(LC_COLLATE, lc_collate_default);
1774 if(returned_len == 0)
1776 ERR("mbstowcs failed. The native version of the translated string "
1777 "(%s) may contain an invalid character.\n", dst_native);
1778 SetLastError(ERROR_INVALID_PARAMETER);
1779 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1780 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1781 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1782 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1783 return 0;
1785 if(dstlen)
1787 if(returned_len > dstlen)
1789 ERR("mbstowcs returned a string that was longer (%d chars) "
1790 "than the buffer provided (%d chars).\n", returned_len,
1791 dstlen);
1792 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1793 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1794 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1795 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1796 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1797 return 0;
1799 dstlen = returned_len;
1801 /* Convert a libc Unicode string to the destination string. */
1802 for(str_idx = 0; str_idx < dstlen; str_idx++)
1804 dststr[str_idx] = dststr_libc[str_idx];
1806 TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
1807 *(((int *)dststr) + 0),
1808 *(((int *)dststr) + 1),
1809 *(((int *)dststr) + 2),
1810 *(((int *)dststr) + 3));
1812 else
1814 dstlen = returned_len;
1816 TRACE("dstlen (return) = %d\n", dstlen);
1817 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1818 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1819 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1820 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1821 return dstlen;
1823 else
1825 int (*f)(int)=identity;
1827 if (dstlen==0)
1828 return srclen;
1829 if (dstlen<srclen)
1831 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1832 return 0;
1835 if (mapflags & LCMAP_UPPERCASE)
1836 f = toupper;
1837 else if (mapflags & LCMAP_LOWERCASE)
1838 f = tolower;
1839 for (i=0; i < srclen; i++)
1840 dststr[i] = (WCHAR) f(srcstr[i]);
1841 return srclen;
1846 /***********************************************************************
1847 * OLE2NLS_EstimateMappingLength
1849 * Estimates the number of characters required to hold the string
1850 * computed by LCMapStringA.
1852 * The size is always over-estimated, with a fixed limit on the
1853 * amount of estimation error.
1855 * Note that len == -1 is not permitted.
1857 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
1858 LPCSTR str, DWORD len)
1860 /* Estimate only for small strings to keep the estimation error from
1861 * becoming too large. */
1862 if (len < 128) return len * 8 + 5;
1863 else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
1866 /******************************************************************************
1867 * CompareStringA [KERNEL32.@]
1868 * Compares two strings using locale
1870 * RETURNS
1872 * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
1873 * failure: 0
1875 * NOTES
1877 * Defaults to a word sort, but uses a string sort if
1878 * SORT_STRINGSORT is set.
1879 * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
1881 * BUGS
1883 * This implementation ignores the locale
1885 * FIXME
1887 * Quite inefficient.
1889 int WINAPI CompareStringA(
1890 LCID lcid, /* [in] locale ID */
1891 DWORD fdwStyle, /* [in] comparison-style options */
1892 LPCSTR s1, /* [in] first string */
1893 int l1, /* [in] length of first string */
1894 LPCSTR s2, /* [in] second string */
1895 int l2) /* [in] length of second string */
1897 int mapstring_flags;
1898 int len1,len2;
1899 int result;
1900 LPSTR sk1,sk2;
1901 TRACE("%s and %s\n",
1902 debugstr_an (s1,l1), debugstr_an (s2,l2));
1904 if ( (s1==NULL) || (s2==NULL) )
1906 ERR("(s1=%s,s2=%s): Invalid NULL string\n",
1907 debugstr_an(s1,l1), debugstr_an(s2,l2));
1908 SetLastError(ERROR_INVALID_PARAMETER);
1909 return 0;
1912 if(fdwStyle & NORM_IGNORESYMBOLS)
1913 FIXME("IGNORESYMBOLS not supported\n");
1915 if (l1 == -1) l1 = strlen(s1);
1916 if (l2 == -1) l2 = strlen(s2);
1918 mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1919 len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
1920 len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
1922 if ((len1==0)||(len2==0))
1923 return 0; /* something wrong happened */
1925 sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
1926 sk2 = sk1 + len1;
1927 if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
1928 || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1930 ERR("Bug in LCmapStringA.\n");
1931 result = 0;
1933 else
1935 /* strcmp doesn't necessarily return -1, 0, or 1 */
1936 result = strcmp(sk1,sk2);
1938 HeapFree(GetProcessHeap(),0,sk1);
1940 if (result < 0)
1941 return 1;
1942 if (result == 0)
1943 return 2;
1945 /* must be greater, if we reach this point */
1946 return 3;
1949 /******************************************************************************
1950 * CompareStringW [KERNEL32.@]
1951 * This implementation ignores the locale
1952 * FIXME : Does only string sort. Should
1953 * be reimplemented the same way as CompareStringA.
1955 int WINAPI CompareStringW(LCID lcid, DWORD fdwStyle,
1956 LPCWSTR s1, int l1, LPCWSTR s2, int l2)
1958 int len,ret;
1959 if(fdwStyle & NORM_IGNORENONSPACE)
1960 FIXME("IGNORENONSPACE not supported\n");
1961 if(fdwStyle & NORM_IGNORESYMBOLS)
1962 FIXME("IGNORESYMBOLS not supported\n");
1964 /* Is strcmp defaulting to string sort or to word sort?? */
1965 /* FIXME: Handle NORM_STRINGSORT */
1966 l1 = (l1==-1)?strlenW(s1):l1;
1967 l2 = (l2==-1)?strlenW(s2):l2;
1968 len = l1<l2 ? l1:l2;
1969 ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
1970 /* not equal, return 1 or 3 */
1971 if(ret!=0) {
1972 /* need to translate result */
1973 return ((int)ret < 0) ? 1 : 3;
1975 /* same len, return 2 */
1976 if(l1==l2) return 2;
1977 /* the longer one is lexically greater */
1978 return (l1<l2)? 1 : 3;
1981 /******************************************************************************
1982 * OLE_GetFormatA [Internal]
1984 * FIXME
1985 * If datelen == 0, it should return the reguired string length.
1987 This function implements stuff for GetDateFormat() and
1988 GetTimeFormat().
1990 d single-digit (no leading zero) day (of month)
1991 dd two-digit day (of month)
1992 ddd short day-of-week name
1993 dddd long day-of-week name
1994 M single-digit month
1995 MM two-digit month
1996 MMM short month name
1997 MMMM full month name
1998 y two-digit year, no leading 0
1999 yy two-digit year
2000 yyyy four-digit year
2001 gg era string
2002 h hours with no leading zero (12-hour)
2003 hh hours with full two digits
2004 H hours with no leading zero (24-hour)
2005 HH hours with full two digits
2006 m minutes with no leading zero
2007 mm minutes with full two digits
2008 s seconds with no leading zero
2009 ss seconds with full two digits
2010 t time marker (A or P)
2011 tt time marker (AM, PM)
2012 '' used to quote literal characters
2013 '' (within a quoted string) indicates a literal '
2015 These functions REQUIRE valid locale, date, and format.
2017 static INT OLE_GetFormatA(LCID locale,
2018 DWORD flags,
2019 DWORD tflags,
2020 const SYSTEMTIME* xtime,
2021 LPCSTR _format, /*in*/
2022 LPSTR date, /*out*/
2023 INT datelen)
2025 INT inpos, outpos;
2026 int count, type, inquote, Overflow;
2027 char buf[40];
2028 char format[40];
2029 char * pos;
2030 int buflen;
2032 const char * _dgfmt[] = { "%d", "%02d" };
2033 const char ** dgfmt = _dgfmt - 1;
2035 /* report, for debugging */
2036 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",
2037 locale, flags, tflags,
2038 xtime->wYear,xtime->wMonth,xtime->wDayOfWeek,xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2039 _format, _format, date, datelen);
2041 if(datelen == 0) {
2042 FIXME("datelen = 0, returning 255\n");
2043 return 255;
2046 /* initalize state variables and output buffer */
2047 inpos = outpos = 0;
2048 count = 0; inquote = 0; Overflow = 0;
2049 type = '\0';
2050 date[0] = buf[0] = '\0';
2052 strcpy(format,_format);
2054 /* alter the formatstring, while it works for all languages now in wine
2055 its possible that it fails when the time looks like ss:mm:hh as example*/
2056 if (tflags & (TIME_NOMINUTESORSECONDS))
2057 { if ((pos = strstr ( format, ":mm")))
2058 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2061 if (tflags & (TIME_NOSECONDS))
2062 { if ((pos = strstr ( format, ":ss")))
2063 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2067 for (inpos = 0;; inpos++) {
2068 /* 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]); */
2069 if (inquote) {
2070 if (format[inpos] == '\'') {
2071 if (format[inpos+1] == '\'') {
2072 inpos += 1;
2073 date[outpos++] = '\'';
2074 } else {
2075 inquote = 0;
2076 continue; /* we did nothing to the output */
2078 } else if (format[inpos] == '\0') {
2079 date[outpos++] = '\0';
2080 if (outpos > datelen) Overflow = 1;
2081 break;
2082 } else {
2083 date[outpos++] = format[inpos];
2084 if (outpos > datelen) {
2085 Overflow = 1;
2086 date[outpos-1] = '\0'; /* this is the last place where
2087 it's safe to write */
2088 break;
2091 } else if ( (count && (format[inpos] != type))
2092 || count == 4
2093 || (count == 2 && strchr("ghHmst", type)) ) {
2094 if (type == 'h' && (tflags & TIME_FORCE24HOURFORMAT)) type= 'H';
2095 if (type == 'd') {
2096 if (count == 4) {
2097 GetLocaleInfoA(locale,
2098 LOCALE_SDAYNAME1
2099 + (xtime->wDayOfWeek+6)%7,
2100 buf, sizeof(buf));
2101 } else if (count == 3) {
2102 GetLocaleInfoA(locale,
2103 LOCALE_SABBREVDAYNAME1
2104 + (xtime->wDayOfWeek+6)%7,
2105 buf, sizeof(buf));
2106 } else {
2107 sprintf(buf, dgfmt[count], xtime->wDay);
2109 } else if (type == 'M') {
2110 if (count == 3) {
2111 GetLocaleInfoA(locale,
2112 LOCALE_SABBREVMONTHNAME1
2113 + xtime->wMonth - 1,
2114 buf, sizeof(buf));
2115 } else if (count == 4) {
2116 GetLocaleInfoA(locale,
2117 LOCALE_SMONTHNAME1
2118 + xtime->wMonth - 1,
2119 buf, sizeof(buf));
2120 } else {
2121 sprintf(buf, dgfmt[count], xtime->wMonth);
2123 } else if (type == 'y') {
2124 if (count == 4) {
2125 sprintf(buf, "%d", xtime->wYear);
2126 } else if (count == 3) {
2127 strcpy(buf, "yyy");
2128 WARN("unknown format, c=%c, n=%d\n", type, count);
2129 } else {
2130 sprintf(buf, dgfmt[count], xtime->wYear % 100);
2132 } else if (type == 'g') {
2133 if (count == 2) {
2134 FIXME("LOCALE_ICALENDARTYPE unimp.\n");
2135 strcpy(buf, "AD");
2136 } else {
2137 strcpy(buf, "g");
2138 WARN("unknown format, c=%c, n=%d\n", type, count);
2140 } else if (type == 'h') {
2141 /* gives us hours 1:00 -- 12:00 */
2142 sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2143 } else if (type == 'H') {
2144 /* 24-hour time */
2145 sprintf(buf, dgfmt[count], xtime->wHour);
2146 } else if ( type == 'm') {
2147 sprintf(buf, dgfmt[count], xtime->wMinute);
2148 } else if ( type == 's') {
2149 sprintf(buf, dgfmt[count], xtime->wSecond);
2150 } else if (type == 't') {
2151 if ((tflags & TIME_NOTIMEMARKER))
2152 buf[0]='\0';
2153 else if (count == 1) {
2154 sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2155 } else if (count == 2) {
2156 /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2157 GetLocaleInfoA(locale,
2158 (xtime->wHour<12)
2159 ? LOCALE_S1159 : LOCALE_S2359,
2160 buf, sizeof(buf));
2164 /* we need to check the next char in the format string
2165 again, no matter what happened */
2166 inpos--;
2168 /* add the contents of buf to the output */
2169 buflen = strlen(buf);
2170 if (outpos + buflen < datelen) {
2171 date[outpos] = '\0'; /* for strcat to hook onto */
2172 strcat(date, buf);
2173 outpos += buflen;
2174 } else {
2175 date[outpos] = '\0';
2176 strncat(date, buf, datelen - outpos);
2177 date[datelen - 1] = '\0';
2178 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2179 WARN("insufficient buffer\n");
2180 return 0;
2183 /* reset the variables we used to keep track of this item */
2184 count = 0;
2185 type = '\0';
2186 } else if (format[inpos] == '\0') {
2187 /* we can't check for this at the loop-head, because
2188 that breaks the printing of the last format-item */
2189 date[outpos] = '\0';
2190 break;
2191 } else if (count) {
2192 /* continuing a code for an item */
2193 count +=1;
2194 continue;
2195 } else if (strchr("hHmstyMdg", format[inpos])) {
2196 type = format[inpos];
2197 count = 1;
2198 continue;
2199 } else if (format[inpos] == '\'') {
2200 inquote = 1;
2201 continue;
2202 } else {
2203 date[outpos++] = format[inpos];
2205 /* now deal with a possible buffer overflow */
2206 if (outpos >= datelen) {
2207 date[datelen - 1] = '\0';
2208 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2209 return 0;
2213 if (Overflow) {
2214 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2217 /* finish it off with a string terminator */
2218 outpos++;
2219 /* sanity check */
2220 if (outpos > datelen-1) outpos = datelen-1;
2221 date[outpos] = '\0';
2223 TRACE("returns string '%s', len %d\n", date, outpos);
2224 return outpos;
2227 /******************************************************************************
2228 * OLE_GetFormatW [INTERNAL]
2230 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
2231 const SYSTEMTIME* xtime,
2232 LPCWSTR format,
2233 LPWSTR output, INT outlen)
2235 INT inpos, outpos;
2236 int count, type=0, inquote;
2237 int Overflow; /* loop check */
2238 char tmp[16];
2239 WCHAR buf[40];
2240 int buflen=0;
2241 WCHAR arg0[] = {0}, arg1[] = {'%','d',0};
2242 WCHAR arg2[] = {'%','0','2','d',0};
2243 WCHAR *argarr[3];
2244 int datevars=0, timevars=0;
2246 argarr[0] = arg0;
2247 argarr[1] = arg1;
2248 argarr[2] = arg2;
2250 /* make a debug report */
2251 TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
2252 "%p with max len %d\n",
2253 locale, flags, tflags,
2254 xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2255 debugstr_w(format), format, output, outlen);
2257 if(outlen == 0) {
2258 FIXME("outlen = 0, returning 255\n");
2259 return 255;
2262 /* initialize state variables */
2263 inpos = outpos = 0;
2264 count = 0;
2265 inquote = Overflow = 0;
2266 /* this is really just a sanity check */
2267 output[0] = buf[0] = 0;
2269 /* this loop is the core of the function */
2270 for (inpos = 0; /* we have several break points */ ; inpos++) {
2271 if (inquote) {
2272 if (format[inpos] == (WCHAR) '\'') {
2273 if (format[inpos+1] == '\'') {
2274 inpos++;
2275 output[outpos++] = '\'';
2276 } else {
2277 inquote = 0;
2278 continue;
2280 } else if (format[inpos] == 0) {
2281 output[outpos++] = 0;
2282 if (outpos > outlen) Overflow = 1;
2283 break; /* normal exit (within a quote) */
2284 } else {
2285 output[outpos++] = format[inpos]; /* copy input */
2286 if (outpos > outlen) {
2287 Overflow = 1;
2288 output[outpos-1] = 0;
2289 break;
2292 } else if ( (count && (format[inpos] != type))
2293 || ( (count==4 && type =='y') ||
2294 (count==4 && type =='M') ||
2295 (count==4 && type =='d') ||
2296 (count==2 && type =='g') ||
2297 (count==2 && type =='h') ||
2298 (count==2 && type =='H') ||
2299 (count==2 && type =='m') ||
2300 (count==2 && type =='s') ||
2301 (count==2 && type =='t') ) ) {
2302 switch(type)
2304 case 'd':
2305 if (count == 4) {
2306 GetLocaleInfoW(locale,
2307 LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7,
2308 buf, sizeof(buf)/sizeof(WCHAR) );
2309 } else if (count == 3) {
2310 GetLocaleInfoW(locale,
2311 LOCALE_SABBREVDAYNAME1 +
2312 (xtime->wDayOfWeek +6)%7,
2313 buf, sizeof(buf)/sizeof(WCHAR) );
2314 } else {
2315 sprintf( tmp, "%.*d", count, xtime->wDay );
2316 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2318 break;
2320 case 'M':
2321 if (count == 4) {
2322 GetLocaleInfoW(locale, LOCALE_SMONTHNAME1 +
2323 xtime->wMonth -1, buf,
2324 sizeof(buf)/sizeof(WCHAR) );
2325 } else if (count == 3) {
2326 GetLocaleInfoW(locale, LOCALE_SABBREVMONTHNAME1 +
2327 xtime->wMonth -1, buf,
2328 sizeof(buf)/sizeof(WCHAR) );
2329 } else {
2330 sprintf( tmp, "%.*d", count, xtime->wMonth );
2331 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2333 break;
2334 case 'y':
2335 if (count == 4) {
2336 sprintf( tmp, "%d", xtime->wYear );
2337 } else if (count == 3) {
2338 strcpy( tmp, "yyy" );
2339 } else {
2340 sprintf( tmp, "%.*d", count, xtime->wYear % 100 );
2342 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2343 break;
2345 case 'g':
2346 if (count == 2) {
2347 FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
2348 strcpy( tmp, "AD" );
2349 } else {
2350 /* Win API sez we copy it verbatim */
2351 strcpy( tmp, "g" );
2353 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2354 break;
2356 case 'h':
2357 /* hours 1:00-12:00 --- is this right? */
2358 sprintf( tmp, "%.*d", count, (xtime->wHour-1)%12 +1);
2359 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2360 break;
2362 case 'H':
2363 sprintf( tmp, "%.*d", count, xtime->wHour );
2364 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2365 break;
2367 case 'm':
2368 sprintf( tmp, "%.*d", count, xtime->wMinute );
2369 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2370 break;
2372 case 's':
2373 sprintf( tmp, "%.*d", count, xtime->wSecond );
2374 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2375 break;
2377 case 't':
2378 GetLocaleInfoW(locale, (xtime->wHour < 12) ?
2379 LOCALE_S1159 : LOCALE_S2359,
2380 buf, sizeof(buf) );
2381 if (count == 1) {
2382 buf[1] = 0;
2384 break;
2387 /* no matter what happened, we need to check this next
2388 character the next time we loop through */
2389 inpos--;
2391 /* cat buf onto the output */
2392 outlen = strlenW(buf);
2393 if (outpos + buflen < outlen) {
2394 strcpyW( output + outpos, buf );
2395 outpos += buflen;
2396 } else {
2397 lstrcpynW( output + outpos, buf, outlen - outpos );
2398 Overflow = 1;
2399 break; /* Abnormal exit */
2402 /* reset the variables we used this time */
2403 count = 0;
2404 type = '\0';
2405 } else if (format[inpos] == 0) {
2406 /* we can't check for this at the beginning, because that
2407 would keep us from printing a format spec that ended the
2408 string */
2409 output[outpos] = 0;
2410 break; /* NORMAL EXIT */
2411 } else if (count) {
2412 /* how we keep track of the middle of a format spec */
2413 count++;
2414 continue;
2415 } else if ( (datevars && (format[inpos]=='d' ||
2416 format[inpos]=='M' ||
2417 format[inpos]=='y' ||
2418 format[inpos]=='g') ) ||
2419 (timevars && (format[inpos]=='H' ||
2420 format[inpos]=='h' ||
2421 format[inpos]=='m' ||
2422 format[inpos]=='s' ||
2423 format[inpos]=='t') ) ) {
2424 type = format[inpos];
2425 count = 1;
2426 continue;
2427 } else if (format[inpos] == '\'') {
2428 inquote = 1;
2429 continue;
2430 } else {
2431 /* unquoted literals */
2432 output[outpos++] = format[inpos];
2436 if (Overflow) {
2437 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2438 WARN(" buffer overflow\n");
2441 /* final string terminator and sanity check */
2442 outpos++;
2443 if (outpos > outlen-1) outpos = outlen-1;
2444 output[outpos] = '0';
2446 TRACE(" returning %s\n", debugstr_w(output));
2448 return (!Overflow) ? outlen : 0;
2453 /******************************************************************************
2454 * GetDateFormatA [KERNEL32.@]
2455 * Makes an ASCII string of the date
2457 * This function uses format to format the date, or, if format
2458 * is NULL, uses the default for the locale. format is a string
2459 * of literal fields and characters as follows:
2461 * - d single-digit (no leading zero) day (of month)
2462 * - dd two-digit day (of month)
2463 * - ddd short day-of-week name
2464 * - dddd long day-of-week name
2465 * - M single-digit month
2466 * - MM two-digit month
2467 * - MMM short month name
2468 * - MMMM full month name
2469 * - y two-digit year, no leading 0
2470 * - yy two-digit year
2471 * - yyyy four-digit year
2472 * - gg era string
2475 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
2476 const SYSTEMTIME* xtime,
2477 LPCSTR format, LPSTR date,INT datelen)
2480 char format_buf[40];
2481 LPCSTR thisformat;
2482 SYSTEMTIME t;
2483 LPSYSTEMTIME thistime;
2484 LCID thislocale;
2485 INT ret;
2486 FILETIME ft;
2487 BOOL res;
2489 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2490 locale,flags,xtime,format,date,datelen);
2492 if (!locale) {
2493 locale = LOCALE_SYSTEM_DEFAULT;
2496 if (locale == LOCALE_SYSTEM_DEFAULT) {
2497 thislocale = GetSystemDefaultLCID();
2498 } else if (locale == LOCALE_USER_DEFAULT) {
2499 thislocale = GetUserDefaultLCID();
2500 } else {
2501 thislocale = locale;
2504 if (xtime == NULL) {
2505 GetSystemTime(&t);
2506 } else {
2507 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
2508 res=SystemTimeToFileTime(xtime,&ft);
2509 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
2510 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
2511 if(!res)
2513 SetLastError(ERROR_INVALID_PARAMETER);
2514 return 0;
2516 FileTimeToSystemTime(&ft,&t);
2519 thistime = &t;
2521 if (format == NULL) {
2522 GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE)
2523 ? LOCALE_SLONGDATE
2524 : LOCALE_SSHORTDATE),
2525 format_buf, sizeof(format_buf));
2526 thisformat = format_buf;
2527 } else {
2528 thisformat = format;
2532 ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat,
2533 date, datelen);
2536 TRACE(
2537 "GetDateFormatA() returning %d, with data=%s\n",
2538 ret, date);
2539 return ret;
2542 /******************************************************************************
2543 * GetDateFormatW [KERNEL32.@]
2544 * Makes a Unicode string of the date
2546 * Acts the same as GetDateFormatA(), except that it's Unicode.
2547 * Accepts & returns sizes as counts of Unicode characters.
2550 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
2551 const SYSTEMTIME* xtime,
2552 LPCWSTR format,
2553 LPWSTR date, INT datelen)
2555 unsigned short datearr[] = {'1','9','9','4','-','1','-','1',0};
2557 FIXME("STUB (should call OLE_GetFormatW)\n");
2558 lstrcpynW(date, datearr, datelen);
2559 return ( datelen < 9) ? datelen : 9;
2564 /**************************************************************************
2565 * EnumDateFormatsA (KERNEL32.@)
2567 BOOL WINAPI EnumDateFormatsA(
2568 DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2570 LCID Loc = GetUserDefaultLCID();
2571 if(!lpDateFmtEnumProc)
2573 SetLastError(ERROR_INVALID_PARAMETER);
2574 return FALSE;
2577 switch( Loc )
2580 case 0x00000407: /* (Loc,"de_DE") */
2582 switch(dwFlags)
2584 case DATE_SHORTDATE:
2585 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2586 if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
2587 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2588 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2589 return TRUE;
2590 case DATE_LONGDATE:
2591 if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
2592 if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
2593 if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
2594 return TRUE;
2595 default:
2596 FIXME("Unknown date format (%ld)\n", dwFlags);
2597 SetLastError(ERROR_INVALID_PARAMETER);
2598 return FALSE;
2602 case 0x0000040c: /* (Loc,"fr_FR") */
2604 switch(dwFlags)
2606 case DATE_SHORTDATE:
2607 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2608 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2609 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2610 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2611 return TRUE;
2612 case DATE_LONGDATE:
2613 if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
2614 if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
2615 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2616 return TRUE;
2617 default:
2618 FIXME("Unknown date format (%ld)\n", dwFlags);
2619 SetLastError(ERROR_INVALID_PARAMETER);
2620 return FALSE;
2624 case 0x00000c0c: /* (Loc,"fr_CA") */
2626 switch(dwFlags)
2628 case DATE_SHORTDATE:
2629 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2630 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2631 if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
2632 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2633 return TRUE;
2634 case DATE_LONGDATE:
2635 if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
2636 if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
2637 return TRUE;
2638 default:
2639 FIXME("Unknown date format (%ld)\n", dwFlags);
2640 SetLastError(ERROR_INVALID_PARAMETER);
2641 return FALSE;
2645 case 0x00000809: /* (Loc,"en_UK") */
2647 switch(dwFlags)
2649 case DATE_SHORTDATE:
2650 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2651 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2652 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2653 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2654 return TRUE;
2655 case DATE_LONGDATE:
2656 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2657 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2658 return TRUE;
2659 default:
2660 FIXME("Unknown date format (%ld)\n", dwFlags);
2661 SetLastError(ERROR_INVALID_PARAMETER);
2662 return FALSE;
2666 case 0x00000c09: /* (Loc,"en_AU") */
2668 switch(dwFlags)
2670 case DATE_SHORTDATE:
2671 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2672 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2673 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2674 return TRUE;
2675 case DATE_LONGDATE:
2676 if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
2677 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2678 return TRUE;
2679 default:
2680 FIXME("Unknown date format (%ld)\n", dwFlags);
2681 SetLastError(ERROR_INVALID_PARAMETER);
2682 return FALSE;
2686 case 0x00001009: /* (Loc,"en_CA") */
2688 switch(dwFlags)
2690 case DATE_SHORTDATE:
2691 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2692 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2693 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2694 if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
2695 return TRUE;
2696 case DATE_LONGDATE:
2697 if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
2698 if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
2699 return TRUE;
2700 default:
2701 FIXME("Unknown date format (%ld)\n", dwFlags);
2702 SetLastError(ERROR_INVALID_PARAMETER);
2703 return FALSE;
2707 case 0x00001409: /* (Loc,"en_NZ") */
2709 switch(dwFlags)
2711 case DATE_SHORTDATE:
2712 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2713 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2714 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2715 return TRUE;
2716 case DATE_LONGDATE:
2717 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2718 if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
2719 return TRUE;
2720 default:
2721 FIXME("Unknown date format (%ld)\n", dwFlags);
2722 SetLastError(ERROR_INVALID_PARAMETER);
2723 return FALSE;
2727 case 0x00001809: /* (Loc,"en_IE") */
2729 switch(dwFlags)
2731 case DATE_SHORTDATE:
2732 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2733 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2734 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2735 return TRUE;
2736 case DATE_LONGDATE:
2737 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2738 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2739 return TRUE;
2740 default:
2741 FIXME("Unknown date format (%ld)\n", dwFlags);
2742 SetLastError(ERROR_INVALID_PARAMETER);
2743 return FALSE;
2747 case 0x00001c09: /* (Loc,"en_ZA") */
2749 switch(dwFlags)
2751 case DATE_SHORTDATE:
2752 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2753 return TRUE;
2754 case DATE_LONGDATE:
2755 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2756 return TRUE;
2757 default:
2758 FIXME("Unknown date format (%ld)\n", dwFlags);
2759 SetLastError(ERROR_INVALID_PARAMETER);
2760 return FALSE;
2764 case 0x00002009: /* (Loc,"en_JM") */
2766 switch(dwFlags)
2768 case DATE_SHORTDATE:
2769 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2770 return TRUE;
2771 case DATE_LONGDATE:
2772 if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
2773 if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
2774 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
2775 if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
2776 return TRUE;
2777 default:
2778 FIXME("Unknown date format (%ld)\n", dwFlags);
2779 SetLastError(ERROR_INVALID_PARAMETER);
2780 return FALSE;
2784 case 0x00002809: /* (Loc,"en_BZ") */
2785 case 0x00002c09: /* (Loc,"en_TT") */
2787 switch(dwFlags)
2789 case DATE_SHORTDATE:
2790 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2791 return TRUE;
2792 case DATE_LONGDATE:
2793 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
2794 return TRUE;
2795 default:
2796 FIXME("Unknown date format (%ld)\n", dwFlags);
2797 SetLastError(ERROR_INVALID_PARAMETER);
2798 return FALSE;
2802 default: /* default to US English "en_US" */
2804 switch(dwFlags)
2806 case DATE_SHORTDATE:
2807 if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
2808 if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
2809 if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
2810 if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
2811 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2812 if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
2813 return TRUE;
2814 case DATE_LONGDATE:
2815 if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
2816 if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
2817 if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
2818 if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
2819 return TRUE;
2820 default:
2821 FIXME("Unknown date format (%ld)\n", dwFlags);
2822 SetLastError(ERROR_INVALID_PARAMETER);
2823 return FALSE;
2829 /**************************************************************************
2830 * EnumDateFormatsW (KERNEL32.@)
2832 BOOL WINAPI EnumDateFormatsW(
2833 DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2835 FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
2836 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2837 return FALSE;
2840 /**************************************************************************
2841 * EnumTimeFormatsA (KERNEL32.@)
2843 BOOL WINAPI EnumTimeFormatsA(
2844 TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2846 LCID Loc = GetUserDefaultLCID();
2847 if(!lpTimeFmtEnumProc)
2849 SetLastError(ERROR_INVALID_PARAMETER);
2850 return FALSE;
2852 if(dwFlags)
2854 FIXME("Unknown time format (%ld)\n", dwFlags);
2857 switch( Loc )
2859 case 0x00000407: /* (Loc,"de_DE") */
2861 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2862 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2863 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2864 if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
2865 if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
2866 return TRUE;
2869 case 0x0000040c: /* (Loc,"fr_FR") */
2870 case 0x00000c0c: /* (Loc,"fr_CA") */
2872 if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
2873 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2874 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2875 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2876 if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
2877 return TRUE;
2880 case 0x00000809: /* (Loc,"en_UK") */
2881 case 0x00000c09: /* (Loc,"en_AU") */
2882 case 0x00001409: /* (Loc,"en_NZ") */
2883 case 0x00001809: /* (Loc,"en_IE") */
2885 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2886 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2887 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2888 return TRUE;
2891 case 0x00001c09: /* (Loc,"en_ZA") */
2892 case 0x00002809: /* (Loc,"en_BZ") */
2893 case 0x00002c09: /* (Loc,"en_TT") */
2895 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2896 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2897 return TRUE;
2900 default: /* default to US style "en_US" */
2902 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2903 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2904 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2905 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2906 return TRUE;
2911 /**************************************************************************
2912 * EnumTimeFormatsW (KERNEL32.@)
2914 BOOL WINAPI EnumTimeFormatsW(
2915 TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2917 FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
2918 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2919 return FALSE;
2922 /**************************************************************************
2923 * This function is used just locally !
2924 * Description: Inverts a string.
2926 static void OLE_InvertString(char* string)
2928 char sTmpArray[128];
2929 INT counter, i = 0;
2931 for (counter = strlen(string); counter > 0; counter--)
2933 memcpy(sTmpArray + i, string + counter-1, 1);
2934 i++;
2936 memcpy(sTmpArray + i, "\0", 1);
2937 strcpy(string, sTmpArray);
2940 /***************************************************************************************
2941 * This function is used just locally !
2942 * Description: Test if the given string (psNumber) is valid or not.
2943 * The valid characters are the following:
2944 * - Characters '0' through '9'.
2945 * - One decimal point (dot) if the number is a floating-point value.
2946 * - A minus sign in the first character position if the number is
2947 * a negative value.
2948 * If the function succeeds, psBefore/psAfter will point to the string
2949 * on the right/left of the decimal symbol. pbNegative indicates if the
2950 * number is negative.
2952 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
2954 char sNumberSet[] = "0123456789";
2955 BOOL bInDecimal = FALSE;
2957 /* Test if we do have a minus sign */
2958 if ( *pInput == '-' )
2960 *pbNegative = TRUE;
2961 pInput++; /* Jump to the next character. */
2964 while(*pInput != '\0')
2966 /* Do we have a valid numeric character */
2967 if ( strchr(sNumberSet, *pInput) != NULL )
2969 if (bInDecimal == TRUE)
2970 *psAfter++ = *pInput;
2971 else
2972 *psBefore++ = *pInput;
2974 else
2976 /* Is this a decimal point (dot) */
2977 if ( *pInput == '.' )
2979 /* Is it the first time we find it */
2980 if ((bInDecimal == FALSE))
2981 bInDecimal = TRUE;
2982 else
2983 return -1; /* ERROR: Invalid parameter */
2985 else
2987 /* It's neither a numeric character, nor a decimal point.
2988 * Thus, return an error.
2990 return -1;
2993 pInput++;
2996 /* Add an End of Line character to the output buffers */
2997 *psBefore = '\0';
2998 *psAfter = '\0';
3000 return 0;
3003 /**************************************************************************
3004 * This function is used just locally !
3005 * Description: A number could be formatted using different numbers
3006 * of "digits in group" (example: 4;3;2;0).
3007 * The first parameter of this function is an array
3008 * containing the rule to be used. Its format is the following:
3009 * |NDG|DG1|DG2|...|0|
3010 * where NDG is the number of used "digits in group" and DG1, DG2,
3011 * are the corresponding "digits in group".
3012 * Thus, this function returns the grouping value in the array
3013 * pointed by the second parameter.
3015 static INT OLE_GetGrouping(char* sRule, INT index)
3017 char sData[2], sRuleSize[2];
3018 INT nData, nRuleSize;
3020 memcpy(sRuleSize, sRule, 1);
3021 memcpy(sRuleSize+1, "\0", 1);
3022 nRuleSize = atoi(sRuleSize);
3024 if (index > 0 && index < nRuleSize)
3026 memcpy(sData, sRule+index, 1);
3027 memcpy(sData+1, "\0", 1);
3028 nData = atoi(sData);
3031 else
3033 memcpy(sData, sRule+nRuleSize-1, 1);
3034 memcpy(sData+1, "\0", 1);
3035 nData = atoi(sData);
3038 return nData;
3041 /**************************************************************************
3042 * GetNumberFormatA (KERNEL32.@)
3044 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
3045 LPCSTR lpvalue, const NUMBERFMTA * lpFormat,
3046 LPSTR lpNumberStr, int cchNumber)
3048 char sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
3049 INT nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
3050 char sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
3051 char sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];
3052 char *pStr = NULL, *pTmpStr = NULL;
3053 LCID systemDefaultLCID;
3054 BOOL bNegative = FALSE;
3055 enum Operations
3057 USE_PARAMETER,
3058 USE_LOCALEINFO,
3059 USE_SYSTEMDEFAULT,
3060 RETURN_ERROR
3061 } used_operation;
3063 strncpy(sNumber, lpvalue, 128);
3064 sNumber[127] = '\0';
3066 /* Make sure we have a valid input string, get the number
3067 * of digits before and after the decimal symbol, and check
3068 * if this is a negative number.
3070 if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
3072 nNumberDecimal = strlen(sDigitsBeforeDecimal);
3073 nDigits = strlen(sDigitsAfterDecimal);
3075 else
3077 SetLastError(ERROR_INVALID_PARAMETER);
3078 return 0;
3081 /* Which source will we use to format the string */
3082 used_operation = RETURN_ERROR;
3083 if (lpFormat != NULL)
3085 if (dwflags == 0)
3086 used_operation = USE_PARAMETER;
3088 else
3090 if (dwflags & LOCALE_NOUSEROVERRIDE)
3091 used_operation = USE_LOCALEINFO;
3092 else
3093 used_operation = USE_SYSTEMDEFAULT;
3096 /* Load the fields we need */
3097 switch(used_operation)
3099 case USE_LOCALEINFO:
3100 GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3101 GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3102 GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3103 GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3104 GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3105 GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3106 break;
3107 case USE_PARAMETER:
3108 sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
3109 strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
3110 sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
3111 strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
3112 sprintf(sILZero, "%d",lpFormat->LeadingZero);
3113 sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
3114 break;
3115 case USE_SYSTEMDEFAULT:
3116 systemDefaultLCID = GetSystemDefaultLCID();
3117 GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3118 GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3119 GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3120 GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3121 GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3122 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3123 break;
3124 default:
3125 SetLastError(ERROR_INVALID_PARAMETER);
3126 return 0;
3129 nNumberDigits = atoi(sNumberDigits);
3131 /* Remove the ";" */
3132 i=0;
3133 j = 1;
3134 for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
3136 if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
3138 memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
3139 i++;
3140 j++;
3143 sprintf(sBuffer, "%d", i);
3144 memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
3145 memcpy(sRule + j, "\0", 1);
3147 /* First, format the digits before the decimal. */
3148 if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0))
3150 /* Working on an inverted string is easier ! */
3151 OLE_InvertString(sDigitsBeforeDecimal);
3153 nStep = nCounter = i = j = 0;
3154 nRuleIndex = 1;
3155 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3157 /* Here, we will loop until we reach the end of the string.
3158 * An internal counter (j) is used in order to know when to
3159 * insert the "digit group symbol".
3161 while (nNumberDecimal > 0)
3163 i = nCounter + nStep;
3164 memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
3165 nCounter++;
3166 j++;
3167 if (j >= nGrouping)
3169 j = 0;
3170 if (nRuleIndex < sRule[0])
3171 nRuleIndex++;
3172 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3173 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
3174 nStep+= strlen(sDigitGroupSymbol);
3177 nNumberDecimal--;
3180 memcpy(sDestination + i+1, "\0", 1);
3181 /* Get the string in the right order ! */
3182 OLE_InvertString(sDestination);
3184 else
3186 nLZ = atoi(sILZero);
3187 if (nLZ != 0)
3189 /* Use 0.xxx instead of .xxx */
3190 memcpy(sDestination, "0", 1);
3191 memcpy(sDestination+1, "\0", 1);
3193 else
3194 memcpy(sDestination, "\0", 1);
3198 /* Second, format the digits after the decimal. */
3199 j = 0;
3200 nCounter = nNumberDigits;
3201 if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
3203 i = strlen(sNumber) - strlen(pStr) + 1;
3204 strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
3205 j = strlen(sDigitsAfterDecimal);
3206 if (j < nNumberDigits)
3207 nCounter = nNumberDigits-j;
3209 for (i=0;i<nCounter;i++)
3210 memcpy(sDigitsAfterDecimal+i+j, "0", 1);
3211 memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);
3213 i = strlen(sDestination);
3214 j = strlen(sDigitsAfterDecimal);
3215 /* Finally, construct the resulting formatted string. */
3217 for (nCounter=0; nCounter<i; nCounter++)
3218 memcpy(sNumber + nCounter, sDestination + nCounter, 1);
3220 memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
3222 for (i=0; i<j; i++)
3223 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
3224 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
3226 /* Is it a negative number */
3227 if (bNegative == TRUE)
3229 i = atoi(sNegNumber);
3230 pStr = sDestination;
3231 pTmpStr = sNumber;
3232 switch (i)
3234 case 0:
3235 *pStr++ = '(';
3236 while (*sNumber != '\0')
3237 *pStr++ = *pTmpStr++;
3238 *pStr++ = ')';
3239 break;
3240 case 1:
3241 *pStr++ = '-';
3242 while (*pTmpStr != '\0')
3243 *pStr++ = *pTmpStr++;
3244 break;
3245 case 2:
3246 *pStr++ = '-';
3247 *pStr++ = ' ';
3248 while (*pTmpStr != '\0')
3249 *pStr++ = *pTmpStr++;
3250 break;
3251 case 3:
3252 while (*pTmpStr != '\0')
3253 *pStr++ = *pTmpStr++;
3254 *pStr++ = '-';
3255 break;
3256 case 4:
3257 while (*pTmpStr != '\0')
3258 *pStr++ = *pTmpStr++;
3259 *pStr++ = ' ';
3260 *pStr++ = '-';
3261 break;
3262 default:
3263 while (*pTmpStr != '\0')
3264 *pStr++ = *pTmpStr++;
3265 break;
3268 else
3269 strcpy(sDestination, sNumber);
3271 /* If cchNumber is zero, then returns the number of bytes or characters
3272 * required to hold the formatted number string
3274 if (cchNumber==0)
3275 retVal = strlen(sDestination) + 1;
3276 else
3278 strncpy (lpNumberStr, sDestination, cchNumber-1);
3279 *(lpNumberStr+cchNumber-1) = '\0'; /* ensure we got a NULL at the end */
3280 retVal = strlen(lpNumberStr);
3283 return retVal;
3286 /**************************************************************************
3287 * GetNumberFormatW (KERNEL32.@)
3289 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
3290 LPCWSTR lpvalue, const NUMBERFMTW * lpFormat,
3291 LPWSTR lpNumberStr, int cchNumber)
3293 FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue));
3295 lstrcpynW( lpNumberStr, lpvalue, cchNumber );
3296 return cchNumber? strlenW( lpNumberStr ) : 0;
3299 /**************************************************************************
3300 * GetCurrencyFormatA (KERNEL32.@)
3302 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
3303 LPCSTR lpvalue, const CURRENCYFMTA * lpFormat,
3304 LPSTR lpCurrencyStr, int cchCurrency)
3306 UINT nPosOrder, nNegOrder;
3307 INT retVal;
3308 char sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
3309 char *pDestination = sDestination;
3310 char sNumberFormated[128];
3311 char *pNumberFormated = sNumberFormated;
3312 LCID systemDefaultLCID;
3313 BOOL bIsPositive = FALSE, bValidFormat = FALSE;
3314 enum Operations
3316 USE_PARAMETER,
3317 USE_LOCALEINFO,
3318 USE_SYSTEMDEFAULT,
3319 RETURN_ERROR
3320 } used_operation;
3322 NUMBERFMTA numberFmt;
3324 /* Which source will we use to format the string */
3325 used_operation = RETURN_ERROR;
3326 if (lpFormat != NULL)
3328 if (dwflags == 0)
3329 used_operation = USE_PARAMETER;
3331 else
3333 if (dwflags & LOCALE_NOUSEROVERRIDE)
3334 used_operation = USE_LOCALEINFO;
3335 else
3336 used_operation = USE_SYSTEMDEFAULT;
3339 /* Load the fields we need */
3340 switch(used_operation)
3342 case USE_LOCALEINFO:
3343 /* Specific to CURRENCYFMTA */
3344 GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3345 GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3346 GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3348 nPosOrder = atoi(sPosOrder);
3349 nNegOrder = atoi(sNegOrder);
3350 break;
3351 case USE_PARAMETER:
3352 /* Specific to CURRENCYFMTA */
3353 nNegOrder = lpFormat->NegativeOrder;
3354 nPosOrder = lpFormat->PositiveOrder;
3355 strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
3356 break;
3357 case USE_SYSTEMDEFAULT:
3358 systemDefaultLCID = GetSystemDefaultLCID();
3359 /* Specific to CURRENCYFMTA */
3360 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3361 GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3362 GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3364 nPosOrder = atoi(sPosOrder);
3365 nNegOrder = atoi(sNegOrder);
3366 break;
3367 default:
3368 SetLastError(ERROR_INVALID_PARAMETER);
3369 return 0;
3372 /* Construct a temporary number format structure */
3373 if (lpFormat != NULL)
3375 numberFmt.NumDigits = lpFormat->NumDigits;
3376 numberFmt.LeadingZero = lpFormat->LeadingZero;
3377 numberFmt.Grouping = lpFormat->Grouping;
3378 numberFmt.NegativeOrder = 0;
3379 numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
3380 numberFmt.lpThousandSep = lpFormat->lpThousandSep;
3381 bValidFormat = TRUE;
3384 /* Make a call to GetNumberFormatA() */
3385 if (*lpvalue == '-')
3387 bIsPositive = FALSE;
3388 retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3390 else
3392 bIsPositive = TRUE;
3393 retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3396 if (retVal == 0)
3397 return 0;
3399 /* construct the formatted string */
3400 if (bIsPositive)
3402 switch (nPosOrder)
3404 case 0: /* Prefix, no separation */
3405 strcpy (pDestination, sCurrencySymbol);
3406 strcat (pDestination, pNumberFormated);
3407 break;
3408 case 1: /* Suffix, no separation */
3409 strcpy (pDestination, pNumberFormated);
3410 strcat (pDestination, sCurrencySymbol);
3411 break;
3412 case 2: /* Prefix, 1 char separation */
3413 strcpy (pDestination, sCurrencySymbol);
3414 strcat (pDestination, " ");
3415 strcat (pDestination, pNumberFormated);
3416 break;
3417 case 3: /* Suffix, 1 char separation */
3418 strcpy (pDestination, pNumberFormated);
3419 strcat (pDestination, " ");
3420 strcat (pDestination, sCurrencySymbol);
3421 break;
3422 default:
3423 SetLastError(ERROR_INVALID_PARAMETER);
3424 return 0;
3427 else /* negative number */
3429 switch (nNegOrder)
3431 case 0: /* format: ($1.1) */
3432 strcpy (pDestination, "(");
3433 strcat (pDestination, sCurrencySymbol);
3434 strcat (pDestination, pNumberFormated);
3435 strcat (pDestination, ")");
3436 break;
3437 case 1: /* format: -$1.1 */
3438 strcpy (pDestination, "-");
3439 strcat (pDestination, sCurrencySymbol);
3440 strcat (pDestination, pNumberFormated);
3441 break;
3442 case 2: /* format: $-1.1 */
3443 strcpy (pDestination, sCurrencySymbol);
3444 strcat (pDestination, "-");
3445 strcat (pDestination, pNumberFormated);
3446 break;
3447 case 3: /* format: $1.1- */
3448 strcpy (pDestination, sCurrencySymbol);
3449 strcat (pDestination, pNumberFormated);
3450 strcat (pDestination, "-");
3451 break;
3452 case 4: /* format: (1.1$) */
3453 strcpy (pDestination, "(");
3454 strcat (pDestination, pNumberFormated);
3455 strcat (pDestination, sCurrencySymbol);
3456 strcat (pDestination, ")");
3457 break;
3458 case 5: /* format: -1.1$ */
3459 strcpy (pDestination, "-");
3460 strcat (pDestination, pNumberFormated);
3461 strcat (pDestination, sCurrencySymbol);
3462 break;
3463 case 6: /* format: 1.1-$ */
3464 strcpy (pDestination, pNumberFormated);
3465 strcat (pDestination, "-");
3466 strcat (pDestination, sCurrencySymbol);
3467 break;
3468 case 7: /* format: 1.1$- */
3469 strcpy (pDestination, pNumberFormated);
3470 strcat (pDestination, sCurrencySymbol);
3471 strcat (pDestination, "-");
3472 break;
3473 case 8: /* format: -1.1 $ */
3474 strcpy (pDestination, "-");
3475 strcat (pDestination, pNumberFormated);
3476 strcat (pDestination, " ");
3477 strcat (pDestination, sCurrencySymbol);
3478 break;
3479 case 9: /* format: -$ 1.1 */
3480 strcpy (pDestination, "-");
3481 strcat (pDestination, sCurrencySymbol);
3482 strcat (pDestination, " ");
3483 strcat (pDestination, pNumberFormated);
3484 break;
3485 case 10: /* format: 1.1 $- */
3486 strcpy (pDestination, pNumberFormated);
3487 strcat (pDestination, " ");
3488 strcat (pDestination, sCurrencySymbol);
3489 strcat (pDestination, "-");
3490 break;
3491 case 11: /* format: $ 1.1- */
3492 strcpy (pDestination, sCurrencySymbol);
3493 strcat (pDestination, " ");
3494 strcat (pDestination, pNumberFormated);
3495 strcat (pDestination, "-");
3496 break;
3497 case 12: /* format: $ -1.1 */
3498 strcpy (pDestination, sCurrencySymbol);
3499 strcat (pDestination, " ");
3500 strcat (pDestination, "-");
3501 strcat (pDestination, pNumberFormated);
3502 break;
3503 case 13: /* format: 1.1- $ */
3504 strcpy (pDestination, pNumberFormated);
3505 strcat (pDestination, "-");
3506 strcat (pDestination, " ");
3507 strcat (pDestination, sCurrencySymbol);
3508 break;
3509 case 14: /* format: ($ 1.1) */
3510 strcpy (pDestination, "(");
3511 strcat (pDestination, sCurrencySymbol);
3512 strcat (pDestination, " ");
3513 strcat (pDestination, pNumberFormated);
3514 strcat (pDestination, ")");
3515 break;
3516 case 15: /* format: (1.1 $) */
3517 strcpy (pDestination, "(");
3518 strcat (pDestination, pNumberFormated);
3519 strcat (pDestination, " ");
3520 strcat (pDestination, sCurrencySymbol);
3521 strcat (pDestination, ")");
3522 break;
3523 default:
3524 SetLastError(ERROR_INVALID_PARAMETER);
3525 return 0;
3529 if (cchCurrency == 0)
3530 return strlen(pDestination) + 1;
3532 else
3534 strncpy (lpCurrencyStr, pDestination, cchCurrency-1);
3535 *(lpCurrencyStr+cchCurrency-1) = '\0'; /* ensure we got a NULL at the end */
3536 return strlen(lpCurrencyStr);
3540 /**************************************************************************
3541 * GetCurrencyFormatW (KERNEL32.@)
3543 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
3544 LPCWSTR lpvalue, const CURRENCYFMTW * lpFormat,
3545 LPWSTR lpCurrencyStr, int cchCurrency)
3547 FIXME("This API function is NOT implemented !\n");
3548 return 0;
3551 /******************************************************************************
3552 * OLE2NLS_CheckLocale [intern]
3554 static LCID OLE2NLS_CheckLocale (LCID locale)
3556 if (!locale)
3557 { locale = LOCALE_SYSTEM_DEFAULT;
3560 if (locale == LOCALE_SYSTEM_DEFAULT)
3561 { return GetSystemDefaultLCID();
3563 else if (locale == LOCALE_USER_DEFAULT)
3564 { return GetUserDefaultLCID();
3566 else
3567 { return locale;
3570 /******************************************************************************
3571 * GetTimeFormatA [KERNEL32.@]
3572 * Makes an ASCII string of the time
3574 * Formats date according to format, or locale default if format is
3575 * NULL. The format consists of literal characters and fields as follows:
3577 * h hours with no leading zero (12-hour)
3578 * hh hours with full two digits
3579 * H hours with no leading zero (24-hour)
3580 * HH hours with full two digits
3581 * m minutes with no leading zero
3582 * mm minutes with full two digits
3583 * s seconds with no leading zero
3584 * ss seconds with full two digits
3585 * t time marker (A or P)
3586 * tt time marker (AM, PM)
3589 INT WINAPI
3590 GetTimeFormatA(LCID locale, /* [in] */
3591 DWORD flags, /* [in] */
3592 const SYSTEMTIME* xtime, /* [in] */
3593 LPCSTR format, /* [in] */
3594 LPSTR timestr, /* [out] */
3595 INT timelen /* [in] */)
3596 { char format_buf[40];
3597 LPCSTR thisformat;
3598 SYSTEMTIME t;
3599 const SYSTEMTIME* thistime;
3600 LCID thislocale=0;
3601 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3602 INT ret;
3604 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
3606 thislocale = OLE2NLS_CheckLocale ( locale );
3608 if (format == NULL)
3609 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3610 { thislocale = GetSystemDefaultLCID();
3612 GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
3613 thisformat = format_buf;
3615 else
3616 { thisformat = format;
3619 if (xtime == NULL) /* NULL means use the current local time */
3620 { GetLocalTime(&t);
3621 thistime = &t;
3623 else
3624 { thistime = xtime;
3625 /* Check that hour,min and sec is in range */
3627 ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
3628 timestr, timelen);
3629 return ret;
3633 /******************************************************************************
3634 * GetTimeFormatW [KERNEL32.@]
3635 * Makes a Unicode string of the time
3637 INT WINAPI
3638 GetTimeFormatW(LCID locale, /* [in] */
3639 DWORD flags, /* [in] */
3640 const SYSTEMTIME* xtime, /* [in] */
3641 LPCWSTR format, /* [in] */
3642 LPWSTR timestr, /* [out] */
3643 INT timelen /* [in] */)
3644 { WCHAR format_buf[40];
3645 LPCWSTR thisformat;
3646 SYSTEMTIME t;
3647 const SYSTEMTIME* thistime;
3648 LCID thislocale=0;
3649 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3650 INT ret;
3652 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
3653 xtime,debugstr_w(format),timestr,timelen);
3655 thislocale = OLE2NLS_CheckLocale ( locale );
3657 if (format == NULL)
3658 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3659 { thislocale = GetSystemDefaultLCID();
3661 GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
3662 thisformat = format_buf;
3664 else
3665 { thisformat = format;
3668 if (xtime == NULL) /* NULL means use the current local time */
3669 { GetLocalTime(&t);
3670 thistime = &t;
3672 else
3673 { thistime = xtime;
3676 ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
3677 timestr, timelen);
3678 return ret;
3681 /******************************************************************************
3682 * EnumCalendarInfoA [KERNEL32.@]
3684 BOOL WINAPI EnumCalendarInfoA(
3685 CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
3687 FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
3688 return FALSE;