Release 980726
[wine] / ole / ole2nls.c
1 /*
2  *      OLE2NLS library
3  *
4  *      Copyright 1995  Martin von Loewis
5  *      Copyright 1998  David Lee Lambert
6  */
7
8 #include <string.h>
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include "windows.h"
12 #include "heap.h"
13 #include "ole.h"
14 #include "options.h"
15 #include "winnls.h"
16 #include "winreg.h"
17 #include "winerror.h"
18 #include "debug.h"
19
20 /* Locale name to id map. used by EnumSystemLocales, GetLocalInfoA 
21  * MUST contain all #defines from winnls.h
22  * last entry has NULL name, 0 id.
23  */ 
24 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
25 static struct tagLOCALE_NAME2ID {
26         char    *name;
27         DWORD   id;
28 } locale_name2id[]= {
29         LOCALE_ENTRY(ILANGUAGE),
30         LOCALE_ENTRY(SLANGUAGE),
31         LOCALE_ENTRY(SENGLANGUAGE),
32         LOCALE_ENTRY(SABBREVLANGNAME),
33         LOCALE_ENTRY(SNATIVELANGNAME),
34         LOCALE_ENTRY(ICOUNTRY),
35         LOCALE_ENTRY(SCOUNTRY),
36         LOCALE_ENTRY(SENGCOUNTRY),
37         LOCALE_ENTRY(SABBREVCTRYNAME),
38         LOCALE_ENTRY(SNATIVECTRYNAME),
39         LOCALE_ENTRY(IDEFAULTLANGUAGE),
40         LOCALE_ENTRY(IDEFAULTCOUNTRY),
41         LOCALE_ENTRY(IDEFAULTCODEPAGE),
42         LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
43         LOCALE_ENTRY(SLIST),
44         LOCALE_ENTRY(IMEASURE),
45         LOCALE_ENTRY(SDECIMAL),
46         LOCALE_ENTRY(STHOUSAND),
47         LOCALE_ENTRY(SGROUPING),
48         LOCALE_ENTRY(IDIGITS),
49         LOCALE_ENTRY(ILZERO),
50         LOCALE_ENTRY(INEGNUMBER),
51         LOCALE_ENTRY(SNATIVEDIGITS),
52         LOCALE_ENTRY(SCURRENCY),
53         LOCALE_ENTRY(SINTLSYMBOL),
54         LOCALE_ENTRY(SMONDECIMALSEP),
55         LOCALE_ENTRY(SMONTHOUSANDSEP),
56         LOCALE_ENTRY(SMONGROUPING),
57         LOCALE_ENTRY(ICURRDIGITS),
58         LOCALE_ENTRY(IINTLCURRDIGITS),
59         LOCALE_ENTRY(ICURRENCY),
60         LOCALE_ENTRY(INEGCURR),
61         LOCALE_ENTRY(SDATE),
62         LOCALE_ENTRY(STIME),
63         LOCALE_ENTRY(SSHORTDATE),
64         LOCALE_ENTRY(SLONGDATE),
65         LOCALE_ENTRY(STIMEFORMAT),
66         LOCALE_ENTRY(IDATE),
67         LOCALE_ENTRY(ILDATE),
68         LOCALE_ENTRY(ITIME),
69         LOCALE_ENTRY(ITIMEMARKPOSN),
70         LOCALE_ENTRY(ICENTURY),
71         LOCALE_ENTRY(ITLZERO),
72         LOCALE_ENTRY(IDAYLZERO),
73         LOCALE_ENTRY(IMONLZERO),
74         LOCALE_ENTRY(S1159),
75         LOCALE_ENTRY(S2359),
76         LOCALE_ENTRY(ICALENDARTYPE),
77         LOCALE_ENTRY(IOPTIONALCALENDAR),
78         LOCALE_ENTRY(IFIRSTDAYOFWEEK),
79         LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
80         LOCALE_ENTRY(SDAYNAME1),
81         LOCALE_ENTRY(SDAYNAME2),
82         LOCALE_ENTRY(SDAYNAME3),
83         LOCALE_ENTRY(SDAYNAME4),
84         LOCALE_ENTRY(SDAYNAME5),
85         LOCALE_ENTRY(SDAYNAME6),
86         LOCALE_ENTRY(SDAYNAME7),
87         LOCALE_ENTRY(SABBREVDAYNAME1),
88         LOCALE_ENTRY(SABBREVDAYNAME2),
89         LOCALE_ENTRY(SABBREVDAYNAME3),
90         LOCALE_ENTRY(SABBREVDAYNAME4),
91         LOCALE_ENTRY(SABBREVDAYNAME5),
92         LOCALE_ENTRY(SABBREVDAYNAME6),
93         LOCALE_ENTRY(SABBREVDAYNAME7),
94         LOCALE_ENTRY(SMONTHNAME1),
95         LOCALE_ENTRY(SMONTHNAME2),
96         LOCALE_ENTRY(SMONTHNAME3),
97         LOCALE_ENTRY(SMONTHNAME4),
98         LOCALE_ENTRY(SMONTHNAME5),
99         LOCALE_ENTRY(SMONTHNAME6),
100         LOCALE_ENTRY(SMONTHNAME7),
101         LOCALE_ENTRY(SMONTHNAME8),
102         LOCALE_ENTRY(SMONTHNAME9),
103         LOCALE_ENTRY(SMONTHNAME10),
104         LOCALE_ENTRY(SMONTHNAME11),
105         LOCALE_ENTRY(SMONTHNAME12),
106         LOCALE_ENTRY(SMONTHNAME13),
107         LOCALE_ENTRY(SABBREVMONTHNAME1),
108         LOCALE_ENTRY(SABBREVMONTHNAME2),
109         LOCALE_ENTRY(SABBREVMONTHNAME3),
110         LOCALE_ENTRY(SABBREVMONTHNAME4),
111         LOCALE_ENTRY(SABBREVMONTHNAME5),
112         LOCALE_ENTRY(SABBREVMONTHNAME6),
113         LOCALE_ENTRY(SABBREVMONTHNAME7),
114         LOCALE_ENTRY(SABBREVMONTHNAME8),
115         LOCALE_ENTRY(SABBREVMONTHNAME9),
116         LOCALE_ENTRY(SABBREVMONTHNAME10),
117         LOCALE_ENTRY(SABBREVMONTHNAME11),
118         LOCALE_ENTRY(SABBREVMONTHNAME12),
119         LOCALE_ENTRY(SABBREVMONTHNAME13),
120         LOCALE_ENTRY(SPOSITIVESIGN),
121         LOCALE_ENTRY(SNEGATIVESIGN),
122         LOCALE_ENTRY(IPOSSIGNPOSN),
123         LOCALE_ENTRY(INEGSIGNPOSN),
124         LOCALE_ENTRY(IPOSSYMPRECEDES),
125         LOCALE_ENTRY(IPOSSEPBYSPACE),
126         LOCALE_ENTRY(INEGSYMPRECEDES),
127         LOCALE_ENTRY(INEGSEPBYSPACE),
128 /*      LOCALE_ENTRY(FONTSIGNATURE),*/
129         {NULL,0},
130 };
131
132 const struct map_lcid2str {
133         LCID            langid;
134         const char      *langname;
135 } languages[]={
136         {0x0401,"Arabisch"},
137         {0x0402,"Bulgarisch"},
138         {0x0403,"Katalanisch"},
139         {0x0404,"Traditionales Chinesisch"},
140         {0x0405,"Tschecisch"},
141         {0x0406,"Dänisch"},
142         {0x0407,"Deutsch"},
143         {0x0408,"Griechisch"},
144         {0x0409,"Amerikanisches Englisch"},
145         {0x040A,"Kastilisches Spanisch"},
146         {0x040B,"Finnisch"},
147         {0x040C,"Franzvsisch"},
148         {0x040D,"Hebrdisch"},
149         {0x040E,"Ungarisch"},
150         {0x040F,"Isldndisch"},
151         {0x0410,"Italienisch"},
152         {0x0411,"Japanisch"},
153         {0x0412,"Koreanisch"},
154         {0x0413,"Niederldndisch"},
155         {0x0414,"Norwegisch-Bokmal"},
156         {0x0415,"Polnisch"},
157         {0x0416,"Brasilianisches Portugiesisch"},
158         {0x0417,"Rdtoromanisch"},
159         {0x0418,"Rumdnisch"},
160         {0x0419,"Russisch"},
161         {0x041A,"Kroatoserbisch (lateinisch)"},
162         {0x041B,"Slowenisch"},
163         {0x041C,"Albanisch"},
164         {0x041D,"Schwedisch"},
165         {0x041E,"Thai"},
166         {0x041F,"T|rkisch"},
167         {0x0420,"Urdu"},
168         {0x0421,"Bahasa"},
169         {0x0804,"Vereinfachtes Chinesisch"},
170         {0x0807,"Schweizerdeutsch"},
171         {0x0809,"Britisches Englisch"},
172         {0x080A,"Mexikanisches Spanisch"},
173         {0x080C,"Belgisches Franzvsisch"},
174         {0x0810,"Schweizerisches Italienisch"},
175         {0x0813,"Belgisches Niederldndisch"},
176         {0x0814,"Norgwegisch-Nynorsk"},
177         {0x0816,"Portugiesisch"},
178         {0x081A,"Serbokratisch (kyrillisch)"},
179         {0x0C1C,"Kanadisches Franzvsisch"},
180         {0x100C,"Schweizerisches Franzvsisch"},
181         {0x0000,"Unbekannt"},
182 };
183
184 /***********************************************************************
185  *           GetUserDefaultLCID       (OLE2NLS.1)
186  */
187 LCID WINAPI GetUserDefaultLCID()
188 {
189 /* Default sorting, neutral sublanguage */
190     switch(Options.language)
191     {
192  /* case LANG_Bu: return 0x02; */      /* Bulgarian */
193  /* case LANG_Ch: return 0x04; */      /* Chinese */
194     case LANG_Cz: return 0x05;         /* Czech */
195     case LANG_Da: return 0x06;         /* Danish */
196     case LANG_De: return 0x07;         /* German */
197  /* case LANG_Gr: return 0x08; */      /* Greek */
198     case LANG_En: return 0x09;         /* English */
199     case LANG_Es: return 0x0a;         /* Spanish */
200     case LANG_Fi: return 0x0b;         /* Finnish */
201     case LANG_Fr: return 0x0c;         /* French */
202     case LANG_Hu: return 0x0e;         /* Hungarian */
203  /* case LANG_Ic: return 0x0f; */      /* Icelandic */
204     case LANG_It: return 0x10;         /* Italian */
205  /* case LANG_Jp: return 0x11; */      /* Japanese */
206     case LANG_Ko: return 0x12;         /* Korean */
207  /* case LANG_Du: return 0x13; */      /* Dutch */
208     case LANG_No: return 0x14;         /* Norwegian */
209     case LANG_Pl: return 0x15;         /* Polish */
210     case LANG_Po: return 0x16;         /* Portuguese */
211  /* case LANG_Ro: return 0x18; */      /* Romanian */
212  /* case LANG_Ru: return 0x19; */      /* Russian */
213  /* case LANG_Cr: return 0x1a; */      /* Croatian */
214  /* case LANG_Sl: return 0x1b; */      /* Slovak */
215     case LANG_Sw: return 0x1d;         /* Swedish */
216  /* case LANG_Tu: return 0x1f; */      /* Turkish */
217  /* case LANG_Sv: return 0x24; */      /* Slovenian */
218     case LANG_Eo: return 0x25;         /* Esperanto (not official) */
219     case LANG_Ca: return 0x26;         /* Catalan */
220
221     default:
222         return 0x00;                   /* Neutral language */
223     }
224 }
225
226 /***********************************************************************
227  *         GetSystemDefaultLCID       (OLE2NLS.2)
228  */
229 LCID WINAPI GetSystemDefaultLCID()
230 {
231         return GetUserDefaultLCID();
232 }
233
234 /***********************************************************************
235  *         GetUserDefaultLangID       (OLE2NLS.3)
236  */
237 LANGID WINAPI GetUserDefaultLangID()
238 {
239         return (WORD)GetUserDefaultLCID();
240 }
241
242 /***********************************************************************
243  *         GetSystemDefaultLangID     (OLE2NLS.4)
244  */
245 LANGID WINAPI GetSystemDefaultLangID()
246 {
247         return GetUserDefaultLangID();
248 }
249
250 /***********************************************************************
251  *         GetLocaleInfoA             (OLE2NLS.5)
252  * Is the last parameter really WORD for Win16?
253  */
254 INT16 WINAPI GetLocaleInfo16(LCID lcid,LCTYPE LCType,LPSTR buf,INT16 len)
255 {
256         return GetLocaleInfo32A(lcid,LCType,buf,len);
257 }
258
259 INT32 WINAPI GetLocaleInfo32A(LCID lcid,LCTYPE LCType,LPSTR buf,INT32 len)
260 {
261         char    *retString;
262         int     found,i;
263
264         TRACE(ole,"(%8lX,%8lX,%p,%4X)\n",
265                         lcid,LCType,buf,len);
266
267         LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
268
269         /* As an option, we could obtain the value from win.ini.
270            This would not match the Wine compile-time option.
271            Also, not all identifiers are available from win.ini */
272         retString=0;
273         /* If we are through all of this, retLen should not be zero anymore.
274            If it is, the value is not supported */
275         i=0;
276         while (locale_name2id[i].name!=NULL) {
277                 if (LCType == locale_name2id[i].id) {
278                         retString = locale_name2id[i].name;
279                         break;
280                 }
281                 i++;
282         }
283         if (!retString) {
284                 WARN(ole,"Unkown LC type %lX\n",LCType);
285                 return 0;
286         }
287
288 #define LOCVAL(type,value) case type:retString=value;break;
289
290 /* Now, the language specific definitions. They don't have to be
291    complete */
292     found=1;
293     switch(Options.language)
294     {
295     case LANG_De:
296         switch (LCType) {
297 #include "nls/deu.nls"
298         default: found=0;break;
299         }
300     break;  /* LANG(De) */
301
302     case LANG_Da:
303         switch (LCType) {
304 #include "nls/dnk.nls"
305         default: found=0;break;
306         }
307     break; /* LANG(Da) */
308
309     case LANG_En:
310         switch (LCType) {
311 #include "nls/enu.nls"
312         default: found=0;break;
313         }
314     break;  /* LANG(En) */
315
316     case LANG_Eo:
317         switch (LCType) {
318 #include "nls/esperanto.nls"
319         default: found=0;break;
320         }
321     break;  /* LANG(Eo) */
322
323     case LANG_Fi:
324         switch (LCType) {
325 #include "nls/fin.nls"
326         default: found=0;break;
327         }
328     break;  /* LANG(Fi) */
329
330     case LANG_It:
331         switch (LCType) {
332 #include "nls/ita.nls"
333         default: found=0;break;
334         }
335     break;  /* LANG(It) */
336
337     case 0x0809:
338         switch (LCType) {
339 #include "nls/eng.nls"
340         default: found=0;break;
341         }
342     break; /* LANG(0x0809) (U.K. English) */
343
344     case LANG_Ko: /* string using codepage 949 */
345         switch (LCType) {
346 #include "nls/kor.nls"
347         default: found=0;break;
348         }
349     break;  /* LANG(Ko) */
350
351     case LANG_Hu:
352         switch (LCType) {
353 #include "nls/hun.nls"
354         default: found=0;break;
355         }
356     break;  /* LANG(En) */
357
358     case LANG_Pl:
359         switch (LCType) {
360 #include "nls/plk.nls"
361         default: found=0;break;
362         }
363     break;  /* LANG(Pl) */
364
365     case LANG_Po:
366         switch (LCType) {
367 #include "nls/ptb.nls"
368         default: found=0;break;
369         }
370     break; /* LANG(Po) */
371
372     case LANG_Sw:
373         switch (LCType) {
374 #include "nls/sve.nls"
375         default: found=0;break;
376         }
377     break; /* LANG(Sw) */
378
379 /*Insert other languages here*/
380
381     default:
382         found=0;
383         break;
384     }  /* switch */
385
386         if(!found) {
387                 ERR(ole,"'%s' not supported for your language.\n",
388                         retString);
389                 retString = "<WINE-NLS-unknown>";
390                 /*return 0;*/
391         }
392         if (buf)
393                 lstrcpyn32A(buf,retString,len);
394         return strlen(retString)+1;
395 }
396
397 /***********************************************************************
398  *         GetLocaleInfo32W             (KERNEL32.230)
399  * Is the last parameter really WORD for Win16?
400  */
401 INT32 WINAPI GetLocaleInfo32W(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT32 len)
402 {
403         LPSTR abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
404
405         INT32 n = GetLocaleInfo32A(lcid, LCType, abuf, len);
406         if (wbuf)
407                 lstrcpynAtoW(wbuf,abuf,len);
408         HeapFree(GetProcessHeap(),0,abuf);
409         return n;
410 }
411
412 /***********************************************************************
413  *           SetLocaleInfoA       [KERNEL32.499]
414  */
415 BOOL16 WINAPI SetLocaleInfoA(DWORD lcid, DWORD lctype, LPCSTR data)
416 {
417     FIXME(ole,"(%ld,%ld,%s): stub\n",lcid,lctype,data);
418     return TRUE;
419 }
420
421 /***********************************************************************
422  *           IsValidLocale       [KERNEL32.361]
423  */
424 BOOL32 WINAPI IsValidLocale(LCID lcid,DWORD flags)
425 {
426         /* we support ANY language. Well, at least say that...*/
427         return TRUE;
428 }
429
430 /***********************************************************************
431  *              EnumSystemLocales32W                [KERNEL32.93]
432  */
433 BOOL32 WINAPI EnumSystemLocales32W( LOCALE_ENUMPROC32W lpfnLocaleEnum,
434                                     DWORD flags )
435 {
436         int     i;
437         BOOL32  ret;
438         WCHAR   buffer[200];
439         HKEY    xhkey;
440
441         TRACE(win32,"(%p,%08lx)\n",lpfnLocaleEnum,flags );
442         /* see if we can reuse the Win95 registry entries.... */
443         if (ERROR_SUCCESS==RegOpenKey32A(HKEY_LOCAL_MACHINE,"\\System\\CurrentControlSet\\control\\Nls\\Locale\\",&xhkey)) {
444                 i=0;
445                 while (1) {
446                         if (ERROR_SUCCESS!=RegEnumKey32W(xhkey,i,buffer,sizeof(buffer)))
447                                 break;
448                         if (!lpfnLocaleEnum(buffer))
449                                 break;
450                         i++;
451                 }
452                 RegCloseKey(xhkey);
453                 return TRUE;
454         }
455
456         i=0;
457         while (languages[i].langname!=NULL)
458         {
459             LPWSTR cp;
460             char   xbuffer[10];
461         
462             sprintf(xbuffer,"%08lx",(DWORD)languages[i].langid);
463
464             cp = HEAP_strdupAtoW( GetProcessHeap(), 0, xbuffer );
465             ret = lpfnLocaleEnum(cp);
466             HeapFree( GetProcessHeap(), 0, cp );
467             if (!ret) break;
468             i++;
469         }
470         return TRUE;
471 }
472
473 /***********************************************************************
474  *              EnumSystemLocales32A                [KERNEL32.92]
475  */
476 BOOL32 WINAPI EnumSystemLocales32A(LOCALE_ENUMPROC32A lpfnLocaleEnum,
477                                    DWORD flags)
478 {
479         int     i;
480         CHAR    buffer[200];
481         HKEY    xhkey;
482
483         TRACE(win32,"(%p,%08lx)\n",
484                 lpfnLocaleEnum,flags
485         );
486         if (ERROR_SUCCESS==RegOpenKey32A(HKEY_LOCAL_MACHINE,"\\System\\CurrentControlSet\\control\\Nls\\Locale\\",&xhkey)) {
487                 i=0;
488                 while (1) {
489                         if (ERROR_SUCCESS!=RegEnumKey32A(xhkey,i,buffer,sizeof(buffer)))
490                                 break;
491                         if (!lpfnLocaleEnum(buffer))
492                                 break;
493                         i++;
494                 }
495                 RegCloseKey(xhkey);
496                 return TRUE;
497         }
498         i=0;
499         while (languages[i].langname!=NULL) {
500                 sprintf(buffer,"%08lx",(DWORD)languages[i].langid);
501                 if (!lpfnLocaleEnum(buffer))
502                         break;
503                 i++;
504         }
505         return TRUE;
506 }
507
508 static const unsigned char CT_CType2_LUT[] = {
509   C2_NOTAPPLICABLE, /*   -   0 */
510   C2_NOTAPPLICABLE, /*   -   1 */
511   C2_NOTAPPLICABLE, /*   -   2 */
512   C2_NOTAPPLICABLE, /*   -   3 */
513   C2_NOTAPPLICABLE, /*   -   4 */
514   C2_NOTAPPLICABLE, /*   -   5 */
515   C2_NOTAPPLICABLE, /*   -   6 */
516   C2_NOTAPPLICABLE, /*   -   7 */
517   C2_NOTAPPLICABLE, /*   -   8 */
518   C2_SEGMENTSEPARATOR, /*   -   9 */
519   C2_NOTAPPLICABLE, /*   -  10 */
520   C2_NOTAPPLICABLE, /*   -  11 */
521   C2_NOTAPPLICABLE, /*   -  12 */
522   C2_NOTAPPLICABLE, /*   -  13 */
523   C2_NOTAPPLICABLE, /*   -  14 */
524   C2_NOTAPPLICABLE, /*   -  15 */
525   C2_NOTAPPLICABLE, /*   -  16 */
526   C2_NOTAPPLICABLE, /*   -  17 */
527   C2_NOTAPPLICABLE, /*   -  18 */
528   C2_NOTAPPLICABLE, /*   -  19 */
529   C2_NOTAPPLICABLE, /*   -  20 */
530   C2_NOTAPPLICABLE, /*   -  21 */
531   C2_NOTAPPLICABLE, /*   -  22 */
532   C2_NOTAPPLICABLE, /*   -  23 */
533   C2_NOTAPPLICABLE, /*   -  24 */
534   C2_NOTAPPLICABLE, /*   -  25 */
535   C2_NOTAPPLICABLE, /*   -  26 */
536   C2_NOTAPPLICABLE, /*   -  27 */
537   C2_NOTAPPLICABLE, /*   -  28 */
538   C2_NOTAPPLICABLE, /*   -  29 */
539   C2_NOTAPPLICABLE, /*   -  30 */
540   C2_NOTAPPLICABLE, /*   -  31 */
541   C2_WHITESPACE, /*   -  32 */
542   C2_OTHERNEUTRAL, /* ! -  33 */
543   C2_OTHERNEUTRAL, /* " -  34 */ /* " */
544   C2_EUROPETERMINATOR, /* # -  35 */
545   C2_EUROPETERMINATOR, /* $ -  36 */
546   C2_EUROPETERMINATOR, /* % -  37 */
547   C2_LEFTTORIGHT, /* & -  38 */
548   C2_OTHERNEUTRAL, /* ' -  39 */
549   C2_OTHERNEUTRAL, /* ( -  40 */
550   C2_OTHERNEUTRAL, /* ) -  41 */
551   C2_OTHERNEUTRAL, /* * -  42 */
552   C2_EUROPETERMINATOR, /* + -  43 */
553   C2_COMMONSEPARATOR, /* , -  44 */
554   C2_EUROPETERMINATOR, /* - -  45 */
555   C2_EUROPESEPARATOR, /* . -  46 */
556   C2_EUROPESEPARATOR, /* / -  47 */
557   C2_EUROPENUMBER, /* 0 -  48 */
558   C2_EUROPENUMBER, /* 1 -  49 */
559   C2_EUROPENUMBER, /* 2 -  50 */
560   C2_EUROPENUMBER, /* 3 -  51 */
561   C2_EUROPENUMBER, /* 4 -  52 */
562   C2_EUROPENUMBER, /* 5 -  53 */
563   C2_EUROPENUMBER, /* 6 -  54 */
564   C2_EUROPENUMBER, /* 7 -  55 */
565   C2_EUROPENUMBER, /* 8 -  56 */
566   C2_EUROPENUMBER, /* 9 -  57 */
567   C2_COMMONSEPARATOR, /* : -  58 */
568   C2_OTHERNEUTRAL, /* ; -  59 */
569   C2_OTHERNEUTRAL, /* < -  60 */
570   C2_OTHERNEUTRAL, /* = -  61 */
571   C2_OTHERNEUTRAL, /* > -  62 */
572   C2_OTHERNEUTRAL, /* ? -  63 */
573   C2_LEFTTORIGHT, /* @ -  64 */
574   C2_LEFTTORIGHT, /* A -  65 */
575   C2_LEFTTORIGHT, /* B -  66 */
576   C2_LEFTTORIGHT, /* C -  67 */
577   C2_LEFTTORIGHT, /* D -  68 */
578   C2_LEFTTORIGHT, /* E -  69 */
579   C2_LEFTTORIGHT, /* F -  70 */
580   C2_LEFTTORIGHT, /* G -  71 */
581   C2_LEFTTORIGHT, /* H -  72 */
582   C2_LEFTTORIGHT, /* I -  73 */
583   C2_LEFTTORIGHT, /* J -  74 */
584   C2_LEFTTORIGHT, /* K -  75 */
585   C2_LEFTTORIGHT, /* L -  76 */
586   C2_LEFTTORIGHT, /* M -  77 */
587   C2_LEFTTORIGHT, /* N -  78 */
588   C2_LEFTTORIGHT, /* O -  79 */
589   C2_LEFTTORIGHT, /* P -  80 */
590   C2_LEFTTORIGHT, /* Q -  81 */
591   C2_LEFTTORIGHT, /* R -  82 */
592   C2_LEFTTORIGHT, /* S -  83 */
593   C2_LEFTTORIGHT, /* T -  84 */
594   C2_LEFTTORIGHT, /* U -  85 */
595   C2_LEFTTORIGHT, /* V -  86 */
596   C2_LEFTTORIGHT, /* W -  87 */
597   C2_LEFTTORIGHT, /* X -  88 */
598   C2_LEFTTORIGHT, /* Y -  89 */
599   C2_LEFTTORIGHT, /* Z -  90 */
600   C2_OTHERNEUTRAL, /* [ -  91 */
601   C2_OTHERNEUTRAL, /* \ -  92 */
602   C2_OTHERNEUTRAL, /* ] -  93 */
603   C2_OTHERNEUTRAL, /* ^ -  94 */
604   C2_OTHERNEUTRAL, /* _ -  95 */
605   C2_OTHERNEUTRAL, /* ` -  96 */
606   C2_LEFTTORIGHT, /* a -  97 */
607   C2_LEFTTORIGHT, /* b -  98 */
608   C2_LEFTTORIGHT, /* c -  99 */
609   C2_LEFTTORIGHT, /* d - 100 */
610   C2_LEFTTORIGHT, /* e - 101 */
611   C2_LEFTTORIGHT, /* f - 102 */
612   C2_LEFTTORIGHT, /* g - 103 */
613   C2_LEFTTORIGHT, /* h - 104 */
614   C2_LEFTTORIGHT, /* i - 105 */
615   C2_LEFTTORIGHT, /* j - 106 */
616   C2_LEFTTORIGHT, /* k - 107 */
617   C2_LEFTTORIGHT, /* l - 108 */
618   C2_LEFTTORIGHT, /* m - 109 */
619   C2_LEFTTORIGHT, /* n - 110 */
620   C2_LEFTTORIGHT, /* o - 111 */
621   C2_LEFTTORIGHT, /* p - 112 */
622   C2_LEFTTORIGHT, /* q - 113 */
623   C2_LEFTTORIGHT, /* r - 114 */
624   C2_LEFTTORIGHT, /* s - 115 */
625   C2_LEFTTORIGHT, /* t - 116 */
626   C2_LEFTTORIGHT, /* u - 117 */
627   C2_LEFTTORIGHT, /* v - 118 */
628   C2_LEFTTORIGHT, /* w - 119 */
629   C2_LEFTTORIGHT, /* x - 120 */
630   C2_LEFTTORIGHT, /* y - 121 */
631   C2_LEFTTORIGHT, /* z - 122 */
632   C2_OTHERNEUTRAL, /* { - 123 */
633   C2_OTHERNEUTRAL, /* | - 124 */
634   C2_OTHERNEUTRAL, /* } - 125 */
635   C2_OTHERNEUTRAL, /* ~ - 126 */
636   C2_NOTAPPLICABLE, /* \7f - 127 */
637   C2_NOTAPPLICABLE, /* \80 - 128 */
638   C2_NOTAPPLICABLE, /* \81 - 129 */
639   C2_OTHERNEUTRAL, /* \82 - 130 */
640   C2_LEFTTORIGHT, /* \83 - 131 */
641   C2_OTHERNEUTRAL, /* \84 - 132 */
642   C2_OTHERNEUTRAL, /* \85 - 133 */
643   C2_OTHERNEUTRAL, /* \86 - 134 */
644   C2_OTHERNEUTRAL, /* \87 - 135 */
645   C2_LEFTTORIGHT, /* \88 - 136 */
646   C2_EUROPETERMINATOR, /* \89 - 137 */
647   C2_LEFTTORIGHT, /* \8a - 138 */
648   C2_OTHERNEUTRAL, /* \8b - 139 */
649   C2_LEFTTORIGHT, /* \8c - 140 */
650   C2_NOTAPPLICABLE, /* \8d - 141 */
651   C2_NOTAPPLICABLE, /* \8e - 142 */
652   C2_NOTAPPLICABLE, /* \8f - 143 */
653   C2_NOTAPPLICABLE, /* \90 - 144 */
654   C2_OTHERNEUTRAL, /* \91 - 145 */
655   C2_OTHERNEUTRAL, /* \92 - 146 */
656   C2_OTHERNEUTRAL, /* \93 - 147 */
657   C2_OTHERNEUTRAL, /* \94 - 148 */
658   C2_OTHERNEUTRAL, /* \95 - 149 */
659   C2_OTHERNEUTRAL, /* \96 - 150 */
660   C2_OTHERNEUTRAL, /* \97 - 151 */
661   C2_LEFTTORIGHT, /* \98 - 152 */
662   C2_OTHERNEUTRAL, /* \99 - 153 */
663   C2_LEFTTORIGHT, /* \9a - 154 */
664   C2_OTHERNEUTRAL, /* \9b - 155 */
665   C2_LEFTTORIGHT, /* \9c - 156 */
666   C2_NOTAPPLICABLE, /* \9d - 157 */
667   C2_NOTAPPLICABLE, /* \9e - 158 */
668   C2_LEFTTORIGHT, /* \9f - 159 */
669   C2_WHITESPACE, /*   - 160 */
670   C2_OTHERNEUTRAL, /* ¡ - 161 */
671   C2_EUROPETERMINATOR, /* ¢ - 162 */
672   C2_EUROPETERMINATOR, /* £ - 163 */
673   C2_EUROPETERMINATOR, /* ¤ - 164 */
674   C2_EUROPETERMINATOR, /* ¥ - 165 */
675   C2_OTHERNEUTRAL, /* ¦ - 166 */
676   C2_OTHERNEUTRAL, /* § - 167 */
677   C2_OTHERNEUTRAL, /* ¨ - 168 */
678   C2_OTHERNEUTRAL, /* © - 169 */
679   C2_OTHERNEUTRAL, /* ª - 170 */
680   C2_OTHERNEUTRAL, /* « - 171 */
681   C2_OTHERNEUTRAL, /* ¬ - 172 */
682   C2_OTHERNEUTRAL, /* ­ - 173 */
683   C2_OTHERNEUTRAL, /* ® - 174 */
684   C2_OTHERNEUTRAL, /* ¯ - 175 */
685   C2_EUROPETERMINATOR, /* ° - 176 */
686   C2_EUROPETERMINATOR, /* ± - 177 */
687   C2_EUROPENUMBER, /* ² - 178 */
688   C2_EUROPENUMBER, /* ³ - 179 */
689   C2_OTHERNEUTRAL, /* ´ - 180 */
690   C2_OTHERNEUTRAL, /* µ - 181 */
691   C2_OTHERNEUTRAL, /* ¶ - 182 */
692   C2_OTHERNEUTRAL, /* · - 183 */
693   C2_OTHERNEUTRAL, /* ¸ - 184 */
694   C2_EUROPENUMBER, /* ¹ - 185 */
695   C2_OTHERNEUTRAL, /* º - 186 */
696   C2_OTHERNEUTRAL, /* » - 187 */
697   C2_OTHERNEUTRAL, /* ¼ - 188 */
698   C2_OTHERNEUTRAL, /* ½ - 189 */
699   C2_OTHERNEUTRAL, /* ¾ - 190 */
700   C2_OTHERNEUTRAL, /* ¿ - 191 */
701   C2_LEFTTORIGHT, /* À - 192 */
702   C2_LEFTTORIGHT, /* Á - 193 */
703   C2_LEFTTORIGHT, /* Â - 194 */
704   C2_LEFTTORIGHT, /* Ã - 195 */
705   C2_LEFTTORIGHT, /* Ä - 196 */
706   C2_LEFTTORIGHT, /* Å - 197 */
707   C2_LEFTTORIGHT, /* Æ - 198 */
708   C2_LEFTTORIGHT, /* Ç - 199 */
709   C2_LEFTTORIGHT, /* È - 200 */
710   C2_LEFTTORIGHT, /* É - 201 */
711   C2_LEFTTORIGHT, /* Ê - 202 */
712   C2_LEFTTORIGHT, /* Ë - 203 */
713   C2_LEFTTORIGHT, /* Ì - 204 */
714   C2_LEFTTORIGHT, /* Í - 205 */
715   C2_LEFTTORIGHT, /* Î - 206 */
716   C2_LEFTTORIGHT, /* Ï - 207 */
717   C2_LEFTTORIGHT, /* Ð - 208 */
718   C2_LEFTTORIGHT, /* Ñ - 209 */
719   C2_LEFTTORIGHT, /* Ò - 210 */
720   C2_LEFTTORIGHT, /* Ó - 211 */
721   C2_LEFTTORIGHT, /* Ô - 212 */
722   C2_LEFTTORIGHT, /* Õ - 213 */
723   C2_LEFTTORIGHT, /* Ö - 214 */
724   C2_OTHERNEUTRAL, /* × - 215 */
725   C2_LEFTTORIGHT, /* Ø - 216 */
726   C2_LEFTTORIGHT, /* Ù - 217 */
727   C2_LEFTTORIGHT, /* Ú - 218 */
728   C2_LEFTTORIGHT, /* Û - 219 */
729   C2_LEFTTORIGHT, /* Ü - 220 */
730   C2_LEFTTORIGHT, /* Ý - 221 */
731   C2_LEFTTORIGHT, /* Þ - 222 */
732   C2_LEFTTORIGHT, /* ß - 223 */
733   C2_LEFTTORIGHT, /* à - 224 */
734   C2_LEFTTORIGHT, /* á - 225 */
735   C2_LEFTTORIGHT, /* â - 226 */
736   C2_LEFTTORIGHT, /* ã - 227 */
737   C2_LEFTTORIGHT, /* ä - 228 */
738   C2_LEFTTORIGHT, /* å - 229 */
739   C2_LEFTTORIGHT, /* æ - 230 */
740   C2_LEFTTORIGHT, /* ç - 231 */
741   C2_LEFTTORIGHT, /* è - 232 */
742   C2_LEFTTORIGHT, /* é - 233 */
743   C2_LEFTTORIGHT, /* ê - 234 */
744   C2_LEFTTORIGHT, /* ë - 235 */
745   C2_LEFTTORIGHT, /* ì - 236 */
746   C2_LEFTTORIGHT, /* í - 237 */
747   C2_LEFTTORIGHT, /* î - 238 */
748   C2_LEFTTORIGHT, /* ï - 239 */
749   C2_LEFTTORIGHT, /* ð - 240 */
750   C2_LEFTTORIGHT, /* ñ - 241 */
751   C2_LEFTTORIGHT, /* ò - 242 */
752   C2_LEFTTORIGHT, /* ó - 243 */
753   C2_LEFTTORIGHT, /* ô - 244 */
754   C2_LEFTTORIGHT, /* õ - 245 */
755   C2_LEFTTORIGHT, /* ö - 246 */
756   C2_OTHERNEUTRAL, /* ÷ - 247 */
757   C2_LEFTTORIGHT, /* ø - 248 */
758   C2_LEFTTORIGHT, /* ù - 249 */
759   C2_LEFTTORIGHT, /* ú - 250 */
760   C2_LEFTTORIGHT, /* û - 251 */
761   C2_LEFTTORIGHT, /* ü - 252 */
762   C2_LEFTTORIGHT, /* ý - 253 */
763   C2_LEFTTORIGHT, /* þ - 254 */
764   C2_LEFTTORIGHT /* ÿ - 255 */
765 };
766
767 static const WORD CT_CType3_LUT[] = { 
768   0x0000, /*   -   0 */
769   0x0000, /*   -   1 */
770   0x0000, /*   -   2 */
771   0x0000, /*   -   3 */
772   0x0000, /*   -   4 */
773   0x0000, /*   -   5 */
774   0x0000, /*   -   6 */
775   0x0000, /*   -   7 */
776   0x0000, /*   -   8 */
777   0x0008, /*   -   9 */
778   0x0008, /*   -  10 */
779   0x0008, /*   -  11 */
780   0x0008, /*   -  12 */
781   0x0008, /*   -  13 */
782   0x0000, /*   -  14 */
783   0x0000, /*   -  15 */
784   0x0000, /*   -  16 */
785   0x0000, /*   -  17 */
786   0x0000, /*   -  18 */
787   0x0000, /*   -  19 */
788   0x0000, /*   -  20 */
789   0x0000, /*   -  21 */
790   0x0000, /*   -  22 */
791   0x0000, /*   -  23 */
792   0x0000, /*   -  24 */
793   0x0000, /*   -  25 */
794   0x0000, /*   -  26 */
795   0x0000, /*   -  27 */
796   0x0000, /*   -  28 */
797   0x0000, /*   -  29 */
798   0x0000, /*   -  30 */
799   0x0000, /*   -  31 */
800   0x0048, /*   -  32 */
801   0x0048, /* ! -  33 */
802   0x0448, /* " -  34 */ /* " */
803   0x0048, /* # -  35 */
804   0x0448, /* $ -  36 */
805   0x0048, /* % -  37 */
806   0x0048, /* & -  38 */
807   0x0440, /* ' -  39 */
808   0x0048, /* ( -  40 */
809   0x0048, /* ) -  41 */
810   0x0048, /* * -  42 */
811   0x0048, /* + -  43 */
812   0x0048, /* , -  44 */
813   0x0440, /* - -  45 */
814   0x0048, /* . -  46 */
815   0x0448, /* / -  47 */
816   0x0040, /* 0 -  48 */
817   0x0040, /* 1 -  49 */
818   0x0040, /* 2 -  50 */
819   0x0040, /* 3 -  51 */
820   0x0040, /* 4 -  52 */
821   0x0040, /* 5 -  53 */
822   0x0040, /* 6 -  54 */
823   0x0040, /* 7 -  55 */
824   0x0040, /* 8 -  56 */
825   0x0040, /* 9 -  57 */
826   0x0048, /* : -  58 */
827   0x0048, /* ; -  59 */
828   0x0048, /* < -  60 */
829   0x0448, /* = -  61 */
830   0x0048, /* > -  62 */
831   0x0048, /* ? -  63 */
832   0x0448, /* @ -  64 */
833   0x8040, /* A -  65 */
834   0x8040, /* B -  66 */
835   0x8040, /* C -  67 */
836   0x8040, /* D -  68 */
837   0x8040, /* E -  69 */
838   0x8040, /* F -  70 */
839   0x8040, /* G -  71 */
840   0x8040, /* H -  72 */
841   0x8040, /* I -  73 */
842   0x8040, /* J -  74 */
843   0x8040, /* K -  75 */
844   0x8040, /* L -  76 */
845   0x8040, /* M -  77 */
846   0x8040, /* N -  78 */
847   0x8040, /* O -  79 */
848   0x8040, /* P -  80 */
849   0x8040, /* Q -  81 */
850   0x8040, /* R -  82 */
851   0x8040, /* S -  83 */
852   0x8040, /* T -  84 */
853   0x8040, /* U -  85 */
854   0x8040, /* V -  86 */
855   0x8040, /* W -  87 */
856   0x8040, /* X -  88 */
857   0x8040, /* Y -  89 */
858   0x8040, /* Z -  90 */
859   0x0048, /* [ -  91 */
860   0x0448, /* \ -  92 */
861   0x0048, /* ] -  93 */
862   0x0448, /* ^ -  94 */
863   0x0448, /* _ -  95 */
864   0x0448, /* ` -  96 */
865   0x8040, /* a -  97 */
866   0x8040, /* b -  98 */
867   0x8040, /* c -  99 */
868   0x8040, /* d - 100 */
869   0x8040, /* e - 101 */
870   0x8040, /* f - 102 */
871   0x8040, /* g - 103 */
872   0x8040, /* h - 104 */
873   0x8040, /* i - 105 */
874   0x8040, /* j - 106 */
875   0x8040, /* k - 107 */
876   0x8040, /* l - 108 */
877   0x8040, /* m - 109 */
878   0x8040, /* n - 110 */
879   0x8040, /* o - 111 */
880   0x8040, /* p - 112 */
881   0x8040, /* q - 113 */
882   0x8040, /* r - 114 */
883   0x8040, /* s - 115 */
884   0x8040, /* t - 116 */
885   0x8040, /* u - 117 */
886   0x8040, /* v - 118 */
887   0x8040, /* w - 119 */
888   0x8040, /* x - 120 */
889   0x8040, /* y - 121 */
890   0x8040, /* z - 122 */
891   0x0048, /* { - 123 */
892   0x0048, /* | - 124 */
893   0x0048, /* } - 125 */
894   0x0448, /* ~ - 126 */
895   0x0000, /* \7f - 127 */
896   0x0000, /* \80 - 128 */
897   0x0000, /* \81 - 129 */
898   0x0008, /* \82 - 130 */
899   0x8000, /* \83 - 131 */
900   0x0008, /* \84 - 132 */
901   0x0008, /* \85 - 133 */
902   0x0008, /* \86 - 134 */
903   0x0008, /* \87 - 135 */
904   0x0001, /* \88 - 136 */
905   0x0008, /* \89 - 137 */
906   0x8003, /* \8a - 138 */
907   0x0008, /* \8b - 139 */
908   0x8000, /* \8c - 140 */
909   0x0000, /* \8d - 141 */
910   0x0000, /* \8e - 142 */
911   0x0000, /* \8f - 143 */
912   0x0000, /* \90 - 144 */
913   0x0088, /* \91 - 145 */
914   0x0088, /* \92 - 146 */
915   0x0088, /* \93 - 147 */
916   0x0088, /* \94 - 148 */
917   0x0008, /* \95 - 149 */
918   0x0400, /* \96 - 150 */
919   0x0400, /* \97 - 151 */
920   0x0408, /* \98 - 152 */
921   0x0000, /* \99 - 153 */
922   0x8003, /* \9a - 154 */
923   0x0008, /* \9b - 155 */
924   0x8000, /* \9c - 156 */
925   0x0000, /* \9d - 157 */
926   0x0000, /* \9e - 158 */
927   0x8003, /* \9f - 159 */
928   0x0008, /*   - 160 */
929   0x0008, /* ¡ - 161 */
930   0x0048, /* ¢ - 162 */
931   0x0048, /* £ - 163 */
932   0x0008, /* ¤ - 164 */
933   0x0048, /* ¥ - 165 */
934   0x0048, /* ¦ - 166 */
935   0x0008, /* § - 167 */
936   0x0408, /* ¨ - 168 */
937   0x0008, /* © - 169 */
938   0x0400, /* ª - 170 */
939   0x0008, /* « - 171 */
940   0x0048, /* ¬ - 172 */
941   0x0408, /* ­ - 173 */
942   0x0008, /* ® - 174 */
943   0x0448, /* ¯ - 175 */
944   0x0008, /* ° - 176 */
945   0x0008, /* ± - 177 */
946   0x0000, /* ² - 178 */
947   0x0000, /* ³ - 179 */
948   0x0408, /* ´ - 180 */
949   0x0008, /* µ - 181 */
950   0x0008, /* ¶ - 182 */
951   0x0008, /* · - 183 */
952   0x0408, /* ¸ - 184 */
953   0x0000, /* ¹ - 185 */
954   0x0400, /* º - 186 */
955   0x0008, /* » - 187 */
956   0x0000, /* ¼ - 188 */
957   0x0000, /* ½ - 189 */
958   0x0000, /* ¾ - 190 */
959   0x0008, /* ¿ - 191 */
960   0x8003, /* À - 192 */
961   0x8003, /* Á - 193 */
962   0x8003, /* Â - 194 */
963   0x8003, /* Ã - 195 */
964   0x8003, /* Ä - 196 */
965   0x8003, /* Å - 197 */
966   0x8000, /* Æ - 198 */
967   0x8003, /* Ç - 199 */
968   0x8003, /* È - 200 */
969   0x8003, /* É - 201 */
970   0x8003, /* Ê - 202 */
971   0x8003, /* Ë - 203 */
972   0x8003, /* Ì - 204 */
973   0x8003, /* Í - 205 */
974   0x8003, /* Î - 206 */
975   0x8003, /* Ï - 207 */
976   0x8000, /* Ð - 208 */
977   0x8003, /* Ñ - 209 */
978   0x8003, /* Ò - 210 */
979   0x8003, /* Ó - 211 */
980   0x8003, /* Ô - 212 */
981   0x8003, /* Õ - 213 */
982   0x8003, /* Ö - 214 */
983   0x0008, /* × - 215 */
984   0x8003, /* Ø - 216 */
985   0x8003, /* Ù - 217 */
986   0x8003, /* Ú - 218 */
987   0x8003, /* Û - 219 */
988   0x8003, /* Ü - 220 */
989   0x8003, /* Ý - 221 */
990   0x8000, /* Þ - 222 */
991   0x8000, /* ß - 223 */
992   0x8003, /* à - 224 */
993   0x8003, /* á - 225 */
994   0x8003, /* â - 226 */
995   0x8003, /* ã - 227 */
996   0x8003, /* ä - 228 */
997   0x8003, /* å - 229 */
998   0x8000, /* æ - 230 */
999   0x8003, /* ç - 231 */
1000   0x8003, /* è - 232 */
1001   0x8003, /* é - 233 */
1002   0x8003, /* ê - 234 */
1003   0x8003, /* ë - 235 */
1004   0x8003, /* ì - 236 */
1005   0x8003, /* í - 237 */
1006   0x8003, /* î - 238 */
1007   0x8003, /* ï - 239 */
1008   0x8000, /* ð - 240 */
1009   0x8003, /* ñ - 241 */
1010   0x8003, /* ò - 242 */
1011   0x8003, /* ó - 243 */
1012   0x8003, /* ô - 244 */
1013   0x8003, /* õ - 245 */
1014   0x8003, /* ö - 246 */
1015   0x0008, /* ÷ - 247 */
1016   0x8003, /* ø - 248 */
1017   0x8003, /* ù - 249 */
1018   0x8003, /* ú - 250 */
1019   0x8003, /* û - 251 */
1020   0x8003, /* ü - 252 */
1021   0x8003, /* ý - 253 */
1022   0x8000, /* þ - 254 */
1023   0x8003  /* ÿ - 255 */
1024 };
1025
1026 /***********************************************************************
1027  *              GetStringTypeA                [OLE2NLS.7]
1028  */
1029 BOOL16 WINAPI GetStringType16(LCID locale,DWORD dwInfoType,LPCSTR src,
1030                               INT16 cchSrc,LPWORD chartype)
1031 {
1032         return GetStringTypeEx32A(locale,dwInfoType,src,cchSrc,chartype);
1033 }
1034 /***********************************************************************
1035  *              GetStringTypeA                [KERNEL32.277]
1036  */
1037 BOOL32 WINAPI GetStringType32A(LCID locale,DWORD dwInfoType,LPCSTR src,
1038                                INT32 cchSrc,LPWORD chartype)
1039 {
1040         return GetStringTypeEx32A(locale,dwInfoType,src,cchSrc,chartype);
1041 }
1042
1043 /***********************************************************************
1044  *              GetStringTypeExA                [KERNEL32.276]
1045  *
1046  * FIXME: Ignores the locale.
1047  */
1048 BOOL32 WINAPI GetStringTypeEx32A(LCID locale,DWORD dwInfoType,LPCSTR src,
1049                                  INT32 cchSrc,LPWORD chartype)
1050 {
1051         int     i;
1052         
1053         if ((src==NULL) || (chartype==NULL) || (src==(LPSTR)chartype))
1054         {
1055           SetLastError(ERROR_INVALID_PARAMETER);
1056           return FALSE;
1057         }
1058
1059         if (cchSrc==-1)
1060           cchSrc=lstrlen32A(src)+1;
1061           
1062         switch (dwInfoType) {
1063         case CT_CTYPE1:
1064           for (i=0;i<cchSrc;i++) 
1065           {
1066             chartype[i] = 0;
1067             if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1068             if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1069             if (islower(src[i])) chartype[i]|=C1_LOWER;
1070             if (isupper(src[i])) chartype[i]|=C1_UPPER;
1071             if (isspace(src[i])) chartype[i]|=C1_SPACE;
1072             if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1073             if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1074 /* FIXME: isblank() is a GNU extension */
1075 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1076             if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1077             /* C1_XDIGIT */
1078         }
1079         return TRUE;
1080
1081         case CT_CTYPE2:
1082           for (i=0;i<cchSrc;i++) 
1083           {
1084             chartype[i]=(WORD)CT_CType2_LUT[i];
1085           }
1086           return TRUE;
1087
1088         case CT_CTYPE3:
1089           for (i=0;i<cchSrc;i++) 
1090           {
1091             chartype[i]=CT_CType3_LUT[i];
1092           }
1093           return TRUE;
1094
1095         default:
1096           ERR(ole,"Unknown dwInfoType:%ld\n",dwInfoType);
1097           return FALSE;
1098         }
1099 }
1100
1101 /*********************************************************************
1102  *  GetStringTypeW [KERNEL32.279]
1103  *
1104  * NOTES
1105  * Yes, this is missing LCID locale. MS fault.
1106  */
1107 BOOL32 WINAPI GetStringType32W(DWORD dwInfoType,LPCWSTR src,INT32 cchSrc,
1108                                LPWORD chartype)
1109 {
1110         return GetStringTypeEx32W(0/*defaultlocale*/,dwInfoType,src,cchSrc,chartype);
1111 }
1112
1113 /*********************************************************************
1114  *   GetStringTypeW   [KERNEL32.278]
1115  * FIXME: unicode chars are assumed chars
1116  */
1117 BOOL32 WINAPI GetStringTypeEx32W(LCID locale,DWORD dwInfoType,LPCWSTR src,
1118                                  INT32 cchSrc,LPWORD chartype)
1119 {
1120         int     i;
1121
1122
1123         if (cchSrc==-1)
1124           cchSrc=lstrlen32W(src)+1;
1125         
1126         switch (dwInfoType) {
1127         case CT_CTYPE2:
1128                 FIXME(ole,"CT_CTYPE2 not supported.\n");
1129                 return FALSE;
1130         case CT_CTYPE3:
1131                 FIXME(ole,"CT_CTYPE3 not supported.\n");
1132                 return FALSE;
1133         default:break;
1134         }
1135         for (i=0;i<cchSrc;i++) {
1136                 chartype[i] = 0;
1137                 if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1138                 if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1139                 if (islower(src[i])) chartype[i]|=C1_LOWER;
1140                 if (isupper(src[i])) chartype[i]|=C1_UPPER;
1141                 if (isspace(src[i])) chartype[i]|=C1_SPACE;
1142                 if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1143                 if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1144 /* FIXME: isblank() is a GNU extension */
1145 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1146                 if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1147                 /* C1_XDIGIT */
1148         }
1149         return TRUE;
1150 }
1151
1152 /*****************************************************************
1153  * VerLanguageName16   [VER.10] 
1154  */
1155 DWORD WINAPI VerLanguageName16(UINT16 langid,LPSTR langname,UINT16 langnamelen)
1156 {
1157         int     i;
1158         DWORD   result;
1159         char    buffer[80];
1160
1161         TRACE(ver,"(%d,%p,%d)\n",langid,langname,langnamelen);
1162         /* First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
1163          * from the registry. 
1164          */
1165         sprintf(buffer,
1166                 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
1167                 langid);
1168         result = RegQueryValue16(HKEY_LOCAL_MACHINE, buffer, langname,
1169                                 (LPDWORD)&langnamelen);
1170         if (result == ERROR_SUCCESS) {
1171                 langname[langnamelen-1]='\0';
1172                 return langnamelen;
1173         }
1174         /* if that fails, use the internal table */
1175         for (i=0;languages[i].langid!=0;i++)
1176                 if (langid==languages[i].langid)
1177                         break;
1178         strncpy(langname,languages[i].langname,langnamelen);
1179         langname[langnamelen-1]='\0';
1180         return strlen(languages[i].langname);
1181 }
1182
1183 /*****************************************************************
1184  * VerLanguageName32A                           [VERSION.9] 
1185  */
1186 DWORD WINAPI VerLanguageName32A(UINT32 langid,LPSTR langname,
1187                                 UINT32 langnamelen)
1188 {
1189         return VerLanguageName16(langid,langname,langnamelen);
1190 }
1191
1192 /*****************************************************************
1193  * VerLanguageName32W                           [VERSION.10] 
1194  */
1195 DWORD WINAPI VerLanguageName32W(UINT32 langid,LPWSTR langname,
1196                                 UINT32 langnamelen)
1197 {
1198         int     i;
1199         LPWSTR  keyname;
1200         DWORD   result;
1201         char    buffer[80];
1202
1203         /* First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
1204          * from the registry. 
1205          */
1206         sprintf(buffer,
1207                 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
1208                 langid);
1209         keyname = HEAP_strdupAtoW( GetProcessHeap(), 0, buffer );
1210         result = RegQueryValue32W(HKEY_LOCAL_MACHINE, keyname, langname,
1211                                 (LPDWORD)&langnamelen);
1212         HeapFree( GetProcessHeap(), 0, keyname );
1213         if (result != ERROR_SUCCESS) {
1214                 /* if that fails, use the internal table */
1215                 for (i=0;languages[i].langid!=0;i++)
1216                         if (langid==languages[i].langid)
1217                                 break;
1218                 lstrcpyAtoW( langname, languages[i].langname );
1219                 langnamelen = strlen(languages[i].langname);
1220                 /* same as strlenW(langname); */
1221         }
1222         return langnamelen;
1223 }
1224  
1225 static const unsigned char LCM_Unicode_LUT[] = {
1226   6      ,   3, /*   -   1 */  
1227   6      ,   4, /*   -   2 */  
1228   6      ,   5, /*   -   3 */  
1229   6      ,   6, /*   -   4 */  
1230   6      ,   7, /*   -   5 */  
1231   6      ,   8, /*   -   6 */  
1232   6      ,   9, /*   -   7 */  
1233   6      ,  10, /*   -   8 */  
1234   7      ,   5, /*   -   9 */  
1235   7      ,   6, /*   -  10 */  
1236   7      ,   7, /*   -  11 */  
1237   7      ,   8, /*   -  12 */  
1238   7      ,   9, /*   -  13 */  
1239   6      ,  11, /*   -  14 */  
1240   6      ,  12, /*   -  15 */  
1241   6      ,  13, /*   -  16 */  
1242   6      ,  14, /*   -  17 */  
1243   6      ,  15, /*   -  18 */  
1244   6      ,  16, /*   -  19 */  
1245   6      ,  17, /*   -  20 */  
1246   6      ,  18, /*   -  21 */  
1247   6      ,  19, /*   -  22 */  
1248   6      ,  20, /*   -  23 */  
1249   6      ,  21, /*   -  24 */  
1250   6      ,  22, /*   -  25 */  
1251   6      ,  23, /*   -  26 */  
1252   6      ,  24, /*   -  27 */  
1253   6      ,  25, /*   -  28 */  
1254   6      ,  26, /*   -  29 */  
1255   6      ,  27, /*   -  30 */  
1256   6      ,  28, /*   -  31 */  
1257   7      ,   2, /*   -  32 */
1258   7      ,  28, /* ! -  33 */
1259   7      ,  29, /* " -  34 */ /* " */
1260   7      ,  31, /* # -  35 */
1261   7      ,  33, /* $ -  36 */
1262   7      ,  35, /* % -  37 */
1263   7      ,  37, /* & -  38 */
1264   6      , 128, /* ' -  39 */
1265   7      ,  39, /* ( -  40 */
1266   7      ,  42, /* ) -  41 */
1267   7      ,  45, /* * -  42 */
1268   8      ,   3, /* + -  43 */
1269   7      ,  47, /* , -  44 */
1270   6      , 130, /* - -  45 */
1271   7      ,  51, /* . -  46 */
1272   7      ,  53, /* / -  47 */
1273  12      ,   3, /* 0 -  48 */
1274  12      ,  33, /* 1 -  49 */
1275  12      ,  51, /* 2 -  50 */
1276  12      ,  70, /* 3 -  51 */
1277  12      ,  88, /* 4 -  52 */
1278  12      , 106, /* 5 -  53 */
1279  12      , 125, /* 6 -  54 */
1280  12      , 144, /* 7 -  55 */
1281  12      , 162, /* 8 -  56 */
1282  12      , 180, /* 9 -  57 */
1283   7      ,  55, /* : -  58 */
1284   7      ,  58, /* ; -  59 */
1285   8      ,  14, /* < -  60 */
1286   8      ,  18, /* = -  61 */
1287   8      ,  20, /* > -  62 */
1288   7      ,  60, /* ? -  63 */
1289   7      ,  62, /* @ -  64 */
1290  14      ,   2, /* A -  65 */
1291  14      ,   9, /* B -  66 */
1292  14      ,  10, /* C -  67 */
1293  14      ,  26, /* D -  68 */
1294  14      ,  33, /* E -  69 */
1295  14      ,  35, /* F -  70 */
1296  14      ,  37, /* G -  71 */
1297  14      ,  44, /* H -  72 */
1298  14      ,  50, /* I -  73 */
1299  14      ,  53, /* J -  74 */
1300  14      ,  54, /* K -  75 */
1301  14      ,  72, /* L -  76 */
1302  14      ,  81, /* M -  77 */
1303  14      , 112, /* N -  78 */
1304  14      , 124, /* O -  79 */
1305  14      , 126, /* P -  80 */
1306  14      , 137, /* Q -  81 */
1307  14      , 138, /* R -  82 */
1308  14      , 145, /* S -  83 */
1309  14      , 153, /* T -  84 */
1310  14      , 159, /* U -  85 */
1311  14      , 162, /* V -  86 */
1312  14      , 164, /* W -  87 */
1313  14      , 166, /* X -  88 */
1314  14      , 167, /* Y -  89 */
1315  14      , 169, /* Z -  90 */
1316   7      ,  63, /* [ -  91 */
1317   7      ,  65, /* \ -  92 */
1318   7      ,  66, /* ] -  93 */
1319   7      ,  67, /* ^ -  94 */
1320   7      ,  68, /* _ -  95 */
1321   7      ,  72, /* ` -  96 */
1322  14      ,   2, /* a -  97 */
1323  14      ,   9, /* b -  98 */
1324  14      ,  10, /* c -  99 */
1325  14      ,  26, /* d - 100 */
1326  14      ,  33, /* e - 101 */
1327  14      ,  35, /* f - 102 */
1328  14      ,  37, /* g - 103 */
1329  14      ,  44, /* h - 104 */
1330  14      ,  50, /* i - 105 */
1331  14      ,  53, /* j - 106 */
1332  14      ,  54, /* k - 107 */
1333  14      ,  72, /* l - 108 */
1334  14      ,  81, /* m - 109 */
1335  14      , 112, /* n - 110 */
1336  14      , 124, /* o - 111 */
1337  14      , 126, /* p - 112 */
1338  14      , 137, /* q - 113 */
1339  14      , 138, /* r - 114 */
1340  14      , 145, /* s - 115 */
1341  14      , 153, /* t - 116 */
1342  14      , 159, /* u - 117 */
1343  14      , 162, /* v - 118 */
1344  14      , 164, /* w - 119 */
1345  14      , 166, /* x - 120 */
1346  14      , 167, /* y - 121 */
1347  14      , 169, /* z - 122 */
1348   7      ,  74, /* { - 123 */
1349   7      ,  76, /* | - 124 */
1350   7      ,  78, /* } - 125 */
1351   7      ,  80, /* ~ - 126 */
1352   6      ,  29, /* \7f - 127 */
1353   6      ,  30, /* \80 - 128 */
1354   6      ,  31, /* \81 - 129 */
1355   7      , 123, /* \82 - 130 */
1356  14      ,  35, /* \83 - 131 */
1357   7      , 127, /* \84 - 132 */
1358  10      ,  21, /* \85 - 133 */
1359  10      ,  15, /* \86 - 134 */
1360  10      ,  16, /* \87 - 135 */
1361   7      ,  67, /* \88 - 136 */
1362  10      ,  22, /* \89 - 137 */
1363  14      , 145, /* \8a - 138 */
1364   7      , 136, /* \8b - 139 */
1365  14 + 16 , 124, /* \8c - 140 */
1366   6      ,  43, /* \8d - 141 */
1367   6      ,  44, /* \8e - 142 */
1368   6      ,  45, /* \8f - 143 */
1369   6      ,  46, /* \90 - 144 */
1370   7      , 121, /* \91 - 145 */
1371   7      , 122, /* \92 - 146 */
1372   7      , 125, /* \93 - 147 */
1373   7      , 126, /* \94 - 148 */
1374  10      ,  17, /* \95 - 149 */
1375   6      , 137, /* \96 - 150 */
1376   6      , 139, /* \97 - 151 */
1377   7      ,  93, /* \98 - 152 */
1378  14      , 156, /* \99 - 153 */
1379  14      , 145, /* \9a - 154 */
1380   7      , 137, /* \9b - 155 */
1381  14 + 16 , 124, /* \9c - 156 */
1382   6      ,  59, /* \9d - 157 */
1383   6      ,  60, /* \9e - 158 */
1384  14      , 167, /* \9f - 159 */
1385   7      ,   4, /*   - 160 */
1386   7      ,  81, /* ¡ - 161 */
1387  10      ,   2, /* ¢ - 162 */
1388  10      ,   3, /* £ - 163 */
1389  10      ,   4, /* ¤ - 164 */
1390  10      ,   5, /* ¥ - 165 */
1391   7      ,  82, /* ¦ - 166 */
1392  10      ,   6, /* § - 167 */
1393   7      ,  83, /* ¨ - 168 */
1394  10      ,   7, /* © - 169 */
1395  14      ,   2, /* ª - 170 */
1396   8      ,  24, /* « - 171 */
1397  10      ,   8, /* ¬ - 172 */
1398   6      , 131, /* ­ - 173 */
1399  10      ,   9, /* ® - 174 */
1400   7      ,  84, /* ¯ - 175 */
1401  10      ,  10, /* ° - 176 */
1402   8      ,  23, /* ± - 177 */
1403  12      ,  51, /* ² - 178 */
1404  12      ,  70, /* ³ - 179 */
1405   7      ,  85, /* ´ - 180 */
1406  10      ,  11, /* µ - 181 */
1407  10      ,  12, /* ¶ - 182 */
1408  10      ,  13, /* · - 183 */
1409   7      ,  86, /* ¸ - 184 */
1410  12      ,  33, /* ¹ - 185 */
1411  14      , 124, /* º - 186 */
1412   8      ,  26, /* » - 187 */
1413  12      ,  21, /* ¼ - 188 */
1414  12      ,  25, /* ½ - 189 */
1415  12      ,  29, /* ¾ - 190 */
1416   7      ,  87, /* ¿ - 191 */
1417  14      ,   2, /* À - 192 */
1418  14      ,   2, /* Á - 193 */
1419  14      ,   2, /* Â - 194 */
1420  14      ,   2, /* Ã - 195 */
1421  14      ,   2, /* Ä - 196 */
1422  14      ,   2, /* Å - 197 */
1423  14 + 16 ,   2, /* Æ - 198 */
1424  14      ,  10, /* Ç - 199 */
1425  14      ,  33, /* È - 200 */
1426  14      ,  33, /* É - 201 */
1427  14      ,  33, /* Ê - 202 */
1428  14      ,  33, /* Ë - 203 */
1429  14      ,  50, /* Ì - 204 */
1430  14      ,  50, /* Í - 205 */
1431  14      ,  50, /* Î - 206 */
1432  14      ,  50, /* Ï - 207 */
1433  14      ,  26, /* Ð - 208 */
1434  14      , 112, /* Ñ - 209 */
1435  14      , 124, /* Ò - 210 */
1436  14      , 124, /* Ó - 211 */
1437  14      , 124, /* Ô - 212 */
1438  14      , 124, /* Õ - 213 */
1439  14      , 124, /* Ö - 214 */
1440   8      ,  28, /* × - 215 */
1441  14      , 124, /* Ø - 216 */
1442  14      , 159, /* Ù - 217 */
1443  14      , 159, /* Ú - 218 */
1444  14      , 159, /* Û - 219 */
1445  14      , 159, /* Ü - 220 */
1446  14      , 167, /* Ý - 221 */
1447  14 + 32 , 153, /* Þ - 222 */
1448  14 + 48 , 145, /* ß - 223 */
1449  14      ,   2, /* à - 224 */
1450  14      ,   2, /* á - 225 */
1451  14      ,   2, /* â - 226 */
1452  14      ,   2, /* ã - 227 */
1453  14      ,   2, /* ä - 228 */
1454  14      ,   2, /* å - 229 */
1455  14 + 16 ,   2, /* æ - 230 */
1456  14      ,  10, /* ç - 231 */
1457  14      ,  33, /* è - 232 */
1458  14      ,  33, /* é - 233 */
1459  14      ,  33, /* ê - 234 */
1460  14      ,  33, /* ë - 235 */
1461  14      ,  50, /* ì - 236 */
1462  14      ,  50, /* í - 237 */
1463  14      ,  50, /* î - 238 */
1464  14      ,  50, /* ï - 239 */
1465  14      ,  26, /* ð - 240 */
1466  14      , 112, /* ñ - 241 */
1467  14      , 124, /* ò - 242 */
1468  14      , 124, /* ó - 243 */
1469  14      , 124, /* ô - 244 */
1470  14      , 124, /* õ - 245 */
1471  14      , 124, /* ö - 246 */
1472   8      ,  29, /* ÷ - 247 */
1473  14      , 124, /* ø - 248 */
1474  14      , 159, /* ù - 249 */
1475  14      , 159, /* ú - 250 */
1476  14      , 159, /* û - 251 */
1477  14      , 159, /* ü - 252 */
1478  14      , 167, /* ý - 253 */
1479  14 + 32 , 153, /* þ - 254 */
1480  14      , 167  /* ÿ - 255 */ };
1481
1482 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1483
1484 #define LCM_Diacritic_Start 131
1485
1486 static const unsigned char LCM_Diacritic_LUT[] = { 
1487 123,  /* \83 - 131 */
1488   2,  /* \84 - 132 */
1489   2,  /* \85 - 133 */
1490   2,  /* \86 - 134 */
1491   2,  /* \87 - 135 */
1492   3,  /* \88 - 136 */
1493   2,  /* \89 - 137 */
1494  20,  /* \8a - 138 */
1495   2,  /* \8b - 139 */
1496   2,  /* \8c - 140 */
1497   2,  /* \8d - 141 */
1498   2,  /* \8e - 142 */
1499   2,  /* \8f - 143 */
1500   2,  /* \90 - 144 */
1501   2,  /* \91 - 145 */
1502   2,  /* \92 - 146 */
1503   2,  /* \93 - 147 */
1504   2,  /* \94 - 148 */
1505   2,  /* \95 - 149 */
1506   2,  /* \96 - 150 */
1507   2,  /* \97 - 151 */
1508   2,  /* \98 - 152 */
1509   2,  /* \99 - 153 */
1510  20,  /* \9a - 154 */
1511   2,  /* \9b - 155 */
1512   2,  /* \9c - 156 */
1513   2,  /* \9d - 157 */
1514   2,  /* \9e - 158 */
1515  19,  /* \9f - 159 */
1516   2,  /*   - 160 */
1517   2,  /* ¡ - 161 */
1518   2,  /* ¢ - 162 */
1519   2,  /* £ - 163 */
1520   2,  /* ¤ - 164 */
1521   2,  /* ¥ - 165 */
1522   2,  /* ¦ - 166 */
1523   2,  /* § - 167 */
1524   2,  /* ¨ - 168 */
1525   2,  /* © - 169 */
1526   3,  /* ª - 170 */
1527   2,  /* « - 171 */
1528   2,  /* ¬ - 172 */
1529   2,  /* ­ - 173 */
1530   2,  /* ® - 174 */
1531   2,  /* ¯ - 175 */
1532   2,  /* ° - 176 */
1533   2,  /* ± - 177 */
1534   2,  /* ² - 178 */
1535   2,  /* ³ - 179 */
1536   2,  /* ´ - 180 */
1537   2,  /* µ - 181 */
1538   2,  /* ¶ - 182 */
1539   2,  /* · - 183 */
1540   2,  /* ¸ - 184 */
1541   2,  /* ¹ - 185 */
1542   3,  /* º - 186 */
1543   2,  /* » - 187 */
1544   2,  /* ¼ - 188 */
1545   2,  /* ½ - 189 */
1546   2,  /* ¾ - 190 */
1547   2,  /* ¿ - 191 */
1548  15,  /* À - 192 */
1549  14,  /* Á - 193 */
1550  18,  /* Â - 194 */
1551  25,  /* Ã - 195 */
1552  19,  /* Ä - 196 */
1553  26,  /* Å - 197 */
1554   2,  /* Æ - 198 */
1555  28,  /* Ç - 199 */
1556  15,  /* È - 200 */
1557  14,  /* É - 201 */
1558  18,  /* Ê - 202 */
1559  19,  /* Ë - 203 */
1560  15,  /* Ì - 204 */
1561  14,  /* Í - 205 */
1562  18,  /* Î - 206 */
1563  19,  /* Ï - 207 */
1564 104,  /* Ð - 208 */
1565  25,  /* Ñ - 209 */
1566  15,  /* Ò - 210 */
1567  14,  /* Ó - 211 */
1568  18,  /* Ô - 212 */
1569  25,  /* Õ - 213 */
1570  19,  /* Ö - 214 */
1571   2,  /* × - 215 */
1572  33,  /* Ø - 216 */
1573  15,  /* Ù - 217 */
1574  14,  /* Ú - 218 */
1575  18,  /* Û - 219 */
1576  19,  /* Ü - 220 */
1577  14,  /* Ý - 221 */
1578   2,  /* Þ - 222 */
1579   2,  /* ß - 223 */
1580  15,  /* à - 224 */
1581  14,  /* á - 225 */
1582  18,  /* â - 226 */
1583  25,  /* ã - 227 */
1584  19,  /* ä - 228 */
1585  26,  /* å - 229 */
1586   2,  /* æ - 230 */
1587  28,  /* ç - 231 */
1588  15,  /* è - 232 */
1589  14,  /* é - 233 */
1590  18,  /* ê - 234 */
1591  19,  /* ë - 235 */
1592  15,  /* ì - 236 */
1593  14,  /* í - 237 */
1594  18,  /* î - 238 */
1595  19,  /* ï - 239 */
1596 104,  /* ð - 240 */
1597  25,  /* ñ - 241 */
1598  15,  /* ò - 242 */
1599  14,  /* ó - 243 */
1600  18,  /* ô - 244 */
1601  25,  /* õ - 245 */
1602  19,  /* ö - 246 */
1603   2,  /* ÷ - 247 */
1604  33,  /* ø - 248 */
1605  15,  /* ù - 249 */
1606  14,  /* ú - 250 */
1607  18,  /* û - 251 */
1608  19,  /* ü - 252 */
1609  14,  /* ý - 253 */
1610   2,  /* þ - 254 */
1611  19,  /* ÿ - 255 */
1612 } ;
1613
1614 static int is_punctuation(unsigned char c) 
1615 {
1616   /* "punctuation character" in this context is a character which is 
1617      considered "less important" during word sort comparison.
1618      See LCMapString implementation for the precise definition 
1619      of "less important". */
1620
1621   return (LCM_Unicode_LUT[-2+2*c]==6);
1622 }
1623
1624 static int identity(int c)
1625 {
1626   return c;
1627 }
1628
1629 /*************************************************************************
1630  *              LCMapString32A                [KERNEL32.492]
1631  *
1632  * Convert a string, or generate a sort key from it.
1633  *
1634  * If (mapflags & LCMAP_SORTKEY), the function will generate
1635  * a sort key for the source string.  Else, it will convert it
1636  * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1637  *
1638  * RETURNS
1639  *    Error : 0.
1640  *    Success : length of the result string.
1641  *
1642  * REMARKS
1643  *    If called with scrlen = -1, the function will compute the length
1644  *      of the 0-terminated string strsrc by itself.      
1645  * 
1646  *    If called with dstlen = 0, returns the buffer length that 
1647  *      would be required.
1648  */
1649 INT32 WINAPI LCMapString32A(
1650         LCID lcid /* locale identifier created with MAKELCID; 
1651                      LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are predefined
1652                      values. */,
1653         DWORD mapflags /* flags */,
1654         LPCSTR srcstr  /* source buffer */,
1655         INT32 srclen   /* source length */,
1656         LPSTR dststr   /* destination buffer */,
1657         INT32 dstlen   /* destination buffer length */) 
1658 {
1659   int i;
1660
1661   TRACE(string,"(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1662         lcid,mapflags,srcstr,srclen,dststr,dstlen);
1663
1664   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1665   {
1666     SetLastError(ERROR_INVALID_PARAMETER);
1667     return 0;
1668   }
1669   if (srclen==-1) 
1670     srclen = lstrlen32A(srcstr) + 1 ;    /* (include final '\0') */
1671
1672   if (mapflags & ~ ( LCMAP_UPPERCASE | LCMAP_LOWERCASE | LCMAP_SORTKEY |
1673                      NORM_IGNORECASE | NORM_IGNORENONSPACE | SORT_STRINGSORT) )
1674   {
1675     FIXME(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1676           "unimplemented flags: 0x%08lx\n",
1677           lcid,mapflags,srcstr,srclen,dststr,dstlen,mapflags);
1678   }
1679   
1680   if ( !(mapflags & LCMAP_SORTKEY) )
1681   {
1682     int (*f)(int)=identity; 
1683
1684     if (dstlen==0)
1685       return srclen;  /* dstlen=0 means "do nothing but return required length" */
1686     if (dstlen<srclen)
1687       srclen=dstlen;  /* No, this case is not an error under Windows 95.
1688                          And no '\0' gets written. */
1689     if (mapflags & LCMAP_UPPERCASE)
1690       f = toupper;
1691     else if (mapflags & LCMAP_LOWERCASE)
1692       f = tolower;
1693     for (i=0; i < srclen; i++)
1694       dststr[i] = (CHAR) f(srcstr[i]);
1695     return srclen;
1696   }
1697
1698   /* else ... (mapflags & LCMAP_SORTKEY)  */
1699   {
1700     int unicode_len=0;
1701     int case_len=0;
1702     int diacritic_len=0;
1703     int delayed_punctuation_len=0;
1704     char *case_component;
1705     char *diacritic_component;
1706     char *delayed_punctuation_component;
1707     int room,count;
1708     int flag_stringsort = mapflags & SORT_STRINGSORT;
1709
1710     /* compute how much room we will need */
1711     for (i=0;i<srclen;i++)
1712     {
1713       int ofs;
1714       unsigned char source_char = srcstr[i];
1715       if (source_char!='\0') 
1716       {
1717         if (flag_stringsort || !is_punctuation(source_char))
1718         {
1719           unicode_len++;
1720           if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1721             unicode_len++;             /* double letter */
1722         }
1723         else
1724         {
1725           delayed_punctuation_len++;
1726         }         
1727       }
1728           
1729       if (isupper(source_char))
1730         case_len=unicode_len; 
1731
1732       ofs = source_char - LCM_Diacritic_Start;
1733       if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1734         diacritic_len=unicode_len;
1735     }
1736
1737     if (mapflags & NORM_IGNORECASE)
1738       case_len=0;                   
1739     if (mapflags & NORM_IGNORENONSPACE)
1740       diacritic_len=0;
1741
1742     room =  2 * unicode_len              /* "unicode" component */
1743       +     diacritic_len                /* "diacritic" component */
1744       +     case_len                     /* "case" component */
1745       +     4 * delayed_punctuation_len  /* punctuation in word sort mode */
1746       +     4                            /* four '\1' separators */
1747       +     1  ;                         /* terminal '\0' */
1748     if (dstlen==0)
1749       return room;      
1750     else if (dstlen<room)
1751     {
1752       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1753       return 0;
1754     }
1755
1756     /*FIXME the Pointercheck should not be nessesary */
1757     if (IsBadWritePtr32 (dststr,room))
1758     { ERR (string,"bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1759       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1760       return 0;
1761     }
1762
1763     /* locate each component, write separators */
1764     diacritic_component = dststr + 2*unicode_len ;
1765     *diacritic_component++ = '\1'; 
1766     case_component = diacritic_component + diacritic_len ;
1767     *case_component++ = '\1'; 
1768     delayed_punctuation_component = case_component + case_len ;
1769     *delayed_punctuation_component++ = '\1';
1770     *delayed_punctuation_component++ = '\1';
1771
1772     /* read source string char by char, write 
1773        corresponding weight in each component. */
1774     for (i=0,count=0;i<srclen;i++)
1775     {
1776       unsigned char source_char=srcstr[i];
1777       if (source_char!='\0') 
1778       {
1779         int type,longcode;
1780         type = LCM_Unicode_LUT[-2+2*source_char];
1781         longcode = type >> 4;
1782         type &= 15;
1783         if (!flag_stringsort && is_punctuation(source_char)) 
1784         {
1785           UINT16 encrypted_location = (1<<15) + 7 + 4*count;
1786           *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1787           *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1788                      /* big-endian is used here because it lets string comparison be
1789                         compatible with numerical comparison */
1790
1791           *delayed_punctuation_component++ = type;
1792           *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];  
1793                      /* assumption : a punctuation character is never a 
1794                         double or accented letter */
1795         }
1796         else
1797         {
1798           dststr[2*count] = type;
1799           dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];  
1800           if (longcode)
1801           {
1802             if (count<case_len)
1803               case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1804             if (count<diacritic_len)
1805               diacritic_component[count] = 2; /* assumption: a double letter
1806                                                  is never accented */
1807             count++;
1808             
1809             dststr[2*count] = type;
1810             dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode); 
1811             /* 16 in the first column of LCM_Unicode_LUT  -->  longcode = 1 
1812                32 in the first column of LCM_Unicode_LUT  -->  longcode = 2 
1813                48 in the first column of LCM_Unicode_LUT  -->  longcode = 3 */
1814           }
1815
1816           if (count<case_len)
1817             case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1818           if (count<diacritic_len)
1819           {
1820             int ofs = source_char - LCM_Diacritic_Start;
1821             diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1822           }
1823           count++;
1824         }
1825       }
1826     }
1827     dststr[room-1] = '\0';
1828     return room;
1829   }
1830 }
1831                      
1832 INT32 WINAPI LCMapString32W(
1833         LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT32 srclen,LPWSTR dststr,
1834         INT32 dstlen)
1835 {
1836   int i;
1837  
1838   TRACE(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
1839         lcid,mapflags,srcstr,srclen,dststr,dstlen);
1840
1841   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1842   {
1843     SetLastError(ERROR_INVALID_PARAMETER);
1844     return 0;
1845   }
1846   if (srclen==-1) 
1847     srclen = lstrlen32W(srcstr)+1;
1848   if (mapflags & LCMAP_SORTKEY) 
1849   {
1850     FIXME(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1851           "unimplemented flags: 0x%08lx\n",
1852           lcid,mapflags,srcstr,srclen,dststr,dstlen,mapflags);
1853     return 0;
1854   }
1855   else
1856   {
1857     int (*f)(int)=identity; 
1858
1859     if (dstlen==0)
1860       return srclen;  
1861     if (dstlen<srclen)
1862       return 0;       
1863     if (mapflags & LCMAP_UPPERCASE)
1864       f = toupper;
1865     else if (mapflags & LCMAP_LOWERCASE)
1866       f = tolower;
1867     for (i=0; i < srclen; i++)
1868       dststr[i] = (WCHAR) f(srcstr[i]);
1869     return srclen;
1870   }
1871 }
1872
1873 /***********************************************************************
1874  *           CompareString16       (OLE2NLS.8)
1875  */
1876 UINT16 WINAPI CompareString16(DWORD lcid,DWORD fdwStyle,
1877                               LPCSTR s1,DWORD l1,LPCSTR s2,DWORD l2)
1878 {
1879         return (UINT16)CompareString32A(lcid,fdwStyle,s1,l1,s2,l2);
1880 }
1881
1882 /***********************************************************************
1883  *           CompareString32A   (KERNEL32.29)
1884  * This implementation ignores the locale
1885  * FIXME
1886  * Moreover it is quite inefficient. FIXME too!
1887  */
1888 UINT32 WINAPI CompareString32A(DWORD lcid, DWORD fdwStyle, 
1889                                LPCSTR s1, DWORD l1, LPCSTR s2,DWORD l2)
1890 {
1891   int mapstring_flags;
1892   int len1,len2;
1893   int result;
1894   LPSTR sk1,sk2;
1895   TRACE(ole,"%s and %s\n",
1896         debugstr_a (s1), debugstr_a (s2));
1897
1898   if ( (s1==NULL) || (s2==NULL) )
1899   {    
1900     SetLastError(ERROR_INVALID_PARAMETER);
1901     return 0;
1902   }
1903
1904   if(fdwStyle & NORM_IGNORESYMBOLS)
1905     FIXME(ole, "IGNORESYMBOLS not supported\n");
1906         
1907   mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1908   len1 = LCMapString32A(lcid,mapstring_flags,s1,l1,NULL,0);
1909   len2 = LCMapString32A(lcid,mapstring_flags,s2,l2,NULL,0);
1910
1911   if ((len1==0)||(len2==0))
1912     return 0;     /* something wrong happened */
1913
1914   sk1 = (LPSTR)HeapAlloc(GetProcessHeap(),0,len1);
1915   sk2 = (LPSTR)HeapAlloc(GetProcessHeap(),0,len2);
1916   if ( (!LCMapString32A(lcid,mapstring_flags,s1,l1,sk1,len1))
1917          || (!LCMapString32A(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1918   { ERR(ole,"Bug in LCmapString32A.\n");
1919     result = 0;
1920   }
1921   else
1922   { result = strcmp(sk1,sk2)+2;
1923   }
1924   HeapFree(GetProcessHeap(),0,sk1);
1925   HeapFree(GetProcessHeap(),0,sk2);
1926   return result;
1927 }
1928
1929 /***********************************************************************
1930  *           CompareString32W       (KERNEL32.30)
1931  * This implementation ignores the locale
1932  * FIXME :  Does only string sort.  Should
1933  * be reimplemented the same way as CompareString32A.
1934  */
1935 UINT32 WINAPI CompareString32W(DWORD lcid, DWORD fdwStyle, 
1936                                LPCWSTR s1, DWORD l1, LPCWSTR s2,DWORD l2)
1937 {
1938         int len,ret;
1939         if(fdwStyle & NORM_IGNORENONSPACE)
1940                 FIXME(ole,"IGNORENONSPACE not supprted\n");
1941         if(fdwStyle & NORM_IGNORESYMBOLS)
1942                 FIXME(ole,"IGNORESYMBOLS not supported\n");
1943
1944         /* Is strcmp defaulting to string sort or to word sort?? */
1945         /* FIXME: Handle NORM_STRINGSORT */
1946         l1 = (l1==-1)?lstrlen32W(s1):l1;
1947         l2 = (l2==-1)?lstrlen32W(s2):l2;
1948         len = l1<l2 ? l1:l2;
1949         ret = (fdwStyle & NORM_IGNORECASE) ?
1950                 lstrncmpi32W(s1,s2,len) : lstrncmp32W(s1,s2,len);
1951         /* not equal, return 1 or 3 */
1952         if(ret!=0) return ret+2;
1953         /* same len, return 2 */
1954         if(l1==l2) return 2;
1955         /* the longer one is lexically greater */
1956         return (l1<l2)? 1 : 3;
1957 }
1958
1959 /*****************************************************************
1960  *
1961  *  OLE_GetFormatA() [internal]
1962
1963
1964  This function implements stuff for GetDateFormat() and 
1965  GetTimeFormat().
1966
1967   d    single-digit (no leading zero) day (of month)
1968   dd   two-digit day (of month)
1969   ddd  short day-of-week name
1970   dddd long day-of-week name
1971   M    single-digit month
1972   MM   two-digit month
1973   MMM  short month name
1974   MMMM full month name
1975   y    two-digit year, no leading 0
1976   yy   two-digit year
1977   yyyy four-digit year
1978   gg   era string
1979   h    hours with no leading zero (12-hour)
1980   hh   hours with full two digits
1981   H    hours with no leading zero (24-hour)
1982   HH   hours with full two digits
1983   m    minutes with no leading zero
1984   mm   minutes with full two digits
1985   s    seconds with no leading zero
1986   ss   seconds with full two digits
1987   t    time marker (A or P)
1988   tt   time marker (AM, PM)
1989   ''   used to quote literal characters
1990   ''   (within a quoted string) indicates a literal '
1991
1992  These functions REQUIRE valid locale, date,  and format. 
1993
1994  */
1995
1996 INT32 WINAPI OLE_GetFormatA(LCID locale,
1997                             DWORD flags,
1998                             LPSYSTEMTIME xtime,
1999                             LPCSTR format, 
2000                             LPSTR date, INT32 datelen)
2001 {
2002    INT32 inpos, outpos;
2003    int count, type, inquote, Overflow;
2004    char buf[40];
2005    int buflen;
2006
2007    const char * _dgfmt[] = { "%d", "%02d" };
2008    const char ** dgfmt = _dgfmt - 1; 
2009
2010    /* report, for debugging */
2011    TRACE(ole, "func(%8lx,%8lx, time(d=%d,h=%d,m=%d,s=%d), fmt:\'%s\' (at %p), %p (%9s), len=%d)\n", locale, flags,
2012          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2013          format, format, date, date, datelen);
2014   
2015    /* initalize state variables and output buffer */
2016    inpos = outpos = 0;
2017    count = 0; inquote = 0; Overflow = 0;
2018    type = '\0';
2019    date[0] = buf[0] = '\0';
2020       
2021    for (inpos = 0;; inpos++) {
2022       /* TRACE(ole, "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]); */
2023       if (inquote) {
2024          if (format[inpos] == '\'') {
2025             if (format[inpos+1] == '\'') {
2026                inpos += 1;
2027                date[outpos++] = '\'';
2028             } else {
2029                inquote = 0;
2030                continue; /* we did nothing to the output */
2031             }
2032          } else if (format[inpos] == '\0') {
2033             date[outpos++] = '\0';
2034             if (outpos > datelen) Overflow = 1;
2035             break;
2036          } else {
2037             date[outpos++] = format[inpos];
2038             if (outpos > datelen) {
2039                Overflow = 1;
2040                date[outpos-1] = '\0'; /* this is the last place where
2041                                          it's safe to write */
2042                break;
2043             }
2044          }
2045       } else if (  (count && (format[inpos] != type))
2046                    || count == 4
2047                    || (count == 2 && strchr("ghHmst", type)) )
2048        {
2049             if         (type == 'd') {
2050                if        (count == 4) {
2051                   GetLocaleInfo32A(locale,
2052                                    LOCALE_SDAYNAME1
2053                                    + xtime->wDayOfWeek - 1,
2054                                    buf, sizeof(buf));
2055                } else if (count == 3) {
2056                            GetLocaleInfo32A(locale, 
2057                                             LOCALE_SABBREVDAYNAME1 
2058                                             + xtime->wDayOfWeek - 1,
2059                                             buf, sizeof(buf));
2060                       } else {
2061                   sprintf(buf, dgfmt[count], xtime->wDay);
2062                }
2063             } else if (type == 'M') {
2064                if (count == 3) {
2065                   GetLocaleInfo32A(locale, 
2066                                    LOCALE_SABBREVMONTHNAME1
2067                                    + xtime->wMonth - 1,
2068                                    buf, sizeof(buf));
2069                } else if (count == 4) {
2070                   GetLocaleInfo32A(locale,
2071                                    LOCALE_SMONTHNAME1
2072                                    + xtime->wMonth - 1,
2073                                    buf, sizeof(buf));
2074                  } else {
2075                   sprintf(buf, dgfmt[count], xtime->wMonth);
2076                }
2077             } else if (type == 'y') {
2078                if (count == 4) {
2079                       sprintf(buf, "%d", xtime->wYear);
2080                } else if (count == 3) {
2081                   strcpy(buf, "yyy");
2082                   WARN(ole,
2083                               "unknown format,\
2084 c=%c, n=%d\n",  type, count);
2085                  } else {
2086                   sprintf(buf, dgfmt[count], xtime->wYear % 100);
2087                }
2088             } else if (type == 'g') {
2089                if        (count == 2) {
2090                   FIXME(ole, "LOCALE_ICALENDARTYPE unimp.\n");
2091                   strcpy(buf, "AD");
2092             } else {
2093                   strcpy(buf, "g");
2094                   WARN(ole,
2095                                "unknown format, \
2096 c=%c, n=%d\n", type, count);
2097                }
2098             } else if (type == 'h') {
2099                /* gives us hours 1:00 -- 12:00 */
2100                sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2101             } else if (type == 'H') {
2102                /* 24-hour time */
2103                sprintf(buf, dgfmt[count], xtime->wHour);
2104             } else if (type == 'm') {
2105                sprintf(buf, dgfmt[count], xtime->wMinute);
2106             } else if (type == 's') {
2107                sprintf(buf, dgfmt[count], xtime->wSecond);
2108             } else if (type == 't') {
2109                if        (count == 1) {
2110                   sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2111                } else if (count == 2) {
2112                   /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2113                   GetLocaleInfo32A(locale,
2114                                    (xtime->wHour<12) 
2115                                    ? LOCALE_S1159 : LOCALE_S2359,
2116                                    buf, sizeof(buf));
2117                }
2118             };
2119
2120             /* we need to check the next char in the format string 
2121                again, no matter what happened */
2122             inpos--;
2123             
2124             /* add the contents of buf to the output */
2125             buflen = strlen(buf);
2126             if (outpos + buflen < datelen) {
2127                date[outpos] = '\0'; /* for strcat to hook onto */
2128                  strcat(date, buf);
2129                outpos += buflen;
2130             } else {
2131                date[outpos] = '\0';
2132                strncat(date, buf, datelen - outpos);
2133                  date[datelen - 1] = '\0';
2134                  SetLastError(ERROR_INSUFFICIENT_BUFFER);
2135                WARN(ole, "insufficient buffer\n");
2136                  return 0;
2137             }
2138
2139             /* reset the variables we used to keep track of this item */
2140             count = 0;
2141             type = '\0';
2142          } else if (format[inpos] == '\0') {
2143             /* we can't check for this at the loop-head, because
2144                that breaks the printing of the last format-item */
2145             date[outpos] = '\0';
2146             break;
2147          } else if (count) {
2148             /* continuing a code for an item */
2149             count +=1;
2150             continue;
2151          } else if (strchr("hHmstyMdg", format[inpos])) {
2152             type = format[inpos];
2153             count = 1;
2154             continue;
2155          } else if (format[inpos] == '\'') {
2156             inquote = 1;
2157             continue;
2158        } else {
2159             date[outpos++] = format[inpos];
2160          }
2161       /* now deal with a possible buffer overflow */
2162       if (outpos >= datelen) {
2163        date[datelen - 1] = '\0';
2164        SetLastError(ERROR_INSUFFICIENT_BUFFER);
2165        return 0;
2166       }
2167    }
2168    
2169    if (Overflow) {
2170       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2171    };
2172
2173    /* finish it off with a string terminator */
2174    outpos++;
2175    /* sanity check */
2176    if (outpos > datelen-1) outpos = datelen-1;
2177    date[outpos] = '\0';
2178    
2179    TRACE(ole, "OLE_GetFormatA returns string '%s', len %d\n",
2180                date, outpos);
2181    return outpos;
2182 }
2183
2184 /**************************************************************
2185  * OLE_GetFormatW  [internal]  
2186  */
2187 INT32 WINAPI OLE_GetFormatW(LCID locale, DWORD flags,
2188                             LPSYSTEMTIME xtime,
2189                             LPCWSTR format,
2190                             LPWSTR output, INT32 outlen)
2191 {
2192    INT32   inpos, outpos;
2193    int     count, type=0, inquote;
2194    int     Overflow; /* loop check */
2195    int usedate, usetime;
2196    WCHAR   buf[40];
2197    int     buflen=0;
2198    char    abuf[40];
2199    WCHAR   arg0[] = {0}, arg1[] = {'%','d',0};
2200    WCHAR   arg2[] = {'%','0','2','d',0};
2201    WCHAR  *argarr[] = {arg0, arg1, arg2};
2202    int     datevars=0, timevars=0;
2203
2204    /* make a debug report */
2205    lstrcpynWtoA(abuf, format, sizeof(format));
2206    TRACE(ole, "args: %8lx, %8lx, time(d=%d,h=%d,m=%d,s=%d), fmt:\'%s\' (at %p), %p with max len %d\n",
2207          locale, flags, 
2208          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2209          abuf, format, output, outlen);
2210    
2211
2212    /* initialize state variables */
2213    inpos = outpos = 0;
2214    count = 0;
2215    inquote = Overflow = 0;
2216    /* this is really just a sanity check */
2217    output[0] = buf[0] = 0;
2218    abuf[0] = '\0';
2219    /* for compatibility with official Windows behavior */
2220    usedate = flags & DATE_DATEVARSONLY;
2221    usetime = flags & TIME_TIMEVARSONLY;
2222    
2223    /* this loop is the core of the function */
2224    for (inpos = 0; /* we have several break points */ ; inpos++) {
2225       if (inquote) {
2226          if (format[inpos] == (WCHAR) '\'') {
2227             if (format[inpos+1] == '\'') {
2228                inpos++;
2229                output[outpos++] = '\'';
2230             } else {
2231                inquote = 0;
2232                continue;
2233             }
2234          } else if (format[inpos] == 0) {
2235             output[outpos++] = 0;
2236             if (outpos > outlen) Overflow = 1;
2237             break;  /*  normal exit (within a quote) */
2238          } else {
2239             output[outpos++] = format[inpos]; /* copy input */
2240             if (outpos > outlen) {
2241                Overflow = 1;
2242                output[outpos-1] = 0; 
2243                break;
2244             }
2245          }
2246       } else if (  (count && (format[inpos] != type))
2247                    || ( (count==4 && type =='y') ||
2248                         (count==4 && type =='M') ||
2249                         (count==4 && type =='d') ||
2250                         (count==2 && type =='g') ||
2251                         (count==2 && type =='h') ||
2252                         (count==2 && type =='H') ||
2253                         (count==2 && type =='m') ||
2254                         (count==2 && type =='s') ||
2255                         (count==2 && type =='t') )  ) {
2256          if        (type == 'd') {
2257             if        (count == 3) {
2258                GetLocaleInfo32W(locale,
2259                              LOCALE_SDAYNAME1 + xtime->wDayOfWeek -1,
2260                              buf, sizeof(buf)/sizeof(WCHAR) );
2261             } else if (count == 3) {
2262                GetLocaleInfo32W(locale,
2263                                 LOCALE_SABBREVDAYNAME1 +
2264                                 xtime->wDayOfWeek -1,
2265                                 buf, sizeof(buf)/sizeof(WCHAR) );
2266             } else {
2267                wsnprintf32W(buf, 5, argarr[count], xtime->wDay );
2268             };
2269          } else if (type == 'M') {
2270             if        (count == 4) {
2271                GetLocaleInfo32W(locale,  LOCALE_SMONTHNAME1 +
2272                                 xtime->wMonth -1, buf,
2273                                 sizeof(buf)/sizeof(WCHAR) );
2274             } else if (count == 3) {
2275                GetLocaleInfo32W(locale,  LOCALE_SABBREVMONTHNAME1 +
2276                                 xtime->wMonth -1, buf,
2277                                 sizeof(buf)/sizeof(WCHAR) );
2278             } else {
2279                wsnprintf32W(buf, 5, argarr[count], xtime->wMonth);
2280             }
2281          } else if (type == 'y') {
2282             if        (count == 4) {
2283                wsnprintf32W(buf, 6, argarr[1] /* "%d" */,
2284                          xtime->wYear);
2285             } else if (count == 3) {
2286                lstrcpynAtoW(buf, "yyy", 5);
2287             } else {
2288                wsnprintf32W(buf, 6, argarr[count],
2289                             xtime->wYear % 100);
2290             }
2291          } else if (type == 'g') {
2292             if        (count == 2) {
2293                FIXME(ole, "LOCALE_ICALENDARTYPE unimplemented\n");
2294                lstrcpynAtoW(buf, "AD", 5);
2295             } else {
2296                /* Win API sez we copy it verbatim */
2297                lstrcpynAtoW(buf, "g", 5);
2298             }
2299          } else if (type == 'h') {
2300             /* hours 1:00-12:00 --- is this right? */
2301             wsnprintf32W(buf, 5, argarr[count], 
2302                          (xtime->wHour-1)%12 +1);
2303          } else if (type == 'H') {
2304             wsnprintf32W(buf, 5, argarr[count], 
2305                          xtime->wHour);
2306          } else if (type == 'm') {
2307             wsnprintf32W(buf, 5, argarr[count],
2308                          xtime->wMinute);
2309          } else if (type == 's') {
2310             wsnprintf32W(buf, 5, argarr[count],
2311                          xtime->wSecond);
2312          } else if (type == 't') {
2313             GetLocaleInfo32W(locale, (xtime->wHour < 12) ?
2314                              LOCALE_S1159 : LOCALE_S2359,
2315                              buf, sizeof(buf) );
2316             if        (count == 1) {
2317                buf[1] = 0;
2318             }
2319 }
2320
2321          /* no matter what happened,  we need to check this next 
2322             character the next time we loop through */
2323          inpos--;
2324
2325          /* cat buf onto the output */
2326          outlen = lstrlen32W(buf);
2327          if (outpos + buflen < outlen) {
2328             output[outpos] = 0;  /* a "hook" for strcat */
2329             lstrcat32W(output, buf);
2330             outpos += buflen;
2331          } else {
2332             output[outpos] = 0;
2333             lstrcatn32W(output, buf, outlen - outpos);
2334             output[outlen - 1] = 0;
2335             Overflow = 1;
2336             break; /* Abnormal exit */
2337          }
2338
2339          /* reset the variables we used this time */
2340          count = 0;
2341          type = '\0';
2342       } else if (format[inpos] == 0) {
2343          /* we can't check for this at the beginning,  because that 
2344          would keep us from printing a format spec that ended the 
2345          string */
2346          output[outpos] = 0;
2347          break;  /*  NORMAL EXIT  */
2348       } else if (count) {
2349          /* how we keep track of the middle of a format spec */
2350          count++;
2351          continue;
2352       } else if ( (datevars && (format[inpos]=='d' ||
2353                                 format[inpos]=='M' ||
2354                                 format[inpos]=='y' ||
2355                                 format[inpos]=='g')  ) ||
2356                   (timevars && (format[inpos]=='H' ||
2357                                 format[inpos]=='h' ||
2358                                 format[inpos]=='m' ||
2359                                 format[inpos]=='s' ||
2360                                 format[inpos]=='t') )    ) {
2361          type = format[inpos];
2362          count = 1;
2363          continue;
2364       } else if (format[inpos] == '\'') {
2365          inquote = 1;
2366          continue;
2367       } else {
2368          /* unquoted literals */
2369          output[outpos++] = format[inpos];
2370       }
2371    }
2372
2373    if (Overflow) {
2374       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2375       WARN(ole, " buffer overflow\n");
2376    };
2377
2378    /* final string terminator and sanity check */
2379    outpos++;
2380    if (outpos > outlen-1) outpos = outlen-1;
2381    output[outpos] = '0';
2382
2383    lstrcpynWtoA(abuf, output, sizeof(abuf) );
2384    TRACE(ole, " returning string \'%s\'\n", abuf);
2385         
2386    return (!Overflow) ? outlen : 0;
2387    
2388 }
2389
2390
2391 /*****************************************************************
2392  *  GetDateFormat32A() [KERNEL32.310] Makes an ASCII string of the date
2393  *
2394  * This function uses format to format the date,  or,  if format
2395  * is NULL, uses the default for the locale.  format is a string
2396  * of literal fields and characters as follows:
2397  *
2398  * - d    single-digit (no leading zero) day (of month)
2399  * - dd   two-digit day (of month)
2400  * - ddd  short day-of-week name
2401  * - dddd long day-of-week name
2402  * - M    single-digit month
2403  * - MM   two-digit month
2404  * - MMM  short month name
2405  * - MMMM full month name
2406  * - y    two-digit year, no leading 0
2407  * - yy   two-digit year
2408  * - yyyy four-digit year
2409  * - gg   era string
2410  *
2411  */
2412
2413 INT32 WINAPI GetDateFormat32A(LCID locale,DWORD flags,
2414                               LPSYSTEMTIME xtime,
2415                               LPCSTR format, LPSTR date,INT32 datelen) 
2416 {
2417    
2418   char format_buf[40];
2419   LPCSTR thisformat;
2420   SYSTEMTIME t;
2421   LPSYSTEMTIME thistime;
2422   LCID thislocale;
2423
2424   INT32 ret;
2425
2426   TRACE(ole,"(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2427               locale,flags,xtime,format,date,datelen);
2428   
2429   if (!locale) {
2430      locale = LOCALE_SYSTEM_DEFAULT;
2431      };
2432   
2433   if (locale == LOCALE_SYSTEM_DEFAULT) {
2434      thislocale = GetSystemDefaultLCID();
2435   } else if (locale == LOCALE_USER_DEFAULT) {
2436      thislocale = GetUserDefaultLCID();
2437   } else {
2438      thislocale = locale;
2439    };
2440
2441   if (xtime == NULL) {
2442      GetSystemTime(&t);
2443      thistime = &t;
2444   } else {
2445      thistime = xtime;
2446   };
2447
2448   if (format == NULL) {
2449      GetLocaleInfo32A(thislocale, ((flags&DATE_LONGDATE) 
2450                                    ? LOCALE_SLONGDATE
2451                                    : LOCALE_SSHORTDATE),
2452                       format_buf, sizeof(format_buf));
2453      thisformat = format_buf;
2454   } else {
2455      thisformat = format;
2456   };
2457
2458   
2459   ret = OLE_GetFormatA(thislocale, flags, thistime, thisformat, 
2460                        date, datelen);
2461   
2462
2463    TRACE(ole, 
2464                "GetDateFormat32A() returning %d, with data=%s\n",
2465                ret, date);
2466   return ret;
2467 }
2468
2469 /* ****************************************************************
2470  * GetDateFormat32W() [KERNEL32.311] Makes a Unicode string of the date
2471  *
2472  * Acts the same as GetDateFormat32A(),  except that it's Unicode.
2473  * Accepts & returns sizes as counts of Unicode characters.
2474  *
2475  */
2476 INT32 WINAPI GetDateFormat32W(LCID locale,DWORD flags,
2477                               LPSYSTEMTIME xtime,
2478                               LPCWSTR format,
2479                               LPWSTR date, INT32 datelen)
2480 {
2481    short datearr[] = {'1','9','9','4','-','1','-','1',0};
2482
2483    FIXME(ole, "STUB (should call OLE_GetFormatW)\n");   
2484    lstrcpyn32W(date, datearr, datelen);
2485    return (  datelen < 9) ? datelen : 9;
2486    
2487    
2488 }
2489
2490
2491 /**************************************************************************
2492  *              GetNumberFormat32A      (KERNEL32.355)
2493  * NOTE: type of lpFormat should be CONST NUMBERFORMAT 
2494  */
2495 INT32 WINAPI GetNumberFormat32A(LCID locale, DWORD dwflags,
2496                                LPCSTR lpvalue,  char *lpFormat,
2497                                LPSTR lpNumberStr, int cchNumber)
2498 {
2499  int n;
2500
2501  FIXME(file,"%s: stub, no reformating done\n",lpvalue);
2502
2503  n = strlen(lpvalue);
2504  if (cchNumber) { 
2505    strncpy(lpNumberStr,lpvalue,cchNumber);
2506    if (cchNumber <= n) {
2507      lpNumberStr[cchNumber-1] = 0;
2508      n = cchNumber-1;
2509    }
2510  }
2511  return n;
2512 }
2513  
2514
2515
2516 /*****************************************************************
2517  *
2518  * GetTimeFormat32A() [KERNEL32.422] Makes an ASCII string of the time
2519  *
2520  * Formats date according to format,  or locale default if format is
2521  * NULL. The format consists of literal characters and fields as follows:
2522  *
2523  * h  hours with no leading zero (12-hour)
2524  * hh hours with full two digits
2525  * H  hours with no leading zero (24-hour)
2526  * HH hours with full two digits
2527  * m  minutes with no leading zero
2528  * mm minutes with full two digits
2529  * s  seconds with no leading zero
2530  * ss seconds with full two digits
2531  * t  time marker (A or P)
2532  * tt time marker (AM, PM)
2533  *
2534  */
2535
2536 INT32 WINAPI 
2537 GetTimeFormat32A(LCID locale,        /* in  */
2538                  DWORD flags,        /* in  */
2539                  LPSYSTEMTIME xtime, /* in  */ 
2540                  LPCSTR format,      /* in  */
2541                  LPSTR timestr,      /* out */
2542                  INT32 timelen       /* in  */) 
2543 { char format_buf[40];
2544   LPCSTR thisformat;
2545   SYSTEMTIME t;
2546   LPSYSTEMTIME thistime;
2547   LCID thislocale=0;
2548   DWORD thisflags=LOCALE_STIMEFORMAT; /* standart timeformat */;
2549   
2550   TRACE(ole,"GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
2551   if (!locale) 
2552   { locale = LOCALE_SYSTEM_DEFAULT;
2553   }
2554   
2555   if (locale == LOCALE_SYSTEM_DEFAULT) 
2556   { thislocale = GetSystemDefaultLCID();
2557   } 
2558   else if (locale == LOCALE_USER_DEFAULT) 
2559   { thislocale = GetUserDefaultLCID();
2560         }
2561   else
2562   { thislocale = locale;
2563   }
2564   if (format == NULL) 
2565   { if (flags&LOCALE_NOUSEROVERRIDE)  /*use system default*/
2566     { thislocale = GetSystemDefaultLCID();
2567     }
2568     switch (flags)
2569     { case TIME_NOSECONDS:
2570         FIXME(ole,"TIME_NOSECONDS not yet implemented");
2571         break;
2572       case TIME_NOMINUTESORSECONDS:
2573         FIXME(ole,"TIME_NOMINUTESORSECONDS not yet implemented");
2574        break;
2575     }   
2576     GetLocaleInfo32A(thislocale, thisflags, format_buf,sizeof(format_buf));
2577     thisformat = format_buf;
2578   }
2579   else 
2580   { thisformat = format;
2581   }
2582   
2583   if (!locale) 
2584   {  locale = GetSystemDefaultLCID();
2585   }
2586   if (xtime == NULL) /* NULL means use the current local time*/
2587   { GetSystemTime(&t);
2588     thistime = &t;
2589   } 
2590   else
2591   { thistime = xtime;
2592   }
2593   return OLE_GetFormatA(thislocale, thisflags, thistime, thisformat,timestr, timelen);
2594 }
2595
2596
2597 /* ****************************************************************
2598  *  GetTimeFormat32W()  [KERNEL32.423] Makes a Unicode string of the time
2599  * 
2600  *
2601  */
2602
2603 INT32 WINAPI 
2604 GetTimeFormat32W(LCID locale,DWORD flags,
2605                               LPSYSTEMTIME xtime,
2606                               LPCWSTR format, 
2607                               LPWSTR timestr,INT32 timelen) 
2608 {
2609    char debugbuf[40];
2610    WCHAR buf[20];
2611    LPCWSTR realformat=0;
2612    SYSTEMTIME t;
2613    LPSYSTEMTIME realtime;
2614    WCHAR * fmt_buf = NULL;
2615    int fmt_buf_size = 0; /* units of WCHARs */
2616    INT32 retval;
2617    
2618    lstrcpynWtoA(debugbuf, format, (sizeof(buf))/2);
2619    TRACE(ole, "GetTimeFormatW len %d flags 0x%lX format >%s<\n",
2620                 timelen, flags, debugbuf);
2621
2622    /* Enforce the Windows behavior */
2623    flags |= ~LOCALE_TIMEDATEBOTH;
2624    flags |= TIME_TIMEVARSONLY;
2625
2626    /* take care of the format or locale not being given */
2627    if (format) {
2628       realformat = format;
2629    } else if (locale) {
2630       /* allocate memory */
2631       retval = ERROR_INSUFFICIENT_BUFFER;
2632       fmt_buf = malloc((fmt_buf_size+=7) * sizeof(WCHAR));
2633       if (!fmt_buf)
2634         goto out_nomem;
2635
2636       while(!GetLocaleInfo32W(locale, LOCALE_STIMEFORMAT,
2637                               fmt_buf, fmt_buf_size)) {
2638         retval = ERROR_OUTOFMEMORY;
2639         fmt_buf = realloc(fmt_buf, 
2640                            (fmt_buf_size += 10) * sizeof(WCHAR));
2641         if (!fmt_buf)
2642                 goto out_nomem;
2643       }
2644       realformat = fmt_buf;
2645    } else {
2646       FIXME(ole,  "caller gave no locale,  no format;  what should we do?\n");
2647       SetLastError(ERROR_BAD_FORMAT);
2648    }
2649    if (!locale)
2650       locale = GetSystemDefaultLCID();
2651
2652    realtime = xtime;
2653    if (!realtime) {
2654       realtime = &t;
2655       GetSystemTime(realtime);
2656    }
2657
2658    retval = OLE_GetFormatW(locale, flags, realtime, realformat, timestr,  timelen);
2659    if (fmt_buf)
2660         free(fmt_buf);
2661    return retval;
2662
2663 out_nomem:
2664         SetLastError(retval);
2665         WARN(ole, "could not allocate %d chars of memory\n", fmt_buf_size); 
2666         return 0;
2667 }