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