1 | /* |
---|
2 | FUNCTION |
---|
3 | <<mbstowcs>>---minimal multibyte string to wide char converter |
---|
4 | |
---|
5 | INDEX |
---|
6 | mbstowcs |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | int mbstowcs(wchar_t *restrict <[pwc]>, const char *restrict <[s]>, size_t <[n]>); |
---|
11 | |
---|
12 | DESCRIPTION |
---|
13 | When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming |
---|
14 | implementation of <<mbstowcs>>. In this case, the |
---|
15 | only ``multi-byte character sequences'' recognized are single bytes, |
---|
16 | and they are ``converted'' to wide-char versions simply by byte |
---|
17 | extension. |
---|
18 | |
---|
19 | When _MB_CAPABLE is defined, this routine calls <<_mbstowcs_r>> to perform |
---|
20 | the conversion, passing a state variable to allow state dependent |
---|
21 | decoding. The result is based on the locale setting which may |
---|
22 | be restricted to a defined set of locales. |
---|
23 | |
---|
24 | RETURNS |
---|
25 | This implementation of <<mbstowcs>> returns <<0>> if |
---|
26 | <[s]> is <<NULL>> or is the empty string; |
---|
27 | it returns <<-1>> if _MB_CAPABLE and one of the |
---|
28 | multi-byte characters is invalid or incomplete; |
---|
29 | otherwise it returns the minimum of: <<n>> or the |
---|
30 | number of multi-byte characters in <<s>> plus 1 (to |
---|
31 | compensate for the nul character). |
---|
32 | If the return value is -1, the state of the <<pwc>> string is |
---|
33 | indeterminate. If the input has a length of 0, the output |
---|
34 | string will be modified to contain a wchar_t nul terminator. |
---|
35 | |
---|
36 | PORTABILITY |
---|
37 | <<mbstowcs>> is required in the ANSI C standard. However, the precise |
---|
38 | effects vary with the locale. |
---|
39 | |
---|
40 | <<mbstowcs>> requires no supporting OS subroutines. |
---|
41 | */ |
---|
42 | |
---|
43 | #ifndef _REENT_ONLY |
---|
44 | |
---|
45 | #include <newlib.h> |
---|
46 | #include <stdlib.h> |
---|
47 | #include <wchar.h> |
---|
48 | |
---|
49 | size_t |
---|
50 | mbstowcs (wchar_t *__restrict pwcs, |
---|
51 | const char *__restrict s, |
---|
52 | size_t n) |
---|
53 | { |
---|
54 | #ifdef _MB_CAPABLE |
---|
55 | mbstate_t state; |
---|
56 | state.__count = 0; |
---|
57 | |
---|
58 | return _mbstowcs_r (_REENT, pwcs, s, n, &state); |
---|
59 | #else /* not _MB_CAPABLE */ |
---|
60 | |
---|
61 | int count = 0; |
---|
62 | |
---|
63 | if (n != 0) { |
---|
64 | do { |
---|
65 | if ((*pwcs++ = (wchar_t) *s++) == 0) |
---|
66 | break; |
---|
67 | count++; |
---|
68 | } while (--n != 0); |
---|
69 | } |
---|
70 | |
---|
71 | return count; |
---|
72 | #endif /* not _MB_CAPABLE */ |
---|
73 | } |
---|
74 | |
---|
75 | #endif /* !_REENT_ONLY */ |
---|