1 | /* |
---|
2 | FUNCTION |
---|
3 | <<mbtowc>>---minimal multibyte to wide char converter |
---|
4 | |
---|
5 | INDEX |
---|
6 | mbtowc |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | int mbtowc(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 <<mbtowc>>. In this case, |
---|
15 | only ``multi-byte character sequences'' recognized are single bytes, |
---|
16 | and they are ``converted'' to themselves. |
---|
17 | Each call to <<mbtowc>> copies one character from <<*<[s]>>> to |
---|
18 | <<*<[pwc]>>>, unless <[s]> is a null pointer. The argument n |
---|
19 | is ignored. |
---|
20 | |
---|
21 | When _MB_CAPABLE is defined, this routine calls <<_mbtowc_r>> to perform |
---|
22 | the conversion, passing a state variable to allow state dependent |
---|
23 | decoding. The result is based on the locale setting which may |
---|
24 | be restricted to a defined set of locales. |
---|
25 | |
---|
26 | RETURNS |
---|
27 | This implementation of <<mbtowc>> returns <<0>> if |
---|
28 | <[s]> is <<NULL>> or is the empty string; |
---|
29 | it returns <<1>> if not _MB_CAPABLE or |
---|
30 | the character is a single-byte character; it returns <<-1>> |
---|
31 | if n is <<0>> or the multi-byte character is invalid; |
---|
32 | otherwise it returns the number of bytes in the multibyte character. |
---|
33 | If the return value is -1, no changes are made to the <<pwc>> |
---|
34 | output string. If the input is the empty string, a wchar_t nul |
---|
35 | is placed in the output string and 0 is returned. If the input |
---|
36 | has a length of 0, no changes are made to the <<pwc>> output string. |
---|
37 | |
---|
38 | PORTABILITY |
---|
39 | <<mbtowc>> is required in the ANSI C standard. However, the precise |
---|
40 | effects vary with the locale. |
---|
41 | |
---|
42 | <<mbtowc>> requires no supporting OS subroutines. |
---|
43 | */ |
---|
44 | |
---|
45 | #ifndef _REENT_ONLY |
---|
46 | |
---|
47 | #include <newlib.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include <wchar.h> |
---|
50 | #include "local.h" |
---|
51 | |
---|
52 | int |
---|
53 | mbtowc (wchar_t *__restrict pwc, |
---|
54 | const char *__restrict s, |
---|
55 | size_t n) |
---|
56 | { |
---|
57 | #ifdef _MB_CAPABLE |
---|
58 | int retval = 0; |
---|
59 | struct _reent *reent = _REENT; |
---|
60 | mbstate_t *ps; |
---|
61 | |
---|
62 | _REENT_CHECK_MISC(reent); |
---|
63 | ps = &(_REENT_MBTOWC_STATE(reent)); |
---|
64 | |
---|
65 | retval = __MBTOWC (reent, pwc, s, n, ps); |
---|
66 | |
---|
67 | if (retval < 0) |
---|
68 | { |
---|
69 | ps->__count = 0; |
---|
70 | return -1; |
---|
71 | } |
---|
72 | return retval; |
---|
73 | #else /* not _MB_CAPABLE */ |
---|
74 | if (s == NULL) |
---|
75 | return 0; |
---|
76 | if (n == 0) |
---|
77 | return -1; |
---|
78 | if (pwc) |
---|
79 | *pwc = (wchar_t) *s; |
---|
80 | return (*s != '\0'); |
---|
81 | #endif /* not _MB_CAPABLE */ |
---|
82 | } |
---|
83 | |
---|
84 | #endif /* !_REENT_ONLY */ |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | |
---|