5fa6c9e3d8e3e78fcccdfce12f0aee6c9abfa03b
[reactos.git] / reactos / lib / kernel32 / nls / mbtowc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 Addition copyrights might be specified in LGPL.c
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/nls/mbtowc.c
6 * PURPOSE: National language support functions
7 * PROGRAMMER: Boudewijn ( ariadne@xs4all.nl)
8 * UPDATE HISTORY: Modified from Onno Hovers wfc. ( 08/02/99 )
9 *
10 */
11 /*
12 * nls/mbtowc.c
13 * Copyright (c) 1996, Onno Hovers, All rights reserved
14 */
15
16 #include <windows.h>
17 #include <kernel32/nls.h>
18 #include <kernel32/thread.h>
19 #include <wchar.h>
20 #include <string.h>
21
22 #define NDEBUG
23 #include <kernel32/kernel32.h>
24
25 extern PLOCALE __TebLocale;
26
27 #define GetTebLocale() __TebLocale
28
29 INT MultiByteToWideChar(UINT cpid, DWORD flags, LPCSTR src, int srclen,
30 LPWSTR dest, int destlen)
31 {
32 PCODEPAGE pcodepage =__CPFirst;
33 INT copylen;
34 INT retlen;
35 WCHAR **atou;
36 CHAR c;
37
38 DPRINT("MultiByteToWideChar( %u, 0x%lX, %s, %d, 0x%lX, %d )\n",
39 cpid, flags, src, srclen, (ULONG) dest, destlen);
40
41 /* get codepage */
42 switch(cpid)
43 {
44 case CP_ACP: pcodepage= GetTebLocale()->OemCodePage; break;
45 case CP_OEMCP: pcodepage= GetTebLocale()->AnsiCodePage; break;
46 case CP_MACCP: pcodepage=&__CP10000; break;
47 default:
48 pcodepage=__CPFirst;
49 while((pcodepage)&&(pcodepage->Id!=cpid))
50 pcodepage=pcodepage->Next;
51 }
52 if(pcodepage==NULL)
53 { SetLastError(ERROR_INVALID_PARAMETER); return 0; }
54
55 /* get conversion table */
56 atou=pcodepage->ToUnicode;
57
58 if(destlen!=0)
59 {
60 /* how long are we ?? */
61 if(srclen==0)
62 srclen=strlen(src);
63
64 copylen=min(srclen,destlen);
65 retlen=copylen;
66
67 /* XXX: maybe some inline assembly to speed things up, here ??? */
68 while(copylen>0)
69 {
70 c=*src;
71 *dest=atou [c>>5] [(c)&0x1F];
72 src++;
73 dest++;
74 copylen--;
75 }
76 }
77 else
78 {
79 /* XXXX: composites etc. */
80 retlen = strlen(src);
81 }
82 return retlen;
83 }