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
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;
45 if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
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();
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 );
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 );
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.@)
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 )
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 );
176 SetLastError( ERROR_INVALID_PARAMETER );
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;
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) );
192 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
198 /***********************************************************************
199 * EnumSystemCodePagesA (KERNEL32.@)
201 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
203 const union cptable *table;
209 if (!(table = cp_enum_table( index++ ))) break;
210 sprintf( buffer, "%d", table->info.codepage );
211 if (!lpfnCodePageEnum( buffer )) break;
217 /***********************************************************************
218 * EnumSystemCodePagesW (KERNEL32.@)
220 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
222 const union cptable *table;
223 WCHAR buffer[10], *p;
228 if (!(table = cp_enum_table( index++ ))) break;
229 p = buffer + sizeof(buffer)/sizeof(WCHAR);
231 page = table->info.codepage;
234 *--p = '0' + (page % 10);
237 if (!lpfnCodePageEnum( p )) break;
243 /***********************************************************************
244 * MultiByteToWideChar (KERNEL32.@)
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
255 * The returned length includes the null terminator character.
258 * Success: If dstlen > 0, number of characters written to destination
259 * buffer. If dstlen == 0, number of characters needed to do
261 * Failure: 0. Occurs if not enough space is available.
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;
275 if (!src || (!dst && dstlen))
277 SetLastError( ERROR_INVALID_PARAMETER );
281 if (srclen == -1) srclen = strlen(src) + 1;
283 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
288 FIXME("UTF not supported\n");
289 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
292 ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
295 if (!(table = get_codepage_table( page )))
297 SetLastError( ERROR_INVALID_PARAMETER );
300 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
308 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
309 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
317 /***********************************************************************
318 * WideCharToMultiByte (KERNEL32.@)
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
332 * The returned length includes the null terminator character.
335 * Success: If dstlen > 0, number of characters written to destination
336 * buffer. If dstlen == 0, number of characters needed to do
338 * Failure: 0. Occurs if not enough space is available.
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;
350 if (!src || (!dst && dstlen))
352 SetLastError( ERROR_INVALID_PARAMETER );
356 if (srclen == -1) srclen = strlenW(src) + 1;
361 FIXME("UTF-7 not supported\n");
362 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
365 ret = utf8_wcstombs( src, srclen, dst, dstlen );
368 if (!(table = get_codepage_table( page )))
370 SetLastError( ERROR_INVALID_PARAMETER );
373 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
374 defchar, used ? &used_tmp : NULL );
375 if (used) *used = used_tmp;
381 SetLastError( ERROR_INSUFFICIENT_BUFFER );
388 /******************************************************************************
389 * GetStringTypeW (KERNEL32.@)
392 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
394 if (count == -1) count = strlenW(src) + 1;
398 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
401 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
405 WARN("CT_CTYPE3: semi-stub.\n");
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;
441 SetLastError( ERROR_INVALID_PARAMETER );
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)
468 if(count == -1) count = strlen(src) + 1;
470 if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
473 FIXME("For locale %04lx using current ANSI code page\n", locale);
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);
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);