Fixed compile if we do not have the xf86vidmode extension.
[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  */
135 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
136 {
137     if (!ansi_cptable) init_codepages();
138     return is_dbcs_leadbyte( ansi_cptable, testchar );
139 }
140
141
142 /***********************************************************************
143  *           GetCPInfo   (KERNEL32)
144  */
145 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
146 {
147     const union cptable *table = get_codepage_table( codepage );
148
149     if (!table) 
150     {
151         SetLastError( ERROR_INVALID_PARAMETER );
152         return FALSE;
153     }
154     if (table->info.def_char & 0xff00)
155     {
156         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
157         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
158     }
159     else
160     {
161         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
162         cpinfo->DefaultChar[1] = 0;
163     }
164     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
165         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
166     else
167         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
168
169     return TRUE;
170 }
171
172
173 /***********************************************************************
174  *              EnumSystemCodePagesA   (KERNEL32)
175  */
176 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
177 {
178     const union cptable *table;
179     char buffer[10];
180     int index = 0;
181
182     for (;;)
183     {
184         if (!(table = cp_enum_table( index++ ))) break;
185         sprintf( buffer, "%d", table->info.codepage );
186         if (!lpfnCodePageEnum( buffer )) break;
187     }
188     return TRUE;
189 }
190
191
192 /***********************************************************************
193  *              EnumSystemCodePagesW   (KERNEL32)
194  */
195 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
196 {
197     const union cptable *table;
198     WCHAR buffer[10], *p;
199     int page, index = 0;
200
201     for (;;)
202     {
203         if (!(table = cp_enum_table( index++ ))) break;
204         p = buffer + sizeof(buffer)/sizeof(WCHAR);
205         *--p = 0;
206         page = table->info.codepage;
207         do
208         {
209             *--p = '0' + (page % 10);
210             page /= 10;
211         } while( page );
212         if (!lpfnCodePageEnum( p )) break;
213     }
214     return TRUE;
215 }
216
217
218 /***********************************************************************
219  *              MultiByteToWideChar   (KERNEL32)
220  *
221  * PARAMS
222  *   page [in]    Codepage character set to convert from
223  *   flags [in]   Character mapping flags
224  *   src [in]     Source string buffer
225  *   srclen [in]  Length of source string buffer
226  *   dst [in]     Destination buffer
227  *   dstlen [in]  Length of destination buffer
228  *
229  * NOTES
230  *   The returned length includes the null terminator character.
231  *
232  * RETURNS
233  *   Success: If dstlen > 0, number of characters written to destination
234  *            buffer.  If dstlen == 0, number of characters needed to do
235  *            conversion.
236  *   Failure: 0. Occurs if not enough space is available.
237  *
238  * ERRORS
239  *   ERROR_INSUFFICIENT_BUFFER
240  *   ERROR_INVALID_PARAMETER
241  *   ERROR_NO_UNICODE_TRANSLATION
242  *
243  */
244 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
245                                 LPWSTR dst, INT dstlen )
246 {
247     const union cptable *table;
248     int ret;
249
250     if (!src || (!dst && dstlen))
251     {
252         SetLastError( ERROR_INVALID_PARAMETER );
253         return 0;
254     }
255
256     if (srclen == -1) srclen = strlen(src) + 1;
257
258     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
259
260     switch(page)
261     {
262     case CP_UTF7:
263         FIXME("UTF not supported\n");
264         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
265         return 0;
266     case CP_UTF8:
267         ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
268         break;
269     default:
270         if (!(table = get_codepage_table( page )))
271         {
272             SetLastError( ERROR_INVALID_PARAMETER );
273             return 0;
274         }
275         ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
276         break;
277     }
278
279     if (ret < 0)
280     {
281         switch(ret)
282         {
283         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
284         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
285         }
286         ret = 0;
287     }
288     return ret;
289 }
290
291
292 /***********************************************************************
293  *              WideCharToMultiByte   (KERNEL32)
294  *
295  * PARAMS
296  *   page [in]    Codepage character set to convert to
297  *   flags [in]   Character mapping flags
298  *   src [in]     Source string buffer
299  *   srclen [in]  Length of source string buffer
300  *   dst [in]     Destination buffer
301  *   dstlen [in]  Length of destination buffer
302  *   defchar [in] Default character to use for conversion if no exact
303  *                  conversion can be made
304  *   used [out]   Set if default character was used in the conversion
305  *
306  * NOTES
307  *   The returned length includes the null terminator character.
308  *
309  * RETURNS
310  *   Success: If dstlen > 0, number of characters written to destination
311  *            buffer.  If dstlen == 0, number of characters needed to do
312  *            conversion.
313  *   Failure: 0. Occurs if not enough space is available.
314  *
315  * ERRORS
316  *   ERROR_INSUFFICIENT_BUFFER
317  *   ERROR_INVALID_PARAMETER
318  */
319 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
320                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
321 {
322     const union cptable *table;
323     int ret, used_tmp;
324
325     if (!src || (!dst && dstlen))
326     {
327         SetLastError( ERROR_INVALID_PARAMETER );
328         return 0;
329     }
330
331     if (srclen == -1) srclen = strlenW(src) + 1;
332
333     switch(page)
334     {
335     case CP_UTF7:
336         FIXME("UTF-7 not supported\n");
337         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
338         return 0;
339     case CP_UTF8:
340         ret = utf8_wcstombs( src, srclen, dst, dstlen );
341         break;
342     default:
343         if (!(table = get_codepage_table( page )))
344         {
345             SetLastError( ERROR_INVALID_PARAMETER );
346             return 0;
347         }
348         ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
349                            defchar, used ? &used_tmp : NULL );
350         if (used) *used = used_tmp;
351         break;
352     }
353
354     if (ret == -1)
355     {
356         SetLastError( ERROR_INSUFFICIENT_BUFFER );
357         ret = 0;
358     }
359     return ret;
360 }
361
362
363 /******************************************************************************
364  *              GetStringTypeW   (KERNEL32)
365  *
366  */
367 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
368 {
369     if (count == -1) count = strlenW(src) + 1;
370     switch(type)
371     {
372     case CT_CTYPE1:
373         while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
374         break;
375     case CT_CTYPE2:
376         while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
377         break;
378     case CT_CTYPE3:
379         FIXME("CT_CTYPE3 not supported.\n");
380     default:
381         SetLastError( ERROR_INVALID_PARAMETER );
382         return FALSE;
383     }
384     return TRUE;
385 }
386
387
388 /******************************************************************************
389  *              GetStringTypeExW   (KERNEL32)
390  */
391 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
392 {
393     /* locale is ignored for Unicode */
394     return GetStringTypeW( type, src, count, chartype );
395 }