xref: /reactos/sdk/lib/crt/wstring/mbrtowc.c (revision db93cb17)
1 /*
2  * msvcrt.dll mbcs functions
3  *
4  * Copyright 1999 Alexandre Julliard
5  * Copyright 2000 Jon Griffths
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 #include <precomp.h>
23 #include <locale.h>
24 
25 /*********************************************************************
26  *              mbrtowc(MSVCRT.@)
27  */
28 size_t CDECL mbrtowc(wchar_t *dst, const char *str,
29         size_t n, mbstate_t *state)
30 {
31     mbstate_t s = (state ? *state : 0);
32     char tmpstr[2];
33     int len = 0;
34 
35     if(dst)
36         *dst = 0;
37 
38     if(!n || !str || !*str)
39         return 0;
40 
41     if(___mb_cur_max_func() == 1) {
42         tmpstr[len++] = *str;
43     }else if(!s && isleadbyte((unsigned char)*str)) {
44         if(n == 1) {
45             s = (unsigned char)*str;
46             len = -2;
47         }else {
48             tmpstr[0] = str[0];
49             tmpstr[1] = str[1];
50             len = 2;
51         }
52     }else if(!s) {
53         tmpstr[len++] = *str;
54     }else {
55         tmpstr[0] = s;
56         tmpstr[1] = *str;
57         len = 2;
58         s = 0;
59     }
60 
61     if(len > 0) {
62         if(!MultiByteToWideChar(___lc_codepage_func(), 0, tmpstr, len, dst, dst ? 1 : 0))
63             len = -1;
64     }
65 
66     if(state)
67         *state = s;
68     return len;
69 }
70