Reimplement CharToOemA/W, CharToOemBuffA/W, OemToCharA/W,

OemToCharBuffA/W using MultiByteToWideChar/WideCharToMultiByte.
This commit is contained in:
Dmitry Timoshkov 2000-06-18 17:18:18 +00:00 committed by Alexandre Julliard
parent 20af4c3c87
commit 73c32e79c5
1 changed files with 27 additions and 69 deletions

View File

@ -24,45 +24,6 @@
DEFAULT_DEBUG_CHANNEL(string); DEFAULT_DEBUG_CHANNEL(string);
static const BYTE STRING_Oem2Ansi[256] =
"\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
"\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
"\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
"\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
"\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
"\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
"\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
"\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
"\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
"\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
"\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
"\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
"\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
"\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
"\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
"\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
static const BYTE STRING_Ansi2Oem[256] =
"\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
"\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
"\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
"\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
"\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
"\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
"\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
"\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
"\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
"\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
"\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
"\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
"\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
"\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
"\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
#define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
#define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
/* Internaly used by strchr family functions */ /* Internaly used by strchr family functions */
static BOOL ChrCmpA( WORD word1, WORD word2); static BOOL ChrCmpA( WORD word1, WORD word2);
@ -423,12 +384,10 @@ LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
*/ */
INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage ) INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
{ {
if ( codepage != -1 ) if ( codepage == -1 )
FIXME("codepage %d not supported\n", codepage ); codepage = CP_ACP;
lstrcpyWtoA( dst, src ); return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
return (INT16)lstrlenA( dst );
} }
@ -483,12 +442,7 @@ void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
*/ */
BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d ) BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
{ {
LPSTR oldd = d; return CharToOemBuffA( s, d, strlen( s ) + 1 );
if (!s || !d) return TRUE;
TRACE("CharToOem %s\n", debugstr_a (s));
while ((*d++ = ANSI_TO_OEM(*s++)));
TRACE(" to %s\n", debugstr_a (oldd));
return TRUE;
} }
@ -497,7 +451,15 @@ BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
*/ */
BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len ) BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
{ {
while (len--) *d++ = ANSI_TO_OEM(*s++); WCHAR *bufW;
bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
if( bufW )
{
MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
HeapFree( GetProcessHeap(), 0, bufW );
}
return TRUE; return TRUE;
} }
@ -507,7 +469,7 @@ BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
*/ */
BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len ) BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
{ {
while (len--) *d++ = ANSI_TO_OEM(*s++); WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
return TRUE; return TRUE;
} }
@ -517,12 +479,7 @@ BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
*/ */
BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d ) BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
{ {
LPSTR oldd = d; return CharToOemBuffW( s, d, strlenW( s ) + 1 );
if (!s || !d) return TRUE;
TRACE("CharToOem %s\n", debugstr_w (s));
while ((*d++ = ANSI_TO_OEM(*s++)));
TRACE(" to %s\n", debugstr_a (oldd));
return TRUE;
} }
@ -531,11 +488,7 @@ BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
*/ */
BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d ) BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
{ {
LPSTR oldd = d; return OemToCharBuffA( s, d, strlen( s ) + 1 );
TRACE("OemToChar %s\n", debugstr_a (s));
while ((*d++ = OEM_TO_ANSI(*s++)));
TRACE(" to %s\n", debugstr_a (oldd));
return TRUE;
} }
@ -544,8 +497,15 @@ BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
*/ */
BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len ) BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
{ {
TRACE("OemToCharBuff %s\n", debugstr_an (s, len)); WCHAR *bufW;
while (len--) *d++ = OEM_TO_ANSI(*s++);
bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
if( bufW )
{
MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
HeapFree( GetProcessHeap(), 0, bufW );
}
return TRUE; return TRUE;
} }
@ -555,8 +515,7 @@ BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
*/ */
BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len ) BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
{ {
TRACE("OemToCharBuff %s\n", debugstr_an (s, len)); MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
return TRUE; return TRUE;
} }
@ -566,8 +525,7 @@ BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
*/ */
BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d ) BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
{ {
while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++))); return OemToCharBuffW( s, d, strlen( s ) + 1 );
return TRUE;
} }
/*********************************************************************** /***********************************************************************