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 LCID default_lcid
= MAKELCID( MAKELANGID(LANG_ENGLISH
,SUBLANG_DEFAULT
), SORT_DEFAULT
);
41 /* setup default codepage info before we can get at the locale stuff */
42 static void init_codepages(void)
44 ansi_cptable
= cp_get_table( 1252 );
45 oem_cptable
= cp_get_table( 437 );
46 mac_cptable
= cp_get_table( 10000 );
47 assert( ansi_cptable
);
48 assert( oem_cptable
);
49 assert( mac_cptable
);
52 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
53 static const union cptable
*get_codepage_table( unsigned int codepage
)
55 const union cptable
*ret
= NULL
;
57 if (!ansi_cptable
) init_codepages();
71 if (!(codepage
= NtCurrentTeb()->code_page
)) return ansi_cptable
;
74 if (codepage
== ansi_cptable
->info
.codepage
) return ansi_cptable
;
75 if (codepage
== oem_cptable
->info
.codepage
) return oem_cptable
;
76 if (codepage
== mac_cptable
->info
.codepage
) return mac_cptable
;
77 ret
= cp_get_table( codepage
);
83 /* initialize default code pages from locale info */
84 /* FIXME: should be done in init_codepages, but it can't right now */
85 /* since it needs KERNEL32 to be loaded for the locale info. */
86 void CODEPAGE_Init( UINT ansi
, UINT oem
, UINT mac
, LCID lcid
)
88 extern void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
);
89 const union cptable
*table
;
92 if (!ansi_cptable
) init_codepages(); /* just in case */
94 if ((table
= cp_get_table( ansi
))) ansi_cptable
= table
;
95 if ((table
= cp_get_table( oem
))) oem_cptable
= table
;
96 if ((table
= cp_get_table( mac
))) mac_cptable
= table
;
97 __wine_init_codepages( ansi_cptable
, oem_cptable
);
99 TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable
->info
.codepage
,
100 oem_cptable
->info
.codepage
, mac_cptable
->info
.codepage
);
103 /******************************************************************************
104 * GetACP (KERNEL32.@)
107 * Current ANSI code-page identifier, default if no current defined
109 UINT WINAPI
GetACP(void)
111 if (!ansi_cptable
) return 1252;
112 return ansi_cptable
->info
.codepage
;
116 /***********************************************************************
117 * GetOEMCP (KERNEL32.@)
119 UINT WINAPI
GetOEMCP(void)
121 if (!oem_cptable
) return 437;
122 return oem_cptable
->info
.codepage
;
126 /***********************************************************************
127 * IsValidCodePage (KERNEL32.@)
129 BOOL WINAPI
IsValidCodePage( UINT codepage
)
138 return cp_get_table( codepage
) != NULL
;
143 /***********************************************************************
144 * GetUserDefaultLangID (KERNEL32.@)
146 LANGID WINAPI
GetUserDefaultLangID(void)
148 return LANGIDFROMLCID(default_lcid
);
152 /***********************************************************************
153 * GetSystemDefaultLangID (KERNEL32.@)
155 LANGID WINAPI
GetSystemDefaultLangID(void)
157 return GetUserDefaultLangID();
161 /***********************************************************************
162 * GetUserDefaultLCID (KERNEL32.@)
164 LCID WINAPI
GetUserDefaultLCID(void)
170 /***********************************************************************
171 * GetSystemDefaultLCID (KERNEL32.@)
173 LCID WINAPI
GetSystemDefaultLCID(void)
175 return GetUserDefaultLCID();
179 /***********************************************************************
180 * GetUserDefaultUILanguage (KERNEL32.@)
182 LANGID WINAPI
GetUserDefaultUILanguage(void)
184 return GetUserDefaultLangID();
188 /***********************************************************************
189 * GetSystemDefaultUILanguage (KERNEL32.@)
191 LANGID WINAPI
GetSystemDefaultUILanguage(void)
193 return GetSystemDefaultLangID();
197 /***********************************************************************
198 * IsDBCSLeadByteEx (KERNEL32.@)
200 BOOL WINAPI
IsDBCSLeadByteEx( UINT codepage
, BYTE testchar
)
202 const union cptable
*table
= get_codepage_table( codepage
);
203 return table
&& is_dbcs_leadbyte( table
, testchar
);
207 /***********************************************************************
208 * IsDBCSLeadByte (KERNEL32.@)
209 * IsDBCSLeadByte (KERNEL.207)
211 BOOL WINAPI
IsDBCSLeadByte( BYTE testchar
)
213 if (!ansi_cptable
) return FALSE
;
214 return is_dbcs_leadbyte( ansi_cptable
, testchar
);
218 /***********************************************************************
219 * GetCPInfo (KERNEL32.@)
221 BOOL WINAPI
GetCPInfo( UINT codepage
, LPCPINFO cpinfo
)
223 const union cptable
*table
= get_codepage_table( codepage
);
227 SetLastError( ERROR_INVALID_PARAMETER
);
230 if (table
->info
.def_char
& 0xff00)
232 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff00;
233 cpinfo
->DefaultChar
[1] = table
->info
.def_char
& 0x00ff;
237 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff;
238 cpinfo
->DefaultChar
[1] = 0;
240 if ((cpinfo
->MaxCharSize
= table
->info
.char_size
) == 2)
241 memcpy( cpinfo
->LeadByte
, table
->dbcs
.lead_bytes
, sizeof(cpinfo
->LeadByte
) );
243 cpinfo
->LeadByte
[0] = cpinfo
->LeadByte
[1] = 0;
249 /***********************************************************************
250 * EnumSystemCodePagesA (KERNEL32.@)
252 BOOL WINAPI
EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum
, DWORD flags
)
254 const union cptable
*table
;
260 if (!(table
= cp_enum_table( index
++ ))) break;
261 sprintf( buffer
, "%d", table
->info
.codepage
);
262 if (!lpfnCodePageEnum( buffer
)) break;
268 /***********************************************************************
269 * EnumSystemCodePagesW (KERNEL32.@)
271 BOOL WINAPI
EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum
, DWORD flags
)
273 const union cptable
*table
;
274 WCHAR buffer
[10], *p
;
279 if (!(table
= cp_enum_table( index
++ ))) break;
280 p
= buffer
+ sizeof(buffer
)/sizeof(WCHAR
);
282 page
= table
->info
.codepage
;
285 *--p
= '0' + (page
% 10);
288 if (!lpfnCodePageEnum( p
)) break;
294 /***********************************************************************
295 * MultiByteToWideChar (KERNEL32.@)
298 * page [in] Codepage character set to convert from
299 * flags [in] Character mapping flags
300 * src [in] Source string buffer
301 * srclen [in] Length of source string buffer
302 * dst [in] Destination buffer
303 * dstlen [in] Length of destination buffer
306 * The returned length includes the null terminator character.
309 * Success: If dstlen > 0, number of characters written to destination
310 * buffer. If dstlen == 0, number of characters needed to do
312 * Failure: 0. Occurs if not enough space is available.
315 * ERROR_INSUFFICIENT_BUFFER
316 * ERROR_INVALID_PARAMETER
317 * ERROR_NO_UNICODE_TRANSLATION
320 INT WINAPI
MultiByteToWideChar( UINT page
, DWORD flags
, LPCSTR src
, INT srclen
,
321 LPWSTR dst
, INT dstlen
)
323 const union cptable
*table
;
326 if (!src
|| (!dst
&& dstlen
))
328 SetLastError( ERROR_INVALID_PARAMETER
);
332 if (srclen
< 0) srclen
= strlen(src
) + 1;
334 if (flags
& MB_USEGLYPHCHARS
) FIXME("MB_USEGLYPHCHARS not supported\n");
339 FIXME("UTF not supported\n");
340 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
343 ret
= utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
346 if (!(table
= get_codepage_table( page
)))
348 SetLastError( ERROR_INVALID_PARAMETER
);
351 ret
= cp_mbstowcs( table
, flags
, src
, srclen
, dst
, dstlen
);
359 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER
); break;
360 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION
); break;
368 /***********************************************************************
369 * WideCharToMultiByte (KERNEL32.@)
372 * page [in] Codepage character set to convert to
373 * flags [in] Character mapping flags
374 * src [in] Source string buffer
375 * srclen [in] Length of source string buffer
376 * dst [in] Destination buffer
377 * dstlen [in] Length of destination buffer
378 * defchar [in] Default character to use for conversion if no exact
379 * conversion can be made
380 * used [out] Set if default character was used in the conversion
383 * The returned length includes the null terminator character.
386 * Success: If dstlen > 0, number of characters written to destination
387 * buffer. If dstlen == 0, number of characters needed to do
389 * Failure: 0. Occurs if not enough space is available.
392 * ERROR_INSUFFICIENT_BUFFER
393 * ERROR_INVALID_PARAMETER
395 INT WINAPI
WideCharToMultiByte( UINT page
, DWORD flags
, LPCWSTR src
, INT srclen
,
396 LPSTR dst
, INT dstlen
, LPCSTR defchar
, BOOL
*used
)
398 const union cptable
*table
;
401 if (!src
|| (!dst
&& dstlen
))
403 SetLastError( ERROR_INVALID_PARAMETER
);
407 if (srclen
< 0) srclen
= strlenW(src
) + 1;
412 FIXME("UTF-7 not supported\n");
413 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
416 ret
= utf8_wcstombs( src
, srclen
, dst
, dstlen
);
419 if (!(table
= get_codepage_table( page
)))
421 SetLastError( ERROR_INVALID_PARAMETER
);
424 ret
= cp_wcstombs( table
, flags
, src
, srclen
, dst
, dstlen
,
425 defchar
, used
? &used_tmp
: NULL
);
426 if (used
) *used
= used_tmp
;
432 SetLastError( ERROR_INSUFFICIENT_BUFFER
);