1 /* @(#)wctype.c	1.3 17/08/13 Copyright 2017 J. Schilling */
2 /*
3  *	Emulate the behavior of wctype() and iswctype()
4  *
5  *	Copyright (c) 2017 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #include <schily/ctype.h>
22 #include <schily/wctype.h>
23 #include <schily/wchar.h>
24 #include <schily/string.h>
25 #include <schily/schily.h>
26 
27 #ifndef	HAVE_WCTYPE
28 LOCAL struct wct {
29 	char		*name;
30 	wctype_t	val;
31 } wct[] = {
32 	{"alnum", 1},
33 	{"alpha", 2},
34 	{"blank", 3},
35 	{"cntrl", 4},
36 	{"digit", 5},
37 	{"graph", 6},
38 	{"lower", 7},
39 	{"print", 8},
40 	{"punct", 9},
41 	{"space", 10},
42 	{"upper", 11},
43 	{"xdigit", 12},
44 	{ NULL, 0}
45 };
46 
47 wctype_t
wctype(n)48 wctype(n)
49 	const char	*n;
50 {
51 	register struct wct *wp = wct;
52 
53 	for (; wp->name; wp++) {
54 		if (*n != *wp->name)
55 			continue;
56 		if (strcmp(n, wp->name) == 0)
57 			return (wp->val);
58 	}
59 	return (0);
60 }
61 
62 int
iswctype(wc,t)63 iswctype(wc, t)
64 	wint_t		wc;
65 	wctype_t	t;
66 {
67 	switch (t) {
68 
69 	case 1: return (iswalnum(wc));
70 	case 2: return (iswalpha(wc));
71 #if defined(HAVE_ISWBLANK) || ((MB_LEN_MAX == 1) && defined(HAVE_ISBLANK))
72 	case 3: return (iswblank(wc));
73 #else
74 	case 3: return (isspace(wc));
75 #endif
76 	case 4: return (iswcntrl(wc));
77 	case 5: return (iswdigit(wc));
78 	case 6: return (iswgraph(wc));
79 	case 7: return (iswlower(wc));
80 	case 8: return (iswprint(wc));
81 	case 9: return (iswpunct(wc));
82 	case 10: return (iswspace(wc));
83 	case 11: return (iswupper(wc));
84 	case 12: return (iswxdigit(wc));
85 
86 	default:
87 		return (0);
88 	}
89 }
90 #endif	/* HAVE_WCTYPE */
91