RawModeSwitch can now be used to return DPMI application back to
[wine/multimedia.git] / memory / codepage.c
blob21915ad1d5498922bf295fbd635a9776fb8331b6
1 /*
2 * Code page functions
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(string);
34 /* current code pages */
35 static const union cptable *ansi_cptable;
36 static const union cptable *oem_cptable;
37 static const union cptable *mac_cptable;
39 /* retrieve a code page table from the locale info */
40 static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
42 const union cptable *table = NULL;
43 char buf[32];
45 if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
46 return table;
49 /* setup default codepage info before we can get at the locale stuff */
50 static void init_codepages(void)
52 ansi_cptable = cp_get_table( 1252 );
53 oem_cptable = cp_get_table( 437 );
54 mac_cptable = cp_get_table( 10000 );
55 assert( ansi_cptable );
56 assert( oem_cptable );
57 assert( mac_cptable );
60 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
61 static const union cptable *get_codepage_table( unsigned int codepage )
63 const union cptable *ret = NULL;
65 if (!ansi_cptable) init_codepages();
67 switch(codepage)
69 case CP_ACP: return ansi_cptable;
70 case CP_OEMCP: return oem_cptable;
71 case CP_MACCP: return mac_cptable;
72 case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
73 case CP_UTF7:
74 case CP_UTF8:
75 break;
76 default:
77 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
78 if (codepage == oem_cptable->info.codepage) return oem_cptable;
79 if (codepage == mac_cptable->info.codepage) return mac_cptable;
80 ret = cp_get_table( codepage );
81 break;
83 return ret;
86 /* initialize default code pages from locale info */
87 /* FIXME: should be done in init_codepages, but it can't right now */
88 /* since it needs KERNEL32 to be loaded for the locale info. */
89 void CODEPAGE_Init(void)
91 extern void __wine_init_codepages( const union cptable *ansi, const union cptable *oem );
92 const union cptable *table;
93 LCID lcid = GetUserDefaultLCID();
95 if (!ansi_cptable) init_codepages(); /* just in case */
97 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE ))) ansi_cptable = table;
98 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE ))) mac_cptable = table;
99 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE ))) oem_cptable = table;
100 __wine_init_codepages( ansi_cptable, oem_cptable );
102 TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
103 oem_cptable->info.codepage, mac_cptable->info.codepage );
106 /******************************************************************************
107 * GetACP (KERNEL32.@)
109 * RETURNS
110 * Current ANSI code-page identifier, default if no current defined
112 UINT WINAPI GetACP(void)
114 if (!ansi_cptable) init_codepages();
115 return ansi_cptable->info.codepage;
119 /***********************************************************************
120 * GetOEMCP (KERNEL32.@)
122 UINT WINAPI GetOEMCP(void)
124 if (!oem_cptable) init_codepages();
125 return oem_cptable->info.codepage;
129 /***********************************************************************
130 * IsValidCodePage (KERNEL32.@)
132 BOOL WINAPI IsValidCodePage( UINT codepage )
134 return cp_get_table( codepage ) != NULL;
138 /***********************************************************************
139 * IsDBCSLeadByteEx (KERNEL32.@)
141 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
143 const union cptable *table = get_codepage_table( codepage );
144 return table && is_dbcs_leadbyte( table, testchar );
148 /***********************************************************************
149 * IsDBCSLeadByte (KERNEL32.@)
150 * IsDBCSLeadByte (KERNEL.207)
152 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
154 if (!ansi_cptable) init_codepages();
155 return is_dbcs_leadbyte( ansi_cptable, testchar );
159 /***********************************************************************
160 * GetCPInfo (KERNEL32.@)
162 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
164 const union cptable *table = get_codepage_table( codepage );
166 if (!table)
168 SetLastError( ERROR_INVALID_PARAMETER );
169 return FALSE;
171 if (table->info.def_char & 0xff00)
173 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
174 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
176 else
178 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
179 cpinfo->DefaultChar[1] = 0;
181 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
182 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
183 else
184 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
186 return TRUE;
190 /***********************************************************************
191 * EnumSystemCodePagesA (KERNEL32.@)
193 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
195 const union cptable *table;
196 char buffer[10];
197 int index = 0;
199 for (;;)
201 if (!(table = cp_enum_table( index++ ))) break;
202 sprintf( buffer, "%d", table->info.codepage );
203 if (!lpfnCodePageEnum( buffer )) break;
205 return TRUE;
209 /***********************************************************************
210 * EnumSystemCodePagesW (KERNEL32.@)
212 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
214 const union cptable *table;
215 WCHAR buffer[10], *p;
216 int page, index = 0;
218 for (;;)
220 if (!(table = cp_enum_table( index++ ))) break;
221 p = buffer + sizeof(buffer)/sizeof(WCHAR);
222 *--p = 0;
223 page = table->info.codepage;
226 *--p = '0' + (page % 10);
227 page /= 10;
228 } while( page );
229 if (!lpfnCodePageEnum( p )) break;
231 return TRUE;
235 /***********************************************************************
236 * MultiByteToWideChar (KERNEL32.@)
238 * PARAMS
239 * page [in] Codepage character set to convert from
240 * flags [in] Character mapping flags
241 * src [in] Source string buffer
242 * srclen [in] Length of source string buffer
243 * dst [in] Destination buffer
244 * dstlen [in] Length of destination buffer
246 * NOTES
247 * The returned length includes the null terminator character.
249 * RETURNS
250 * Success: If dstlen > 0, number of characters written to destination
251 * buffer. If dstlen == 0, number of characters needed to do
252 * conversion.
253 * Failure: 0. Occurs if not enough space is available.
255 * ERRORS
256 * ERROR_INSUFFICIENT_BUFFER
257 * ERROR_INVALID_PARAMETER
258 * ERROR_NO_UNICODE_TRANSLATION
261 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
262 LPWSTR dst, INT dstlen )
264 const union cptable *table;
265 int ret;
267 if (!src || (!dst && dstlen))
269 SetLastError( ERROR_INVALID_PARAMETER );
270 return 0;
273 if (srclen == -1) srclen = strlen(src) + 1;
275 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
277 switch(page)
279 case CP_UTF7:
280 FIXME("UTF not supported\n");
281 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
282 return 0;
283 case CP_UTF8:
284 ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
285 break;
286 default:
287 if (!(table = get_codepage_table( page )))
289 SetLastError( ERROR_INVALID_PARAMETER );
290 return 0;
292 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
293 break;
296 if (ret < 0)
298 switch(ret)
300 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
301 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
303 ret = 0;
305 return ret;
309 /***********************************************************************
310 * WideCharToMultiByte (KERNEL32.@)
312 * PARAMS
313 * page [in] Codepage character set to convert to
314 * flags [in] Character mapping flags
315 * src [in] Source string buffer
316 * srclen [in] Length of source string buffer
317 * dst [in] Destination buffer
318 * dstlen [in] Length of destination buffer
319 * defchar [in] Default character to use for conversion if no exact
320 * conversion can be made
321 * used [out] Set if default character was used in the conversion
323 * NOTES
324 * The returned length includes the null terminator character.
326 * RETURNS
327 * Success: If dstlen > 0, number of characters written to destination
328 * buffer. If dstlen == 0, number of characters needed to do
329 * conversion.
330 * Failure: 0. Occurs if not enough space is available.
332 * ERRORS
333 * ERROR_INSUFFICIENT_BUFFER
334 * ERROR_INVALID_PARAMETER
336 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
337 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
339 const union cptable *table;
340 int ret, used_tmp;
342 if (!src || (!dst && dstlen))
344 SetLastError( ERROR_INVALID_PARAMETER );
345 return 0;
348 if (srclen == -1) srclen = strlenW(src) + 1;
350 switch(page)
352 case CP_UTF7:
353 FIXME("UTF-7 not supported\n");
354 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
355 return 0;
356 case CP_UTF8:
357 ret = utf8_wcstombs( src, srclen, dst, dstlen );
358 break;
359 default:
360 if (!(table = get_codepage_table( page )))
362 SetLastError( ERROR_INVALID_PARAMETER );
363 return 0;
365 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
366 defchar, used ? &used_tmp : NULL );
367 if (used) *used = used_tmp;
368 break;
371 if (ret == -1)
373 SetLastError( ERROR_INSUFFICIENT_BUFFER );
374 ret = 0;
376 return ret;
380 /******************************************************************************
381 * GetStringTypeW (KERNEL32.@)
384 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
386 if (count == -1) count = strlenW(src) + 1;
387 switch(type)
389 case CT_CTYPE1:
390 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
391 break;
392 case CT_CTYPE2:
393 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
394 break;
395 case CT_CTYPE3:
396 FIXME("CT_CTYPE3 not supported.\n");
397 default:
398 SetLastError( ERROR_INVALID_PARAMETER );
399 return FALSE;
401 return TRUE;
405 /******************************************************************************
406 * GetStringTypeExW (KERNEL32.@)
408 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
410 /* locale is ignored for Unicode */
411 return GetStringTypeW( type, src, count, chartype );
414 /******************************************************************************
415 * GetStringTypeA [KERNEL32.@]
417 BOOL WINAPI GetStringTypeA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
419 char buf[20];
420 UINT cp;
421 INT countW;
422 LPWSTR srcW;
423 BOOL ret = FALSE;
425 if(count == -1) count = strlen(src) + 1;
427 if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
428 buf, sizeof(buf)))
430 FIXME("For locale %04lx using current ANSI code page\n", locale);
431 cp = GetACP();
433 else
434 cp = atoi(buf);
436 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
437 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
439 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
440 ret = GetStringTypeW(type, srcW, count, chartype);
441 HeapFree(GetProcessHeap(), 0, srcW);
443 return ret;
446 /******************************************************************************
447 * GetStringTypeExA [KERNEL32.@]
449 BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
451 return GetStringTypeA(locale, type, src, count, chartype);