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
)
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
);
168 SetLastError( ERROR_INVALID_PARAMETER
);
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;
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
) );
184 cpinfo
->LeadByte
[0] = cpinfo
->LeadByte
[1] = 0;
190 /***********************************************************************
191 * EnumSystemCodePagesA (KERNEL32.@)
193 BOOL WINAPI
EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum
, DWORD flags
)
195 const union cptable
*table
;
201 if (!(table
= cp_enum_table( index
++ ))) break;
202 sprintf( buffer
, "%d", table
->info
.codepage
);
203 if (!lpfnCodePageEnum( buffer
)) break;
209 /***********************************************************************
210 * EnumSystemCodePagesW (KERNEL32.@)
212 BOOL WINAPI
EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum
, DWORD flags
)
214 const union cptable
*table
;
215 WCHAR buffer
[10], *p
;
220 if (!(table
= cp_enum_table( index
++ ))) break;
221 p
= buffer
+ sizeof(buffer
)/sizeof(WCHAR
);
223 page
= table
->info
.codepage
;
226 *--p
= '0' + (page
% 10);
229 if (!lpfnCodePageEnum( p
)) break;
235 /***********************************************************************
236 * MultiByteToWideChar (KERNEL32.@)
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
247 * The returned length includes the null terminator character.
250 * Success: If dstlen > 0, number of characters written to destination
251 * buffer. If dstlen == 0, number of characters needed to do
253 * Failure: 0. Occurs if not enough space is available.
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
;
267 if (!src
|| (!dst
&& dstlen
))
269 SetLastError( ERROR_INVALID_PARAMETER
);
273 if (srclen
== -1) srclen
= strlen(src
) + 1;
275 if (flags
& MB_USEGLYPHCHARS
) FIXME("MB_USEGLYPHCHARS not supported\n");
280 FIXME("UTF not supported\n");
281 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
284 ret
= utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
287 if (!(table
= get_codepage_table( page
)))
289 SetLastError( ERROR_INVALID_PARAMETER
);
292 ret
= cp_mbstowcs( table
, flags
, src
, srclen
, dst
, dstlen
);
300 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER
); break;
301 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION
); break;
309 /***********************************************************************
310 * WideCharToMultiByte (KERNEL32.@)
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
324 * The returned length includes the null terminator character.
327 * Success: If dstlen > 0, number of characters written to destination
328 * buffer. If dstlen == 0, number of characters needed to do
330 * Failure: 0. Occurs if not enough space is available.
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
;
342 if (!src
|| (!dst
&& dstlen
))
344 SetLastError( ERROR_INVALID_PARAMETER
);
348 if (srclen
== -1) srclen
= strlenW(src
) + 1;
353 FIXME("UTF-7 not supported\n");
354 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
357 ret
= utf8_wcstombs( src
, srclen
, dst
, dstlen
);
360 if (!(table
= get_codepage_table( page
)))
362 SetLastError( ERROR_INVALID_PARAMETER
);
365 ret
= cp_wcstombs( table
, flags
, src
, srclen
, dst
, dstlen
,
366 defchar
, used
? &used_tmp
: NULL
);
367 if (used
) *used
= used_tmp
;
373 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
380 /******************************************************************************
381 * GetStringTypeW (KERNEL32.@)
384 BOOL WINAPI
GetStringTypeW( DWORD type
, LPCWSTR src
, INT count
, LPWORD chartype
)
386 if (count
== -1) count
= strlenW(src
) + 1;
390 while (count
--) *chartype
++ = get_char_typeW( *src
++ ) & 0xfff;
393 while (count
--) *chartype
++ = get_char_typeW( *src
++ ) >> 12;
396 FIXME("CT_CTYPE3 not supported.\n");
398 SetLastError( ERROR_INVALID_PARAMETER
);
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
)
425 if(count
== -1) count
= strlen(src
) + 1;
427 if(!GetLocaleInfoA(locale
, LOCALE_IDEFAULTANSICODEPAGE
| LOCALE_NOUSEROVERRIDE
,
430 FIXME("For locale %04lx using current ANSI code page\n", locale
);
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
);
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
);