Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / locale / iswctype_sb.c
blobb11172dc7c79591c46d511facf5005218b7d5683
1 /* $NetBSD: iswctype_sb.c,v 1.5.6.2 2009/01/10 22:59:51 christos Exp $ */
3 /*-
4 * Copyright (c)2008 Citrus Project,
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: iswctype_sb.c,v 1.5.6.2 2009/01/10 22:59:51 christos Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wctype.h>
42 #include <wchar.h>
44 #define _ISWCTYPE_FUNC(name) \
45 int \
46 isw##name(wint_t wc) \
47 { \
48 int c; \
50 c = (wc == WEOF) ? EOF : (unsigned char)wc; \
51 return is##name(c); \
53 _ISWCTYPE_FUNC(alnum)
54 _ISWCTYPE_FUNC(alpha)
55 _ISWCTYPE_FUNC(blank)
56 _ISWCTYPE_FUNC(cntrl)
57 _ISWCTYPE_FUNC(digit)
58 _ISWCTYPE_FUNC(graph)
59 _ISWCTYPE_FUNC(lower)
60 _ISWCTYPE_FUNC(print)
61 _ISWCTYPE_FUNC(punct)
62 _ISWCTYPE_FUNC(space)
63 _ISWCTYPE_FUNC(upper)
64 _ISWCTYPE_FUNC(xdigit)
66 #define _TOWCTRANS_FUNC(name) \
67 wint_t \
68 tow##name(wint_t wc) \
69 { \
70 int c; \
71 c = (wc == WEOF) ? EOF : (unsigned char)wc; \
72 return to##name(c); \
74 _TOWCTRANS_FUNC(upper)
75 _TOWCTRANS_FUNC(lower)
77 struct _wctype_priv_t {
78 const char *name;
79 int (*iswctype)(wint_t);
82 static const struct _wctype_priv_t _wctype_decl[] = {
83 { "alnum", &iswalnum },
84 { "alpha", &iswalpha },
85 { "blank", &iswblank },
86 { "cntrl", &iswcntrl },
87 { "digit", &iswdigit },
88 { "graph", &iswgraph },
89 { "lower", &iswlower },
90 { "print", &iswprint },
91 { "punct", &iswpunct },
92 { "space", &iswspace },
93 { "upper", &iswupper },
94 { "xdigit", &iswxdigit },
96 static const size_t _wctype_decl_size =
97 sizeof(_wctype_decl) / sizeof(struct _wctype_priv_t);
99 wctype_t
100 wctype(const char *charclass)
102 size_t i;
104 for (i = 0; i < _wctype_decl_size; ++i) {
105 if (!strcmp(charclass, _wctype_decl[i].name))
106 return (wctype_t)__UNCONST(&_wctype_decl[i]);
108 return (wctrans_t)NULL;
111 struct _wctrans_priv_t {
112 const char *name;
113 wint_t (*towctrans)(wint_t);
116 static const struct _wctrans_priv_t _wctrans_decl[] = {
117 { "upper", &towupper },
118 { "lower", &towlower },
120 static const size_t _wctrans_decl_size =
121 sizeof(_wctrans_decl) / sizeof(struct _wctrans_priv_t);
123 wctrans_t
124 /*ARGSUSED*/
125 wctrans(const char *charmap)
127 size_t i;
129 for (i = 0; i < _wctrans_decl_size; ++i) {
130 if (!strcmp(charmap, _wctrans_decl[i].name))
131 return (wctrans_t)__UNCONST(&_wctrans_decl[i]);
133 return (wctrans_t)NULL;
137 /*ARGSUSED*/
138 iswctype(wint_t wc, wctype_t charclass)
140 const struct _wctype_priv_t *p;
142 p = (const struct _wctype_priv_t *)(void *)charclass;
143 if (p < &_wctype_decl[0] || p > &_wctype_decl[_wctype_decl_size - 1]) {
144 errno = EINVAL;
145 return 0;
147 return (*p->iswctype)(wc);
150 wint_t
151 /*ARGSUSED*/
152 towctrans(wint_t wc, wctrans_t charmap)
154 const struct _wctrans_priv_t *p;
156 p = (const struct _wctrans_priv_t *)(void *)charmap;
157 if (p < &_wctrans_decl[0] || p > &_wctrans_decl[_wctrans_decl_size - 1]) {
158 errno = EINVAL;
159 return wc;
161 return (*p->towctrans)(wc);
164 __weak_alias(wcwidth,_wcwidth)
167 wcwidth(wchar_t wc)
169 int c;
171 switch (wc) {
172 case L'\0':
173 return 0;
174 case WEOF:
175 c = EOF;
176 break;
177 default:
178 c = (unsigned char)wc;
180 if (isprint(c))
181 return 1;
182 return -1;
186 wcswidth(const wchar_t * __restrict ws, size_t wn)
188 const wchar_t *pws;
189 int c;
191 pws = ws;
192 while (wn > 0 && *ws != L'\0') {
193 c = (*ws == WEOF) ? EOF : (unsigned char)*ws;
194 if (!isprint(c))
195 return -1;
196 ++ws, --wn;
198 return (int)(ws - pws);