citrus_none.c revision 1.3.2.5 1 1.3.2.5 thorpej /* $NetBSD: citrus_none.c,v 1.3.2.5 2003/01/08 20:32:05 thorpej Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*-
4 1.3.2.2 nathanw * Copyright (c)2002 Citrus Project,
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.3.2.2 nathanw * are met:
10 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.3.2.2 nathanw *
16 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.3.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.3.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.3.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.3.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.3.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.3.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.3.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.3.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.2.2 nathanw * SUCH DAMAGE.
27 1.3.2.2 nathanw */
28 1.3.2.2 nathanw
29 1.3.2.2 nathanw #include <sys/cdefs.h>
30 1.3.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
31 1.3.2.5 thorpej __RCSID("$NetBSD: citrus_none.c,v 1.3.2.5 2003/01/08 20:32:05 thorpej Exp $");
32 1.3.2.2 nathanw #endif /* LIBC_SCCS and not lint */
33 1.3.2.2 nathanw
34 1.3.2.2 nathanw #include <assert.h>
35 1.3.2.2 nathanw #include <errno.h>
36 1.3.2.2 nathanw #include <string.h>
37 1.3.2.2 nathanw #include <stdio.h>
38 1.3.2.2 nathanw #include <stdlib.h>
39 1.3.2.2 nathanw #include <stddef.h>
40 1.3.2.2 nathanw #include <locale.h>
41 1.3.2.2 nathanw #include <wchar.h>
42 1.3.2.2 nathanw #include <sys/types.h>
43 1.3.2.2 nathanw #include "citrus_module.h"
44 1.3.2.2 nathanw #include "citrus_ctype.h"
45 1.3.2.2 nathanw #include "citrus_none.h"
46 1.3.2.2 nathanw
47 1.3.2.2 nathanw /* ---------------------------------------------------------------------- */
48 1.3.2.2 nathanw
49 1.3.2.2 nathanw _CITRUS_CTYPE_DECLS(NONE);
50 1.3.2.2 nathanw _CITRUS_CTYPE_DEF_OPS(NONE);
51 1.3.2.2 nathanw
52 1.3.2.2 nathanw
53 1.3.2.2 nathanw /* ---------------------------------------------------------------------- */
54 1.3.2.2 nathanw
55 1.3.2.2 nathanw static int
56 1.3.2.2 nathanw /*ARGSUSED*/
57 1.3.2.2 nathanw _citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var,
58 1.3.2.2 nathanw size_t lenvar, size_t lenps)
59 1.3.2.2 nathanw {
60 1.3.2.2 nathanw *cl = NULL;
61 1.3.2.2 nathanw return (0);
62 1.3.2.2 nathanw }
63 1.3.2.2 nathanw
64 1.3.2.2 nathanw static void
65 1.3.2.2 nathanw /*ARGSUSED*/
66 1.3.2.2 nathanw _citrus_NONE_ctype_uninit(void *cl)
67 1.3.2.2 nathanw {
68 1.3.2.2 nathanw }
69 1.3.2.2 nathanw
70 1.3.2.2 nathanw static unsigned
71 1.3.2.2 nathanw /*ARGSUSED*/
72 1.3.2.2 nathanw _citrus_NONE_ctype_get_mb_cur_max(void *cl)
73 1.3.2.2 nathanw {
74 1.3.2.2 nathanw return (1);
75 1.3.2.2 nathanw }
76 1.3.2.2 nathanw
77 1.3.2.2 nathanw static int
78 1.3.2.2 nathanw /*ARGSUSED*/
79 1.3.2.2 nathanw _citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s,
80 1.3.2.2 nathanw size_t n, int * __restrict nresult)
81 1.3.2.2 nathanw {
82 1.3.2.2 nathanw if (!s) {
83 1.3.2.2 nathanw *nresult = 0; /* state independent */
84 1.3.2.2 nathanw return (0);
85 1.3.2.2 nathanw }
86 1.3.2.2 nathanw if (n==0) {
87 1.3.2.2 nathanw *nresult = -1;
88 1.3.2.2 nathanw return (EILSEQ);
89 1.3.2.2 nathanw }
90 1.3.2.2 nathanw *nresult = (*s == 0) ? 0 : 1;
91 1.3.2.2 nathanw return (0);
92 1.3.2.2 nathanw }
93 1.3.2.2 nathanw
94 1.3.2.2 nathanw static int
95 1.3.2.2 nathanw /*ARGSUSED*/
96 1.3.2.2 nathanw _citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s,
97 1.3.2.2 nathanw size_t n, void * __restrict pspriv,
98 1.3.2.2 nathanw size_t * __restrict nresult)
99 1.3.2.2 nathanw {
100 1.3.2.2 nathanw if (!s) {
101 1.3.2.2 nathanw *nresult = 0;
102 1.3.2.2 nathanw return (0);
103 1.3.2.2 nathanw }
104 1.3.2.2 nathanw if (n==0) {
105 1.3.2.2 nathanw *nresult = (size_t)-2;
106 1.3.2.2 nathanw return (0);
107 1.3.2.2 nathanw }
108 1.3.2.2 nathanw *nresult = (*s == 0) ? 0 : 1;
109 1.3.2.2 nathanw return (0);
110 1.3.2.2 nathanw }
111 1.3.2.2 nathanw
112 1.3.2.2 nathanw static int
113 1.3.2.2 nathanw /*ARGSUSED*/
114 1.3.2.2 nathanw _citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc,
115 1.3.2.2 nathanw const char * __restrict s, size_t n,
116 1.3.2.2 nathanw void * __restrict pspriv,
117 1.3.2.2 nathanw size_t * __restrict nresult)
118 1.3.2.2 nathanw {
119 1.3.2.2 nathanw if (s == NULL) {
120 1.3.2.2 nathanw *nresult = 0;
121 1.3.2.2 nathanw return (0);
122 1.3.2.2 nathanw }
123 1.3.2.2 nathanw if (n == 0) {
124 1.3.2.2 nathanw *nresult = (size_t)-2;
125 1.3.2.2 nathanw return (0);
126 1.3.2.2 nathanw }
127 1.3.2.2 nathanw
128 1.3.2.2 nathanw if (pwc != NULL)
129 1.3.2.2 nathanw *pwc = (wchar_t)(unsigned char) *s;
130 1.3.2.2 nathanw
131 1.3.2.2 nathanw *nresult = *s == '\0' ? 0 : 1;
132 1.3.2.2 nathanw return (0);
133 1.3.2.2 nathanw }
134 1.3.2.2 nathanw
135 1.3.2.2 nathanw static int
136 1.3.2.2 nathanw /*ARGSUSED*/
137 1.3.2.2 nathanw _citrus_NONE_ctype_mbsinit(void * __restrict cl,
138 1.3.2.2 nathanw const void * __restrict pspriv,
139 1.3.2.2 nathanw int * __restrict nresult)
140 1.3.2.2 nathanw {
141 1.3.2.2 nathanw *nresult = 1; /* always initial state */
142 1.3.2.2 nathanw return (0);
143 1.3.2.2 nathanw }
144 1.3.2.2 nathanw
145 1.3.2.2 nathanw static int
146 1.3.2.2 nathanw /*ARGSUSED*/
147 1.3.2.5 thorpej _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs,
148 1.3.2.2 nathanw const char ** __restrict s, size_t n,
149 1.3.2.2 nathanw void * __restrict pspriv,
150 1.3.2.2 nathanw size_t * __restrict nresult)
151 1.3.2.2 nathanw {
152 1.3.2.5 thorpej int cnt;
153 1.3.2.2 nathanw const char *s0;
154 1.3.2.2 nathanw
155 1.3.2.5 thorpej /* if pwcs is NULL, ignore n */
156 1.3.2.5 thorpej if (pwcs == NULL)
157 1.3.2.5 thorpej n = 1; /* arbitrary >0 value */
158 1.3.2.5 thorpej
159 1.3.2.5 thorpej cnt = 0;
160 1.3.2.5 thorpej s0 = *s; /* to keep *s unchanged for now, use copy instead. */
161 1.3.2.5 thorpej while (n > 0) {
162 1.3.2.5 thorpej if (pwcs != NULL) {
163 1.3.2.5 thorpej *pwcs = (wchar_t)(unsigned char)*s0;
164 1.3.2.5 thorpej }
165 1.3.2.2 nathanw if (*s0 == '\0') {
166 1.3.2.2 nathanw s0 = NULL;
167 1.3.2.2 nathanw break;
168 1.3.2.2 nathanw }
169 1.3.2.2 nathanw s0++;
170 1.3.2.5 thorpej if (pwcs != NULL) {
171 1.3.2.5 thorpej pwcs++;
172 1.3.2.5 thorpej n--;
173 1.3.2.5 thorpej }
174 1.3.2.5 thorpej cnt++;
175 1.3.2.2 nathanw }
176 1.3.2.5 thorpej if (pwcs)
177 1.3.2.2 nathanw *s = s0;
178 1.3.2.2 nathanw
179 1.3.2.5 thorpej *nresult = (size_t)cnt;
180 1.3.2.2 nathanw
181 1.3.2.2 nathanw return (0);
182 1.3.2.2 nathanw }
183 1.3.2.2 nathanw
184 1.3.2.2 nathanw static int
185 1.3.2.2 nathanw _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs,
186 1.3.2.2 nathanw const char * __restrict s, size_t n,
187 1.3.2.2 nathanw size_t * __restrict nresult)
188 1.3.2.2 nathanw {
189 1.3.2.4 nathanw return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, (const char **)&s, n, NULL, nresult));
190 1.3.2.2 nathanw }
191 1.3.2.2 nathanw
192 1.3.2.2 nathanw static int
193 1.3.2.2 nathanw /*ARGSUSED*/
194 1.3.2.2 nathanw _citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc,
195 1.3.2.2 nathanw const char * __restrict s, size_t n,
196 1.3.2.2 nathanw int * __restrict nresult)
197 1.3.2.2 nathanw {
198 1.3.2.2 nathanw
199 1.3.2.2 nathanw if (s == NULL) {
200 1.3.2.2 nathanw *nresult = 0; /* state independent */
201 1.3.2.2 nathanw return (0);
202 1.3.2.2 nathanw }
203 1.3.2.2 nathanw if (n == 0) {
204 1.3.2.2 nathanw return (EILSEQ);
205 1.3.2.2 nathanw }
206 1.3.2.2 nathanw if (pwc == NULL) {
207 1.3.2.2 nathanw if (*s == '\0') {
208 1.3.2.2 nathanw *nresult = 0;
209 1.3.2.2 nathanw } else {
210 1.3.2.2 nathanw *nresult = 1;
211 1.3.2.2 nathanw }
212 1.3.2.2 nathanw return (0);
213 1.3.2.2 nathanw }
214 1.3.2.2 nathanw
215 1.3.2.2 nathanw *pwc = (wchar_t)*s;
216 1.3.2.2 nathanw *nresult = *s == '\0' ? 0 : 1;
217 1.3.2.2 nathanw
218 1.3.2.2 nathanw return (0);
219 1.3.2.2 nathanw }
220 1.3.2.2 nathanw
221 1.3.2.2 nathanw static int
222 1.3.2.2 nathanw /*ARGSUSED*/
223 1.3.2.2 nathanw _citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s,
224 1.3.2.2 nathanw wchar_t wc, void * __restrict pspriv,
225 1.3.2.2 nathanw size_t * __restrict nresult)
226 1.3.2.2 nathanw {
227 1.3.2.2 nathanw if ((wc&~0xFFU) != 0) {
228 1.3.2.2 nathanw *nresult = (size_t)-1;
229 1.3.2.2 nathanw return (EILSEQ);
230 1.3.2.2 nathanw }
231 1.3.2.2 nathanw
232 1.3.2.2 nathanw *nresult = 1;
233 1.3.2.2 nathanw if (s!=NULL)
234 1.3.2.2 nathanw *s = (char)wc;
235 1.3.2.2 nathanw
236 1.3.2.2 nathanw return (0);
237 1.3.2.2 nathanw }
238 1.3.2.2 nathanw
239 1.3.2.2 nathanw static int
240 1.3.2.2 nathanw /*ARGSUSED*/
241 1.3.2.2 nathanw _citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s,
242 1.3.2.2 nathanw const wchar_t ** __restrict pwcs, size_t n,
243 1.3.2.2 nathanw void * __restrict pspriv,
244 1.3.2.2 nathanw size_t * __restrict nresult)
245 1.3.2.2 nathanw {
246 1.3.2.2 nathanw size_t count;
247 1.3.2.2 nathanw const wchar_t *pwcs0;
248 1.3.2.2 nathanw
249 1.3.2.2 nathanw pwcs0 = *pwcs;
250 1.3.2.2 nathanw count = 0;
251 1.3.2.3 nathanw
252 1.3.2.3 nathanw if (s == NULL)
253 1.3.2.3 nathanw n = 1;
254 1.3.2.3 nathanw
255 1.3.2.3 nathanw while (n > 0) {
256 1.3.2.2 nathanw if ((*pwcs0 & ~0xFFU) != 0) {
257 1.3.2.3 nathanw *nresult = (size_t)-1;
258 1.3.2.2 nathanw return (EILSEQ);
259 1.3.2.2 nathanw }
260 1.3.2.3 nathanw if (s != NULL) {
261 1.3.2.2 nathanw *s++ = (char)*pwcs0;
262 1.3.2.3 nathanw n--;
263 1.3.2.3 nathanw }
264 1.3.2.2 nathanw if (*pwcs0 == L'\0') {
265 1.3.2.2 nathanw pwcs0 = NULL;
266 1.3.2.2 nathanw break;
267 1.3.2.2 nathanw }
268 1.3.2.2 nathanw count++;
269 1.3.2.2 nathanw pwcs0++;
270 1.3.2.2 nathanw }
271 1.3.2.2 nathanw if (s != NULL)
272 1.3.2.2 nathanw *pwcs = pwcs0;
273 1.3.2.2 nathanw
274 1.3.2.2 nathanw *nresult = count;
275 1.3.2.2 nathanw
276 1.3.2.2 nathanw return (0);
277 1.3.2.2 nathanw }
278 1.3.2.2 nathanw
279 1.3.2.2 nathanw static int
280 1.3.2.2 nathanw _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s,
281 1.3.2.2 nathanw const wchar_t * __restrict pwcs, size_t n,
282 1.3.2.2 nathanw size_t * __restrict nresult)
283 1.3.2.2 nathanw {
284 1.3.2.4 nathanw return (_citrus_NONE_ctype_wcsrtombs(cl, s, (const wchar_t **)&pwcs, n, NULL, nresult));
285 1.3.2.2 nathanw }
286 1.3.2.2 nathanw
287 1.3.2.2 nathanw static int
288 1.3.2.2 nathanw _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s,
289 1.3.2.2 nathanw wchar_t wc, int * __restrict nresult)
290 1.3.2.2 nathanw {
291 1.3.2.2 nathanw int ret;
292 1.3.2.2 nathanw size_t nr;
293 1.3.2.2 nathanw
294 1.3.2.5 thorpej if (s == 0) {
295 1.3.2.5 thorpej /*
296 1.3.2.5 thorpej * initialize state here.
297 1.3.2.5 thorpej * (nothing to do for us.)
298 1.3.2.5 thorpej */
299 1.3.2.5 thorpej *nresult = 0; /* we're state independent */
300 1.3.2.5 thorpej return (0);
301 1.3.2.5 thorpej }
302 1.3.2.5 thorpej
303 1.3.2.2 nathanw ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr);
304 1.3.2.2 nathanw *nresult = (int)nr;
305 1.3.2.2 nathanw
306 1.3.2.2 nathanw return (ret);
307 1.3.2.2 nathanw }
308