Make the generated wctype.h more self-contained.
[gnulib.git] / lib / wctype_.h
1 /* A substitute for ISO C99 <wctype.h>, for platforms that lack it.
2
3    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Bruno Haible and Paul Eggert.  */
20
21 /*
22  * ISO C 99 <wctype.h> for platforms that lack it.
23  * <http://www.opengroup.org/susv3xbd/wctype.h.html>
24  *
25  * iswctype, towctrans, towlower, towupper, wctrans, wctype,
26  * wctrans_t, and wctype_t are not yet implemented.
27  */
28
29 #if @HAVE_WINT_T@
30 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.
31    Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
32    <wchar.h>.
33    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
34    included before <wchar.h>.  */
35 # include <stddef.h>
36 # include <stdio.h>
37 # include <time.h>
38 # include <wchar.h>
39 #endif
40
41 #if @HAVE_WCTYPE_H@
42 # if defined __DECC && __DECC_VER >= 60000000
43 #  include_next <wctype.h>
44 # endif
45 #endif
46
47 #ifndef _GL_WCTYPE_H
48 #define _GL_WCTYPE_H
49
50 /* Include the original <wctype.h> if it exists.
51    BeOS 5 has the functions but no <wctype.h>.  */
52 #if @HAVE_WCTYPE_H@
53 # if !(defined __DECC && __DECC_VER >= 60000000)
54 #  include @ABSOLUTE_WCTYPE_H@
55 # endif
56 #endif
57
58 #if @HAVE_WINT_T@
59 typedef wint_t __wctype_wint_t;
60 #else
61 typedef int __wctype_wint_t;
62 #endif
63
64 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
65    Assume all 12 functions are implemented the same way, or not at all.  */
66 #if ! @HAVE_ISWCNTRL@
67
68 /* IRIX 5.3 has macros but no functions, its isw* macros refer to an
69    undefined variable _ctmp_ and to <ctype.h> macros like _P, and they
70    refer to system functions like _iswctype that are not in the
71    standard C library.  Rather than try to get ancient buggy
72    implementations like this to work, just disable them.  */
73 #  undef iswalnum
74 #  undef iswalpha
75 #  undef iswblank
76 #  undef iswcntrl
77 #  undef iswdigit
78 #  undef iswgraph
79 #  undef iswlower
80 #  undef iswprint
81 #  undef iswpunct
82 #  undef iswspace
83 #  undef iswupper
84 #  undef iswxdigit
85
86 static inline int
87 iswalnum (__wctype_wint_t wc)
88 {
89   return ((wc >= '0' && wc <= '9')
90           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'));
91 }
92
93 static inline int
94 iswalpha (__wctype_wint_t wc)
95 {
96   return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z';
97 }
98
99 static inline int
100 iswblank (__wctype_wint_t wc)
101 {
102   return wc == ' ' || wc == '\t';
103 }
104
105 static inline int
106 iswcntrl (__wctype_wint_t wc)
107 {
108   return (wc & ~0x1f) == 0 || wc == 0x7f;
109 }
110
111 static inline int
112 iswdigit (__wctype_wint_t wc)
113 {
114   return wc >= '0' && wc <= '9';
115 }
116
117 static inline int
118 iswgraph (__wctype_wint_t wc)
119 {
120   return wc >= '!' && wc <= '~';
121 }
122
123 static inline int
124 iswlower (__wctype_wint_t wc)
125 {
126   return wc >= 'a' && wc <= 'z';
127 }
128
129 static inline int
130 iswprint (__wctype_wint_t wc)
131 {
132   return wc >= ' ' && wc <= '~';
133 }
134
135 static inline int
136 iswpunct (__wctype_wint_t wc)
137 {
138   return (wc >= '!' && wc <= '~'
139           && !((wc >= '0' && wc <= '9')
140                || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')));
141 }
142
143 static inline int
144 iswspace (__wctype_wint_t wc)
145 {
146   return (wc == ' ' || wc == '\t'
147           || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r');
148 }
149
150 static inline int
151 iswupper (__wctype_wint_t wc)
152 {
153   return wc >= 'A' && wc <= 'Z';
154 }
155
156 static inline int
157 iswxdigit (__wctype_wint_t wc)
158 {
159   return ((wc >= '0' && wc <= '9')
160           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F'));
161 }
162
163 # endif /* ! HAVE_ISWCNTRL */
164
165 #endif /* _GL_WCTYPE_H */