1 /* $NetBSD: src/lib/libc/citrus/citrus_ctype.c,v 1.4 2003/03/05 20:18:15 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_ctype.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
41 #include "citrus_module.h"
42 #include "citrus_ctype.h"
43 #include "citrus_ctype_fallback.h"
44 #include "citrus_none.h"
45 #include _CITRUS_DEFAULT_CTYPE_HEADER
47 _citrus_ctype_rec_t _citrus_ctype_default = {
48 &_CITRUS_DEFAULT_CTYPE_OPS, /* cc_ops */
49 NULL, /* cc_closure */
55 static int _initctypemodule(_citrus_ctype_t, char const *, _citrus_module_t,
56 void *, size_t, size_t);
59 _initctypemodule(_citrus_ctype_t cc, char const *modname,
60 _citrus_module_t handle, void *variable, size_t lenvar,
64 _citrus_ctype_getops_t getops;
66 _DIAGASSERT(cc != NULL);
68 cc->cc_module = handle;
70 getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module,
76 cc->cc_ops = malloc(sizeof(*cc->cc_ops));
77 if (cc->cc_ops == NULL)
80 ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops),
81 _CITRUS_CTYPE_ABI_VERSION);
85 /* If return ABI version is not expected, fixup it here*/
86 switch (cc->cc_ops->co_abi_version) {
88 cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback;
89 cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback;
97 /* validation check */
98 if (cc->cc_ops->co_init == NULL ||
99 cc->cc_ops->co_uninit == NULL ||
100 cc->cc_ops->co_get_mb_cur_max == NULL ||
101 cc->cc_ops->co_mblen == NULL ||
102 cc->cc_ops->co_mbrlen == NULL ||
103 cc->cc_ops->co_mbrtowc == NULL ||
104 cc->cc_ops->co_mbsinit == NULL ||
105 cc->cc_ops->co_mbsrtowcs == NULL ||
106 cc->cc_ops->co_mbstowcs == NULL ||
107 cc->cc_ops->co_mbtowc == NULL ||
108 cc->cc_ops->co_wcrtomb == NULL ||
109 cc->cc_ops->co_wcsrtombs == NULL ||
110 cc->cc_ops->co_wcstombs == NULL ||
111 cc->cc_ops->co_wctomb == NULL ||
112 cc->cc_ops->co_btowc == NULL ||
113 cc->cc_ops->co_wctob == NULL)
116 /* init and get closure */
117 ret = (*cc->cc_ops->co_init)(
118 &cc->cc_closure, variable, lenvar, szpriv);
133 _citrus_ctype_open(_citrus_ctype_t *rcc,
134 char const *encname, void *variable, size_t lenvar,
138 _citrus_module_t handle;
141 _DIAGASSERT(encname != NULL);
142 _DIAGASSERT(!lenvar || variable!=NULL);
143 _DIAGASSERT(rcc != NULL);
145 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) {
146 *rcc = &_citrus_ctype_default;
149 ret = _citrus_load_module(&handle, encname);
153 cc = calloc(1, sizeof(*cc));
155 _citrus_unload_module(handle);
159 ret = _initctypemodule(cc, encname, handle, variable, lenvar, szpriv);
161 _citrus_unload_module(cc->cc_module);
172 _citrus_ctype_close(_citrus_ctype_t cc)
175 _DIAGASSERT(cc != NULL);
177 if (cc == &_citrus_ctype_default)
179 (*cc->cc_ops->co_uninit)(cc->cc_closure);
181 _citrus_unload_module(cc->cc_module);
190 _citrus_ctype_open(_citrus_ctype_t *rcc,
191 char const *encname, void *variable, size_t lenvar,
194 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) {
195 *rcc = &_citrus_ctype_default;
203 _citrus_ctype_close(_citrus_ctype_t cc)