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"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(string
);
35 /* current code pages */
36 static const union cptable
*ansi_cptable
;
37 static const union cptable
*oem_cptable
;
38 static const union cptable
*mac_cptable
;
39 static const union cptable
*unix_cptable
; /* NULL if UTF8 */
40 static LCID default_lcid
= MAKELCID( MAKELANGID(LANG_ENGLISH
,SUBLANG_DEFAULT
), SORT_DEFAULT
);
42 /* setup default codepage info before we can get at the locale stuff */
43 static void init_codepages(void)
45 ansi_cptable
= wine_cp_get_table( 1252 );
46 oem_cptable
= wine_cp_get_table( 437 );
47 mac_cptable
= wine_cp_get_table( 10000 );
48 unix_cptable
= wine_cp_get_table( 28591 );
49 assert( ansi_cptable
);
50 assert( oem_cptable
);
51 assert( mac_cptable
);
52 assert( unix_cptable
);
55 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
56 static const union cptable
*get_codepage_table( unsigned int codepage
)
58 const union cptable
*ret
= NULL
;
60 if (!ansi_cptable
) init_codepages();
74 if (!(codepage
= NtCurrentTeb()->code_page
)) return ansi_cptable
;
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
= wine_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( UINT ansi_cp
, UINT oem_cp
, UINT mac_cp
, UINT unix_cp
, LCID lcid
)
91 extern void __wine_init_codepages( const union cptable
*ansi_cp
, const union cptable
*oem_cp
);
92 const union cptable
*table
;
95 if (!ansi_cptable
) init_codepages(); /* just in case */
97 if ((table
= wine_cp_get_table( ansi_cp
))) ansi_cptable
= table
;
98 if ((table
= wine_cp_get_table( oem_cp
))) oem_cptable
= table
;
99 if ((table
= wine_cp_get_table( mac_cp
))) mac_cptable
= table
;
100 if (unix_cp
== CP_UTF8
)
102 else if ((table
= wine_cp_get_table( unix_cp
)))
103 unix_cptable
= table
;
105 __wine_init_codepages( ansi_cptable
, oem_cptable
);
107 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
108 ansi_cptable
->info
.codepage
, oem_cptable
->info
.codepage
,
109 mac_cptable
->info
.codepage
, unix_cp
);
112 /******************************************************************************
113 * GetACP (KERNEL32.@)
116 * Current ANSI code-page identifier, default if no current defined
118 UINT WINAPI
GetACP(void)
120 if (!ansi_cptable
) return 1252;
121 return ansi_cptable
->info
.codepage
;
125 /***********************************************************************
126 * GetOEMCP (KERNEL32.@)
128 UINT WINAPI
GetOEMCP(void)
130 if (!oem_cptable
) return 437;
131 return oem_cptable
->info
.codepage
;
135 /***********************************************************************
136 * IsValidCodePage (KERNEL32.@)
138 BOOL WINAPI
IsValidCodePage( UINT codepage
)
147 return wine_cp_get_table( codepage
) != NULL
;
152 /***********************************************************************
153 * GetUserDefaultLangID (KERNEL32.@)
155 LANGID WINAPI
GetUserDefaultLangID(void)
157 return LANGIDFROMLCID(default_lcid
);
161 /***********************************************************************
162 * GetSystemDefaultLangID (KERNEL32.@)
164 LANGID WINAPI
GetSystemDefaultLangID(void)
166 return GetUserDefaultLangID();
170 /***********************************************************************
171 * GetUserDefaultLCID (KERNEL32.@)
173 LCID WINAPI
GetUserDefaultLCID(void)
179 /***********************************************************************
180 * GetSystemDefaultLCID (KERNEL32.@)
182 LCID WINAPI
GetSystemDefaultLCID(void)
184 return GetUserDefaultLCID();
188 /***********************************************************************
189 * GetUserDefaultUILanguage (KERNEL32.@)
191 LANGID WINAPI
GetUserDefaultUILanguage(void)
193 return GetUserDefaultLangID();
197 /***********************************************************************
198 * GetSystemDefaultUILanguage (KERNEL32.@)
200 LANGID WINAPI
GetSystemDefaultUILanguage(void)
202 return GetSystemDefaultLangID();
206 /***********************************************************************
207 * IsDBCSLeadByteEx (KERNEL32.@)
209 BOOL WINAPI
IsDBCSLeadByteEx( UINT codepage
, BYTE testchar
)
211 const union cptable
*table
= get_codepage_table( codepage
);
212 return table
&& is_dbcs_leadbyte( table
, testchar
);
216 /***********************************************************************
217 * IsDBCSLeadByte (KERNEL32.@)
218 * IsDBCSLeadByte (KERNEL.207)
220 BOOL WINAPI
IsDBCSLeadByte( BYTE testchar
)
222 if (!ansi_cptable
) return FALSE
;
223 return is_dbcs_leadbyte( ansi_cptable
, testchar
);
227 /***********************************************************************
228 * GetCPInfo (KERNEL32.@)
230 BOOL WINAPI
GetCPInfo( UINT codepage
, LPCPINFO cpinfo
)
232 const union cptable
*table
= get_codepage_table( codepage
);
236 SetLastError( ERROR_INVALID_PARAMETER
);
239 if (table
->info
.def_char
& 0xff00)
241 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff00;
242 cpinfo
->DefaultChar
[1] = table
->info
.def_char
& 0x00ff;
246 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff;
247 cpinfo
->DefaultChar
[1] = 0;
249 if ((cpinfo
->MaxCharSize
= table
->info
.char_size
) == 2)
250 memcpy( cpinfo
->LeadByte
, table
->dbcs
.lead_bytes
, sizeof(cpinfo
->LeadByte
) );
252 cpinfo
->LeadByte
[0] = cpinfo
->LeadByte
[1] = 0;
258 /***********************************************************************
259 * EnumSystemCodePagesA (KERNEL32.@)
261 BOOL WINAPI
EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum
, DWORD flags
)
263 const union cptable
*table
;
269 if (!(table
= wine_cp_enum_table( index
++ ))) break;
270 sprintf( buffer
, "%d", table
->info
.codepage
);
271 if (!lpfnCodePageEnum( buffer
)) break;
277 /***********************************************************************
278 * EnumSystemCodePagesW (KERNEL32.@)
280 BOOL WINAPI
EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum
, DWORD flags
)
282 const union cptable
*table
;
283 WCHAR buffer
[10], *p
;
288 if (!(table
= wine_cp_enum_table( index
++ ))) break;
289 p
= buffer
+ sizeof(buffer
)/sizeof(WCHAR
);
291 page
= table
->info
.codepage
;
294 *--p
= '0' + (page
% 10);
297 if (!lpfnCodePageEnum( p
)) break;
303 /***********************************************************************
304 * MultiByteToWideChar (KERNEL32.@)
307 * page [in] Codepage character set to convert from
308 * flags [in] Character mapping flags
309 * src [in] Source string buffer
310 * srclen [in] Length of source string buffer
311 * dst [in] Destination buffer
312 * dstlen [in] Length of destination buffer
315 * The returned length includes the null terminator character.
318 * Success: If dstlen > 0, number of characters written to destination
319 * buffer. If dstlen == 0, number of characters needed to do
321 * Failure: 0. Occurs if not enough space is available.
324 * ERROR_INSUFFICIENT_BUFFER
325 * ERROR_INVALID_PARAMETER
326 * ERROR_NO_UNICODE_TRANSLATION
329 INT WINAPI
MultiByteToWideChar( UINT page
, DWORD flags
, LPCSTR src
, INT srclen
,
330 LPWSTR dst
, INT dstlen
)
332 const union cptable
*table
;
335 if (!src
|| (!dst
&& dstlen
))
337 SetLastError( ERROR_INVALID_PARAMETER
);
341 if (srclen
< 0) srclen
= strlen(src
) + 1;
343 if (flags
& MB_USEGLYPHCHARS
) FIXME("MB_USEGLYPHCHARS not supported\n");
348 FIXME("UTF-7 not supported\n");
349 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
354 ret
= wine_cp_mbstowcs( unix_cptable
, flags
, src
, srclen
, dst
, dstlen
);
359 ret
= wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
362 if (!(table
= get_codepage_table( page
)))
364 SetLastError( ERROR_INVALID_PARAMETER
);
367 ret
= wine_cp_mbstowcs( table
, flags
, src
, srclen
, dst
, dstlen
);
375 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER
); break;
376 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION
); break;
384 /***********************************************************************
385 * WideCharToMultiByte (KERNEL32.@)
388 * page [in] Codepage character set to convert to
389 * flags [in] Character mapping flags
390 * src [in] Source string buffer
391 * srclen [in] Length of source string buffer
392 * dst [in] Destination buffer
393 * dstlen [in] Length of destination buffer
394 * defchar [in] Default character to use for conversion if no exact
395 * conversion can be made
396 * used [out] Set if default character was used in the conversion
399 * The returned length includes the null terminator character.
402 * Success: If dstlen > 0, number of characters written to destination
403 * buffer. If dstlen == 0, number of characters needed to do
405 * Failure: 0. Occurs if not enough space is available.
408 * ERROR_INSUFFICIENT_BUFFER
409 * ERROR_INVALID_PARAMETER
411 INT WINAPI
WideCharToMultiByte( UINT page
, DWORD flags
, LPCWSTR src
, INT srclen
,
412 LPSTR dst
, INT dstlen
, LPCSTR defchar
, BOOL
*used
)
414 const union cptable
*table
;
417 if (!src
|| (!dst
&& dstlen
))
419 SetLastError( ERROR_INVALID_PARAMETER
);
423 if (srclen
< 0) srclen
= strlenW(src
) + 1;
428 FIXME("UTF-7 not supported\n");
429 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
434 ret
= wine_cp_wcstombs( unix_cptable
, flags
, src
, srclen
, dst
, dstlen
,
435 defchar
, used
? &used_tmp
: NULL
);
440 ret
= wine_utf8_wcstombs( src
, srclen
, dst
, dstlen
);
443 if (!(table
= get_codepage_table( page
)))
445 SetLastError( ERROR_INVALID_PARAMETER
);
448 ret
= wine_cp_wcstombs( table
, flags
, src
, srclen
, dst
, dstlen
,
449 defchar
, used
? &used_tmp
: NULL
);
450 if (used
) *used
= used_tmp
;
456 SetLastError( ERROR_INSUFFICIENT_BUFFER
);