4 * Copyright 2000 Alexandre Julliard
13 #include "wine/unicode.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(string);
18 /* current code pages */
19 static unsigned int ansi_cp = 1252; /* Windows 3.1 ISO Latin */
20 static unsigned int oem_cp = 437; /* MS-DOS United States */
21 static unsigned int mac_cp = 10000; /* Mac Roman */
23 static const union cptable *ansi_cptable;
24 static const union cptable *oem_cptable;
25 static const union cptable *mac_cptable;
28 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
29 static const union cptable *get_codepage_table( unsigned int codepage )
31 const union cptable *ret = NULL;
33 if (!ansi_cptable) /* initialize them */
35 /* FIXME: should load from the registry */
36 ansi_cptable = cp_get_table( ansi_cp );
37 oem_cptable = cp_get_table( oem_cp );
38 mac_cptable = cp_get_table( mac_cp );
39 assert( ansi_cptable );
40 assert( oem_cptable );
41 assert( mac_cptable );
47 case CP_ACP: return ansi_cptable;
48 case CP_OEMCP: return oem_cptable;
49 case CP_MACCP: return mac_cptable;
50 case CP_THREAD_ACP: return ansi_cptable; /* FIXME */
55 if (codepage == ansi_cp) return ansi_cptable;
56 if (codepage == oem_cp) return oem_cptable;
57 if (codepage == mac_cp) return mac_cptable;
58 ret = cp_get_table( codepage );
64 /******************************************************************************
68 * Current ANSI code-page identifier, default if no current defined
70 UINT WINAPI GetACP(void)
76 /***********************************************************************
79 UINT WINAPI GetOEMCP(void)
85 /***********************************************************************
86 * IsValidCodePage (KERNEL32)
88 BOOL WINAPI IsValidCodePage( UINT codepage )
90 return cp_get_table( codepage ) != NULL;
94 /***********************************************************************
95 * IsDBCSLeadByteEx (KERNEL32)
97 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
99 const union cptable *table = get_codepage_table( codepage );
100 return table && is_dbcs_leadbyte( table, testchar );
104 /***********************************************************************
105 * IsDBCSLeadByte (KERNEL32)
107 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
109 return is_dbcs_leadbyte( ansi_cptable, testchar );
113 /***********************************************************************
114 * GetCPInfo (KERNEL32)
116 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
118 const union cptable *table = get_codepage_table( codepage );
122 SetLastError( ERROR_INVALID_PARAMETER );
125 if (table->info.def_char & 0xff00)
127 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
128 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
132 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
133 cpinfo->DefaultChar[1] = 0;
135 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
136 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
138 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
144 /***********************************************************************
145 * EnumSystemCodePagesA (KERNEL32)
147 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
149 const union cptable *table;
155 if (!(table = cp_enum_table( index++ ))) break;
156 sprintf( buffer, "%d", table->info.codepage );
157 if (!lpfnCodePageEnum( buffer )) break;
163 /***********************************************************************
164 * EnumSystemCodePagesW (KERNEL32)
166 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
168 const union cptable *table;
169 WCHAR buffer[10], *p;
174 if (!(table = cp_enum_table( index++ ))) break;
175 p = buffer + sizeof(buffer)/sizeof(WCHAR);
177 page = table->info.codepage;
180 *--p = '0' + (page % 10);
183 if (!lpfnCodePageEnum( p )) break;
189 /***********************************************************************
190 * MultiByteToWideChar (KERNEL32)
193 * page [in] Codepage character set to convert from
194 * flags [in] Character mapping flags
195 * src [in] Source string buffer
196 * srclen [in] Length of source string buffer
197 * dst [in] Destination buffer
198 * dstlen [in] Length of destination buffer
201 * The returned length includes the null terminator character.
204 * Success: If dstlen > 0, number of characters written to destination
205 * buffer. If dstlen == 0, number of characters needed to do
207 * Failure: 0. Occurs if not enough space is available.
210 * ERROR_INSUFFICIENT_BUFFER
211 * ERROR_INVALID_PARAMETER
212 * ERROR_NO_UNICODE_TRANSLATION
215 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
216 LPWSTR dst, INT dstlen )
218 const union cptable *table;
221 if (srclen == -1) srclen = strlen(src) + 1;
225 FIXME("UTF not supported\n");
226 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
230 if (!(table = get_codepage_table( page )))
232 SetLastError( ERROR_INVALID_PARAMETER );
236 if (flags & MB_COMPOSITE) FIXME("MB_COMPOSITE not supported\n");
237 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
239 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
245 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
246 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
254 /***********************************************************************
255 * WideCharToMultiByte (KERNEL32)
258 * page [in] Codepage character set to convert to
259 * flags [in] Character mapping flags
260 * src [in] Source string buffer
261 * srclen [in] Length of source string buffer
262 * dst [in] Destination buffer
263 * dstlen [in] Length of destination buffer
264 * defchar [in] Default character to use for conversion if no exact
265 * conversion can be made
266 * used [out] Set if default character was used in the conversion
269 * The returned length includes the null terminator character.
272 * Success: If dstlen > 0, number of characters written to destination
273 * buffer. If dstlen == 0, number of characters needed to do
275 * Failure: 0. Occurs if not enough space is available.
278 * ERROR_INSUFFICIENT_BUFFER
279 * ERROR_INVALID_PARAMETER
281 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
282 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
284 const union cptable *table;
287 if (srclen == -1) srclen = strlenW(src) + 1;
291 FIXME("UTF not supported\n");
292 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
296 if (!(table = get_codepage_table( page )))
298 SetLastError( ERROR_INVALID_PARAMETER );
302 /* if (flags & WC_COMPOSITECHECK) FIXME( "WC_COMPOSITECHECK (%lx) not supported\n", flags );*/
304 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen, defchar, used ? &used_tmp : NULL );
305 if (used) *used = used_tmp;
309 SetLastError( ERROR_INSUFFICIENT_BUFFER );