1 /*
2  * ctype wrappers
3  *
4  * Copyright (c) 2011  Marko Kreen
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * @file
21  * ctype compat.
22  *
23  * Provides wrappers that make sure the functions work on 'char' values.
24  *
25  * @note
26  * POSIX requires that these functions accept EOF/-1 in addition
27  * to ordinary byte values.  That means when working on 'char',
28  * the functions cannot differetiate between 0xFF and EOF.
29  * As no code should give EOF to <ctype.h> functions and no code
30  * should depend whether 0xFF is labeled ispunct() or not,
31  * it seems no worthwhile to fix it.
32  */
33 
34 #ifndef _USUAL_CTYPE_H_
35 #define _USUAL_CTYPE_H_
36 
37 #include <usual/base.h>
38 
39 #include <ctype.h>
40 
41 #ifndef isblank
42 #define isblank usual_isblank
isblank(int c)43 static inline int isblank(int c) { return (c == ' ') || (c == '\t'); }
44 #endif
45 
46 /* keep right signature, cast to uchar internally */
47 #define _WRAP_CTYPE_FN(name) \
48 	static inline int safe_ ## name (int c) { \
49 		return name((unsigned char)(c)); \
50 	}
51 
52 _WRAP_CTYPE_FN(isalnum)
53 #undef isalnum
54 /** Safe isalnum */
55 #define isalnum safe_isalnum
56 
57 _WRAP_CTYPE_FN(isalpha)
58 #undef isalpha
59 /** Safe isalpha */
60 #define isalpha safe_isalpha
61 
62 _WRAP_CTYPE_FN(isascii)
63 #undef isascii
64 /** Safe isascii */
65 #define isascii safe_isascii
66 
67 _WRAP_CTYPE_FN(isblank)
68 #undef isblank
69 /** Safe isblank */
70 #define isblank safe_isblank
71 
72 _WRAP_CTYPE_FN(iscntrl)
73 #undef iscntrl
74 /** Safe iscntrl */
75 #define iscntrl safe_iscntrl
76 
77 _WRAP_CTYPE_FN(isdigit)
78 #undef isdigit
79 /** Safe isdigit */
80 #define isdigit safe_isdigit
81 
82 _WRAP_CTYPE_FN(isgraph)
83 #undef isgraph
84 /** Safe isgraph */
85 #define isgraph safe_isgraph
86 
87 _WRAP_CTYPE_FN(islower)
88 #undef islower
89 /** Safe islower */
90 #define islower safe_islower
91 
92 _WRAP_CTYPE_FN(isprint)
93 #undef isprint
94 /** Safe isprint */
95 #define isprint safe_isprint
96 
97 _WRAP_CTYPE_FN(ispunct)
98 #undef ispunct
99 /** Safe ispunct */
100 #define ispunct safe_ispunct
101 
102 _WRAP_CTYPE_FN(isspace)
103 #undef isspace
104 /** Safe isspace */
105 #define isspace safe_isspace
106 
107 _WRAP_CTYPE_FN(isupper)
108 #undef isupper
109 /** Safe isupper */
110 #define isupper safe_isupper
111 
112 _WRAP_CTYPE_FN(isxdigit)
113 #undef isxdigit
114 /** Safe isxdigit */
115 #define isxdigit safe_isxdigit
116 
117 _WRAP_CTYPE_FN(tolower)
118 #undef tolower
119 /** Safe tolower */
120 #define tolower safe_tolower
121 
122 _WRAP_CTYPE_FN(toupper)
123 #undef toupper
124 /** Safe toupper */
125 #define toupper safe_toupper
126 
127 #undef _WRAP_CTYPE_FN
128 
129 #endif /* _USUAL_CTYPE_H_ */
130