Create at most one glue file for a single dll.
[wine/multimedia.git] / memory / codepage.c
blobfdfb03e39d551f833c9ccaf2a6add15b6a092a3c
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 switch(codepage) {
135 case CP_SYMBOL:
136 return FALSE;
137 case CP_UTF7:
138 case CP_UTF8:
139 return TRUE;
140 default:
141 return cp_get_table( codepage ) != NULL;
146 /***********************************************************************
147 * IsDBCSLeadByteEx (KERNEL32.@)
149 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
151 const union cptable *table = get_codepage_table( codepage );
152 return table && is_dbcs_leadbyte( table, testchar );
156 /***********************************************************************
157 * IsDBCSLeadByte (KERNEL32.@)
158 * IsDBCSLeadByte (KERNEL.207)
160 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
162 if (!ansi_cptable) init_codepages();
163 return is_dbcs_leadbyte( ansi_cptable, testchar );
167 /***********************************************************************
168 * GetCPInfo (KERNEL32.@)
170 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
172 const union cptable *table = get_codepage_table( codepage );
174 if (!table)
176 SetLastError( ERROR_INVALID_PARAMETER );
177 return FALSE;
179 if (table->info.def_char & 0xff00)
181 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
182 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
184 else
186 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
187 cpinfo->DefaultChar[1] = 0;
189 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
190 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
191 else
192 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
194 return TRUE;
198 /***********************************************************************
199 * EnumSystemCodePagesA (KERNEL32.@)
201 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
203 const union cptable *table;
204 char buffer[10];
205 int index = 0;
207 for (;;)
209 if (!(table = cp_enum_table( index++ ))) break;
210 sprintf( buffer, "%d", table->info.codepage );
211 if (!lpfnCodePageEnum( buffer )) break;
213 return TRUE;
217 /***********************************************************************
218 * EnumSystemCodePagesW (KERNEL32.@)
220 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
222 const union cptable *table;
223 WCHAR buffer[10], *p;
224 int page, index = 0;
226 for (;;)
228 if (!(table = cp_enum_table( index++ ))) break;
229 p = buffer + sizeof(buffer)/sizeof(WCHAR);
230 *--p = 0;
231 page = table->info.codepage;
234 *--p = '0' + (page % 10);
235 page /= 10;
236 } while( page );
237 if (!lpfnCodePageEnum( p )) break;
239 return TRUE;
243 /***********************************************************************
244 * MultiByteToWideChar (KERNEL32.@)
246 * PARAMS
247 * page [in] Codepage character set to convert from
248 * flags [in] Character mapping flags
249 * src [in] Source string buffer
250 * srclen [in] Length of source string buffer
251 * dst [in] Destination buffer
252 * dstlen [in] Length of destination buffer
254 * NOTES
255 * The returned length includes the null terminator character.
257 * RETURNS
258 * Success: If dstlen > 0, number of characters written to destination
259 * buffer. If dstlen == 0, number of characters needed to do
260 * conversion.
261 * Failure: 0. Occurs if not enough space is available.
263 * ERRORS
264 * ERROR_INSUFFICIENT_BUFFER
265 * ERROR_INVALID_PARAMETER
266 * ERROR_NO_UNICODE_TRANSLATION
269 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
270 LPWSTR dst, INT dstlen )
272 const union cptable *table;
273 int ret;
275 if (!src || (!dst && dstlen))
277 SetLastError( ERROR_INVALID_PARAMETER );
278 return 0;
281 if (srclen == -1) srclen = strlen(src) + 1;
283 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
285 switch(page)
287 case CP_UTF7:
288 FIXME("UTF not supported\n");
289 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
290 return 0;
291 case CP_UTF8:
292 ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
293 break;
294 default:
295 if (!(table = get_codepage_table( page )))
297 SetLastError( ERROR_INVALID_PARAMETER );
298 return 0;
300 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
301 break;
304 if (ret < 0)
306 switch(ret)
308 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
309 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
311 ret = 0;
313 return ret;
317 /***********************************************************************
318 * WideCharToMultiByte (KERNEL32.@)
320 * PARAMS
321 * page [in] Codepage character set to convert to
322 * flags [in] Character mapping flags
323 * src [in] Source string buffer
324 * srclen [in] Length of source string buffer
325 * dst [in] Destination buffer
326 * dstlen [in] Length of destination buffer
327 * defchar [in] Default character to use for conversion if no exact
328 * conversion can be made
329 * used [out] Set if default character was used in the conversion
331 * NOTES
332 * The returned length includes the null terminator character.
334 * RETURNS
335 * Success: If dstlen > 0, number of characters written to destination
336 * buffer. If dstlen == 0, number of characters needed to do
337 * conversion.
338 * Failure: 0. Occurs if not enough space is available.
340 * ERRORS
341 * ERROR_INSUFFICIENT_BUFFER
342 * ERROR_INVALID_PARAMETER
344 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
345 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
347 const union cptable *table;
348 int ret, used_tmp;
350 if (!src || (!dst && dstlen))
352 SetLastError( ERROR_INVALID_PARAMETER );
353 return 0;
356 if (srclen == -1) srclen = strlenW(src) + 1;
358 switch(page)
360 case CP_UTF7:
361 FIXME("UTF-7 not supported\n");
362 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
363 return 0;
364 case CP_UTF8:
365 ret = utf8_wcstombs( src, srclen, dst, dstlen );
366 break;
367 default:
368 if (!(table = get_codepage_table( page )))
370 SetLastError( ERROR_INVALID_PARAMETER );
371 return 0;
373 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
374 defchar, used ? &used_tmp : NULL );
375 if (used) *used = used_tmp;
376 break;
379 if (ret == -1)
381 SetLastError( ERROR_INSUFFICIENT_BUFFER );
382 ret = 0;
384 return ret;
388 /******************************************************************************
389 * GetStringTypeW (KERNEL32.@)
392 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
394 if (count == -1) count = strlenW(src) + 1;
395 switch(type)
397 case CT_CTYPE1:
398 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
399 break;
400 case CT_CTYPE2:
401 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
402 break;
403 case CT_CTYPE3:
405 WARN("CT_CTYPE3: semi-stub.\n");
406 while (count--)
408 int c = *src;
409 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
411 type1 = get_char_typeW( *src++ ) & 0xfff;
412 /* try to construct type3 from type1 */
413 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
414 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
415 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
416 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
417 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
418 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
419 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
421 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
422 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
423 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
424 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
425 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
426 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
427 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
428 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
430 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
431 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
432 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
433 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
434 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
435 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
436 *chartype++ = type3;
438 break;
440 default:
441 SetLastError( ERROR_INVALID_PARAMETER );
442 return FALSE;
444 return TRUE;
448 /******************************************************************************
449 * GetStringTypeExW (KERNEL32.@)
451 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
453 /* locale is ignored for Unicode */
454 return GetStringTypeW( type, src, count, chartype );
457 /******************************************************************************
458 * GetStringTypeA [KERNEL32.@]
460 BOOL WINAPI GetStringTypeA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
462 char buf[20];
463 UINT cp;
464 INT countW;
465 LPWSTR srcW;
466 BOOL ret = FALSE;
468 if(count == -1) count = strlen(src) + 1;
470 if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
471 buf, sizeof(buf)))
473 FIXME("For locale %04lx using current ANSI code page\n", locale);
474 cp = GetACP();
476 else
477 cp = atoi(buf);
479 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
480 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
482 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
484 * NOTE: the target buffer has 1 word for each CHARACTER in the source
485 * string, with multibyte characters there maybe be more bytes in count
486 * than character space in the buffer!
488 ret = GetStringTypeW(type, srcW, countW, chartype);
489 HeapFree(GetProcessHeap(), 0, srcW);
491 return ret;
494 /******************************************************************************
495 * GetStringTypeExA [KERNEL32.@]
497 BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
499 return GetStringTypeA(locale, type, src, count, chartype);