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