Use the same color conversion algorithm as the DIB code (and Windows),
[wine] / ole / ole2nls.c
1 /*
2  *      National Language Support library
3  *
4  *      Copyright 1995  Martin von Loewis
5  *      Copyright 1998  David Lee Lambert
6  *      Copyright 2000  Julio César Gázquez
7  */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <locale.h>
14
15 #include "windef.h"
16 #include "winbase.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19 #include "wine/unicode.h"
20 #include "heap.h"
21 #include "options.h"
22 #include "winver.h"
23 #include "winnls.h"
24 #include "winreg.h"
25 #include "winerror.h"
26 #include "debugtools.h"
27
28 DEFAULT_DEBUG_CHANNEL(string);
29
30 /* Locale name to id map. used by EnumSystemLocales, GetLocaleInfoA 
31  * MUST contain all #defines from winnls.h
32  * last entry has NULL name, 0 id.
33  */ 
34 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
35 static const struct tagLOCALE_NAME2ID {
36     const char  *name;
37     LCTYPE      id;
38 } locale_name2id[]= {
39         LOCALE_ENTRY(ILANGUAGE),
40         LOCALE_ENTRY(SLANGUAGE),
41         LOCALE_ENTRY(SENGLANGUAGE),
42         LOCALE_ENTRY(SABBREVLANGNAME),
43         LOCALE_ENTRY(SNATIVELANGNAME),
44         LOCALE_ENTRY(ICOUNTRY),
45         LOCALE_ENTRY(SCOUNTRY),
46         LOCALE_ENTRY(SENGCOUNTRY),
47         LOCALE_ENTRY(SABBREVCTRYNAME),
48         LOCALE_ENTRY(SNATIVECTRYNAME),
49         LOCALE_ENTRY(IDEFAULTLANGUAGE),
50         LOCALE_ENTRY(IDEFAULTCOUNTRY),
51         LOCALE_ENTRY(IDEFAULTCODEPAGE),
52         LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
53         LOCALE_ENTRY(IDEFAULTMACCODEPAGE),
54         LOCALE_ENTRY(SLIST),
55         LOCALE_ENTRY(IMEASURE),
56         LOCALE_ENTRY(SDECIMAL),
57         LOCALE_ENTRY(STHOUSAND),
58         LOCALE_ENTRY(SGROUPING),
59         LOCALE_ENTRY(IDIGITS),
60         LOCALE_ENTRY(ILZERO),
61         LOCALE_ENTRY(INEGNUMBER),
62         LOCALE_ENTRY(SNATIVEDIGITS),
63         LOCALE_ENTRY(SCURRENCY),
64         LOCALE_ENTRY(SINTLSYMBOL),
65         LOCALE_ENTRY(SMONDECIMALSEP),
66         LOCALE_ENTRY(SMONTHOUSANDSEP),
67         LOCALE_ENTRY(SMONGROUPING),
68         LOCALE_ENTRY(ICURRDIGITS),
69         LOCALE_ENTRY(IINTLCURRDIGITS),
70         LOCALE_ENTRY(ICURRENCY),
71         LOCALE_ENTRY(INEGCURR),
72         LOCALE_ENTRY(SDATE),
73         LOCALE_ENTRY(STIME),
74         LOCALE_ENTRY(SSHORTDATE),
75         LOCALE_ENTRY(SLONGDATE),
76         LOCALE_ENTRY(STIMEFORMAT),
77         LOCALE_ENTRY(IDATE),
78         LOCALE_ENTRY(ILDATE),
79         LOCALE_ENTRY(ITIME),
80         LOCALE_ENTRY(ITIMEMARKPOSN),
81         LOCALE_ENTRY(ICENTURY),
82         LOCALE_ENTRY(ITLZERO),
83         LOCALE_ENTRY(IDAYLZERO),
84         LOCALE_ENTRY(IMONLZERO),
85         LOCALE_ENTRY(S1159),
86         LOCALE_ENTRY(S2359),
87         LOCALE_ENTRY(ICALENDARTYPE),
88         LOCALE_ENTRY(IOPTIONALCALENDAR),
89         LOCALE_ENTRY(IFIRSTDAYOFWEEK),
90         LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
91         LOCALE_ENTRY(SDAYNAME1),
92         LOCALE_ENTRY(SDAYNAME2),
93         LOCALE_ENTRY(SDAYNAME3),
94         LOCALE_ENTRY(SDAYNAME4),
95         LOCALE_ENTRY(SDAYNAME5),
96         LOCALE_ENTRY(SDAYNAME6),
97         LOCALE_ENTRY(SDAYNAME7),
98         LOCALE_ENTRY(SABBREVDAYNAME1),
99         LOCALE_ENTRY(SABBREVDAYNAME2),
100         LOCALE_ENTRY(SABBREVDAYNAME3),
101         LOCALE_ENTRY(SABBREVDAYNAME4),
102         LOCALE_ENTRY(SABBREVDAYNAME5),
103         LOCALE_ENTRY(SABBREVDAYNAME6),
104         LOCALE_ENTRY(SABBREVDAYNAME7),
105         LOCALE_ENTRY(SMONTHNAME1),
106         LOCALE_ENTRY(SMONTHNAME2),
107         LOCALE_ENTRY(SMONTHNAME3),
108         LOCALE_ENTRY(SMONTHNAME4),
109         LOCALE_ENTRY(SMONTHNAME5),
110         LOCALE_ENTRY(SMONTHNAME6),
111         LOCALE_ENTRY(SMONTHNAME7),
112         LOCALE_ENTRY(SMONTHNAME8),
113         LOCALE_ENTRY(SMONTHNAME9),
114         LOCALE_ENTRY(SMONTHNAME10),
115         LOCALE_ENTRY(SMONTHNAME11),
116         LOCALE_ENTRY(SMONTHNAME12),
117         LOCALE_ENTRY(SMONTHNAME13),
118         LOCALE_ENTRY(SABBREVMONTHNAME1),
119         LOCALE_ENTRY(SABBREVMONTHNAME2),
120         LOCALE_ENTRY(SABBREVMONTHNAME3),
121         LOCALE_ENTRY(SABBREVMONTHNAME4),
122         LOCALE_ENTRY(SABBREVMONTHNAME5),
123         LOCALE_ENTRY(SABBREVMONTHNAME6),
124         LOCALE_ENTRY(SABBREVMONTHNAME7),
125         LOCALE_ENTRY(SABBREVMONTHNAME8),
126         LOCALE_ENTRY(SABBREVMONTHNAME9),
127         LOCALE_ENTRY(SABBREVMONTHNAME10),
128         LOCALE_ENTRY(SABBREVMONTHNAME11),
129         LOCALE_ENTRY(SABBREVMONTHNAME12),
130         LOCALE_ENTRY(SABBREVMONTHNAME13),
131         LOCALE_ENTRY(SPOSITIVESIGN),
132         LOCALE_ENTRY(SNEGATIVESIGN),
133         LOCALE_ENTRY(IPOSSIGNPOSN),
134         LOCALE_ENTRY(INEGSIGNPOSN),
135         LOCALE_ENTRY(IPOSSYMPRECEDES),
136         LOCALE_ENTRY(IPOSSEPBYSPACE),
137         LOCALE_ENTRY(INEGSYMPRECEDES),
138         LOCALE_ENTRY(INEGSEPBYSPACE),
139         LOCALE_ENTRY(FONTSIGNATURE),
140         LOCALE_ENTRY(SISO639LANGNAME),
141         LOCALE_ENTRY(SISO3166CTRYNAME),
142         {NULL,0}
143 };
144
145 static char *GetLocaleSubkeyName( DWORD lctype );
146
147 /***********************************************************************
148  *              GetUserDefaultLCID (KERNEL32.@)
149  */
150 LCID WINAPI GetUserDefaultLCID(void)
151 {
152         return MAKELCID( GetUserDefaultLangID() , SORT_DEFAULT );
153 }
154
155 /***********************************************************************
156  *              GetSystemDefaultLCID (KERNEL32.@)
157  */
158 LCID WINAPI GetSystemDefaultLCID(void)
159 {
160         return GetUserDefaultLCID();
161 }
162
163 #define NLS_MAX_LANGUAGES 20
164 typedef struct {
165     char lang[128];
166     char country[128];
167     LANGID found_lang_id[NLS_MAX_LANGUAGES];
168     char found_language[NLS_MAX_LANGUAGES][3];
169     char found_country[NLS_MAX_LANGUAGES][3];
170     int n_found;
171 } LANG_FIND_DATA;
172
173 static BOOL CALLBACK NLS_FindLanguageID_ProcA(HMODULE hModule, LPCSTR type,
174                                               LPCSTR name, WORD LangID, LONG lParam)
175 {
176     LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
177     LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
178     char buf_language[128];
179     char buf_country[128];
180     char buf_en_language[128];
181
182     TRACE("%04X\n", (UINT)LangID);
183     if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
184         return TRUE; /* continue search */
185
186     buf_language[0] = 0;
187     buf_country[0] = 0;
188
189     GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
190                    buf_language, sizeof(buf_language));
191     TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
192
193     GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
194                    buf_country, sizeof(buf_country));
195     TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
196
197     if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
198     {
199         if(l_data->country && strlen(l_data->country) > 0)
200         {
201             if(!strcasecmp(l_data->country, buf_country))
202             {
203                 l_data->found_lang_id[0] = LangID;
204                 l_data->n_found = 1;
205                 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
206                 return FALSE; /* stop enumeration */
207             }
208         }
209         else /* l_data->country not specified */
210         {
211             if(l_data->n_found < NLS_MAX_LANGUAGES)
212             {
213                 l_data->found_lang_id[l_data->n_found] = LangID;
214                 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
215                 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
216                 l_data->n_found++;
217                 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
218                 return TRUE; /* continue search */
219             }
220         }
221     }
222
223     /* Just in case, check LOCALE_SENGLANGUAGE too,
224      * in hope that possible alias name might have that value.
225      */
226     buf_en_language[0] = 0;
227     GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
228                    buf_en_language, sizeof(buf_en_language));
229     TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
230
231     if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
232     {
233         l_data->found_lang_id[l_data->n_found] = LangID;
234         strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
235         strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
236         l_data->n_found++;
237         TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
238     }
239
240     return TRUE; /* continue search */
241 }
242
243 /***********************************************************************
244  *           NLS_GetLanguageID
245  *
246  * INPUT:
247  *      Lang: a string whose two first chars are the iso name of a language.
248  *      Country: a string whose two first chars are the iso name of country
249  *      Charset: a string defining the chossen charset encoding
250  *      Dialect: a string defining a variation of the locale
251  *
252  *      all those values are from the standardized format of locale
253  *      name in unix which is: Lang[_Country][.Charset][@Dialect]
254  *
255  * RETURNS:
256  *      the numeric code of the language used by Windows
257  *
258  * FIXME: Charset and Dialect are not handled
259  */
260 static LANGID NLS_GetLanguageID(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
261 {
262     LANG_FIND_DATA l_data;
263     char lang_string[256];
264
265     if(!Lang)
266     {
267         l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
268         goto END;
269     }
270
271     memset(&l_data, 0, sizeof(LANG_FIND_DATA));
272     strncpy(l_data.lang, Lang, sizeof(l_data.lang));
273
274     if(Country && strlen(Country) > 0)
275         strncpy(l_data.country, Country, sizeof(l_data.country));
276
277     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
278         (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
279
280     strcpy(lang_string, l_data.lang);
281     if(l_data.country && strlen(l_data.country) > 0)
282     {
283         strcat(lang_string, "_");
284         strcat(lang_string, l_data.country);
285     }
286
287     if(!l_data.n_found)
288     {
289         if(l_data.country && strlen(l_data.country) > 0)
290         {
291             MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
292             l_data.country[0] = 0;
293             EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
294                 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
295         }
296     }
297
298     /* Re-evaluate lang_string */
299     strcpy(lang_string, l_data.lang);
300     if(l_data.country && strlen(l_data.country) > 0)
301     {
302         strcat(lang_string, "_");
303         strcat(lang_string, l_data.country);
304     }
305
306     if(!l_data.n_found)
307     {
308         MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
309         l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
310     }
311     else
312     {
313         if(l_data.n_found == 1)
314             TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
315         else /* l_data->n_found > 1 */
316         {
317             int i;
318             MESSAGE("For language '%s' several language ids were found:\n", lang_string);
319             for(i = 0; i < l_data.n_found; i++)
320                 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
321
322             MESSAGE("\nInstead of using first in the list, suggest to define\n"
323                     "your LANG environment variable like this: LANG=%s_%s\n",
324                     l_data.found_language[0], l_data.found_country[0]);
325         }
326     }
327 END:
328     TRACE("Returning %04X\n", l_data.found_lang_id[0]);
329     return l_data.found_lang_id[0];
330 }
331
332 /***********************************************************************
333  *              GetUserDefaultLangID (KERNEL32.@)
334  */
335 LANGID WINAPI GetUserDefaultLangID(void)
336 {
337         /* caching result, if defined from environment, which should (?) not change during a WINE session */
338         static  LANGID  userLCID = 0;
339
340         if (userLCID == 0)
341         {
342                 char buf[256];
343                 char *lang,*country,*charset,*dialect,*next;
344
345                 if (GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) )) goto ok;
346                 if (GetEnvironmentVariableA( "LANG", buf, sizeof(buf) )) goto ok;
347                 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) )) goto ok;
348                 if (GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) )) goto ok;
349                 if (GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) )) goto ok;
350
351                 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
352
353         ok:
354                 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C"))
355                     return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
356                 
357                 lang=buf;
358                 
359                 do {
360                         next=strchr(lang,':'); if (next) *next++='\0';
361                         dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
362                         charset=strchr(lang,'.'); if (charset) *charset++='\0';
363                         country=strchr(lang,'_'); if (country) *country++='\0';
364                         
365                         userLCID = NLS_GetLanguageID(lang, country, charset, dialect);
366                         
367                         lang=next;
368                 } while (lang && !userLCID);
369
370                 if (!userLCID)
371                 {
372                     MESSAGE( "Warning: language '%s' not recognized, defaulting to English\n",
373                              buf );
374                     userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
375                 }
376         }
377         return userLCID;
378 }
379
380 /***********************************************************************
381  *              GetSystemDefaultLangID (KERNEL32.@)
382  */
383 LANGID WINAPI GetSystemDefaultLangID(void)
384 {
385         return GetUserDefaultLangID();
386 }
387
388 /******************************************************************************
389  *              ConvertDefaultLocale (KERNEL32.@)
390  */
391 LCID WINAPI ConvertDefaultLocale (LCID lcid)
392 {       switch (lcid)
393         {  case LOCALE_SYSTEM_DEFAULT:
394              return GetSystemDefaultLCID();
395            case LOCALE_USER_DEFAULT:
396              return GetUserDefaultLCID();
397            case LOCALE_NEUTRAL:
398              return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
399         }  
400         return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
401 }
402
403 /* Enhanced version of LoadStringW.
404  * It takes LanguageId to find and load language dependant resources.
405  * Resource is copied as is: "binary" strings are not truncated.
406  * Return: length of resource + 1 to distinguish absent resources
407  * from the resources with zero length.
408  */
409 static INT NLS_LoadStringExW(HMODULE hModule, LANGID lang_id, UINT res_id, LPWSTR buffer, INT buflen)
410 {
411     HRSRC hrsrc;
412     HGLOBAL hmem;
413     WCHAR *p;
414     int string_num;
415     int i;
416
417     /* Replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
418     if(SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
419         lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
420
421     hrsrc = FindResourceExW(hModule, RT_STRINGW, (LPCWSTR)((res_id >> 4) + 1), lang_id);
422
423     if(!hrsrc) return 0;
424     hmem = LoadResource(hModule, hrsrc);
425     if(!hmem) return 0;
426     
427     p = LockResource(hmem);
428     string_num = res_id & 0x000f;
429     for(i = 0; i < string_num; i++)
430         p += *p + 1;
431     
432     TRACE("strlen = %d\n", (int)*p );
433     
434     if (buffer == NULL) return *p;
435     i = min(buflen - 1, *p);
436     if (i > 0) {
437         memcpy(buffer, p + 1, i * sizeof (WCHAR));
438         buffer[i] = (WCHAR) 0;
439     } else {
440         if (buflen > 1)
441             buffer[0] = (WCHAR) 0;
442     }
443
444     FreeResource(hmem);
445     TRACE("%s loaded!\n", debugstr_w(buffer));
446     return (i + 1);
447 }
448
449 /******************************************************************************
450  *              GetLocaleInfoA (KERNEL32.@)
451  *
452  * NOTES 
453  *  LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
454  *
455  *  MS online documentation states that the string returned is NULL terminated
456  *  except for LOCALE_FONTSIGNATURE  which "will return a non-NULL
457  *  terminated string".
458  */
459 INT WINAPI GetLocaleInfoA(LCID lcid,LCTYPE LCType,LPSTR buf,INT len)
460 {
461     LPCSTR  retString = NULL;
462     int found = 0, i;
463     char    *pacKey;
464     char    acBuffer[128];
465     DWORD   dwBufferSize=128;
466     BOOL NoUserOverride;
467
468   TRACE("(lcid=0x%lx,lctype=0x%lx,%p,%x)\n",lcid,LCType,buf,len);
469
470   if (len && (! buf) ) {
471     SetLastError(ERROR_INSUFFICIENT_BUFFER);
472                 return 0;
473         }
474
475         if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT)
476         {
477             lcid = GetSystemDefaultLCID();
478         } 
479         else if (lcid == LANG_USER_DEFAULT) /*0x800*/
480         {
481             lcid = GetUserDefaultLCID();
482         }
483
484     /* LOCALE_NOUSEROVERRIDE means: do not get user redefined settings
485        from the registry. Instead, use system default values. */
486     NoUserOverride = (LCType & LOCALE_NOUSEROVERRIDE) != 0;
487
488         LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
489
490     /* First, check if it's in the registry. */
491     /* All user customized values are stored in the registry by SetLocaleInfo */
492     if ( !NoUserOverride && (pacKey = GetLocaleSubkeyName(LCType)) )
493     {
494         char    acRealKey[128];
495         HKEY    hKey;
496
497         sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
498
499         if ( RegOpenKeyExA( HKEY_CURRENT_USER, acRealKey, 
500                             0, KEY_READ, &hKey) == ERROR_SUCCESS )
501         {
502             if ( RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)acBuffer, 
503                                    &dwBufferSize ) == ERROR_SUCCESS )
504             {
505                 retString = acBuffer;
506                 found = 1;
507             }
508             RegCloseKey(hKey);
509         }
510     }
511
512     /* If not in the registry, get it from the NLS entries. */
513     if(!found) {
514         WCHAR wcBuffer[128];
515         int res_size;
516
517         /* check if language is registered in the kernel32 resources */
518         if((res_size = NLS_LoadStringExW(GetModuleHandleA("KERNEL32"), LANGIDFROMLCID(lcid),
519                 LCType, wcBuffer, sizeof(wcBuffer)/sizeof(wcBuffer[0])))) {
520             WideCharToMultiByte(CP_ACP, 0, wcBuffer, res_size, acBuffer, dwBufferSize, NULL, NULL);
521             retString = acBuffer;
522             found = 1;
523         }
524     }
525
526     /* if not found report a most descriptive error */
527     if(!found) {
528         retString=0;
529         /* If we are through all of this, retLen should not be zero anymore.
530            If it is, the value is not supported */
531         i=0;
532         while (locale_name2id[i].name!=NULL) {
533             if (LCType == locale_name2id[i].id) {
534                 retString = locale_name2id[i].name;
535                 break;
536             }
537             i++;
538         }
539         if(!retString)
540             FIXME("Unkown LC type %lX\n", LCType);
541         else
542             FIXME("'%s' is not defined for your language (%04X).\n"
543                 "Please define it in dlls/kernel/nls/YourLanguage.nls\n"
544                 "and submit patch for inclusion into the next Wine release.\n",
545                         retString, LOWORD(lcid));
546         SetLastError(ERROR_INVALID_PARAMETER);
547         return 0;                       
548     }
549
550     /* a FONTSIGNATURE is not a string, just 6 DWORDs  */
551     if (LCType == LOCALE_FONTSIGNATURE) {
552         if (len) {
553             len = (len < sizeof(FONTSIGNATURE)) ? len : sizeof(FONTSIGNATURE);
554             memcpy(buf, retString, len);
555             return len;
556         }
557         return sizeof(FONTSIGNATURE);
558     }
559     /* if len=0 return only the length, don't touch the buffer*/
560     if (len) {
561         lstrcpynA(buf,retString,len);
562         return strlen(buf) + 1;
563     }
564     return strlen(retString)+1;
565 }
566
567 /******************************************************************************
568  *              GetLocaleInfoW (KERNEL32.@)
569  *
570  * NOTES
571  *  MS documentation states that len "specifies the size, in bytes (ANSI version)
572  *  or characters (Unicode version), of" wbuf. Thus the number returned is
573  *  the same between GetLocaleInfoW and GetLocaleInfoA.
574  */
575 INT WINAPI GetLocaleInfoW(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT len)
576 {       WORD wlen;
577         LPSTR abuf;
578         
579         if (len && (! wbuf) )
580         { SetLastError(ERROR_INSUFFICIENT_BUFFER);
581           return 0;
582         }
583
584         abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
585         wlen = GetLocaleInfoA(lcid, LCType, abuf, len);
586
587         if (wlen && len)        /* if len=0 return only the length*/
588             MultiByteToWideChar( CP_ACP, 0, abuf, -1, wbuf, len );
589
590         HeapFree(GetProcessHeap(),0,abuf);
591         return wlen;
592 }
593
594 /******************************************************************************
595  *
596  *              GetLocaleSubkeyName [helper function]
597  *
598  *          - For use with the registry.
599  *          - Gets the registry subkey name for a given lctype.
600  */
601 static char *GetLocaleSubkeyName( DWORD lctype )
602 {
603     char    *pacKey=NULL;
604
605     switch ( lctype )
606     {
607     /* These values are used by SetLocaleInfo and GetLocaleInfo, and
608      * the values are stored in the registry, confirmed under Windows,
609      * for the ones that actually assign pacKey. Cases that aren't finished
610      * have not been confirmed, so that must be done before they can be
611      * added.
612      */
613     case LOCALE_SDATE :        /* The date separator. */
614         pacKey = "sDate";
615         break;
616     case LOCALE_ICURRDIGITS:
617         pacKey = "iCurrDigits";
618         break;
619     case LOCALE_SDECIMAL :
620         pacKey = "sDecimal";
621         break;
622     case LOCALE_ICURRENCY:
623         pacKey = "iCurrency";
624         break;
625     case LOCALE_SGROUPING :
626         pacKey = "sGrouping";
627         break;
628     case LOCALE_IDIGITS:
629         pacKey = "iDigits";
630         break;
631     case LOCALE_SLIST :
632         pacKey = "sList";
633         break;
634     /* case LOCALE_ICALENDARTYPE: */
635     /* case LOCALE_IFIRSTDAYOFWEEK: */
636     /* case LOCALE_IFIRSTWEEKOFYEAR: */
637     /* case LOCALE_SYEARMONTH : */
638     /* case LOCALE_SPOSITIVESIGN : */
639     /* case LOCALE_IPAPERSIZE: */
640     /*     break; */
641     case LOCALE_SLONGDATE :
642         pacKey = "sLongDate";
643         break;
644     case LOCALE_SMONDECIMALSEP :
645         pacKey = "sMonDecimalSep";
646         break;
647     case LOCALE_SMONGROUPING:
648         pacKey = "sMonGrouping";
649         break;
650     case LOCALE_IMEASURE:
651         pacKey = "iMeasure";
652         break;
653     case LOCALE_SMONTHOUSANDSEP :
654         pacKey = "sMonThousandSep";
655         break;
656     case LOCALE_INEGCURR:
657         pacKey = "iNegCurr";
658         break;
659     case LOCALE_SNEGATIVESIGN :
660         pacKey = "sNegativeSign";
661         break;
662     case LOCALE_INEGNUMBER:
663         pacKey = "iNegNumber";
664         break;
665     case LOCALE_SSHORTDATE :
666         pacKey = "sShortDate";
667         break;
668     case LOCALE_ILDATE:        /* Long Date format ordering specifier. */
669         pacKey = "iLDate";
670         break;
671     case LOCALE_ILZERO:
672         pacKey = "iLZero";
673         break;
674     case LOCALE_ITLZERO:
675         pacKey = "iTLZero";
676         break;
677     case LOCALE_ITIME:        /* Time format specifier. */
678         pacKey = "iTime";
679         break;
680     case LOCALE_STHOUSAND :
681         pacKey = "sThousand";
682         break;
683     case LOCALE_S1159:        /* AM */
684         pacKey = "s1159";
685         break;
686     case LOCALE_STIME:
687         pacKey = "sTime";
688         break;
689     case LOCALE_S2359:        /* PM */
690         pacKey = "s2359";
691         break;
692     case LOCALE_STIMEFORMAT :
693         pacKey = "sTimeFormat";
694         break;
695     case LOCALE_SCURRENCY:
696         pacKey = "sCurrency";
697         break;
698
699     /* The following are not listed under MSDN as supported, 
700      * but seem to be used and also stored in the registry.
701      */
702
703     case LOCALE_IDATE:
704         pacKey = "iDate";
705         break;
706     case LOCALE_SCOUNTRY:
707         pacKey = "sCountry";
708         break;
709     case LOCALE_ICOUNTRY:
710         pacKey = "iCountry";
711         break;
712     case LOCALE_SLANGUAGE:
713         pacKey = "sLanguage";
714         break;
715
716     default:
717         break;
718     }
719
720     return( pacKey );
721 }
722
723 /******************************************************************************
724  *              SetLocaleInfoA  [KERNEL32.@]
725  */
726 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
727 {
728     HKEY    hKey;
729     char    *pacKey;
730     char    acRealKey[128];
731
732     if ( (pacKey = GetLocaleSubkeyName(lctype)) )
733     {
734         sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
735         if ( RegCreateKeyA( HKEY_CURRENT_USER, acRealKey, 
736                                &hKey ) == ERROR_SUCCESS )
737         {
738             if ( RegSetValueExA( hKey, NULL, 0, REG_SZ, 
739                                  data, strlen(data)+1 ) != ERROR_SUCCESS )
740             {
741                 ERR("SetLocaleInfoA: %s did not work\n", pacKey );
742             }
743             RegCloseKey( hKey );
744         }
745     }
746     else
747     {
748     FIXME("(%ld,%ld,%s): stub\n",lcid,lctype,data);
749     }
750     return TRUE;
751 }
752
753 /******************************************************************************
754  *              IsValidLocale   [KERNEL32.@]
755  */
756 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
757 {
758     /* check if language is registered in the kernel32 resources */
759     if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
760         return FALSE;
761     else
762         return TRUE;
763 }
764
765 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
766                 LPCWSTR name, WORD LangID, LONG lParam)
767 {
768     CHAR bufA[20];
769     WCHAR bufW[20];
770     LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
771     sprintf(bufA, "%08X", (UINT)LangID);
772     MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
773     return lpfnLocaleEnum(bufW);
774 }
775
776 /******************************************************************************
777  *              EnumSystemLocalesW      [KERNEL32.@]
778  */
779 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
780                                     DWORD flags )
781 {
782     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
783
784     EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
785             (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
786             (LONG)lpfnLocaleEnum);
787
788     return TRUE;
789 }
790
791 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
792                 LPCSTR name, WORD LangID, LONG lParam)
793 {
794     CHAR bufA[20];
795     LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
796     sprintf(bufA, "%08X", (UINT)LangID);
797     return lpfnLocaleEnum(bufA);
798 }
799
800 /******************************************************************************
801  *              EnumSystemLocalesA      [KERNEL32.@]
802  */
803 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
804                                    DWORD flags)
805 {
806     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
807
808     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
809             (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
810             (LONG)lpfnLocaleEnum);
811
812     return TRUE;
813 }
814
815 static const unsigned char CT_CType2_LUT[] = {
816   C2_NOTAPPLICABLE, /*   -   0 */
817   C2_NOTAPPLICABLE, /*   -   1 */
818   C2_NOTAPPLICABLE, /*   -   2 */
819   C2_NOTAPPLICABLE, /*   -   3 */
820   C2_NOTAPPLICABLE, /*   -   4 */
821   C2_NOTAPPLICABLE, /*   -   5 */
822   C2_NOTAPPLICABLE, /*   -   6 */
823   C2_NOTAPPLICABLE, /*   -   7 */
824   C2_NOTAPPLICABLE, /*   -   8 */
825   C2_SEGMENTSEPARATOR, /*   -   9 */
826   C2_NOTAPPLICABLE, /*   -  10 */
827   C2_NOTAPPLICABLE, /*   -  11 */
828   C2_NOTAPPLICABLE, /*   -  12 */
829   C2_NOTAPPLICABLE, /*   -  13 */
830   C2_NOTAPPLICABLE, /*   -  14 */
831   C2_NOTAPPLICABLE, /*   -  15 */
832   C2_NOTAPPLICABLE, /*   -  16 */
833   C2_NOTAPPLICABLE, /*   -  17 */
834   C2_NOTAPPLICABLE, /*   -  18 */
835   C2_NOTAPPLICABLE, /*   -  19 */
836   C2_NOTAPPLICABLE, /*   -  20 */
837   C2_NOTAPPLICABLE, /*   -  21 */
838   C2_NOTAPPLICABLE, /*   -  22 */
839   C2_NOTAPPLICABLE, /*   -  23 */
840   C2_NOTAPPLICABLE, /*   -  24 */
841   C2_NOTAPPLICABLE, /*   -  25 */
842   C2_NOTAPPLICABLE, /*   -  26 */
843   C2_NOTAPPLICABLE, /*   -  27 */
844   C2_NOTAPPLICABLE, /*   -  28 */
845   C2_NOTAPPLICABLE, /*   -  29 */
846   C2_NOTAPPLICABLE, /*   -  30 */
847   C2_NOTAPPLICABLE, /*   -  31 */
848   C2_WHITESPACE, /*   -  32 */
849   C2_OTHERNEUTRAL, /* ! -  33 */
850   C2_OTHERNEUTRAL, /* " -  34 */ /* " */
851   C2_EUROPETERMINATOR, /* # -  35 */
852   C2_EUROPETERMINATOR, /* $ -  36 */
853   C2_EUROPETERMINATOR, /* % -  37 */
854   C2_LEFTTORIGHT, /* & -  38 */
855   C2_OTHERNEUTRAL, /* ' -  39 */
856   C2_OTHERNEUTRAL, /* ( -  40 */
857   C2_OTHERNEUTRAL, /* ) -  41 */
858   C2_OTHERNEUTRAL, /* * -  42 */
859   C2_EUROPETERMINATOR, /* + -  43 */
860   C2_COMMONSEPARATOR, /* , -  44 */
861   C2_EUROPETERMINATOR, /* - -  45 */
862   C2_EUROPESEPARATOR, /* . -  46 */
863   C2_EUROPESEPARATOR, /* / -  47 */
864   C2_EUROPENUMBER, /* 0 -  48 */
865   C2_EUROPENUMBER, /* 1 -  49 */
866   C2_EUROPENUMBER, /* 2 -  50 */
867   C2_EUROPENUMBER, /* 3 -  51 */
868   C2_EUROPENUMBER, /* 4 -  52 */
869   C2_EUROPENUMBER, /* 5 -  53 */
870   C2_EUROPENUMBER, /* 6 -  54 */
871   C2_EUROPENUMBER, /* 7 -  55 */
872   C2_EUROPENUMBER, /* 8 -  56 */
873   C2_EUROPENUMBER, /* 9 -  57 */
874   C2_COMMONSEPARATOR, /* : -  58 */
875   C2_OTHERNEUTRAL, /* ; -  59 */
876   C2_OTHERNEUTRAL, /* < -  60 */
877   C2_OTHERNEUTRAL, /* = -  61 */
878   C2_OTHERNEUTRAL, /* > -  62 */
879   C2_OTHERNEUTRAL, /* ? -  63 */
880   C2_LEFTTORIGHT, /* @ -  64 */
881   C2_LEFTTORIGHT, /* A -  65 */
882   C2_LEFTTORIGHT, /* B -  66 */
883   C2_LEFTTORIGHT, /* C -  67 */
884   C2_LEFTTORIGHT, /* D -  68 */
885   C2_LEFTTORIGHT, /* E -  69 */
886   C2_LEFTTORIGHT, /* F -  70 */
887   C2_LEFTTORIGHT, /* G -  71 */
888   C2_LEFTTORIGHT, /* H -  72 */
889   C2_LEFTTORIGHT, /* I -  73 */
890   C2_LEFTTORIGHT, /* J -  74 */
891   C2_LEFTTORIGHT, /* K -  75 */
892   C2_LEFTTORIGHT, /* L -  76 */
893   C2_LEFTTORIGHT, /* M -  77 */
894   C2_LEFTTORIGHT, /* N -  78 */
895   C2_LEFTTORIGHT, /* O -  79 */
896   C2_LEFTTORIGHT, /* P -  80 */
897   C2_LEFTTORIGHT, /* Q -  81 */
898   C2_LEFTTORIGHT, /* R -  82 */
899   C2_LEFTTORIGHT, /* S -  83 */
900   C2_LEFTTORIGHT, /* T -  84 */
901   C2_LEFTTORIGHT, /* U -  85 */
902   C2_LEFTTORIGHT, /* V -  86 */
903   C2_LEFTTORIGHT, /* W -  87 */
904   C2_LEFTTORIGHT, /* X -  88 */
905   C2_LEFTTORIGHT, /* Y -  89 */
906   C2_LEFTTORIGHT, /* Z -  90 */
907   C2_OTHERNEUTRAL, /* [ -  91 */
908   C2_OTHERNEUTRAL, /* \ -  92 */
909   C2_OTHERNEUTRAL, /* ] -  93 */
910   C2_OTHERNEUTRAL, /* ^ -  94 */
911   C2_OTHERNEUTRAL, /* _ -  95 */
912   C2_OTHERNEUTRAL, /* ` -  96 */
913   C2_LEFTTORIGHT, /* a -  97 */
914   C2_LEFTTORIGHT, /* b -  98 */
915   C2_LEFTTORIGHT, /* c -  99 */
916   C2_LEFTTORIGHT, /* d - 100 */
917   C2_LEFTTORIGHT, /* e - 101 */
918   C2_LEFTTORIGHT, /* f - 102 */
919   C2_LEFTTORIGHT, /* g - 103 */
920   C2_LEFTTORIGHT, /* h - 104 */
921   C2_LEFTTORIGHT, /* i - 105 */
922   C2_LEFTTORIGHT, /* j - 106 */
923   C2_LEFTTORIGHT, /* k - 107 */
924   C2_LEFTTORIGHT, /* l - 108 */
925   C2_LEFTTORIGHT, /* m - 109 */
926   C2_LEFTTORIGHT, /* n - 110 */
927   C2_LEFTTORIGHT, /* o - 111 */
928   C2_LEFTTORIGHT, /* p - 112 */
929   C2_LEFTTORIGHT, /* q - 113 */
930   C2_LEFTTORIGHT, /* r - 114 */
931   C2_LEFTTORIGHT, /* s - 115 */
932   C2_LEFTTORIGHT, /* t - 116 */
933   C2_LEFTTORIGHT, /* u - 117 */
934   C2_LEFTTORIGHT, /* v - 118 */
935   C2_LEFTTORIGHT, /* w - 119 */
936   C2_LEFTTORIGHT, /* x - 120 */
937   C2_LEFTTORIGHT, /* y - 121 */
938   C2_LEFTTORIGHT, /* z - 122 */
939   C2_OTHERNEUTRAL, /* { - 123 */
940   C2_OTHERNEUTRAL, /* | - 124 */
941   C2_OTHERNEUTRAL, /* } - 125 */
942   C2_OTHERNEUTRAL, /* ~ - 126 */
943   C2_NOTAPPLICABLE, /* \7f - 127 */
944   C2_NOTAPPLICABLE, /* \80 - 128 */
945   C2_NOTAPPLICABLE, /* \81 - 129 */
946   C2_OTHERNEUTRAL, /* \82 - 130 */
947   C2_LEFTTORIGHT, /* \83 - 131 */
948   C2_OTHERNEUTRAL, /* \84 - 132 */
949   C2_OTHERNEUTRAL, /* \85 - 133 */
950   C2_OTHERNEUTRAL, /* \86 - 134 */
951   C2_OTHERNEUTRAL, /* \87 - 135 */
952   C2_LEFTTORIGHT, /* \88 - 136 */
953   C2_EUROPETERMINATOR, /* \89 - 137 */
954   C2_LEFTTORIGHT, /* \8a - 138 */
955   C2_OTHERNEUTRAL, /* \8b - 139 */
956   C2_LEFTTORIGHT, /* \8c - 140 */
957   C2_NOTAPPLICABLE, /* \8d - 141 */
958   C2_NOTAPPLICABLE, /* \8e - 142 */
959   C2_NOTAPPLICABLE, /* \8f - 143 */
960   C2_NOTAPPLICABLE, /* \90 - 144 */
961   C2_OTHERNEUTRAL, /* \91 - 145 */
962   C2_OTHERNEUTRAL, /* \92 - 146 */
963   C2_OTHERNEUTRAL, /* \93 - 147 */
964   C2_OTHERNEUTRAL, /* \94 - 148 */
965   C2_OTHERNEUTRAL, /* \95 - 149 */
966   C2_OTHERNEUTRAL, /* \96 - 150 */
967   C2_OTHERNEUTRAL, /* \97 - 151 */
968   C2_LEFTTORIGHT, /* \98 - 152 */
969   C2_OTHERNEUTRAL, /* \99 - 153 */
970   C2_LEFTTORIGHT, /* \9a - 154 */
971   C2_OTHERNEUTRAL, /* \9b - 155 */
972   C2_LEFTTORIGHT, /* \9c - 156 */
973   C2_NOTAPPLICABLE, /* \9d - 157 */
974   C2_NOTAPPLICABLE, /* \9e - 158 */
975   C2_LEFTTORIGHT, /* \9f - 159 */
976   C2_WHITESPACE, /*   - 160 */
977   C2_OTHERNEUTRAL, /* ¡ - 161 */
978   C2_EUROPETERMINATOR, /* ¢ - 162 */
979   C2_EUROPETERMINATOR, /* £ - 163 */
980   C2_EUROPETERMINATOR, /* ¤ - 164 */
981   C2_EUROPETERMINATOR, /* ¥ - 165 */
982   C2_OTHERNEUTRAL, /* ¦ - 166 */
983   C2_OTHERNEUTRAL, /* § - 167 */
984   C2_OTHERNEUTRAL, /* ¨ - 168 */
985   C2_OTHERNEUTRAL, /* © - 169 */
986   C2_OTHERNEUTRAL, /* ª - 170 */
987   C2_OTHERNEUTRAL, /* « - 171 */
988   C2_OTHERNEUTRAL, /* ¬ - 172 */
989   C2_OTHERNEUTRAL, /* ­ - 173 */
990   C2_OTHERNEUTRAL, /* ® - 174 */
991   C2_OTHERNEUTRAL, /* ¯ - 175 */
992   C2_EUROPETERMINATOR, /* ° - 176 */
993   C2_EUROPETERMINATOR, /* ± - 177 */
994   C2_EUROPENUMBER, /* ² - 178 */
995   C2_EUROPENUMBER, /* ³ - 179 */
996   C2_OTHERNEUTRAL, /* ´ - 180 */
997   C2_OTHERNEUTRAL, /* µ - 181 */
998   C2_OTHERNEUTRAL, /* ¶ - 182 */
999   C2_OTHERNEUTRAL, /* · - 183 */
1000   C2_OTHERNEUTRAL, /* ¸ - 184 */
1001   C2_EUROPENUMBER, /* ¹ - 185 */
1002   C2_OTHERNEUTRAL, /* º - 186 */
1003   C2_OTHERNEUTRAL, /* » - 187 */
1004   C2_OTHERNEUTRAL, /* ¼ - 188 */
1005   C2_OTHERNEUTRAL, /* ½ - 189 */
1006   C2_OTHERNEUTRAL, /* ¾ - 190 */
1007   C2_OTHERNEUTRAL, /* ¿ - 191 */
1008   C2_LEFTTORIGHT, /* À - 192 */
1009   C2_LEFTTORIGHT, /* Á - 193 */
1010   C2_LEFTTORIGHT, /* Â - 194 */
1011   C2_LEFTTORIGHT, /* Ã - 195 */
1012   C2_LEFTTORIGHT, /* Ä - 196 */
1013   C2_LEFTTORIGHT, /* Å - 197 */
1014   C2_LEFTTORIGHT, /* Æ - 198 */
1015   C2_LEFTTORIGHT, /* Ç - 199 */
1016   C2_LEFTTORIGHT, /* È - 200 */
1017   C2_LEFTTORIGHT, /* É - 201 */
1018   C2_LEFTTORIGHT, /* Ê - 202 */
1019   C2_LEFTTORIGHT, /* Ë - 203 */
1020   C2_LEFTTORIGHT, /* Ì - 204 */
1021   C2_LEFTTORIGHT, /* Í - 205 */
1022   C2_LEFTTORIGHT, /* Î - 206 */
1023   C2_LEFTTORIGHT, /* Ï - 207 */
1024   C2_LEFTTORIGHT, /* Ð - 208 */
1025   C2_LEFTTORIGHT, /* Ñ - 209 */
1026   C2_LEFTTORIGHT, /* Ò - 210 */
1027   C2_LEFTTORIGHT, /* Ó - 211 */
1028   C2_LEFTTORIGHT, /* Ô - 212 */
1029   C2_LEFTTORIGHT, /* Õ - 213 */
1030   C2_LEFTTORIGHT, /* Ö - 214 */
1031   C2_OTHERNEUTRAL, /* × - 215 */
1032   C2_LEFTTORIGHT, /* Ø - 216 */
1033   C2_LEFTTORIGHT, /* Ù - 217 */
1034   C2_LEFTTORIGHT, /* Ú - 218 */
1035   C2_LEFTTORIGHT, /* Û - 219 */
1036   C2_LEFTTORIGHT, /* Ü - 220 */
1037   C2_LEFTTORIGHT, /* Ý - 221 */
1038   C2_LEFTTORIGHT, /* Þ - 222 */
1039   C2_LEFTTORIGHT, /* ß - 223 */
1040   C2_LEFTTORIGHT, /* à - 224 */
1041   C2_LEFTTORIGHT, /* á - 225 */
1042   C2_LEFTTORIGHT, /* â - 226 */
1043   C2_LEFTTORIGHT, /* ã - 227 */
1044   C2_LEFTTORIGHT, /* ä - 228 */
1045   C2_LEFTTORIGHT, /* å - 229 */
1046   C2_LEFTTORIGHT, /* æ - 230 */
1047   C2_LEFTTORIGHT, /* ç - 231 */
1048   C2_LEFTTORIGHT, /* è - 232 */
1049   C2_LEFTTORIGHT, /* é - 233 */
1050   C2_LEFTTORIGHT, /* ê - 234 */
1051   C2_LEFTTORIGHT, /* ë - 235 */
1052   C2_LEFTTORIGHT, /* ì - 236 */
1053   C2_LEFTTORIGHT, /* í - 237 */
1054   C2_LEFTTORIGHT, /* î - 238 */
1055   C2_LEFTTORIGHT, /* ï - 239 */
1056   C2_LEFTTORIGHT, /* ð - 240 */
1057   C2_LEFTTORIGHT, /* ñ - 241 */
1058   C2_LEFTTORIGHT, /* ò - 242 */
1059   C2_LEFTTORIGHT, /* ó - 243 */
1060   C2_LEFTTORIGHT, /* ô - 244 */
1061   C2_LEFTTORIGHT, /* õ - 245 */
1062   C2_LEFTTORIGHT, /* ö - 246 */
1063   C2_OTHERNEUTRAL, /* ÷ - 247 */
1064   C2_LEFTTORIGHT, /* ø - 248 */
1065   C2_LEFTTORIGHT, /* ù - 249 */
1066   C2_LEFTTORIGHT, /* ú - 250 */
1067   C2_LEFTTORIGHT, /* û - 251 */
1068   C2_LEFTTORIGHT, /* ü - 252 */
1069   C2_LEFTTORIGHT, /* ý - 253 */
1070   C2_LEFTTORIGHT, /* þ - 254 */
1071   C2_LEFTTORIGHT /* ÿ - 255 */
1072 };
1073
1074 static const WORD OLE2NLS_CT_CType3_LUT[] = { 
1075   0x0000, /*   -   0 */
1076   0x0000, /*   -   1 */
1077   0x0000, /*   -   2 */
1078   0x0000, /*   -   3 */
1079   0x0000, /*   -   4 */
1080   0x0000, /*   -   5 */
1081   0x0000, /*   -   6 */
1082   0x0000, /*   -   7 */
1083   0x0000, /*   -   8 */
1084   0x0008, /*   -   9 */
1085   0x0008, /*   -  10 */
1086   0x0008, /*   -  11 */
1087   0x0008, /*   -  12 */
1088   0x0008, /*   -  13 */
1089   0x0000, /*   -  14 */
1090   0x0000, /*   -  15 */
1091   0x0000, /*   -  16 */
1092   0x0000, /*   -  17 */
1093   0x0000, /*   -  18 */
1094   0x0000, /*   -  19 */
1095   0x0000, /*   -  20 */
1096   0x0000, /*   -  21 */
1097   0x0000, /*   -  22 */
1098   0x0000, /*   -  23 */
1099   0x0000, /*   -  24 */
1100   0x0000, /*   -  25 */
1101   0x0000, /*   -  26 */
1102   0x0000, /*   -  27 */
1103   0x0000, /*   -  28 */
1104   0x0000, /*   -  29 */
1105   0x0000, /*   -  30 */
1106   0x0000, /*   -  31 */
1107   0x0048, /*   -  32 */
1108   0x0048, /* ! -  33 */
1109   0x0448, /* " -  34 */ /* " */
1110   0x0048, /* # -  35 */
1111   0x0448, /* $ -  36 */
1112   0x0048, /* % -  37 */
1113   0x0048, /* & -  38 */
1114   0x0440, /* ' -  39 */
1115   0x0048, /* ( -  40 */
1116   0x0048, /* ) -  41 */
1117   0x0048, /* * -  42 */
1118   0x0048, /* + -  43 */
1119   0x0048, /* , -  44 */
1120   0x0440, /* - -  45 */
1121   0x0048, /* . -  46 */
1122   0x0448, /* / -  47 */
1123   0x0040, /* 0 -  48 */
1124   0x0040, /* 1 -  49 */
1125   0x0040, /* 2 -  50 */
1126   0x0040, /* 3 -  51 */
1127   0x0040, /* 4 -  52 */
1128   0x0040, /* 5 -  53 */
1129   0x0040, /* 6 -  54 */
1130   0x0040, /* 7 -  55 */
1131   0x0040, /* 8 -  56 */
1132   0x0040, /* 9 -  57 */
1133   0x0048, /* : -  58 */
1134   0x0048, /* ; -  59 */
1135   0x0048, /* < -  60 */
1136   0x0448, /* = -  61 */
1137   0x0048, /* > -  62 */
1138   0x0048, /* ? -  63 */
1139   0x0448, /* @ -  64 */
1140   0x8040, /* A -  65 */
1141   0x8040, /* B -  66 */
1142   0x8040, /* C -  67 */
1143   0x8040, /* D -  68 */
1144   0x8040, /* E -  69 */
1145   0x8040, /* F -  70 */
1146   0x8040, /* G -  71 */
1147   0x8040, /* H -  72 */
1148   0x8040, /* I -  73 */
1149   0x8040, /* J -  74 */
1150   0x8040, /* K -  75 */
1151   0x8040, /* L -  76 */
1152   0x8040, /* M -  77 */
1153   0x8040, /* N -  78 */
1154   0x8040, /* O -  79 */
1155   0x8040, /* P -  80 */
1156   0x8040, /* Q -  81 */
1157   0x8040, /* R -  82 */
1158   0x8040, /* S -  83 */
1159   0x8040, /* T -  84 */
1160   0x8040, /* U -  85 */
1161   0x8040, /* V -  86 */
1162   0x8040, /* W -  87 */
1163   0x8040, /* X -  88 */
1164   0x8040, /* Y -  89 */
1165   0x8040, /* Z -  90 */
1166   0x0048, /* [ -  91 */
1167   0x0448, /* \ -  92 */
1168   0x0048, /* ] -  93 */
1169   0x0448, /* ^ -  94 */
1170   0x0448, /* _ -  95 */
1171   0x0448, /* ` -  96 */
1172   0x8040, /* a -  97 */
1173   0x8040, /* b -  98 */
1174   0x8040, /* c -  99 */
1175   0x8040, /* d - 100 */
1176   0x8040, /* e - 101 */
1177   0x8040, /* f - 102 */
1178   0x8040, /* g - 103 */
1179   0x8040, /* h - 104 */
1180   0x8040, /* i - 105 */
1181   0x8040, /* j - 106 */
1182   0x8040, /* k - 107 */
1183   0x8040, /* l - 108 */
1184   0x8040, /* m - 109 */
1185   0x8040, /* n - 110 */
1186   0x8040, /* o - 111 */
1187   0x8040, /* p - 112 */
1188   0x8040, /* q - 113 */
1189   0x8040, /* r - 114 */
1190   0x8040, /* s - 115 */
1191   0x8040, /* t - 116 */
1192   0x8040, /* u - 117 */
1193   0x8040, /* v - 118 */
1194   0x8040, /* w - 119 */
1195   0x8040, /* x - 120 */
1196   0x8040, /* y - 121 */
1197   0x8040, /* z - 122 */
1198   0x0048, /* { - 123 */
1199   0x0048, /* | - 124 */
1200   0x0048, /* } - 125 */
1201   0x0448, /* ~ - 126 */
1202   0x0000, /* \7f - 127 */
1203   0x0000, /* \80 - 128 */
1204   0x0000, /* \81 - 129 */
1205   0x0008, /* \82 - 130 */
1206   0x8000, /* \83 - 131 */
1207   0x0008, /* \84 - 132 */
1208   0x0008, /* \85 - 133 */
1209   0x0008, /* \86 - 134 */
1210   0x0008, /* \87 - 135 */
1211   0x0001, /* \88 - 136 */
1212   0x0008, /* \89 - 137 */
1213   0x8003, /* \8a - 138 */
1214   0x0008, /* \8b - 139 */
1215   0x8000, /* \8c - 140 */
1216   0x0000, /* \8d - 141 */
1217   0x0000, /* \8e - 142 */
1218   0x0000, /* \8f - 143 */
1219   0x0000, /* \90 - 144 */
1220   0x0088, /* \91 - 145 */
1221   0x0088, /* \92 - 146 */
1222   0x0088, /* \93 - 147 */
1223   0x0088, /* \94 - 148 */
1224   0x0008, /* \95 - 149 */
1225   0x0400, /* \96 - 150 */
1226   0x0400, /* \97 - 151 */
1227   0x0408, /* \98 - 152 */
1228   0x0000, /* \99 - 153 */
1229   0x8003, /* \9a - 154 */
1230   0x0008, /* \9b - 155 */
1231   0x8000, /* \9c - 156 */
1232   0x0000, /* \9d - 157 */
1233   0x0000, /* \9e - 158 */
1234   0x8003, /* \9f - 159 */
1235   0x0008, /*   - 160 */
1236   0x0008, /* ¡ - 161 */
1237   0x0048, /* ¢ - 162 */
1238   0x0048, /* £ - 163 */
1239   0x0008, /* ¤ - 164 */
1240   0x0048, /* ¥ - 165 */
1241   0x0048, /* ¦ - 166 */
1242   0x0008, /* § - 167 */
1243   0x0408, /* ¨ - 168 */
1244   0x0008, /* © - 169 */
1245   0x0400, /* ª - 170 */
1246   0x0008, /* « - 171 */
1247   0x0048, /* ¬ - 172 */
1248   0x0408, /* ­ - 173 */
1249   0x0008, /* ® - 174 */
1250   0x0448, /* ¯ - 175 */
1251   0x0008, /* ° - 176 */
1252   0x0008, /* ± - 177 */
1253   0x0000, /* ² - 178 */
1254   0x0000, /* ³ - 179 */
1255   0x0408, /* ´ - 180 */
1256   0x0008, /* µ - 181 */
1257   0x0008, /* ¶ - 182 */
1258   0x0008, /* · - 183 */
1259   0x0408, /* ¸ - 184 */
1260   0x0000, /* ¹ - 185 */
1261   0x0400, /* º - 186 */
1262   0x0008, /* » - 187 */
1263   0x0000, /* ¼ - 188 */
1264   0x0000, /* ½ - 189 */
1265   0x0000, /* ¾ - 190 */
1266   0x0008, /* ¿ - 191 */
1267   0x8003, /* À - 192 */
1268   0x8003, /* Á - 193 */
1269   0x8003, /* Â - 194 */
1270   0x8003, /* Ã - 195 */
1271   0x8003, /* Ä - 196 */
1272   0x8003, /* Å - 197 */
1273   0x8000, /* Æ - 198 */
1274   0x8003, /* Ç - 199 */
1275   0x8003, /* È - 200 */
1276   0x8003, /* É - 201 */
1277   0x8003, /* Ê - 202 */
1278   0x8003, /* Ë - 203 */
1279   0x8003, /* Ì - 204 */
1280   0x8003, /* Í - 205 */
1281   0x8003, /* Î - 206 */
1282   0x8003, /* Ï - 207 */
1283   0x8000, /* Ð - 208 */
1284   0x8003, /* Ñ - 209 */
1285   0x8003, /* Ò - 210 */
1286   0x8003, /* Ó - 211 */
1287   0x8003, /* Ô - 212 */
1288   0x8003, /* Õ - 213 */
1289   0x8003, /* Ö - 214 */
1290   0x0008, /* × - 215 */
1291   0x8003, /* Ø - 216 */
1292   0x8003, /* Ù - 217 */
1293   0x8003, /* Ú - 218 */
1294   0x8003, /* Û - 219 */
1295   0x8003, /* Ü - 220 */
1296   0x8003, /* Ý - 221 */
1297   0x8000, /* Þ - 222 */
1298   0x8000, /* ß - 223 */
1299   0x8003, /* à - 224 */
1300   0x8003, /* á - 225 */
1301   0x8003, /* â - 226 */
1302   0x8003, /* ã - 227 */
1303   0x8003, /* ä - 228 */
1304   0x8003, /* å - 229 */
1305   0x8000, /* æ - 230 */
1306   0x8003, /* ç - 231 */
1307   0x8003, /* è - 232 */
1308   0x8003, /* é - 233 */
1309   0x8003, /* ê - 234 */
1310   0x8003, /* ë - 235 */
1311   0x8003, /* ì - 236 */
1312   0x8003, /* í - 237 */
1313   0x8003, /* î - 238 */
1314   0x8003, /* ï - 239 */
1315   0x8000, /* ð - 240 */
1316   0x8003, /* ñ - 241 */
1317   0x8003, /* ò - 242 */
1318   0x8003, /* ó - 243 */
1319   0x8003, /* ô - 244 */
1320   0x8003, /* õ - 245 */
1321   0x8003, /* ö - 246 */
1322   0x0008, /* ÷ - 247 */
1323   0x8003, /* ø - 248 */
1324   0x8003, /* ù - 249 */
1325   0x8003, /* ú - 250 */
1326   0x8003, /* û - 251 */
1327   0x8003, /* ü - 252 */
1328   0x8003, /* ý - 253 */
1329   0x8000, /* þ - 254 */
1330   0x8003  /* ÿ - 255 */
1331 };
1332
1333 /******************************************************************************
1334  *              GetStringTypeA  [KERNEL32.@]
1335  */
1336 BOOL WINAPI GetStringTypeA(LCID locale,DWORD dwInfoType,LPCSTR src,
1337                                INT cchSrc,LPWORD chartype)
1338 {
1339         return GetStringTypeExA(locale,dwInfoType,src,cchSrc,chartype);
1340 }
1341
1342 /******************************************************************************
1343  *              GetStringTypeExA        [KERNEL32.@]
1344  *
1345  * FIXME: Ignores the locale.
1346  */
1347 BOOL WINAPI GetStringTypeExA(LCID locale,DWORD dwInfoType,LPCSTR src,
1348                                  INT cchSrc,LPWORD chartype)
1349 {
1350         int     i;
1351         
1352         if ((src==NULL) || (chartype==NULL) || (src==(LPSTR)chartype))
1353         {
1354           SetLastError(ERROR_INVALID_PARAMETER);
1355           return FALSE;
1356         }
1357
1358         if (cchSrc==-1)
1359           cchSrc=strlen(src)+1;
1360           
1361         switch (dwInfoType) {
1362         case CT_CTYPE1:
1363           for (i=0;i<cchSrc;i++) 
1364           {
1365             chartype[i] = 0;
1366             if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1367             if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1368             if (islower(src[i])) chartype[i]|=C1_LOWER;
1369             if (isupper(src[i])) chartype[i]|=C1_UPPER;
1370             if (isspace(src[i])) chartype[i]|=C1_SPACE;
1371             if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1372             if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1373 /* FIXME: isblank() is a GNU extension */
1374 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1375             if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1376             /* C1_XDIGIT */
1377         }
1378         return TRUE;
1379
1380         case CT_CTYPE2:
1381           for (i=0;i<cchSrc;i++) 
1382           {
1383             chartype[i]=(WORD)CT_CType2_LUT[i];
1384           }
1385           return TRUE;
1386
1387         case CT_CTYPE3:
1388           for (i=0;i<cchSrc;i++) 
1389           {
1390             chartype[i]=OLE2NLS_CT_CType3_LUT[i];
1391           }
1392           return TRUE;
1393
1394         default:
1395           ERR("Unknown dwInfoType:%ld\n",dwInfoType);
1396           return FALSE;
1397         }
1398 }
1399
1400 /***********************************************************************
1401  *           VerLanguageNameA              [KERNEL32.@]
1402  */
1403 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1404 {
1405     if(!szLang)
1406         return 0;
1407
1408     return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
1409 }
1410
1411 /***********************************************************************
1412  *           VerLanguageNameW              [KERNEL32.@]
1413  */
1414 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1415 {
1416     if(!szLang)
1417         return 0;
1418
1419     return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
1420 }
1421
1422  
1423 static const unsigned char LCM_Unicode_LUT[] = {
1424   6      ,   3, /*   -   1 */  
1425   6      ,   4, /*   -   2 */  
1426   6      ,   5, /*   -   3 */  
1427   6      ,   6, /*   -   4 */  
1428   6      ,   7, /*   -   5 */  
1429   6      ,   8, /*   -   6 */  
1430   6      ,   9, /*   -   7 */  
1431   6      ,  10, /*   -   8 */  
1432   7      ,   5, /*   -   9 */  
1433   7      ,   6, /*   -  10 */  
1434   7      ,   7, /*   -  11 */  
1435   7      ,   8, /*   -  12 */  
1436   7      ,   9, /*   -  13 */  
1437   6      ,  11, /*   -  14 */  
1438   6      ,  12, /*   -  15 */  
1439   6      ,  13, /*   -  16 */  
1440   6      ,  14, /*   -  17 */  
1441   6      ,  15, /*   -  18 */  
1442   6      ,  16, /*   -  19 */  
1443   6      ,  17, /*   -  20 */  
1444   6      ,  18, /*   -  21 */  
1445   6      ,  19, /*   -  22 */  
1446   6      ,  20, /*   -  23 */  
1447   6      ,  21, /*   -  24 */  
1448   6      ,  22, /*   -  25 */  
1449   6      ,  23, /*   -  26 */  
1450   6      ,  24, /*   -  27 */  
1451   6      ,  25, /*   -  28 */  
1452   6      ,  26, /*   -  29 */  
1453   6      ,  27, /*   -  30 */  
1454   6      ,  28, /*   -  31 */  
1455   7      ,   2, /*   -  32 */
1456   7      ,  28, /* ! -  33 */
1457   7      ,  29, /* " -  34 */ /* " */
1458   7      ,  31, /* # -  35 */
1459   7      ,  33, /* $ -  36 */
1460   7      ,  35, /* % -  37 */
1461   7      ,  37, /* & -  38 */
1462   6      , 128, /* ' -  39 */
1463   7      ,  39, /* ( -  40 */
1464   7      ,  42, /* ) -  41 */
1465   7      ,  45, /* * -  42 */
1466   8      ,   3, /* + -  43 */
1467   7      ,  47, /* , -  44 */
1468   6      , 130, /* - -  45 */
1469   7      ,  51, /* . -  46 */
1470   7      ,  53, /* / -  47 */
1471  12      ,   3, /* 0 -  48 */
1472  12      ,  33, /* 1 -  49 */
1473  12      ,  51, /* 2 -  50 */
1474  12      ,  70, /* 3 -  51 */
1475  12      ,  88, /* 4 -  52 */
1476  12      , 106, /* 5 -  53 */
1477  12      , 125, /* 6 -  54 */
1478  12      , 144, /* 7 -  55 */
1479  12      , 162, /* 8 -  56 */
1480  12      , 180, /* 9 -  57 */
1481   7      ,  55, /* : -  58 */
1482   7      ,  58, /* ; -  59 */
1483   8      ,  14, /* < -  60 */
1484   8      ,  18, /* = -  61 */
1485   8      ,  20, /* > -  62 */
1486   7      ,  60, /* ? -  63 */
1487   7      ,  62, /* @ -  64 */
1488  14      ,   2, /* A -  65 */
1489  14      ,   9, /* B -  66 */
1490  14      ,  10, /* C -  67 */
1491  14      ,  26, /* D -  68 */
1492  14      ,  33, /* E -  69 */
1493  14      ,  35, /* F -  70 */
1494  14      ,  37, /* G -  71 */
1495  14      ,  44, /* H -  72 */
1496  14      ,  50, /* I -  73 */
1497  14      ,  53, /* J -  74 */
1498  14      ,  54, /* K -  75 */
1499  14      ,  72, /* L -  76 */
1500  14      ,  81, /* M -  77 */
1501  14      , 112, /* N -  78 */
1502  14      , 124, /* O -  79 */
1503  14      , 126, /* P -  80 */
1504  14      , 137, /* Q -  81 */
1505  14      , 138, /* R -  82 */
1506  14      , 145, /* S -  83 */
1507  14      , 153, /* T -  84 */
1508  14      , 159, /* U -  85 */
1509  14      , 162, /* V -  86 */
1510  14      , 164, /* W -  87 */
1511  14      , 166, /* X -  88 */
1512  14      , 167, /* Y -  89 */
1513  14      , 169, /* Z -  90 */
1514   7      ,  63, /* [ -  91 */
1515   7      ,  65, /* \ -  92 */
1516   7      ,  66, /* ] -  93 */
1517   7      ,  67, /* ^ -  94 */
1518   7      ,  68, /* _ -  95 */
1519   7      ,  72, /* ` -  96 */
1520  14      ,   2, /* a -  97 */
1521  14      ,   9, /* b -  98 */
1522  14      ,  10, /* c -  99 */
1523  14      ,  26, /* d - 100 */
1524  14      ,  33, /* e - 101 */
1525  14      ,  35, /* f - 102 */
1526  14      ,  37, /* g - 103 */
1527  14      ,  44, /* h - 104 */
1528  14      ,  50, /* i - 105 */
1529  14      ,  53, /* j - 106 */
1530  14      ,  54, /* k - 107 */
1531  14      ,  72, /* l - 108 */
1532  14      ,  81, /* m - 109 */
1533  14      , 112, /* n - 110 */
1534  14      , 124, /* o - 111 */
1535  14      , 126, /* p - 112 */
1536  14      , 137, /* q - 113 */
1537  14      , 138, /* r - 114 */
1538  14      , 145, /* s - 115 */
1539  14      , 153, /* t - 116 */
1540  14      , 159, /* u - 117 */
1541  14      , 162, /* v - 118 */
1542  14      , 164, /* w - 119 */
1543  14      , 166, /* x - 120 */
1544  14      , 167, /* y - 121 */
1545  14      , 169, /* z - 122 */
1546   7      ,  74, /* { - 123 */
1547   7      ,  76, /* | - 124 */
1548   7      ,  78, /* } - 125 */
1549   7      ,  80, /* ~ - 126 */
1550   6      ,  29, /* \7f - 127 */
1551   6      ,  30, /* \80 - 128 */
1552   6      ,  31, /* \81 - 129 */
1553   7      , 123, /* \82 - 130 */
1554  14      ,  35, /* \83 - 131 */
1555   7      , 127, /* \84 - 132 */
1556  10      ,  21, /* \85 - 133 */
1557  10      ,  15, /* \86 - 134 */
1558  10      ,  16, /* \87 - 135 */
1559   7      ,  67, /* \88 - 136 */
1560  10      ,  22, /* \89 - 137 */
1561  14      , 145, /* \8a - 138 */
1562   7      , 136, /* \8b - 139 */
1563  14 + 16 , 124, /* \8c - 140 */
1564   6      ,  43, /* \8d - 141 */
1565   6      ,  44, /* \8e - 142 */
1566   6      ,  45, /* \8f - 143 */
1567   6      ,  46, /* \90 - 144 */
1568   7      , 121, /* \91 - 145 */
1569   7      , 122, /* \92 - 146 */
1570   7      , 125, /* \93 - 147 */
1571   7      , 126, /* \94 - 148 */
1572  10      ,  17, /* \95 - 149 */
1573   6      , 137, /* \96 - 150 */
1574   6      , 139, /* \97 - 151 */
1575   7      ,  93, /* \98 - 152 */
1576  14      , 156, /* \99 - 153 */
1577  14      , 145, /* \9a - 154 */
1578   7      , 137, /* \9b - 155 */
1579  14 + 16 , 124, /* \9c - 156 */
1580   6      ,  59, /* \9d - 157 */
1581   6      ,  60, /* \9e - 158 */
1582  14      , 167, /* \9f - 159 */
1583   7      ,   4, /*   - 160 */
1584   7      ,  81, /* ¡ - 161 */
1585  10      ,   2, /* ¢ - 162 */
1586  10      ,   3, /* £ - 163 */
1587  10      ,   4, /* ¤ - 164 */
1588  10      ,   5, /* ¥ - 165 */
1589   7      ,  82, /* ¦ - 166 */
1590  10      ,   6, /* § - 167 */
1591   7      ,  83, /* ¨ - 168 */
1592  10      ,   7, /* © - 169 */
1593  14      ,   2, /* ª - 170 */
1594   8      ,  24, /* « - 171 */
1595  10      ,   8, /* ¬ - 172 */
1596   6      , 131, /* ­ - 173 */
1597  10      ,   9, /* ® - 174 */
1598   7      ,  84, /* ¯ - 175 */
1599  10      ,  10, /* ° - 176 */
1600   8      ,  23, /* ± - 177 */
1601  12      ,  51, /* ² - 178 */
1602  12      ,  70, /* ³ - 179 */
1603   7      ,  85, /* ´ - 180 */
1604  10      ,  11, /* µ - 181 */
1605  10      ,  12, /* ¶ - 182 */
1606  10      ,  13, /* · - 183 */
1607   7      ,  86, /* ¸ - 184 */
1608  12      ,  33, /* ¹ - 185 */
1609  14      , 124, /* º - 186 */
1610   8      ,  26, /* » - 187 */
1611  12      ,  21, /* ¼ - 188 */
1612  12      ,  25, /* ½ - 189 */
1613  12      ,  29, /* ¾ - 190 */
1614   7      ,  87, /* ¿ - 191 */
1615  14      ,   2, /* À - 192 */
1616  14      ,   2, /* Á - 193 */
1617  14      ,   2, /* Â - 194 */
1618  14      ,   2, /* Ã - 195 */
1619  14      ,   2, /* Ä - 196 */
1620  14      ,   2, /* Å - 197 */
1621  14 + 16 ,   2, /* Æ - 198 */
1622  14      ,  10, /* Ç - 199 */
1623  14      ,  33, /* È - 200 */
1624  14      ,  33, /* É - 201 */
1625  14      ,  33, /* Ê - 202 */
1626  14      ,  33, /* Ë - 203 */
1627  14      ,  50, /* Ì - 204 */
1628  14      ,  50, /* Í - 205 */
1629  14      ,  50, /* Î - 206 */
1630  14      ,  50, /* Ï - 207 */
1631  14      ,  26, /* Ð - 208 */
1632  14      , 112, /* Ñ - 209 */
1633  14      , 124, /* Ò - 210 */
1634  14      , 124, /* Ó - 211 */
1635  14      , 124, /* Ô - 212 */
1636  14      , 124, /* Õ - 213 */
1637  14      , 124, /* Ö - 214 */
1638   8      ,  28, /* × - 215 */
1639  14      , 124, /* Ø - 216 */
1640  14      , 159, /* Ù - 217 */
1641  14      , 159, /* Ú - 218 */
1642  14      , 159, /* Û - 219 */
1643  14      , 159, /* Ü - 220 */
1644  14      , 167, /* Ý - 221 */
1645  14 + 32 , 153, /* Þ - 222 */
1646  14 + 48 , 145, /* ß - 223 */
1647  14      ,   2, /* à - 224 */
1648  14      ,   2, /* á - 225 */
1649  14      ,   2, /* â - 226 */
1650  14      ,   2, /* ã - 227 */
1651  14      ,   2, /* ä - 228 */
1652  14      ,   2, /* å - 229 */
1653  14 + 16 ,   2, /* æ - 230 */
1654  14      ,  10, /* ç - 231 */
1655  14      ,  33, /* è - 232 */
1656  14      ,  33, /* é - 233 */
1657  14      ,  33, /* ê - 234 */
1658  14      ,  33, /* ë - 235 */
1659  14      ,  50, /* ì - 236 */
1660  14      ,  50, /* í - 237 */
1661  14      ,  50, /* î - 238 */
1662  14      ,  50, /* ï - 239 */
1663  14      ,  26, /* ð - 240 */
1664  14      , 112, /* ñ - 241 */
1665  14      , 124, /* ò - 242 */
1666  14      , 124, /* ó - 243 */
1667  14      , 124, /* ô - 244 */
1668  14      , 124, /* õ - 245 */
1669  14      , 124, /* ö - 246 */
1670   8      ,  29, /* ÷ - 247 */
1671  14      , 124, /* ø - 248 */
1672  14      , 159, /* ù - 249 */
1673  14      , 159, /* ú - 250 */
1674  14      , 159, /* û - 251 */
1675  14      , 159, /* ü - 252 */
1676  14      , 167, /* ý - 253 */
1677  14 + 32 , 153, /* þ - 254 */
1678  14      , 167  /* ÿ - 255 */ };
1679
1680 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1681
1682 #define LCM_Diacritic_Start 131
1683
1684 static const unsigned char LCM_Diacritic_LUT[] = { 
1685 123,  /* \83 - 131 */
1686   2,  /* \84 - 132 */
1687   2,  /* \85 - 133 */
1688   2,  /* \86 - 134 */
1689   2,  /* \87 - 135 */
1690   3,  /* \88 - 136 */
1691   2,  /* \89 - 137 */
1692  20,  /* \8a - 138 */
1693   2,  /* \8b - 139 */
1694   2,  /* \8c - 140 */
1695   2,  /* \8d - 141 */
1696   2,  /* \8e - 142 */
1697   2,  /* \8f - 143 */
1698   2,  /* \90 - 144 */
1699   2,  /* \91 - 145 */
1700   2,  /* \92 - 146 */
1701   2,  /* \93 - 147 */
1702   2,  /* \94 - 148 */
1703   2,  /* \95 - 149 */
1704   2,  /* \96 - 150 */
1705   2,  /* \97 - 151 */
1706   2,  /* \98 - 152 */
1707   2,  /* \99 - 153 */
1708  20,  /* \9a - 154 */
1709   2,  /* \9b - 155 */
1710   2,  /* \9c - 156 */
1711   2,  /* \9d - 157 */
1712   2,  /* \9e - 158 */
1713  19,  /* \9f - 159 */
1714   2,  /*   - 160 */
1715   2,  /* ¡ - 161 */
1716   2,  /* ¢ - 162 */
1717   2,  /* £ - 163 */
1718   2,  /* ¤ - 164 */
1719   2,  /* ¥ - 165 */
1720   2,  /* ¦ - 166 */
1721   2,  /* § - 167 */
1722   2,  /* ¨ - 168 */
1723   2,  /* © - 169 */
1724   3,  /* ª - 170 */
1725   2,  /* « - 171 */
1726   2,  /* ¬ - 172 */
1727   2,  /* ­ - 173 */
1728   2,  /* ® - 174 */
1729   2,  /* ¯ - 175 */
1730   2,  /* ° - 176 */
1731   2,  /* ± - 177 */
1732   2,  /* ² - 178 */
1733   2,  /* ³ - 179 */
1734   2,  /* ´ - 180 */
1735   2,  /* µ - 181 */
1736   2,  /* ¶ - 182 */
1737   2,  /* · - 183 */
1738   2,  /* ¸ - 184 */
1739   2,  /* ¹ - 185 */
1740   3,  /* º - 186 */
1741   2,  /* » - 187 */
1742   2,  /* ¼ - 188 */
1743   2,  /* ½ - 189 */
1744   2,  /* ¾ - 190 */
1745   2,  /* ¿ - 191 */
1746  15,  /* À - 192 */
1747  14,  /* Á - 193 */
1748  18,  /* Â - 194 */
1749  25,  /* Ã - 195 */
1750  19,  /* Ä - 196 */
1751  26,  /* Å - 197 */
1752   2,  /* Æ - 198 */
1753  28,  /* Ç - 199 */
1754  15,  /* È - 200 */
1755  14,  /* É - 201 */
1756  18,  /* Ê - 202 */
1757  19,  /* Ë - 203 */
1758  15,  /* Ì - 204 */
1759  14,  /* Í - 205 */
1760  18,  /* Î - 206 */
1761  19,  /* Ï - 207 */
1762 104,  /* Ð - 208 */
1763  25,  /* Ñ - 209 */
1764  15,  /* Ò - 210 */
1765  14,  /* Ó - 211 */
1766  18,  /* Ô - 212 */
1767  25,  /* Õ - 213 */
1768  19,  /* Ö - 214 */
1769   2,  /* × - 215 */
1770  33,  /* Ø - 216 */
1771  15,  /* Ù - 217 */
1772  14,  /* Ú - 218 */
1773  18,  /* Û - 219 */
1774  19,  /* Ü - 220 */
1775  14,  /* Ý - 221 */
1776   2,  /* Þ - 222 */
1777   2,  /* ß - 223 */
1778  15,  /* à - 224 */
1779  14,  /* á - 225 */
1780  18,  /* â - 226 */
1781  25,  /* ã - 227 */
1782  19,  /* ä - 228 */
1783  26,  /* å - 229 */
1784   2,  /* æ - 230 */
1785  28,  /* ç - 231 */
1786  15,  /* è - 232 */
1787  14,  /* é - 233 */
1788  18,  /* ê - 234 */
1789  19,  /* ë - 235 */
1790  15,  /* ì - 236 */
1791  14,  /* í - 237 */
1792  18,  /* î - 238 */
1793  19,  /* ï - 239 */
1794 104,  /* ð - 240 */
1795  25,  /* ñ - 241 */
1796  15,  /* ò - 242 */
1797  14,  /* ó - 243 */
1798  18,  /* ô - 244 */
1799  25,  /* õ - 245 */
1800  19,  /* ö - 246 */
1801   2,  /* ÷ - 247 */
1802  33,  /* ø - 248 */
1803  15,  /* ù - 249 */
1804  14,  /* ú - 250 */
1805  18,  /* û - 251 */
1806  19,  /* ü - 252 */
1807  14,  /* ý - 253 */
1808   2,  /* þ - 254 */
1809  19,  /* ÿ - 255 */
1810 } ;
1811
1812 /******************************************************************************
1813  * OLE2NLS_isPunctuation [INTERNAL]
1814  */
1815 static int OLE2NLS_isPunctuation(unsigned char c) 
1816 {
1817   /* "punctuation character" in this context is a character which is 
1818      considered "less important" during word sort comparison.
1819      See LCMapString implementation for the precise definition 
1820      of "less important". */
1821
1822   return (LCM_Unicode_LUT[-2+2*c]==6);
1823 }
1824
1825 /******************************************************************************
1826  * OLE2NLS_isNonSpacing [INTERNAL]
1827  */
1828 static int OLE2NLS_isNonSpacing(unsigned char c) 
1829 {
1830   /* This function is used by LCMapStringA.  Characters 
1831      for which it returns true are ignored when mapping a
1832      string with NORM_IGNORENONSPACE */
1833   return ((c==136) || (c==170) || (c==186));
1834 }
1835
1836 /******************************************************************************
1837  * OLE2NLS_isSymbol [INTERNAL]
1838  * FIXME: handle current locale
1839  */
1840 static int OLE2NLS_isSymbol(unsigned char c) 
1841 {
1842   /* This function is used by LCMapStringA.  Characters 
1843      for which it returns true are ignored when mapping a
1844      string with NORM_IGNORESYMBOLS */
1845   return ( (c!=0) && !(isalpha(c) || isdigit(c)) );
1846 }
1847
1848 /******************************************************************************
1849  *              identity        [Internal]
1850  */
1851 static int identity(int c)
1852 {
1853   return c;
1854 }
1855
1856 /*************************************************************************
1857  *              LCMapStringA                [KERNEL32.@]
1858  *
1859  * Convert a string, or generate a sort key from it.
1860  *
1861  * If (mapflags & LCMAP_SORTKEY), the function will generate
1862  * a sort key for the source string.  Else, it will convert it
1863  * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1864  *
1865  * RETURNS
1866  *    Error : 0.
1867  *    Success : length of the result string.
1868  *
1869  * NOTES
1870  *    If called with scrlen = -1, the function will compute the length
1871  *      of the 0-terminated string strsrc by itself.      
1872  * 
1873  *    If called with dstlen = 0, returns the buffer length that 
1874  *      would be required.
1875  *
1876  *    NORM_IGNOREWIDTH means to compare ASCII and wide characters
1877  *    as if they are equal.  
1878  *    In the only code page implemented so far, there may not be
1879  *    wide characters in strings passed to LCMapStringA,
1880  *    so there is nothing to be done for this flag.
1881  */
1882 INT WINAPI LCMapStringA(
1883         LCID lcid,      /* [in] locale identifier created with MAKELCID; 
1884                                 LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are 
1885                                 predefined values. */
1886         DWORD mapflags, /* [in] flags */
1887         LPCSTR srcstr,  /* [in] source buffer */
1888         INT srclen,     /* [in] source length */
1889         LPSTR dststr,   /* [out] destination buffer */
1890         INT dstlen)     /* [in] destination buffer length */
1891 {
1892   int i;
1893
1894   TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1895         lcid,mapflags,debugstr_an(srcstr,srclen),srclen,dststr,dstlen);
1896
1897   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1898   {
1899     ERR("(src=%s,dest=%s): Invalid NULL string\n",
1900         debugstr_an(srcstr,srclen), dststr);
1901     SetLastError(ERROR_INVALID_PARAMETER);
1902     return 0;
1903   }
1904   if (srclen == -1) 
1905     srclen = strlen(srcstr) + 1 ;    /* (include final '\0') */
1906
1907 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE     | \
1908                                         LCMAP_LOWERCASE     | \
1909                                         LCMAP_SORTKEY       | \
1910                                         NORM_IGNORECASE     | \
1911                                         NORM_IGNORENONSPACE | \
1912                                         SORT_STRINGSORT     | \
1913                                         NORM_IGNOREWIDTH    | \
1914                                         NORM_IGNOREKANATYPE)
1915   /* FIXME: as long as we don't support Katakana nor Hiragana 
1916    * characters, we can support NORM_IGNOREKANATYPE
1917    */
1918   if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
1919   {
1920     FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1921           "unimplemented flags: 0x%08lx\n",
1922           lcid,
1923           mapflags,
1924           srcstr,
1925           srclen,
1926           dststr,
1927           dstlen,
1928           mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
1929      );
1930   }
1931
1932   if ( !(mapflags & LCMAP_SORTKEY) )
1933   {
1934     int i,j;
1935     int (*f)(int) = identity; 
1936     int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
1937     int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
1938
1939     if (flag_ignorenonspace || flag_ignoresymbols)
1940     {
1941       /* For some values of mapflags, the length of the resulting 
1942          string is not known at this point.  Windows does map the string
1943          and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
1944          these cases. */
1945       if (dstlen==0)
1946       {
1947         /* Compute required length */
1948         for (i=j=0; i < srclen; i++)
1949         {
1950           if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1951                && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1952             j++;
1953         }
1954         return j;
1955       }
1956     }
1957     else
1958     {
1959       if (dstlen==0)
1960         return srclen;  
1961       if (dstlen<srclen) 
1962            {
1963              SetLastError(ERROR_INSUFFICIENT_BUFFER);
1964              return 0;
1965            }
1966     }
1967     if (mapflags & LCMAP_UPPERCASE)
1968       f = toupper;
1969     else if (mapflags & LCMAP_LOWERCASE)
1970       f = tolower;
1971     /* FIXME: NORM_IGNORENONSPACE requires another conversion */
1972     for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
1973     {
1974       if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1975            && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1976       {
1977         dststr[j] = (CHAR) f(srcstr[i]);
1978         j++;
1979       }
1980     }
1981     return j;
1982   }
1983
1984   /* FIXME: This function completely ignores the "lcid" parameter. */
1985   /* else ... (mapflags & LCMAP_SORTKEY)  */
1986   {
1987     int unicode_len=0;
1988     int case_len=0;
1989     int diacritic_len=0;
1990     int delayed_punctuation_len=0;
1991     char *case_component;
1992     char *diacritic_component;
1993     char *delayed_punctuation_component;
1994     int room,count;
1995     int flag_stringsort = mapflags & SORT_STRINGSORT;
1996
1997     /* compute how much room we will need */
1998     for (i=0;i<srclen;i++)
1999     {
2000       int ofs;
2001       unsigned char source_char = srcstr[i];
2002       if (source_char!='\0') 
2003       {
2004         if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
2005         {
2006           unicode_len++;
2007           if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
2008             unicode_len++;             /* double letter */
2009         }
2010         else
2011         {
2012           delayed_punctuation_len++;
2013         }         
2014       }
2015           
2016       if (isupper(source_char))
2017         case_len=unicode_len; 
2018
2019       ofs = source_char - LCM_Diacritic_Start;
2020       if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
2021         diacritic_len=unicode_len;
2022     }
2023
2024     if (mapflags & NORM_IGNORECASE)
2025       case_len=0;                   
2026     if (mapflags & NORM_IGNORENONSPACE)
2027       diacritic_len=0;
2028
2029     room =  2 * unicode_len              /* "unicode" component */
2030       +     diacritic_len                /* "diacritic" component */
2031       +     case_len                     /* "case" component */
2032       +     4 * delayed_punctuation_len  /* punctuation in word sort mode */
2033       +     4                            /* four '\1' separators */
2034       +     1  ;                         /* terminal '\0' */
2035     if (dstlen==0)
2036       return room;      
2037     else if (dstlen<room)
2038     {
2039       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2040       return 0;
2041     }
2042 #if 0
2043     /*FIXME the Pointercheck should not be nessesary */
2044     if (IsBadWritePtr (dststr,room))
2045     { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
2046       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2047       return 0;
2048     }
2049 #endif
2050     /* locate each component, write separators */
2051     diacritic_component = dststr + 2*unicode_len ;
2052     *diacritic_component++ = '\1'; 
2053     case_component = diacritic_component + diacritic_len ;
2054     *case_component++ = '\1'; 
2055     delayed_punctuation_component = case_component + case_len ;
2056     *delayed_punctuation_component++ = '\1';
2057     *delayed_punctuation_component++ = '\1';
2058
2059     /* read source string char by char, write 
2060        corresponding weight in each component. */
2061     for (i=0,count=0;i<srclen;i++)
2062     {
2063       unsigned char source_char=srcstr[i];
2064       if (source_char!='\0') 
2065       {
2066         int type,longcode;
2067         type = LCM_Unicode_LUT[-2+2*source_char];
2068         longcode = type >> 4;
2069         type &= 15;
2070         if (!flag_stringsort && OLE2NLS_isPunctuation(source_char)) 
2071         {
2072           WORD encrypted_location = (1<<15) + 7 + 4*count;
2073           *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
2074           *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
2075                      /* big-endian is used here because it lets string comparison be
2076                         compatible with numerical comparison */
2077
2078           *delayed_punctuation_component++ = type;
2079           *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];  
2080                      /* assumption : a punctuation character is never a 
2081                         double or accented letter */
2082         }
2083         else
2084         {
2085           dststr[2*count] = type;
2086           dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];  
2087           if (longcode)
2088           {
2089             if (count<case_len)
2090               case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
2091             if (count<diacritic_len)
2092               diacritic_component[count] = 2; /* assumption: a double letter
2093                                                  is never accented */
2094             count++;
2095             
2096             dststr[2*count] = type;
2097             dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode); 
2098             /* 16 in the first column of LCM_Unicode_LUT  -->  longcode = 1 
2099                32 in the first column of LCM_Unicode_LUT  -->  longcode = 2 
2100                48 in the first column of LCM_Unicode_LUT  -->  longcode = 3 */
2101           }
2102
2103           if (count<case_len)
2104             case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
2105           if (count<diacritic_len)
2106           {
2107             int ofs = source_char - LCM_Diacritic_Start;
2108             diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
2109           }
2110           count++;
2111         }
2112       }
2113     }
2114     dststr[room-1] = '\0';
2115     return room;
2116   }
2117 }
2118                      
2119 /*************************************************************************
2120  *              LCMapStringW                [KERNEL32.@]
2121  *
2122  * Convert a string, or generate a sort key from it.
2123  *
2124  * NOTE
2125  *
2126  * See LCMapStringA for documentation
2127  */
2128 INT WINAPI LCMapStringW(
2129         LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
2130         INT dstlen)
2131 {
2132   int i;
2133  
2134   TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
2135                  lcid, mapflags, srcstr, srclen, dststr, dstlen);
2136   
2137   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
2138   {
2139     ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
2140     SetLastError(ERROR_INVALID_PARAMETER);
2141     return 0;
2142   }
2143   if (srclen==-1) 
2144     srclen = strlenW(srcstr)+1;
2145
2146   /* FIXME: Both this function and it's companion LCMapStringA()
2147    * completely ignore the "lcid" parameter.  In place of the "lcid"
2148    * parameter the application must set the "LC_COLLATE" or "LC_ALL"
2149    * environment variable prior to invoking this function.  */
2150   if (mapflags & LCMAP_SORTKEY) 
2151   {
2152       /* Possible values of LC_COLLATE. */
2153       char *lc_collate_default = 0; /* value prior to this function */
2154       char *lc_collate_env = 0;     /* value retrieved from the environment */
2155
2156       /* General purpose index into strings of any type. */
2157       int str_idx = 0;
2158
2159       /* Lengths of various strings where the length is measured in
2160        * wide characters for wide character strings and in bytes for
2161        * native strings.  The lengths include the NULL terminator.  */
2162       size_t returned_len    = 0;
2163       size_t src_native_len  = 0;
2164       size_t dst_native_len  = 0;
2165       size_t dststr_libc_len = 0;
2166
2167       /* Native (character set determined by locale) versions of the
2168        * strings source and destination strings.  */
2169       LPSTR src_native = 0;
2170       LPSTR dst_native = 0;
2171
2172       /* Version of the source and destination strings using the
2173        * "wchar_t" Unicode data type needed by various libc functions.  */
2174       wchar_t *srcstr_libc = 0;
2175       wchar_t *dststr_libc = 0;
2176
2177       if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
2178                                        srclen * sizeof(wchar_t))))
2179       {
2180           ERR("Unable to allocate %d bytes for srcstr_libc\n",
2181               srclen * sizeof(wchar_t));
2182           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2183           return 0;
2184       }
2185
2186       /* Convert source string to a libc Unicode string. */
2187       for(str_idx = 0; str_idx < srclen; str_idx++)
2188       {
2189           srcstr_libc[str_idx] = srcstr[str_idx];
2190       }
2191
2192       /* src_native should contain at most 3 bytes for each
2193        * multibyte characters in the original srcstr string.  */
2194       src_native_len = 3 * srclen;
2195       if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
2196                                           src_native_len)))
2197       {
2198           ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
2199           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2200           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2201           return 0;
2202       }
2203
2204       /* FIXME: Prior to to setting the LC_COLLATE locale category the
2205        * current value is backed up so it can be restored after the
2206        * last LC_COLLATE sensitive function returns.
2207        * 
2208        * Even though the locale is adjusted for a minimum amount of
2209        * time a race condition exists where other threads may be
2210        * affected if they invoke LC_COLLATE sensitive functions.  One
2211        * possible solution is to wrap all LC_COLLATE sensitive Wine
2212        * functions, like LCMapStringW(), in a mutex.
2213        *
2214        * Another enhancement to the following would be to set the
2215        * LC_COLLATE locale category as a function of the "lcid"
2216        * parameter instead of the "LC_COLLATE" environment variable. */
2217       if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
2218       {
2219           ERR("Unable to query the LC_COLLATE catagory\n");
2220           SetLastError(ERROR_INVALID_PARAMETER);
2221           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2222           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2223           return 0;
2224       }
2225
2226       if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
2227       {
2228           ERR("Unable to inherit the LC_COLLATE locale category from the "
2229               "environment.  The \"LC_COLLATE\" environment variable is "
2230               "\"%s\".\n", getenv("LC_COLLATE") ?
2231               getenv("LC_COLLATE") : "<unset>");
2232           SetLastError(ERROR_INVALID_PARAMETER);
2233           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2234           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2235           return 0;
2236       }
2237
2238       TRACE("lc_collate_default = %s\n", lc_collate_default);
2239       TRACE("lc_collate_env = %s\n", lc_collate_env);
2240
2241       /* Convert the libc Unicode string to a native multibyte character
2242        * string. */
2243       returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
2244       if(returned_len == 0)
2245       {
2246           LPSTR srcstr_ascii = (LPSTR)HEAP_strdupWtoA(GetProcessHeap(),
2247                                            0, srcstr);
2248           ERR("wcstombs failed.  The string specified (%s) may contains an "
2249               "invalid character.\n", srcstr_ascii);
2250           SetLastError(ERROR_INVALID_PARAMETER);
2251           if(srcstr_ascii) HeapFree(GetProcessHeap(), 0, srcstr_ascii);
2252           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2253           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2254           setlocale(LC_COLLATE, lc_collate_default);
2255           return 0;
2256       }
2257       else if(returned_len > src_native_len)
2258       {
2259           src_native[src_native_len - 1] = 0;
2260           ERR("wcstombs returned a string (%s) that was longer (%d bytes) " 
2261               "than expected (%d bytes).\n", src_native, returned_len,
2262               dst_native_len);
2263
2264           /* Since this is an internal error I'm not sure what the correct
2265            * error code is.  */
2266           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2267
2268           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2269           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2270           setlocale(LC_COLLATE, lc_collate_default);
2271           return 0;
2272       }
2273       src_native_len = returned_len;
2274
2275       TRACE("src_native = %s  src_native_len = %d\n",
2276              src_native, src_native_len);
2277
2278       /* dst_native seems to contain at most 4 bytes for each byte in
2279        * the original src_native string.  Change if need be since this
2280        * isn't documented by the strxfrm() man page. */
2281       dst_native_len = 4 * src_native_len;
2282       if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
2283       {
2284           ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
2285           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2286           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2287           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2288           setlocale(LC_COLLATE, lc_collate_default);
2289           return 0;
2290       }
2291
2292       /* The actual translation is done by the following call to
2293        * strxfrm().  The surrounding code could have been simplified
2294        * by calling wcsxfrm() instead except that wcsxfrm() is not
2295        * available on older Linux systems (RedHat 4.1 with
2296        * libc-5.3.12-17).
2297        *
2298        * Also, it is possible that the translation could be done by
2299        * various tables as it is done in LCMapStringA().  However, I'm
2300        * not sure what those tables are. */
2301       returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
2302       
2303       if(returned_len > dst_native_len)
2304       {
2305           dst_native[dst_native_len - 1] = 0;
2306           ERR("strxfrm returned a string (%s) that was longer (%d bytes) " 
2307               "than expected (%d bytes).\n", dst_native, returned_len,
2308               dst_native_len);
2309
2310           /* Since this is an internal error I'm not sure what the correct
2311            * error code is.  */
2312           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2313
2314           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2315           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2316           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2317           setlocale(LC_COLLATE, lc_collate_default);
2318           return 0;
2319       }
2320       dst_native_len = returned_len;
2321       
2322       TRACE("dst_native = %s  dst_native_len = %d\n",
2323              dst_native, dst_native_len);
2324
2325       dststr_libc_len = dst_native_len;
2326       if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
2327                                        dststr_libc_len * sizeof(wchar_t))))
2328       {
2329           ERR("Unable to allocate %d bytes for dststr_libc\n",
2330               dststr_libc_len * sizeof(wchar_t));
2331           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2332           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2333           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2334           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2335           setlocale(LC_COLLATE, lc_collate_default);
2336           return 0;
2337       }
2338
2339       /* Convert the native multibyte string to a libc Unicode string. */
2340       returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
2341
2342       /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
2343        * function has returned. */
2344       setlocale(LC_COLLATE, lc_collate_default); 
2345
2346       if(returned_len == 0)
2347       {
2348           ERR("mbstowcs failed.  The native version of the translated string "
2349               "(%s) may contain an invalid character.\n", dst_native);
2350           SetLastError(ERROR_INVALID_PARAMETER);
2351           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2352           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2353           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2354           if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2355           return 0;
2356       }
2357       if(dstlen)
2358       {
2359           if(returned_len > dstlen)
2360           {
2361               ERR("mbstowcs returned a string that was longer (%d chars) " 
2362                   "than the buffer provided (%d chars).\n", returned_len,
2363                   dstlen);
2364               SetLastError(ERROR_INSUFFICIENT_BUFFER);
2365               if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2366               if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2367               if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2368               if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2369               return 0;          
2370           }
2371           dstlen = returned_len;
2372
2373           /* Convert a libc Unicode string to the destination string. */
2374           for(str_idx = 0; str_idx < dstlen; str_idx++)
2375           {
2376               dststr[str_idx] = dststr_libc[str_idx];
2377           }
2378           TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
2379                          *(((int *)dststr) + 0),
2380                          *(((int *)dststr) + 1),
2381                          *(((int *)dststr) + 2),
2382                          *(((int *)dststr) + 3));
2383       }
2384       else
2385       {
2386           dstlen = returned_len;
2387       }
2388       TRACE("dstlen (return) = %d\n", dstlen);
2389       if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2390       if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2391       if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2392       if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2393       return dstlen;
2394   }
2395   else
2396   {
2397     int (*f)(int)=identity; 
2398
2399     if (dstlen==0)
2400         return srclen;  
2401     if (dstlen<srclen) 
2402     {
2403         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2404         return 0;
2405     }
2406
2407     if (mapflags & LCMAP_UPPERCASE)
2408       f = toupper;
2409     else if (mapflags & LCMAP_LOWERCASE)
2410       f = tolower;
2411     for (i=0; i < srclen; i++)
2412       dststr[i] = (WCHAR) f(srcstr[i]);
2413     return srclen;
2414   }
2415 }
2416
2417
2418 /***********************************************************************
2419  *           OLE2NLS_EstimateMappingLength
2420  *
2421  * Estimates the number of characters required to hold the string
2422  * computed by LCMapStringA.
2423  *
2424  * The size is always over-estimated, with a fixed limit on the
2425  * amount of estimation error.
2426  *
2427  * Note that len == -1 is not permitted.
2428  */
2429 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
2430                                                 LPCSTR str, DWORD len)
2431 {
2432     /* Estimate only for small strings to keep the estimation error from
2433      * becoming too large. */
2434     if (len < 128) return len * 8 + 5;
2435     else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
2436 }
2437
2438 /******************************************************************************
2439  *              CompareStringA  [KERNEL32.@]
2440  * Compares two strings using locale
2441  *
2442  * RETURNS
2443  *
2444  * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
2445  * failure: 0
2446  *
2447  * NOTES
2448  *
2449  * Defaults to a word sort, but uses a string sort if
2450  * SORT_STRINGSORT is set.
2451  * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
2452  * 
2453  * BUGS
2454  *
2455  * This implementation ignores the locale
2456  *
2457  * FIXME
2458  * 
2459  * Quite inefficient.
2460  */
2461 int WINAPI CompareStringA(
2462     LCID lcid,      /* [in] locale ID */
2463     DWORD fdwStyle, /* [in] comparison-style options */
2464     LPCSTR s1,      /* [in] first string */
2465     int l1,         /* [in] length of first string */
2466     LPCSTR s2,      /* [in] second string */
2467     int l2)         /* [in] length of second string */
2468 {
2469   int mapstring_flags;
2470   int len1,len2;
2471   int result;
2472   LPSTR sk1,sk2;
2473   TRACE("%s and %s\n",
2474         debugstr_an (s1,l1), debugstr_an (s2,l2));
2475
2476   if ( (s1==NULL) || (s2==NULL) )
2477   {    
2478     ERR("(s1=%s,s2=%s): Invalid NULL string\n",
2479         debugstr_an(s1,l1), debugstr_an(s2,l2));
2480     SetLastError(ERROR_INVALID_PARAMETER);
2481     return 0;
2482   }
2483
2484   if(fdwStyle & NORM_IGNORESYMBOLS)
2485     FIXME("IGNORESYMBOLS not supported\n");
2486
2487   if (l1 == -1) l1 = strlen(s1);
2488   if (l2 == -1) l2 = strlen(s2);
2489         
2490   mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
2491   len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
2492   len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
2493
2494   if ((len1==0)||(len2==0))
2495     return 0;     /* something wrong happened */
2496
2497   sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
2498   sk2 = sk1 + len1;
2499   if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
2500          || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
2501   {
2502     ERR("Bug in LCmapStringA.\n");
2503     result = 0;
2504   }
2505   else
2506   {
2507     /* strcmp doesn't necessarily return -1, 0, or 1 */
2508     result = strcmp(sk1,sk2);
2509   }
2510   HeapFree(GetProcessHeap(),0,sk1);
2511
2512   if (result < 0)
2513     return 1;
2514   if (result == 0)
2515     return 2;
2516
2517   /* must be greater, if we reach this point */
2518   return 3;
2519 }
2520
2521 /******************************************************************************
2522  *              CompareStringW  [KERNEL32.@]
2523  * This implementation ignores the locale
2524  * FIXME :  Does only string sort.  Should
2525  * be reimplemented the same way as CompareStringA.
2526  */
2527 int WINAPI CompareStringW(LCID lcid, DWORD fdwStyle,
2528                           LPCWSTR s1, int l1, LPCWSTR s2, int l2)
2529 {
2530         int len,ret;
2531         if(fdwStyle & NORM_IGNORENONSPACE)
2532                 FIXME("IGNORENONSPACE not supported\n");
2533         if(fdwStyle & NORM_IGNORESYMBOLS)
2534                 FIXME("IGNORESYMBOLS not supported\n");
2535
2536         /* Is strcmp defaulting to string sort or to word sort?? */
2537         /* FIXME: Handle NORM_STRINGSORT */
2538         l1 = (l1==-1)?strlenW(s1):l1;
2539         l2 = (l2==-1)?strlenW(s2):l2;
2540         len = l1<l2 ? l1:l2;
2541         ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
2542         /* not equal, return 1 or 3 */
2543         if(ret!=0) {
2544                 /* need to translate result */
2545                 return ((int)ret < 0) ? 1 : 3;
2546         }
2547         /* same len, return 2 */
2548         if(l1==l2) return 2;
2549         /* the longer one is lexically greater */
2550         return (l1<l2)? 1 : 3;
2551 }
2552
2553 /******************************************************************************
2554  *              OLE_GetFormatA  [Internal]
2555  *
2556  * FIXME
2557  *    If datelen == 0, it should return the reguired string length.
2558  *
2559  This function implements stuff for GetDateFormat() and 
2560  GetTimeFormat().
2561
2562   d    single-digit (no leading zero) day (of month)
2563   dd   two-digit day (of month)
2564   ddd  short day-of-week name
2565   dddd long day-of-week name
2566   M    single-digit month
2567   MM   two-digit month
2568   MMM  short month name
2569   MMMM full month name
2570   y    two-digit year, no leading 0
2571   yy   two-digit year
2572   yyyy four-digit year
2573   gg   era string
2574   h    hours with no leading zero (12-hour)
2575   hh   hours with full two digits
2576   H    hours with no leading zero (24-hour)
2577   HH   hours with full two digits
2578   m    minutes with no leading zero
2579   mm   minutes with full two digits
2580   s    seconds with no leading zero
2581   ss   seconds with full two digits
2582   t    time marker (A or P)
2583   tt   time marker (AM, PM)
2584   ''   used to quote literal characters
2585   ''   (within a quoted string) indicates a literal '
2586
2587  These functions REQUIRE valid locale, date,  and format. 
2588  */
2589 static INT OLE_GetFormatA(LCID locale,
2590                             DWORD flags,
2591                             DWORD tflags,
2592                             const SYSTEMTIME* xtime,
2593                             LPCSTR _format,     /*in*/
2594                             LPSTR date,         /*out*/
2595                             INT datelen)
2596 {
2597    INT inpos, outpos;
2598    int count, type, inquote, Overflow;
2599    char buf[40];
2600    char format[40];
2601    char * pos;
2602    int buflen;
2603
2604    const char * _dgfmt[] = { "%d", "%02d" };
2605    const char ** dgfmt = _dgfmt - 1; 
2606
2607    /* report, for debugging */
2608    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",
2609          locale, flags, tflags,
2610          xtime->wYear,xtime->wMonth,xtime->wDayOfWeek,xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2611          _format, _format, date, datelen);
2612   
2613    if(datelen == 0) {
2614      FIXME("datelen = 0, returning 255\n");
2615      return 255;
2616    }
2617
2618    /* initalize state variables and output buffer */
2619    inpos = outpos = 0;
2620    count = 0; inquote = 0; Overflow = 0;
2621    type = '\0';
2622    date[0] = buf[0] = '\0';
2623       
2624    strcpy(format,_format);
2625
2626    /* alter the formatstring, while it works for all languages now in wine
2627    its possible that it fails when the time looks like ss:mm:hh as example*/   
2628    if (tflags & (TIME_NOMINUTESORSECONDS))
2629    { if ((pos = strstr ( format, ":mm")))
2630      { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2631      }
2632    }
2633    if (tflags & (TIME_NOSECONDS))
2634    { if ((pos = strstr ( format, ":ss")))
2635      { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2636      }
2637    }
2638    
2639    for (inpos = 0;; inpos++) {
2640       /* 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]); */
2641       if (inquote) {
2642          if (format[inpos] == '\'') {
2643             if (format[inpos+1] == '\'') {
2644                inpos += 1;
2645                date[outpos++] = '\'';
2646             } else {
2647                inquote = 0;
2648                continue; /* we did nothing to the output */
2649             }
2650          } else if (format[inpos] == '\0') {
2651             date[outpos++] = '\0';
2652             if (outpos > datelen) Overflow = 1;
2653             break;
2654          } else {
2655             date[outpos++] = format[inpos];
2656             if (outpos > datelen) {
2657                Overflow = 1;
2658                date[outpos-1] = '\0'; /* this is the last place where
2659                                          it's safe to write */
2660                break;
2661             }
2662          }
2663       } else if (  (count && (format[inpos] != type))
2664                    || count == 4
2665                    || (count == 2 && strchr("ghHmst", type)) ) {
2666             if (type == 'h' && (tflags & TIME_FORCE24HOURFORMAT)) type= 'H';
2667             if (type == 'd') {
2668                if (count == 4) {
2669                   GetLocaleInfoA(locale,
2670                                    LOCALE_SDAYNAME1
2671                                    + (xtime->wDayOfWeek+6)%7,
2672                                    buf, sizeof(buf));
2673                } else if (count == 3) {
2674                            GetLocaleInfoA(locale, 
2675                                             LOCALE_SABBREVDAYNAME1 
2676                                             + (xtime->wDayOfWeek+6)%7,
2677                                             buf, sizeof(buf));
2678                       } else {
2679                   sprintf(buf, dgfmt[count], xtime->wDay);
2680                }
2681             } else if (type == 'M') {
2682                if (count == 3) {
2683                   GetLocaleInfoA(locale, 
2684                                    LOCALE_SABBREVMONTHNAME1
2685                                    + xtime->wMonth - 1,
2686                                    buf, sizeof(buf));
2687                } else if (count == 4) {
2688                   GetLocaleInfoA(locale,
2689                                    LOCALE_SMONTHNAME1
2690                                    + xtime->wMonth - 1,
2691                                    buf, sizeof(buf));
2692                  } else {
2693                   sprintf(buf, dgfmt[count], xtime->wMonth);
2694                }
2695             } else if (type == 'y') {
2696                if (count == 4) {
2697                       sprintf(buf, "%d", xtime->wYear);
2698                } else if (count == 3) {
2699                   strcpy(buf, "yyy");
2700                   WARN("unknown format, c=%c, n=%d\n",  type, count);
2701                  } else {
2702                   sprintf(buf, dgfmt[count], xtime->wYear % 100);
2703                }
2704             } else if (type == 'g') {
2705                if        (count == 2) {
2706                   FIXME("LOCALE_ICALENDARTYPE unimp.\n");
2707                   strcpy(buf, "AD");
2708             } else {
2709                   strcpy(buf, "g");
2710                   WARN("unknown format, c=%c, n=%d\n", type, count);
2711                }
2712             } else if (type == 'h') {
2713                /* gives us hours 1:00 -- 12:00 */
2714                sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2715             } else if (type == 'H') {
2716                /* 24-hour time */
2717                sprintf(buf, dgfmt[count], xtime->wHour);
2718             } else if ( type == 'm') {
2719                sprintf(buf, dgfmt[count], xtime->wMinute);
2720             } else if ( type == 's') {
2721                sprintf(buf, dgfmt[count], xtime->wSecond);
2722             } else if (type == 't') {
2723                if ((tflags & TIME_NOTIMEMARKER))
2724                   buf[0]='\0';
2725                else if (count == 1) {
2726                   sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2727                } else if (count == 2) {
2728                  /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2729                   GetLocaleInfoA(locale,
2730                            (xtime->wHour<12) 
2731                            ? LOCALE_S1159 : LOCALE_S2359,
2732                            buf, sizeof(buf));
2733                }
2734             };
2735
2736             /* we need to check the next char in the format string 
2737                again, no matter what happened */
2738             inpos--;
2739             
2740             /* add the contents of buf to the output */
2741             buflen = strlen(buf);
2742             if (outpos + buflen < datelen) {
2743                date[outpos] = '\0'; /* for strcat to hook onto */
2744                  strcat(date, buf);
2745                outpos += buflen;
2746             } else {
2747                date[outpos] = '\0';
2748                strncat(date, buf, datelen - outpos);
2749                  date[datelen - 1] = '\0';
2750                  SetLastError(ERROR_INSUFFICIENT_BUFFER);
2751                WARN("insufficient buffer\n");
2752                  return 0;
2753             }
2754
2755             /* reset the variables we used to keep track of this item */
2756             count = 0;
2757             type = '\0';
2758          } else if (format[inpos] == '\0') {
2759             /* we can't check for this at the loop-head, because
2760                that breaks the printing of the last format-item */
2761             date[outpos] = '\0';
2762             break;
2763          } else if (count) {
2764             /* continuing a code for an item */
2765             count +=1;
2766             continue;
2767          } else if (strchr("hHmstyMdg", format[inpos])) {
2768             type = format[inpos];
2769             count = 1;
2770             continue;
2771          } else if (format[inpos] == '\'') {
2772             inquote = 1;
2773             continue;
2774        } else {
2775             date[outpos++] = format[inpos];
2776          }
2777       /* now deal with a possible buffer overflow */
2778       if (outpos >= datelen) {
2779        date[datelen - 1] = '\0';
2780        SetLastError(ERROR_INSUFFICIENT_BUFFER);
2781        return 0;
2782       }
2783    }
2784    
2785    if (Overflow) {
2786       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2787    };
2788
2789    /* finish it off with a string terminator */
2790    outpos++;
2791    /* sanity check */
2792    if (outpos > datelen-1) outpos = datelen-1;
2793    date[outpos] = '\0';
2794    
2795    TRACE("returns string '%s', len %d\n", date, outpos);
2796    return outpos;
2797 }
2798
2799 /******************************************************************************
2800  * OLE_GetFormatW [INTERNAL]
2801  */
2802 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
2803                             const SYSTEMTIME* xtime,
2804                             LPCWSTR format,
2805                             LPWSTR output, INT outlen)
2806 {
2807    INT   inpos, outpos;
2808    int     count, type=0, inquote;
2809    int     Overflow; /* loop check */
2810    char    tmp[16];
2811    WCHAR   buf[40];
2812    int     buflen=0;
2813    WCHAR   arg0[] = {0}, arg1[] = {'%','d',0};
2814    WCHAR   arg2[] = {'%','0','2','d',0};
2815    WCHAR  *argarr[3];
2816    int     datevars=0, timevars=0;
2817
2818    argarr[0] = arg0;
2819    argarr[1] = arg1;
2820    argarr[2] = arg2;
2821
2822    /* make a debug report */
2823    TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
2824               "%p with max len %d\n",
2825          locale, flags, tflags,
2826          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2827          debugstr_w(format), format, output, outlen);
2828    
2829    if(outlen == 0) {
2830      FIXME("outlen = 0, returning 255\n");
2831      return 255;
2832    }
2833
2834    /* initialize state variables */
2835    inpos = outpos = 0;
2836    count = 0;
2837    inquote = Overflow = 0;
2838    /* this is really just a sanity check */
2839    output[0] = buf[0] = 0;
2840    
2841    /* this loop is the core of the function */
2842    for (inpos = 0; /* we have several break points */ ; inpos++) {
2843       if (inquote) {
2844          if (format[inpos] == (WCHAR) '\'') {
2845             if (format[inpos+1] == '\'') {
2846                inpos++;
2847                output[outpos++] = '\'';
2848             } else {
2849                inquote = 0;
2850                continue;
2851             }
2852          } else if (format[inpos] == 0) {
2853             output[outpos++] = 0;
2854             if (outpos > outlen) Overflow = 1;
2855             break;  /*  normal exit (within a quote) */
2856          } else {
2857             output[outpos++] = format[inpos]; /* copy input */
2858             if (outpos > outlen) {
2859                Overflow = 1;
2860                output[outpos-1] = 0; 
2861                break;
2862             }
2863          }
2864       } else if (  (count && (format[inpos] != type))
2865                    || ( (count==4 && type =='y') ||
2866                         (count==4 && type =='M') ||
2867                         (count==4 && type =='d') ||
2868                         (count==2 && type =='g') ||
2869                         (count==2 && type =='h') ||
2870                         (count==2 && type =='H') ||
2871                         (count==2 && type =='m') ||
2872                         (count==2 && type =='s') ||
2873                         (count==2 && type =='t') )  ) {
2874           switch(type)
2875           {
2876           case 'd':
2877             if        (count == 4) {
2878                GetLocaleInfoW(locale,
2879                              LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7,
2880                              buf, sizeof(buf)/sizeof(WCHAR) );
2881             } else if (count == 3) {
2882                GetLocaleInfoW(locale,
2883                                 LOCALE_SABBREVDAYNAME1 +
2884                                 (xtime->wDayOfWeek +6)%7,
2885                                 buf, sizeof(buf)/sizeof(WCHAR) );
2886             } else {
2887                 sprintf( tmp, "%.*d", count, xtime->wDay );
2888                 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2889             }
2890             break;
2891
2892           case 'M':
2893             if        (count == 4) {
2894                GetLocaleInfoW(locale,  LOCALE_SMONTHNAME1 +
2895                                 xtime->wMonth -1, buf,
2896                                 sizeof(buf)/sizeof(WCHAR) );
2897             } else if (count == 3) {
2898                GetLocaleInfoW(locale,  LOCALE_SABBREVMONTHNAME1 +
2899                                 xtime->wMonth -1, buf,
2900                                 sizeof(buf)/sizeof(WCHAR) );
2901             } else {
2902                 sprintf( tmp, "%.*d", count, xtime->wMonth );
2903                 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2904             }
2905             break;
2906           case 'y':
2907             if        (count == 4) {
2908                 sprintf( tmp, "%d", xtime->wYear );
2909             } else if (count == 3) {
2910                 strcpy( tmp, "yyy" );
2911             } else {
2912                 sprintf( tmp, "%.*d", count, xtime->wYear % 100 );
2913             }
2914             MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2915             break;
2916
2917           case 'g':
2918             if        (count == 2) {
2919                FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
2920                strcpy( tmp, "AD" );
2921             } else {
2922                /* Win API sez we copy it verbatim */
2923                 strcpy( tmp, "g" );
2924             }
2925             MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2926             break;
2927
2928           case 'h':
2929               /* hours 1:00-12:00 --- is this right? */
2930               sprintf( tmp, "%.*d", count, (xtime->wHour-1)%12 +1);
2931               MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2932               break;
2933
2934           case 'H':
2935               sprintf( tmp, "%.*d", count, xtime->wHour );
2936               MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2937               break;
2938
2939           case 'm':
2940               sprintf( tmp, "%.*d", count, xtime->wMinute );
2941               MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2942               break;
2943
2944           case 's':
2945               sprintf( tmp, "%.*d", count, xtime->wSecond );
2946               MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2947               break;
2948
2949           case 't':
2950             GetLocaleInfoW(locale, (xtime->wHour < 12) ?
2951                              LOCALE_S1159 : LOCALE_S2359,
2952                              buf, sizeof(buf) );
2953             if        (count == 1) {
2954                buf[1] = 0;
2955             }
2956             break;
2957           }
2958
2959          /* no matter what happened,  we need to check this next 
2960             character the next time we loop through */
2961          inpos--;
2962
2963          /* cat buf onto the output */
2964          outlen = strlenW(buf);
2965          if (outpos + buflen < outlen) {
2966             strcpyW( output + outpos, buf );
2967             outpos += buflen;
2968          } else {
2969             lstrcpynW( output + outpos, buf, outlen - outpos );
2970             Overflow = 1;
2971             break; /* Abnormal exit */
2972          }
2973
2974          /* reset the variables we used this time */
2975          count = 0;
2976          type = '\0';
2977       } else if (format[inpos] == 0) {
2978          /* we can't check for this at the beginning,  because that 
2979          would keep us from printing a format spec that ended the 
2980          string */
2981          output[outpos] = 0;
2982          break;  /*  NORMAL EXIT  */
2983       } else if (count) {
2984          /* how we keep track of the middle of a format spec */
2985          count++;
2986          continue;
2987       } else if ( (datevars && (format[inpos]=='d' ||
2988                                 format[inpos]=='M' ||
2989                                 format[inpos]=='y' ||
2990                                 format[inpos]=='g')  ) ||
2991                   (timevars && (format[inpos]=='H' ||
2992                                 format[inpos]=='h' ||
2993                                 format[inpos]=='m' ||
2994                                 format[inpos]=='s' ||
2995                                 format[inpos]=='t') )    ) {
2996          type = format[inpos];
2997          count = 1;
2998          continue;
2999       } else if (format[inpos] == '\'') {
3000          inquote = 1;
3001          continue;
3002       } else {
3003          /* unquoted literals */
3004          output[outpos++] = format[inpos];
3005       }
3006    }
3007
3008    if (Overflow) {
3009       SetLastError(ERROR_INSUFFICIENT_BUFFER);
3010       WARN(" buffer overflow\n");
3011    };
3012
3013    /* final string terminator and sanity check */
3014    outpos++;
3015    if (outpos > outlen-1) outpos = outlen-1;
3016    output[outpos] = '0';
3017
3018    TRACE(" returning %s\n", debugstr_w(output));
3019         
3020    return (!Overflow) ? outlen : 0;
3021    
3022 }
3023
3024
3025 /******************************************************************************
3026  *              GetDateFormatA  [KERNEL32.@]
3027  * Makes an ASCII string of the date
3028  *
3029  * This function uses format to format the date,  or,  if format
3030  * is NULL, uses the default for the locale.  format is a string
3031  * of literal fields and characters as follows:
3032  *
3033  * - d    single-digit (no leading zero) day (of month)
3034  * - dd   two-digit day (of month)
3035  * - ddd  short day-of-week name
3036  * - dddd long day-of-week name
3037  * - M    single-digit month
3038  * - MM   two-digit month
3039  * - MMM  short month name
3040  * - MMMM full month name
3041  * - y    two-digit year, no leading 0
3042  * - yy   two-digit year
3043  * - yyyy four-digit year
3044  * - gg   era string
3045  *
3046  */
3047 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
3048                               const SYSTEMTIME* xtime,
3049                               LPCSTR format, LPSTR date,INT datelen) 
3050 {
3051    
3052   char format_buf[40];
3053   LPCSTR thisformat;
3054   SYSTEMTIME t;
3055   LPSYSTEMTIME thistime;
3056   LCID thislocale;
3057   INT ret;
3058   FILETIME ft;
3059   BOOL res;
3060
3061   TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
3062               locale,flags,xtime,format,date,datelen);
3063   
3064   if (!locale) {
3065      locale = LOCALE_SYSTEM_DEFAULT;
3066      };
3067   
3068   if (locale == LOCALE_SYSTEM_DEFAULT) {
3069      thislocale = GetSystemDefaultLCID();
3070   } else if (locale == LOCALE_USER_DEFAULT) {
3071      thislocale = GetUserDefaultLCID();
3072   } else {
3073      thislocale = locale;
3074    };
3075
3076   if (xtime == NULL) {
3077      GetSystemTime(&t);
3078   } else {
3079       /* Silently correct wDayOfWeek by transforming to FileTime and back again */
3080       res=SystemTimeToFileTime(xtime,&ft);
3081       /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER  on error */
3082       /*FIXME: SystemTimeToFileTime doesn't yet do that check */
3083       if(!res)
3084         {
3085           SetLastError(ERROR_INVALID_PARAMETER);
3086           return 0;
3087         }
3088       FileTimeToSystemTime(&ft,&t); 
3089      
3090   };
3091   thistime = &t;
3092
3093   if (format == NULL) {
3094      GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE) 
3095                                    ? LOCALE_SLONGDATE
3096                                    : LOCALE_SSHORTDATE),
3097                       format_buf, sizeof(format_buf));
3098      thisformat = format_buf;
3099   } else {
3100      thisformat = format;
3101   };
3102
3103   
3104   ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat, 
3105                        date, datelen);
3106   
3107
3108    TRACE(
3109                "GetDateFormatA() returning %d, with data=%s\n",
3110                ret, date);
3111   return ret;
3112 }
3113
3114 /******************************************************************************
3115  *              GetDateFormatW  [KERNEL32.@]
3116  * Makes a Unicode string of the date
3117  *
3118  * Acts the same as GetDateFormatA(),  except that it's Unicode.
3119  * Accepts & returns sizes as counts of Unicode characters.
3120  *
3121  */
3122 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
3123                               const SYSTEMTIME* xtime,
3124                               LPCWSTR format,
3125                               LPWSTR date, INT datelen)
3126 {
3127    unsigned short datearr[] = {'1','9','9','4','-','1','-','1',0};
3128
3129    FIXME("STUB (should call OLE_GetFormatW)\n");   
3130    lstrcpynW(date, datearr, datelen);
3131    return (  datelen < 9) ? datelen : 9;
3132    
3133    
3134 }
3135
3136 /**************************************************************************
3137  *              EnumDateFormatsA        (KERNEL32.@)
3138  */
3139 BOOL WINAPI EnumDateFormatsA(
3140   DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale,  DWORD dwFlags)
3141 {
3142   LCID Loc = GetUserDefaultLCID(); 
3143   if(!lpDateFmtEnumProc)
3144     {
3145       SetLastError(ERROR_INVALID_PARAMETER);
3146       return FALSE;
3147     }
3148
3149   switch( Loc )
3150  {
3151
3152    case 0x00000407:  /* (Loc,"de_DE") */
3153    {
3154     switch(dwFlags)
3155     {
3156       case DATE_SHORTDATE:
3157         if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
3158         if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
3159         if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
3160         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3161         return TRUE;
3162       case DATE_LONGDATE:
3163         if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
3164         if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
3165         if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
3166         return TRUE;
3167       default:
3168         FIXME("Unknown date format (%ld)\n", dwFlags); 
3169         SetLastError(ERROR_INVALID_PARAMETER);
3170         return FALSE;
3171      }
3172    }       
3173
3174    case 0x0000040c:  /* (Loc,"fr_FR") */
3175    {
3176     switch(dwFlags)
3177     {
3178       case DATE_SHORTDATE:
3179         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3180         if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
3181         if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
3182         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3183         return TRUE;
3184       case DATE_LONGDATE:
3185         if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
3186         if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
3187         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3188         return TRUE;
3189       default:
3190         FIXME("Unknown date format (%ld)\n", dwFlags); 
3191         SetLastError(ERROR_INVALID_PARAMETER);
3192         return FALSE;
3193      }
3194    }
3195
3196    case 0x00000c0c:  /* (Loc,"fr_CA") */
3197    {
3198     switch(dwFlags)
3199     {
3200       case DATE_SHORTDATE:
3201         if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
3202         if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
3203         if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
3204         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3205         return TRUE;
3206       case DATE_LONGDATE:
3207         if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
3208         if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
3209         return TRUE;
3210       default:
3211         FIXME("Unknown date format (%ld)\n", dwFlags); 
3212         SetLastError(ERROR_INVALID_PARAMETER);
3213         return FALSE;
3214      }
3215    }
3216
3217    case 0x00000809:  /* (Loc,"en_UK") */ 
3218   {    
3219    switch(dwFlags)
3220     {
3221       case DATE_SHORTDATE:
3222         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3223         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3224         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3225         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3226         return TRUE;
3227       case DATE_LONGDATE:
3228         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3229         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3230         return TRUE;
3231       default:
3232         FIXME("Unknown date format (%ld)\n", dwFlags); 
3233         SetLastError(ERROR_INVALID_PARAMETER);
3234         return FALSE;
3235     }
3236   }
3237
3238    case 0x00000c09:  /* (Loc,"en_AU") */   
3239   {    
3240    switch(dwFlags)
3241     {
3242       case DATE_SHORTDATE:
3243         if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
3244         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3245         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3246         return TRUE;
3247       case DATE_LONGDATE:
3248         if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
3249         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3250         return TRUE;
3251       default:
3252         FIXME("Unknown date format (%ld)\n", dwFlags); 
3253         SetLastError(ERROR_INVALID_PARAMETER);
3254         return FALSE;
3255     }
3256   }
3257
3258    case 0x00001009:  /* (Loc,"en_CA") */ 
3259   {    
3260    switch(dwFlags)
3261     {
3262       case DATE_SHORTDATE:
3263         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3264         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3265         if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
3266         if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
3267         return TRUE;
3268       case DATE_LONGDATE:
3269         if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
3270         if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
3271         return TRUE;
3272       default:
3273         FIXME("Unknown date format (%ld)\n", dwFlags); 
3274         SetLastError(ERROR_INVALID_PARAMETER);
3275         return FALSE;
3276     }
3277   }
3278
3279    case 0x00001409:  /* (Loc,"en_NZ") */ 
3280   {    
3281    switch(dwFlags)
3282     {
3283       case DATE_SHORTDATE:
3284         if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
3285         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3286         if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
3287         return TRUE;
3288       case DATE_LONGDATE:
3289         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3290         if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
3291         return TRUE;
3292       default:
3293         FIXME("Unknown date format (%ld)\n", dwFlags); 
3294         SetLastError(ERROR_INVALID_PARAMETER);
3295         return FALSE;
3296     }
3297   }
3298
3299    case 0x00001809:  /* (Loc,"en_IE") */   
3300   {    
3301    switch(dwFlags)
3302     {
3303       case DATE_SHORTDATE:
3304         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3305         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3306         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3307         return TRUE;
3308       case DATE_LONGDATE:
3309         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3310         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3311         return TRUE;
3312       default:
3313         FIXME("Unknown date format (%ld)\n", dwFlags); 
3314         SetLastError(ERROR_INVALID_PARAMETER);
3315         return FALSE;
3316     }
3317   }
3318
3319    case 0x00001c09:  /* (Loc,"en_ZA") */   
3320   {    
3321    switch(dwFlags)
3322     {
3323       case DATE_SHORTDATE:
3324         if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
3325         return TRUE;
3326       case DATE_LONGDATE:
3327         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3328         return TRUE;
3329       default:
3330         FIXME("Unknown date format (%ld)\n", dwFlags); 
3331         SetLastError(ERROR_INVALID_PARAMETER);
3332         return FALSE;
3333     }
3334   }
3335
3336    case 0x00002009:  /* (Loc,"en_JM") */  
3337   {    
3338    switch(dwFlags)
3339     {
3340       case DATE_SHORTDATE:
3341         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3342         return TRUE;
3343       case DATE_LONGDATE:
3344         if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
3345         if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
3346         if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
3347         if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
3348         return TRUE;
3349       default:
3350         FIXME("Unknown date format (%ld)\n", dwFlags); 
3351         SetLastError(ERROR_INVALID_PARAMETER);
3352         return FALSE;
3353     }
3354   }
3355
3356    case 0x00002809:  /* (Loc,"en_BZ") */
3357    case 0x00002c09:  /* (Loc,"en_TT") */  
3358   {    
3359    switch(dwFlags)
3360     {
3361       case DATE_SHORTDATE:
3362         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3363         return TRUE;
3364       case DATE_LONGDATE:
3365         if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
3366         return TRUE;
3367       default:
3368         FIXME("Unknown date format (%ld)\n", dwFlags); 
3369         SetLastError(ERROR_INVALID_PARAMETER);
3370         return FALSE;
3371     }
3372   }
3373
3374    default:  /* default to US English "en_US" */
3375   {
3376    switch(dwFlags)
3377     {
3378       case DATE_SHORTDATE:
3379         if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
3380         if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
3381         if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
3382         if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
3383         if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
3384         if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
3385         return TRUE;
3386       case DATE_LONGDATE:
3387         if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
3388         if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
3389         if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
3390         if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
3391         return TRUE;
3392       default:
3393         FIXME("Unknown date format (%ld)\n", dwFlags); 
3394         SetLastError(ERROR_INVALID_PARAMETER);
3395         return FALSE;
3396     }
3397   }
3398  }
3399 }
3400
3401 /**************************************************************************
3402  *              EnumDateFormatsW        (KERNEL32.@)
3403  */
3404 BOOL WINAPI EnumDateFormatsW(
3405   DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
3406 {
3407   FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
3408   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3409   return FALSE;
3410 }
3411
3412 /**************************************************************************
3413  *              EnumTimeFormatsA        (KERNEL32.@)
3414  */
3415 BOOL WINAPI EnumTimeFormatsA(
3416   TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
3417 {
3418   LCID Loc = GetUserDefaultLCID(); 
3419   if(!lpTimeFmtEnumProc)
3420     {
3421       SetLastError(ERROR_INVALID_PARAMETER);
3422       return FALSE;
3423     }
3424   if(dwFlags)
3425     {
3426       FIXME("Unknown time format (%ld)\n", dwFlags); 
3427     }
3428
3429   switch( Loc )
3430  {
3431    case 0x00000407:  /* (Loc,"de_DE") */
3432    {
3433     if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE; 
3434     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3435     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3436     if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
3437     if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
3438     return TRUE;
3439    }       
3440
3441    case 0x0000040c:  /* (Loc,"fr_FR") */
3442    case 0x00000c0c:  /* (Loc,"fr_CA") */
3443    {
3444     if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
3445     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3446     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3447     if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
3448     if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
3449     return TRUE;
3450    }
3451
3452    case 0x00000809:  /* (Loc,"en_UK") */
3453    case 0x00000c09:  /* (Loc,"en_AU") */ 
3454    case 0x00001409:  /* (Loc,"en_NZ") */
3455    case 0x00001809:  /* (Loc,"en_IE") */ 
3456    {
3457     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3458     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3459     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3460     return TRUE;
3461    }
3462
3463    case 0x00001c09:  /* (Loc,"en_ZA") */   
3464    case 0x00002809:  /* (Loc,"en_BZ") */
3465    case 0x00002c09:  /* (Loc,"en_TT") */ 
3466    {
3467     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3468     if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE; 
3469     return TRUE;  
3470    }
3471
3472    default:  /* default to US style "en_US" */   
3473    {
3474     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3475     if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
3476     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3477     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3478     return TRUE;
3479    }
3480  }
3481 }
3482
3483 /**************************************************************************
3484  *              EnumTimeFormatsW        (KERNEL32.@)
3485  */
3486 BOOL WINAPI EnumTimeFormatsW(
3487   TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
3488 {
3489   FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
3490   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3491   return FALSE;
3492 }
3493
3494 /**************************************************************************
3495  *           This function is used just locally !
3496  *  Description: Inverts a string.
3497  */ 
3498 static void OLE_InvertString(char* string)
3499 {    
3500     char    sTmpArray[128];
3501     INT     counter, i = 0;
3502
3503     for (counter = strlen(string); counter > 0; counter--)
3504     {
3505         memcpy(sTmpArray + i, string + counter-1, 1);
3506         i++;
3507     }
3508     memcpy(sTmpArray + i, "\0", 1);
3509     strcpy(string, sTmpArray);
3510 }
3511
3512 /***************************************************************************************
3513  *           This function is used just locally !
3514  *  Description: Test if the given string (psNumber) is valid or not.
3515  *               The valid characters are the following:
3516  *               - Characters '0' through '9'. 
3517  *               - One decimal point (dot) if the number is a floating-point value. 
3518  *               - A minus sign in the first character position if the number is 
3519  *                 a negative value.
3520  *              If the function succeeds, psBefore/psAfter will point to the string
3521  *              on the right/left of the decimal symbol. pbNegative indicates if the 
3522  *              number is negative.
3523  */
3524 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
3525 {
3526 char    sNumberSet[] = "0123456789";
3527 BOOL    bInDecimal = FALSE;
3528
3529         /* Test if we do have a minus sign */
3530         if ( *pInput == '-' )
3531         {
3532                 *pbNegative = TRUE;
3533                 pInput++; /* Jump to the next character. */
3534         }
3535         
3536         while(*pInput != '\0')
3537         {
3538                 /* Do we have a valid numeric character */
3539                 if ( strchr(sNumberSet, *pInput) != NULL )
3540                 {
3541                         if (bInDecimal == TRUE)
3542                 *psAfter++ = *pInput;
3543                         else
3544                 *psBefore++ = *pInput;
3545                 }
3546                 else
3547                 {
3548                         /* Is this a decimal point (dot) */
3549                         if ( *pInput == '.' )
3550                         {
3551                                 /* Is it the first time we find it */
3552                                 if ((bInDecimal == FALSE))
3553                                         bInDecimal = TRUE;
3554                                 else
3555                                         return -1; /* ERROR: Invalid parameter */
3556                         }
3557                         else
3558                         {
3559                                 /* It's neither a numeric character, nor a decimal point.
3560                                  * Thus, return an error.
3561                  */
3562                                 return -1;
3563                         }
3564                 }
3565         pInput++;
3566         }
3567         
3568         /* Add an End of Line character to the output buffers */
3569         *psBefore = '\0';
3570         *psAfter = '\0';
3571
3572         return 0; 
3573 }
3574
3575 /**************************************************************************
3576  *           This function is used just locally !
3577  *  Description: A number could be formatted using different numbers 
3578  *               of "digits in group" (example: 4;3;2;0).
3579  *               The first parameter of this function is an array
3580  *               containing the rule to be used. Its format is the following:
3581  *               |NDG|DG1|DG2|...|0|
3582  *               where NDG is the number of used "digits in group" and DG1, DG2,
3583  *               are the corresponding "digits in group".
3584  *               Thus, this function returns the grouping value in the array
3585  *               pointed by the second parameter.
3586  */
3587 static INT OLE_GetGrouping(char* sRule, INT index)
3588 {
3589     char    sData[2], sRuleSize[2];
3590     INT     nData, nRuleSize;
3591
3592     memcpy(sRuleSize, sRule, 1);
3593     memcpy(sRuleSize+1, "\0", 1);
3594     nRuleSize = atoi(sRuleSize);
3595
3596     if (index > 0 && index < nRuleSize)
3597     {
3598         memcpy(sData, sRule+index, 1);
3599         memcpy(sData+1, "\0", 1);
3600         nData = atoi(sData);            
3601     }
3602         
3603     else
3604     {
3605         memcpy(sData, sRule+nRuleSize-1, 1);
3606         memcpy(sData+1, "\0", 1);
3607         nData = atoi(sData);            
3608     }
3609     
3610     return nData;
3611 }
3612
3613 /**************************************************************************
3614  *              GetNumberFormatA        (KERNEL32.@)
3615  */
3616 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
3617                                LPCSTR lpvalue,   const NUMBERFMTA * lpFormat,
3618                                LPSTR lpNumberStr, int cchNumber)
3619 {
3620     char   sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
3621     INT    nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
3622     char   sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
3623     char   sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];    
3624     char   *pStr = NULL, *pTmpStr = NULL;
3625     LCID   systemDefaultLCID;
3626     BOOL   bNegative = FALSE;
3627     enum   Operations 
3628     {
3629         USE_PARAMETER,
3630         USE_LOCALEINFO,
3631         USE_SYSTEMDEFAULT,
3632         RETURN_ERROR
3633     } used_operation;
3634
3635     strncpy(sNumber, lpvalue, 128);
3636     sNumber[127] = '\0';
3637
3638     /* Make sure we have a valid input string, get the number
3639      * of digits before and after the decimal symbol, and check
3640      * if this is a negative number.
3641      */
3642     if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
3643     {
3644         nNumberDecimal = strlen(sDigitsBeforeDecimal);
3645         nDigits = strlen(sDigitsAfterDecimal);
3646     }
3647     else
3648     {
3649         SetLastError(ERROR_INVALID_PARAMETER);
3650         return 0;
3651     }
3652
3653     /* Which source will we use to format the string */
3654     used_operation = RETURN_ERROR;
3655     if (lpFormat != NULL)
3656     {
3657         if (dwflags == 0)
3658             used_operation = USE_PARAMETER; 
3659     }
3660     else
3661     {
3662         if (dwflags & LOCALE_NOUSEROVERRIDE)
3663             used_operation = USE_LOCALEINFO;
3664         else
3665             used_operation = USE_SYSTEMDEFAULT;
3666     }
3667
3668     /* Load the fields we need */
3669     switch(used_operation)
3670     {
3671     case USE_LOCALEINFO:
3672         GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3673         GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3674         GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3675         GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3676         GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3677         GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3678         break;
3679     case USE_PARAMETER:
3680         sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
3681         strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
3682         sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
3683         strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
3684         sprintf(sILZero, "%d",lpFormat->LeadingZero);
3685         sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
3686         break;
3687     case USE_SYSTEMDEFAULT:
3688         systemDefaultLCID = GetSystemDefaultLCID();
3689         GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3690         GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3691         GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3692         GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3693         GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3694         GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3695         break;
3696     default:
3697         SetLastError(ERROR_INVALID_PARAMETER);
3698         return 0;
3699     }
3700
3701     nNumberDigits = atoi(sNumberDigits);
3702     
3703     /* Remove the ";" */
3704     i=0;
3705     j = 1;
3706     for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
3707     {
3708         if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
3709         {
3710             memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
3711             i++;
3712             j++;
3713         }
3714     }
3715     sprintf(sBuffer, "%d", i);
3716     memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
3717     memcpy(sRule + j, "\0", 1);
3718     
3719     /* First, format the digits before the decimal. */
3720     if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0)) 
3721     {
3722         /* Working on an inverted string is easier ! */
3723         OLE_InvertString(sDigitsBeforeDecimal);
3724
3725         nStep = nCounter = i = j = 0;
3726         nRuleIndex = 1;
3727         nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3728         
3729         /* Here, we will loop until we reach the end of the string.
3730          * An internal counter (j) is used in order to know when to 
3731          * insert the "digit group symbol".
3732          */
3733         while (nNumberDecimal > 0)
3734         {
3735             i = nCounter + nStep;
3736             memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
3737             nCounter++;
3738             j++;
3739             if (j >= nGrouping)
3740             {
3741                 j = 0;
3742                 if (nRuleIndex < sRule[0])
3743                     nRuleIndex++;
3744                 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3745                 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
3746                 nStep+= strlen(sDigitGroupSymbol);
3747             }
3748
3749             nNumberDecimal--;
3750         }
3751
3752         memcpy(sDestination + i+1, "\0", 1);
3753         /* Get the string in the right order ! */
3754         OLE_InvertString(sDestination);
3755      }
3756      else
3757      {
3758         nLZ = atoi(sILZero);
3759         if (nLZ != 0)
3760         {        
3761             /* Use 0.xxx instead of .xxx */
3762             memcpy(sDestination, "0", 1);
3763             memcpy(sDestination+1, "\0", 1);
3764         }
3765         else
3766             memcpy(sDestination, "\0", 1);
3767
3768      }
3769
3770     /* Second, format the digits after the decimal. */
3771     j = 0;
3772     nCounter = nNumberDigits;
3773     if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
3774     {
3775         i = strlen(sNumber) - strlen(pStr) + 1;        
3776         strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
3777         j = strlen(sDigitsAfterDecimal);
3778         if (j < nNumberDigits)
3779             nCounter = nNumberDigits-j;            
3780     }
3781     for (i=0;i<nCounter;i++)
3782          memcpy(sDigitsAfterDecimal+i+j, "0", 1);    
3783     memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);    
3784
3785     i = strlen(sDestination);
3786     j = strlen(sDigitsAfterDecimal);
3787     /* Finally, construct the resulting formatted string. */
3788         
3789     for (nCounter=0; nCounter<i; nCounter++)
3790         memcpy(sNumber + nCounter, sDestination + nCounter, 1);
3791        
3792     memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
3793
3794     for (i=0; i<j; i++)
3795         memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
3796     memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
3797         
3798     /* Is it a negative number */
3799     if (bNegative == TRUE)
3800     {
3801         i = atoi(sNegNumber);
3802         pStr = sDestination;
3803         pTmpStr = sNumber;
3804         switch (i)
3805         {          
3806         case 0:
3807             *pStr++ = '(';
3808             while (*sNumber != '\0')
3809                 *pStr++ =  *pTmpStr++;
3810             *pStr++ = ')';                
3811             break;  
3812         case 1:
3813             *pStr++ = '-';
3814             while (*pTmpStr != '\0')
3815                 *pStr++ =  *pTmpStr++;
3816             break;
3817         case 2:
3818             *pStr++ = '-';
3819             *pStr++ = ' ';
3820             while (*pTmpStr != '\0')
3821                 *pStr++ =  *pTmpStr++;
3822             break;
3823         case 3:
3824             while (*pTmpStr != '\0')
3825                 *pStr++ =  *pTmpStr++;
3826             *pStr++ = '-';
3827             break;
3828         case 4:
3829             while (*pTmpStr != '\0')
3830                 *pStr++ =  *pTmpStr++;
3831             *pStr++ = ' ';
3832             *pStr++ = '-';
3833             break;
3834         default:
3835             while (*pTmpStr != '\0')
3836                 *pStr++ =  *pTmpStr++;
3837             break;
3838         }
3839     }
3840     else
3841         strcpy(sDestination, sNumber);
3842
3843     /* If cchNumber is zero, then returns the number of bytes or characters 
3844      * required to hold the formatted number string
3845      */
3846     if (cchNumber==0)
3847         retVal = strlen(sDestination) + 1;
3848     else           
3849     {
3850         strncpy (lpNumberStr, sDestination, cchNumber-1);
3851         *(lpNumberStr+cchNumber-1) = '\0';   /* ensure we got a NULL at the end */
3852         retVal = strlen(lpNumberStr);
3853     }
3854           
3855     return retVal;
3856 }
3857
3858 /**************************************************************************
3859  *              GetNumberFormatW        (KERNEL32.@)
3860  */
3861 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
3862                                LPCWSTR lpvalue,  const NUMBERFMTW * lpFormat,
3863                                LPWSTR lpNumberStr, int cchNumber)
3864 {
3865  FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue));
3866
3867  lstrcpynW( lpNumberStr, lpvalue, cchNumber );
3868  return cchNumber? strlenW( lpNumberStr ) : 0;
3869 }
3870
3871 /**************************************************************************
3872  *              GetCurrencyFormatA      (KERNEL32.@)
3873  */
3874 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
3875                                LPCSTR lpvalue,   const CURRENCYFMTA * lpFormat,
3876                                LPSTR lpCurrencyStr, int cchCurrency)
3877 {
3878     UINT   nPosOrder, nNegOrder;
3879     INT    retVal;
3880     char   sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
3881     char   *pDestination = sDestination;
3882     char   sNumberFormated[128];
3883     char   *pNumberFormated = sNumberFormated;
3884     LCID   systemDefaultLCID;
3885     BOOL   bIsPositive = FALSE, bValidFormat = FALSE;
3886     enum   Operations 
3887     {
3888         USE_PARAMETER,
3889         USE_LOCALEINFO,
3890         USE_SYSTEMDEFAULT,
3891         RETURN_ERROR
3892     } used_operation;
3893
3894     NUMBERFMTA  numberFmt;
3895
3896     /* Which source will we use to format the string */
3897     used_operation = RETURN_ERROR;
3898     if (lpFormat != NULL)
3899     {
3900         if (dwflags == 0)
3901             used_operation = USE_PARAMETER; 
3902     }
3903     else
3904     {
3905         if (dwflags & LOCALE_NOUSEROVERRIDE)
3906             used_operation = USE_LOCALEINFO;
3907         else
3908             used_operation = USE_SYSTEMDEFAULT;
3909     }
3910
3911     /* Load the fields we need */
3912     switch(used_operation)
3913     {
3914     case USE_LOCALEINFO:        
3915         /* Specific to CURRENCYFMTA */
3916         GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3917         GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3918         GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3919         
3920         nPosOrder = atoi(sPosOrder);
3921         nNegOrder = atoi(sNegOrder);
3922         break;
3923     case USE_PARAMETER:        
3924         /* Specific to CURRENCYFMTA */
3925         nNegOrder = lpFormat->NegativeOrder;
3926         nPosOrder = lpFormat->PositiveOrder;
3927         strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
3928         break;
3929     case USE_SYSTEMDEFAULT:
3930         systemDefaultLCID = GetSystemDefaultLCID();        
3931         /* Specific to CURRENCYFMTA */
3932         GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3933         GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3934         GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3935         
3936         nPosOrder = atoi(sPosOrder);
3937         nNegOrder = atoi(sNegOrder);
3938         break;
3939     default:
3940         SetLastError(ERROR_INVALID_PARAMETER);
3941         return 0;
3942     }
3943     
3944     /* Construct a temporary number format structure */
3945     if (lpFormat != NULL)
3946     {
3947         numberFmt.NumDigits     = lpFormat->NumDigits;
3948         numberFmt.LeadingZero   = lpFormat->LeadingZero;
3949         numberFmt.Grouping      = lpFormat->Grouping;
3950         numberFmt.NegativeOrder = 0;
3951         numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
3952         numberFmt.lpThousandSep = lpFormat->lpThousandSep;
3953         bValidFormat = TRUE;
3954     }
3955
3956     /* Make a call to GetNumberFormatA() */
3957     if (*lpvalue == '-')
3958     {
3959         bIsPositive = FALSE;
3960         retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3961     }
3962     else
3963     {
3964         bIsPositive = TRUE;
3965         retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3966     }
3967         
3968     if (retVal == 0)
3969         return 0;
3970
3971     /* construct the formatted string */
3972     if (bIsPositive)
3973     {
3974         switch (nPosOrder)
3975         {
3976         case 0:   /* Prefix, no separation */
3977             strcpy (pDestination, sCurrencySymbol);
3978             strcat (pDestination, pNumberFormated);
3979             break;
3980         case 1:   /* Suffix, no separation */
3981             strcpy (pDestination, pNumberFormated);
3982             strcat (pDestination, sCurrencySymbol);
3983             break;
3984         case 2:   /* Prefix, 1 char separation */
3985             strcpy (pDestination, sCurrencySymbol);
3986             strcat (pDestination, " ");
3987             strcat (pDestination, pNumberFormated);
3988             break;
3989         case 3:   /* Suffix, 1 char separation */
3990             strcpy (pDestination, pNumberFormated);
3991             strcat (pDestination, " ");
3992             strcat (pDestination, sCurrencySymbol);
3993             break;
3994         default:
3995             SetLastError(ERROR_INVALID_PARAMETER);
3996             return 0;
3997         }
3998     }
3999     else  /* negative number */
4000     {
4001         switch (nNegOrder)
4002         {
4003         case 0:   /* format: ($1.1) */
4004             strcpy (pDestination, "(");
4005             strcat (pDestination, sCurrencySymbol);
4006             strcat (pDestination, pNumberFormated);
4007             strcat (pDestination, ")");
4008             break;
4009         case 1:   /* format: -$1.1 */
4010             strcpy (pDestination, "-");
4011             strcat (pDestination, sCurrencySymbol);
4012             strcat (pDestination, pNumberFormated);
4013             break;
4014         case 2:   /* format: $-1.1 */
4015             strcpy (pDestination, sCurrencySymbol);
4016             strcat (pDestination, "-");
4017             strcat (pDestination, pNumberFormated);
4018             break;
4019         case 3:   /* format: $1.1- */
4020             strcpy (pDestination, sCurrencySymbol);
4021             strcat (pDestination, pNumberFormated);
4022             strcat (pDestination, "-");
4023             break;
4024         case 4:   /* format: (1.1$) */
4025             strcpy (pDestination, "(");
4026             strcat (pDestination, pNumberFormated);
4027             strcat (pDestination, sCurrencySymbol);
4028             strcat (pDestination, ")");
4029             break;
4030         case 5:   /* format: -1.1$ */
4031             strcpy (pDestination, "-");
4032             strcat (pDestination, pNumberFormated);
4033             strcat (pDestination, sCurrencySymbol);
4034             break;
4035         case 6:   /* format: 1.1-$ */
4036             strcpy (pDestination, pNumberFormated);
4037             strcat (pDestination, "-");
4038             strcat (pDestination, sCurrencySymbol);
4039             break;
4040         case 7:   /* format: 1.1$- */
4041             strcpy (pDestination, pNumberFormated);
4042             strcat (pDestination, sCurrencySymbol);
4043             strcat (pDestination, "-");
4044             break;
4045         case 8:   /* format: -1.1 $ */
4046             strcpy (pDestination, "-");
4047             strcat (pDestination, pNumberFormated);
4048             strcat (pDestination, " ");
4049             strcat (pDestination, sCurrencySymbol);
4050             break;
4051         case 9:   /* format: -$ 1.1 */
4052             strcpy (pDestination, "-");
4053             strcat (pDestination, sCurrencySymbol);
4054             strcat (pDestination, " ");
4055             strcat (pDestination, pNumberFormated);
4056             break;
4057         case 10:   /* format: 1.1 $- */
4058             strcpy (pDestination, pNumberFormated);
4059             strcat (pDestination, " ");
4060             strcat (pDestination, sCurrencySymbol);
4061             strcat (pDestination, "-");
4062             break;
4063         case 11:   /* format: $ 1.1- */
4064             strcpy (pDestination, sCurrencySymbol);
4065             strcat (pDestination, " ");
4066             strcat (pDestination, pNumberFormated);
4067             strcat (pDestination, "-");
4068             break;
4069         case 12:   /* format: $ -1.1 */
4070             strcpy (pDestination, sCurrencySymbol);
4071             strcat (pDestination, " ");
4072             strcat (pDestination, "-");
4073             strcat (pDestination, pNumberFormated);
4074             break;
4075         case 13:   /* format: 1.1- $ */
4076             strcpy (pDestination, pNumberFormated);
4077             strcat (pDestination, "-");
4078             strcat (pDestination, " ");
4079             strcat (pDestination, sCurrencySymbol);
4080             break;
4081         case 14:   /* format: ($ 1.1) */
4082             strcpy (pDestination, "("); 
4083             strcat (pDestination, sCurrencySymbol);
4084             strcat (pDestination, " ");
4085             strcat (pDestination, pNumberFormated);
4086             strcat (pDestination, ")");
4087             break;
4088         case 15:   /* format: (1.1 $) */
4089             strcpy (pDestination, "("); 
4090             strcat (pDestination, pNumberFormated);
4091             strcat (pDestination, " ");
4092             strcat (pDestination, sCurrencySymbol);
4093             strcat (pDestination, ")");
4094             break;
4095         default:
4096             SetLastError(ERROR_INVALID_PARAMETER);
4097             return 0;
4098         }
4099     }
4100
4101     if (cchCurrency == 0)
4102         return strlen(pDestination) + 1;
4103
4104     else
4105     {
4106         strncpy (lpCurrencyStr, pDestination, cchCurrency-1);
4107         *(lpCurrencyStr+cchCurrency-1) = '\0';   /* ensure we got a NULL at the end */
4108         return  strlen(lpCurrencyStr);
4109     }    
4110 }
4111
4112 /**************************************************************************
4113  *              GetCurrencyFormatW      (KERNEL32.@)
4114  */
4115 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
4116                                LPCWSTR lpvalue,   const CURRENCYFMTW * lpFormat,
4117                                LPWSTR lpCurrencyStr, int cchCurrency)
4118 {
4119     FIXME("This API function is NOT implemented !\n");
4120     return 0;
4121 }
4122
4123 /******************************************************************************
4124  *              OLE2NLS_CheckLocale     [intern]
4125  */ 
4126 static LCID OLE2NLS_CheckLocale (LCID locale)
4127 {
4128         if (!locale) 
4129         { locale = LOCALE_SYSTEM_DEFAULT;
4130         }
4131   
4132         if (locale == LOCALE_SYSTEM_DEFAULT) 
4133         { return GetSystemDefaultLCID();
4134         } 
4135         else if (locale == LOCALE_USER_DEFAULT) 
4136         { return GetUserDefaultLCID();
4137         }
4138         else
4139         { return locale;
4140         }
4141 }
4142 /******************************************************************************
4143  *              GetTimeFormatA  [KERNEL32.@]
4144  * Makes an ASCII string of the time
4145  *
4146  * Formats date according to format,  or locale default if format is
4147  * NULL. The format consists of literal characters and fields as follows:
4148  *
4149  * h  hours with no leading zero (12-hour)
4150  * hh hours with full two digits
4151  * H  hours with no leading zero (24-hour)
4152  * HH hours with full two digits
4153  * m  minutes with no leading zero
4154  * mm minutes with full two digits
4155  * s  seconds with no leading zero
4156  * ss seconds with full two digits
4157  * t  time marker (A or P)
4158  * tt time marker (AM, PM)
4159  *
4160  */
4161 INT WINAPI 
4162 GetTimeFormatA(LCID locale,        /* [in]  */
4163                DWORD flags,        /* [in]  */
4164                const SYSTEMTIME* xtime, /* [in]  */
4165                LPCSTR format,      /* [in]  */
4166                LPSTR timestr,      /* [out] */
4167                INT timelen         /* [in]  */) 
4168 { char format_buf[40];
4169   LPCSTR thisformat;
4170   SYSTEMTIME t;
4171   const SYSTEMTIME* thistime;
4172   LCID thislocale=0;
4173   DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
4174   INT ret;
4175     
4176   TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
4177
4178   thislocale = OLE2NLS_CheckLocale ( locale );
4179
4180   if (format == NULL) 
4181   { if (flags & LOCALE_NOUSEROVERRIDE)  /* use system default */
4182     { thislocale = GetSystemDefaultLCID();
4183     }
4184     GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
4185     thisformat = format_buf;
4186   }
4187   else 
4188   { thisformat = format;
4189   }
4190   
4191   if (xtime == NULL) /* NULL means use the current local time */
4192   { GetLocalTime(&t);
4193     thistime = &t;
4194   } 
4195   else
4196   { thistime = xtime;
4197   /* Check that hour,min and sec is in range */
4198   }
4199   ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
4200                          timestr, timelen);
4201   return ret;
4202 }
4203
4204
4205 /******************************************************************************
4206  *              GetTimeFormatW  [KERNEL32.@]
4207  * Makes a Unicode string of the time
4208  */
4209 INT WINAPI 
4210 GetTimeFormatW(LCID locale,        /* [in]  */
4211                DWORD flags,        /* [in]  */
4212                const SYSTEMTIME* xtime, /* [in]  */
4213                LPCWSTR format,     /* [in]  */
4214                LPWSTR timestr,     /* [out] */
4215                INT timelen         /* [in]  */) 
4216 {       WCHAR format_buf[40];
4217         LPCWSTR thisformat;
4218         SYSTEMTIME t;
4219         const SYSTEMTIME* thistime;
4220         LCID thislocale=0;
4221         DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
4222         INT ret;
4223             
4224         TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
4225         xtime,debugstr_w(format),timestr,timelen);
4226
4227         thislocale = OLE2NLS_CheckLocale ( locale );
4228
4229         if (format == NULL) 
4230         { if (flags & LOCALE_NOUSEROVERRIDE)  /* use system default */
4231           { thislocale = GetSystemDefaultLCID();
4232           }
4233           GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
4234           thisformat = format_buf;
4235         }         
4236         else 
4237         { thisformat = format;
4238         }
4239  
4240         if (xtime == NULL) /* NULL means use the current local time */
4241         { GetLocalTime(&t);
4242           thistime = &t;
4243         } 
4244         else
4245         { thistime = xtime;
4246         }
4247
4248         ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
4249                          timestr, timelen);
4250         return ret;
4251 }
4252
4253 /******************************************************************************
4254  *              EnumCalendarInfoA       [KERNEL32.@]
4255  */
4256 BOOL WINAPI EnumCalendarInfoA(
4257         CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
4258 ) {
4259         FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
4260         return FALSE;
4261 }