xref: /dragonfly/lib/libc/locale/iswctype.c (revision 1847e88f)
1 /*	$NetBSD: src/lib/libc/locale/iswctype.c,v 1.14 2003/08/07 16:43:04 agc Exp $	*/
2 /*	$DragonFly: src/lib/libc/locale/iswctype.c,v 1.1 2005/03/16 06:54:41 joerg Exp $ */
3 
4 /*
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <ctype.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <wchar.h>
42 #include <wctype.h>
43 #include "rune.h"
44 #include "runetype.h"
45 #include "rune_local.h"
46 #include "_wctrans_local.h"
47 
48 static _RuneType __runetype_w(wint_t);
49 static int	__isctype_w(wint_t, _RuneType);
50 static wint_t	__toupper_w(wint_t);
51 static wint_t	__tolower_w(wint_t);
52 
53 static _RuneType
54 __runetype_w(wint_t c)
55 {
56 	_RuneLocale *rl = _CurrentRuneLocale;
57 
58 	if (_RUNE_ISCACHED(c))
59 		return(rl->rl_runetype[c]);
60 	else
61 		return(___runetype_mb(c));
62 }
63 
64 static int
65 __isctype_w(wint_t c, _RuneType f)
66 {
67 	if (__runetype_w(c) & f)
68 		return(1);
69 	else
70 		return(0);
71 }
72 
73 static wint_t
74 __toupper_w(wint_t c)
75 {
76 	return(_towctrans(c, _wctrans_upper(_CurrentRuneLocale)));
77 }
78 
79 static wint_t
80 __tolower_w(wint_t c)
81 {
82 	return(_towctrans(c, _wctrans_lower(_CurrentRuneLocale)));
83 }
84 
85 #undef iswalnum
86 int
87 iswalnum(wint_t c)
88 {
89 	return(__isctype_w((c), _CTYPE_A|_CTYPE_D));
90 }
91 
92 #undef iswalpha
93 int
94 iswalpha(wint_t c)
95 {
96 	return(__isctype_w((c), _CTYPE_A));
97 }
98 
99 #undef iswblank
100 int
101 iswblank(wint_t c)
102 {
103 	return(__isctype_w((c), _CTYPE_B));
104 }
105 
106 #undef iswcntrl
107 int
108 iswcntrl(wint_t c)
109 {
110 	return(__isctype_w((c), _CTYPE_C));
111 }
112 
113 #undef iswdigit
114 int
115 iswdigit(wint_t c)
116 {
117 	return(__isctype_w((c), _CTYPE_D));
118 }
119 
120 #undef iswgraph
121 int
122 iswgraph(wint_t c)
123 {
124 	return(__isctype_w((c), _CTYPE_G));
125 }
126 
127 #undef iswlower
128 int
129 iswlower(wint_t c)
130 {
131 	return(__isctype_w((c), _CTYPE_L));
132 }
133 
134 #undef iswprint
135 int
136 iswprint(wint_t c)
137 {
138 	return(__isctype_w((c), _CTYPE_R));
139 }
140 
141 #undef iswpunct
142 int
143 iswpunct(wint_t c)
144 {
145 	return(__isctype_w((c), _CTYPE_P));
146 }
147 
148 #undef iswspace
149 int
150 iswspace(wint_t c)
151 {
152 	return(__isctype_w((c), _CTYPE_S));
153 }
154 
155 #undef iswupper
156 int
157 iswupper(wint_t c)
158 {
159 	return(__isctype_w((c), _CTYPE_U));
160 }
161 
162 #undef iswxdigit
163 int
164 iswxdigit(wint_t c)
165 {
166 	return(__isctype_w((c), _CTYPE_X));
167 }
168 
169 #undef towupper
170 wint_t
171 towupper(wint_t c)
172 {
173 	return(__toupper_w(c));
174 }
175 
176 #undef towlower
177 wint_t
178 towlower(wint_t c)
179 {
180 	return(__tolower_w(c));
181 }
182 
183 #undef wcwidth
184 int
185 wcwidth(wchar_t c)
186 {
187         return(((unsigned)__runetype_w(c) & _CTYPE_SWM) >> _CTYPE_SWS);
188 }
189 
190 #undef wctrans
191 wctrans_t
192 wctrans(const char *charclass)
193 {
194 	int i;
195 	_RuneLocale *rl = _CurrentRuneLocale;
196 
197 	if (rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name == NULL)
198 		_wctrans_init(rl);
199 
200 	for (i = 0; i < _WCTRANS_NINDEXES; i++) {
201 		if (strcmp(rl->rl_wctrans[i].te_name, charclass) == 0)
202 			return((wctrans_t)&rl->rl_wctrans[i]);
203 	}
204 
205 	return((wctrans_t)NULL);
206 }
207 
208 #undef towctrans
209 wint_t
210 towctrans(wint_t c, wctrans_t desc)
211 {
212 	if (desc == NULL) {
213 		errno = EINVAL;
214 		return(c);
215 	}
216 	return(_towctrans(c, (_WCTransEntry *)desc));
217 }
218 
219 #undef wctype
220 wctype_t
221 wctype(const char *property)
222 {
223 	int i;
224 	_RuneLocale *rl = _CurrentRuneLocale;
225 
226 	for (i=0; i < _WCTYPE_NINDEXES; i++) {
227 		if (strcmp(rl->rl_wctype[i].te_name, property) == 0)
228 			return((wctype_t)&rl->rl_wctype[i]);
229 	}
230 	return((wctype_t)NULL);
231 }
232 
233 #undef iswctype
234 int
235 iswctype(wint_t c, wctype_t charclass)
236 {
237 	/*
238 	 * SUSv3: If charclass is 0, iswctype() shall return 0.
239 	 */
240 	if (charclass == (wctype_t)0)
241 		return(0);
242 
243 	return (__isctype_w(c, ((_WCTypeEntry *)charclass)->te_mask));
244 }
245