1 /*-
2  * Copyright (c) 2006, 2008
3  *	Thorsten Glaser <tg@mirbsd.org>
4  *
5  * Provided that these terms and disclaimer and all copyright notices
6  * are retained or reproduced in an accompanying document, permission
7  * is granted to deal in this work without restriction, including un-
8  * limited rights to use, publicly perform, distribute, sell, modify,
9  * merge, give away, or sublicence.
10  *
11  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12  * the utmost extent permitted by applicable law, neither express nor
13  * implied; without malicious intent or gross negligence. In no event
14  * may a licensor, author or contributor be held liable for indirect,
15  * direct, other damage, loss, or other issues arising in any way out
16  * of dealing in the work, even if advised of the possibility of such
17  * damage or existence of a defect, except proven that it results out
18  * of said person's immediate fault when using the work as intended.
19  */
20 
21 #include <wchar.h>
22 
23 #define mir18n_attributes
24 #include "mir18n.h"
25 
26 __RCSID("$MirOS: src/lib/libc/i18n/wctype.c,v 1.5 2008/11/30 13:20:38 tg Exp $");
27 
28 /* this is, admittedly, taken from libutf8 */
29 struct ctype_property {
30 	const char *property;
31 	wctype_t ctype;
32 };
33 
34 static int ctype_property_cmp(const void *, const void *);
35 
36 /*
37  * keep this sorted for bsearch, we're not as stupid as the
38  * FSF to do a linear search if we can do better ;)
39  */
40 static struct ctype_property all_properties[] = {
41 	{ "alnum", wctype_alnum },
42 	{ "alpha", wctype_alpha },
43 	{ "blank", wctype_blank },
44 	{ "cntrl", wctype_cntrl },
45 	{ "digit", wctype_digit },
46 	{ "graph", wctype_graph },
47 	{ "lower", wctype_lower },
48 	{ "print", wctype_print },
49 	{ "punct", wctype_punct },
50 	{ "space", wctype_space },
51 	{ "title", wctype_title },
52 	{ "upper", wctype_upper },
53 	{ "xdigit", wctype_xdigit }
54 };
55 
56 static int
ctype_property_cmp(const void * key,const void * item)57 ctype_property_cmp(const void *key, const void *item)
58 {
59 	return (strcmp((const char *)key,
60 	    ((const struct ctype_property *)item)->property));
61 }
62 
63 wctype_t
wctype(const char * property)64 wctype(const char *property)
65 {
66 	struct ctype_property *rv;
67 
68 	rv = bsearch(property, all_properties,
69 	    (sizeof (all_properties) / sizeof (all_properties[0])),
70 	    sizeof (struct ctype_property), ctype_property_cmp);
71 	return ((rv == NULL) ? 0 : rv->ctype);
72 }
73