Fixed the behavior for SHGetFileInfo when the SHGFI_USEFILEATTRIBUTES
[wine] / memory / codepage.c
1 /*
2  * Code page functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "winbase.h"
13 #include "winerror.h"
14 #include "winnls.h"
15 #include "wine/unicode.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(string);
19
20 /* current code pages */
21 static const union cptable *ansi_cptable;
22 static const union cptable *oem_cptable;
23 static const union cptable *mac_cptable;
24
25 /* retrieve a code page table from the locale info */
26 static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
27 {
28     const union cptable *table = NULL;
29     char buf[32];
30
31     if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
32     return table;
33 }
34
35 /* setup default codepage info before we can get at the locale stuff */
36 static void init_codepages(void)
37 {
38     ansi_cptable = cp_get_table( 1252 );
39     oem_cptable  = cp_get_table( 437 );
40     mac_cptable  = cp_get_table( 10000 );
41     assert( ansi_cptable );
42     assert( oem_cptable );
43     assert( mac_cptable );
44 }
45
46 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
47 static const union cptable *get_codepage_table( unsigned int codepage )
48 {
49     const union cptable *ret = NULL;
50
51     if (!ansi_cptable) init_codepages();
52
53     switch(codepage)
54     {
55     case CP_ACP:        return ansi_cptable;
56     case CP_OEMCP:      return oem_cptable;
57     case CP_MACCP:      return mac_cptable;
58     case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
59     case CP_UTF7:
60     case CP_UTF8:
61         break;
62     default:
63         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
64         if (codepage == oem_cptable->info.codepage) return oem_cptable;
65         if (codepage == mac_cptable->info.codepage) return mac_cptable;
66         ret = cp_get_table( codepage );
67         break;
68     }
69     return ret;
70 }
71
72 /* initialize default code pages from locale info */
73 /* FIXME: should be done in init_codepages, but it can't right now */
74 /* since it needs KERNEL32 to be loaded for the locale info. */
75 void CODEPAGE_Init(void)
76 {
77     const union cptable *table;
78     LCID lcid = GetUserDefaultLCID();
79
80     if (!ansi_cptable) init_codepages();  /* just in case */
81     
82     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE ))) ansi_cptable = table;
83     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE ))) mac_cptable = table;
84     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE ))) oem_cptable = table;
85
86     TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
87            oem_cptable->info.codepage, mac_cptable->info.codepage );
88 }
89
90 /******************************************************************************
91  *              GetACP   (KERNEL32.@)
92  *
93  * RETURNS
94  *    Current ANSI code-page identifier, default if no current defined
95  */
96 UINT WINAPI GetACP(void)
97 {
98     if (!ansi_cptable) init_codepages();
99     return ansi_cptable->info.codepage;
100 }
101
102
103 /***********************************************************************
104  *              GetOEMCP   (KERNEL32.@)
105  */
106 UINT WINAPI GetOEMCP(void)
107 {
108     if (!oem_cptable) init_codepages();
109     return oem_cptable->info.codepage;
110 }
111
112
113 /***********************************************************************
114  *           IsValidCodePage   (KERNEL32.@)
115  */
116 BOOL WINAPI IsValidCodePage( UINT codepage )
117 {
118     return cp_get_table( codepage ) != NULL;
119 }
120
121
122 /***********************************************************************
123  *           IsDBCSLeadByteEx   (KERNEL32.@)
124  */
125 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
126 {
127     const union cptable *table = get_codepage_table( codepage );
128     return table && is_dbcs_leadbyte( table, testchar );
129 }
130
131
132 /***********************************************************************
133  *           IsDBCSLeadByte   (KERNEL32.@)
134  *           IsDBCSLeadByte   (KERNEL.207)
135  */
136 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
137 {
138     if (!ansi_cptable) init_codepages();
139     return is_dbcs_leadbyte( ansi_cptable, testchar );
140 }
141
142
143 /***********************************************************************
144  *           GetCPInfo   (KERNEL32.@)
145  */
146 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
147 {
148     const union cptable *table = get_codepage_table( codepage );
149
150     if (!table) 
151     {
152         SetLastError( ERROR_INVALID_PARAMETER );
153         return FALSE;
154     }
155     if (table->info.def_char & 0xff00)
156     {
157         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
158         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
159     }
160     else
161     {
162         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
163         cpinfo->DefaultChar[1] = 0;
164     }
165     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
166         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
167     else
168         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
169
170     return TRUE;
171 }
172
173
174 /***********************************************************************
175  *              EnumSystemCodePagesA   (KERNEL32.@)
176  */
177 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
178 {
179     const union cptable *table;
180     char buffer[10];
181     int index = 0;
182
183     for (;;)
184     {
185         if (!(table = cp_enum_table( index++ ))) break;
186         sprintf( buffer, "%d", table->info.codepage );
187         if (!lpfnCodePageEnum( buffer )) break;
188     }
189     return TRUE;
190 }
191
192
193 /***********************************************************************
194  *              EnumSystemCodePagesW   (KERNEL32.@)
195  */
196 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
197 {
198     const union cptable *table;
199     WCHAR buffer[10], *p;
200     int page, index = 0;
201
202     for (;;)
203     {
204         if (!(table = cp_enum_table( index++ ))) break;
205         p = buffer + sizeof(buffer)/sizeof(WCHAR);
206         *--p = 0;
207         page = table->info.codepage;
208         do
209         {
210             *--p = '0' + (page % 10);
211             page /= 10;
212         } while( page );
213         if (!lpfnCodePageEnum( p )) break;
214     }
215     return TRUE;
216 }
217
218
219 /***********************************************************************
220  *              MultiByteToWideChar   (KERNEL32.@)
221  *
222  * PARAMS
223  *   page [in]    Codepage character set to convert from
224  *   flags [in]   Character mapping flags
225  *   src [in]     Source string buffer
226  *   srclen [in]  Length of source string buffer
227  *   dst [in]     Destination buffer
228  *   dstlen [in]  Length of destination buffer
229  *
230  * NOTES
231  *   The returned length includes the null terminator character.
232  *
233  * RETURNS
234  *   Success: If dstlen > 0, number of characters written to destination
235  *            buffer.  If dstlen == 0, number of characters needed to do
236  *            conversion.
237  *   Failure: 0. Occurs if not enough space is available.
238  *
239  * ERRORS
240  *   ERROR_INSUFFICIENT_BUFFER
241  *   ERROR_INVALID_PARAMETER
242  *   ERROR_NO_UNICODE_TRANSLATION
243  *
244  */
245 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
246                                 LPWSTR dst, INT dstlen )
247 {
248     const union cptable *table;
249     int ret;
250
251     if (!src || (!dst && dstlen))
252     {
253         SetLastError( ERROR_INVALID_PARAMETER );
254         return 0;
255     }
256
257     if (srclen == -1) srclen = strlen(src) + 1;
258
259     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
260
261     switch(page)
262     {
263     case CP_UTF7:
264         FIXME("UTF not supported\n");
265         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
266         return 0;
267     case CP_UTF8:
268         ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
269         break;
270     default:
271         if (!(table = get_codepage_table( page )))
272         {
273             SetLastError( ERROR_INVALID_PARAMETER );
274             return 0;
275         }
276         ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
277         break;
278     }
279
280     if (ret < 0)
281     {
282         switch(ret)
283         {
284         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
285         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
286         }
287         ret = 0;
288     }
289     return ret;
290 }
291
292
293 /***********************************************************************
294  *              WideCharToMultiByte   (KERNEL32.@)
295  *
296  * PARAMS
297  *   page [in]    Codepage character set to convert to
298  *   flags [in]   Character mapping flags
299  *   src [in]     Source string buffer
300  *   srclen [in]  Length of source string buffer
301  *   dst [in]     Destination buffer
302  *   dstlen [in]  Length of destination buffer
303  *   defchar [in] Default character to use for conversion if no exact
304  *                  conversion can be made
305  *   used [out]   Set if default character was used in the conversion
306  *
307  * NOTES
308  *   The returned length includes the null terminator character.
309  *
310  * RETURNS
311  *   Success: If dstlen > 0, number of characters written to destination
312  *            buffer.  If dstlen == 0, number of characters needed to do
313  *            conversion.
314  *   Failure: 0. Occurs if not enough space is available.
315  *
316  * ERRORS
317  *   ERROR_INSUFFICIENT_BUFFER
318  *   ERROR_INVALID_PARAMETER
319  */
320 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
321                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
322 {
323     const union cptable *table;
324     int ret, used_tmp;
325
326     if (!src || (!dst && dstlen))
327     {
328         SetLastError( ERROR_INVALID_PARAMETER );
329         return 0;
330     }
331
332     if (srclen == -1) srclen = strlenW(src) + 1;
333
334     switch(page)
335     {
336     case CP_UTF7:
337         FIXME("UTF-7 not supported\n");
338         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
339         return 0;
340     case CP_UTF8:
341         ret = utf8_wcstombs( src, srclen, dst, dstlen );
342         break;
343     default:
344         if (!(table = get_codepage_table( page )))
345         {
346             SetLastError( ERROR_INVALID_PARAMETER );
347             return 0;
348         }
349         ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
350                            defchar, used ? &used_tmp : NULL );
351         if (used) *used = used_tmp;
352         break;
353     }
354
355     if (ret == -1)
356     {
357         SetLastError( ERROR_INSUFFICIENT_BUFFER );
358         ret = 0;
359     }
360     return ret;
361 }
362
363
364 /******************************************************************************
365  *              GetStringTypeW   (KERNEL32.@)
366  *
367  */
368 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
369 {
370     if (count == -1) count = strlenW(src) + 1;
371     switch(type)
372     {
373     case CT_CTYPE1:
374         while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
375         break;
376     case CT_CTYPE2:
377         while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
378         break;
379     case CT_CTYPE3:
380         FIXME("CT_CTYPE3 not supported.\n");
381     default:
382         SetLastError( ERROR_INVALID_PARAMETER );
383         return FALSE;
384     }
385     return TRUE;
386 }
387
388
389 /******************************************************************************
390  *              GetStringTypeExW   (KERNEL32.@)
391  */
392 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
393 {
394     /* locale is ignored for Unicode */
395     return GetStringTypeW( type, src, count, chartype );
396 }