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