]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/multibyte_sb.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Locale / multibyte_sb.c
1 /* $NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $ */
2
3 /*
4 * Copyright (c) 1991 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 #include <LibConfig.h>
32 #include <sys/EfiCdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";
36 #else
37 __RCSID("$NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <assert.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <wchar.h>
45
46 /*
47 * Stub multibyte character functions.
48 * This cheezy implementation is fixed to the native single-byte
49 * character set.
50 */
51
52 /*ARGSUSED*/
53 int
54 mbsinit(const mbstate_t *ps)
55 {
56
57 return 1;
58 }
59
60 /*ARGSUSED*/
61 size_t
62 mbrlen(
63 const char *s,
64 size_t n,
65 mbstate_t *ps
66 )
67 {
68
69 /* ps appears to be unused */
70
71 if (s == NULL || *s == '\0')
72 return 0;
73 if (n == 0)
74 return (size_t)-1;
75 return 1;
76 }
77
78 int
79 mblen(
80 const char *s,
81 size_t n
82 )
83 {
84
85 /* s may be NULL */
86
87 return (int)mbrlen(s, n, NULL);
88 }
89
90 /*ARGSUSED*/
91 size_t
92 mbrtowc(
93 wchar_t *pwc,
94 const char *s,
95 size_t n,
96 mbstate_t *ps
97 )
98 {
99
100 /* pwc may be NULL */
101 /* s may be NULL */
102 /* ps appears to be unused */
103
104 if (s == NULL)
105 return 0;
106 if (n == 0)
107 return (size_t)-1;
108 if (pwc)
109 *pwc = (wchar_t) *s;
110 return (*s != '\0');
111 }
112
113 int
114 mbtowc(
115 wchar_t *pwc,
116 const char *s,
117 size_t n
118 )
119 {
120
121 /* pwc may be NULL */
122 /* s may be NULL */
123
124 return (int)mbrtowc(pwc, s, n, NULL);
125 }
126
127 /*ARGSUSED*/
128 size_t
129 wcrtomb(
130 char *s,
131 wchar_t wchar,
132 mbstate_t *ps
133 )
134 {
135
136 /* s may be NULL */
137 /* ps appears to be unused */
138
139 if (s == NULL)
140 return 0;
141
142 *s = (char) wchar;
143 return 1;
144 }
145
146 int
147 wctomb(
148 char *s,
149 wchar_t wchar
150 )
151 {
152
153 /* s may be NULL */
154
155 return (int)wcrtomb(s, wchar, NULL);
156 }
157
158 /*ARGSUSED*/
159 size_t
160 mbsrtowcs(
161 wchar_t *pwcs,
162 const char **s,
163 size_t n,
164 mbstate_t *ps
165 )
166 {
167 int count = 0;
168
169 /* pwcs may be NULL */
170 /* s may be NULL */
171 /* ps appears to be unused */
172
173 if (!s || !*s)
174 return 0;
175
176 if (n != 0) {
177 if (pwcs != NULL) {
178 do {
179 if ((*pwcs++ = (wchar_t) *(*s)++) == 0)
180 break;
181 count++;
182 } while (--n != 0);
183 } else {
184 do {
185 if (((wchar_t)*(*s)++) == 0)
186 break;
187 count++;
188 } while (--n != 0);
189 }
190 }
191
192 return count;
193 }
194
195 size_t
196 mbstowcs(
197 wchar_t *pwcs,
198 const char *s,
199 size_t n
200 )
201 {
202
203 /* pwcs may be NULL */
204 /* s may be NULL */
205
206 return mbsrtowcs(pwcs, &s, n, NULL);
207 }
208
209 /*ARGSUSED*/
210 size_t
211 wcsrtombs(
212 char *s,
213 const wchar_t **pwcs,
214 size_t n,
215 mbstate_t *ps
216 )
217 {
218 int count = 0;
219
220 /* s may be NULL */
221 /* pwcs may be NULL */
222 /* ps appears to be unused */
223
224 if (pwcs == NULL || *pwcs == NULL)
225 return (0);
226
227 if (s == NULL) {
228 while (*(*pwcs)++ != 0)
229 count++;
230 return(count);
231 }
232
233 if (n != 0) {
234 do {
235 if ((*s++ = (char) *(*pwcs)++) == 0)
236 break;
237 count++;
238 } while (--n != 0);
239 }
240
241 return count;
242 }
243
244 size_t
245 wcstombs(
246 char *s,
247 const wchar_t *pwcs,
248 size_t n
249 )
250 {
251
252 /* s may be NULL */
253 /* pwcs may be NULL */
254
255 return wcsrtombs(s, &pwcs, n, NULL);
256 }
257
258 wint_t
259 btowc(int c)
260 {
261 if (c == EOF || c & ~0xFF)
262 return WEOF;
263 return (wint_t)c;
264 }
265
266 int
267 wctob(wint_t c)
268 {
269 if (c == WEOF || c & ~0xFF)
270 return EOF;
271 return (int)c;
272 }