1 | /* |
---|
2 | FUNCTION |
---|
3 | <<mbsrtowcs>>, <<mbsnrtowcs>>---convert a character string to a wide-character string |
---|
4 | |
---|
5 | INDEX |
---|
6 | mbsrtowcs |
---|
7 | INDEX |
---|
8 | _mbsrtowcs_r |
---|
9 | INDEX |
---|
10 | mbsnrtowcs |
---|
11 | INDEX |
---|
12 | _mbsnrtowcs_r |
---|
13 | |
---|
14 | SYNOPSIS |
---|
15 | #include <wchar.h> |
---|
16 | size_t mbsrtowcs(wchar_t *__restrict <[dst]>, |
---|
17 | const char **__restrict <[src]>, |
---|
18 | size_t <[len]>, |
---|
19 | mbstate_t *__restrict <[ps]>); |
---|
20 | |
---|
21 | #include <wchar.h> |
---|
22 | size_t _mbsrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>, |
---|
23 | const char **<[src]>, size_t <[len]>, |
---|
24 | mbstate_t *<[ps]>); |
---|
25 | |
---|
26 | #include <wchar.h> |
---|
27 | size_t mbsnrtowcs(wchar_t *__ restrict <[dst]>, |
---|
28 | const char **__restrict <[src]>, size_t <[nms]>, |
---|
29 | size_t <[len]>, mbstate_t *__restrict <[ps]>); |
---|
30 | |
---|
31 | #include <wchar.h> |
---|
32 | size_t _mbsnrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>, |
---|
33 | const char **<[src]>, size_t <[nms]>, |
---|
34 | size_t <[len]>, mbstate_t *<[ps]>); |
---|
35 | |
---|
36 | DESCRIPTION |
---|
37 | The <<mbsrtowcs>> function converts a sequence of multibyte characters |
---|
38 | pointed to indirectly by <[src]> into a sequence of corresponding wide |
---|
39 | characters and stores at most <[len]> of them in the wchar_t array pointed |
---|
40 | to by <[dst]>, until it encounters a terminating null character ('\0'). |
---|
41 | |
---|
42 | If <[dst]> is NULL, no characters are stored. |
---|
43 | |
---|
44 | If <[dst]> is not NULL, the pointer pointed to by <[src]> is updated to point |
---|
45 | to the character after the one that conversion stopped at. If conversion |
---|
46 | stops because a null character is encountered, *<[src]> is set to NULL. |
---|
47 | |
---|
48 | The mbstate_t argument, <[ps]>, is used to keep track of the shift state. If |
---|
49 | it is NULL, <<mbsrtowcs>> uses an internal, static mbstate_t object, which |
---|
50 | is initialized to the initial conversion state at program startup. |
---|
51 | |
---|
52 | The <<mbsnrtowcs>> function behaves identically to <<mbsrtowcs>>, except that |
---|
53 | conversion stops after reading at most <[nms]> bytes from the buffer pointed |
---|
54 | to by <[src]>. |
---|
55 | |
---|
56 | RETURNS |
---|
57 | The <<mbsrtowcs>> and <<mbsnrtowcs>> functions return the number of wide |
---|
58 | characters stored in the array pointed to by <[dst]> if successful, otherwise |
---|
59 | it returns (size_t)-1. |
---|
60 | |
---|
61 | PORTABILITY |
---|
62 | <<mbsrtowcs>> is defined by the C99 standard. |
---|
63 | <<mbsnrtowcs>> is defined by the POSIX.1-2008 standard. |
---|
64 | */ |
---|
65 | |
---|
66 | #include <reent.h> |
---|
67 | #include <newlib.h> |
---|
68 | #include <wchar.h> |
---|
69 | #include <stdlib.h> |
---|
70 | #include <stdio.h> |
---|
71 | #include <errno.h> |
---|
72 | |
---|
73 | size_t |
---|
74 | _mbsnrtowcs_r (struct _reent *r, |
---|
75 | wchar_t *dst, |
---|
76 | const char **src, |
---|
77 | size_t nms, |
---|
78 | size_t len, |
---|
79 | mbstate_t *ps) |
---|
80 | { |
---|
81 | wchar_t *ptr = dst; |
---|
82 | const char *tmp_src; |
---|
83 | size_t max; |
---|
84 | size_t count = 0; |
---|
85 | int bytes; |
---|
86 | |
---|
87 | #ifdef _MB_CAPABLE |
---|
88 | if (ps == NULL) |
---|
89 | { |
---|
90 | _REENT_CHECK_MISC(r); |
---|
91 | ps = &(_REENT_MBSRTOWCS_STATE(r)); |
---|
92 | } |
---|
93 | #endif |
---|
94 | |
---|
95 | if (dst == NULL) |
---|
96 | { |
---|
97 | /* Ignore original len value and do not alter src pointer if the |
---|
98 | dst pointer is NULL. */ |
---|
99 | len = (size_t)-1; |
---|
100 | tmp_src = *src; |
---|
101 | src = &tmp_src; |
---|
102 | } |
---|
103 | |
---|
104 | max = len; |
---|
105 | while (len > 0) |
---|
106 | { |
---|
107 | bytes = _mbrtowc_r (r, ptr, *src, nms, ps); |
---|
108 | if (bytes > 0) |
---|
109 | { |
---|
110 | *src += bytes; |
---|
111 | nms -= bytes; |
---|
112 | ++count; |
---|
113 | ptr = (dst == NULL) ? NULL : ptr + 1; |
---|
114 | --len; |
---|
115 | } |
---|
116 | else if (bytes == -2) |
---|
117 | { |
---|
118 | *src += nms; |
---|
119 | return count; |
---|
120 | } |
---|
121 | else if (bytes == 0) |
---|
122 | { |
---|
123 | *src = NULL; |
---|
124 | return count; |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | ps->__count = 0; |
---|
129 | r->_errno = EILSEQ; |
---|
130 | return (size_t)-1; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | return (size_t)max; |
---|
135 | } |
---|
136 | |
---|
137 | #ifndef _REENT_ONLY |
---|
138 | size_t |
---|
139 | mbsnrtowcs (wchar_t *__restrict dst, |
---|
140 | const char **__restrict src, |
---|
141 | size_t nms, |
---|
142 | size_t len, |
---|
143 | mbstate_t *__restrict ps) |
---|
144 | { |
---|
145 | return _mbsnrtowcs_r (_REENT, dst, src, nms, len, ps); |
---|
146 | } |
---|
147 | #endif /* !_REENT_ONLY */ |
---|