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