Commit | Line | Data |
---|---|---|
8690e634 JK |
1 | /* A substitute for ISO C99 <wctype.h>, for platforms that lack it. |
2 | ||
3 | Copyright (C) 2006-2012 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 3, 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, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | /* Written by Bruno Haible and Paul Eggert. */ | |
19 | ||
20 | /* | |
21 | * ISO C 99 <wctype.h> for platforms that lack it. | |
22 | * <http://www.opengroup.org/susv3xbd/wctype.h.html> | |
23 | * | |
24 | * iswctype, towctrans, towlower, towupper, wctrans, wctype, | |
25 | * wctrans_t, and wctype_t are not yet implemented. | |
26 | */ | |
27 | ||
28 | #ifndef _@GUARD_PREFIX@_WCTYPE_H | |
29 | ||
30 | #if __GNUC__ >= 3 | |
31 | @PRAGMA_SYSTEM_HEADER@ | |
32 | #endif | |
33 | @PRAGMA_COLUMNS@ | |
34 | ||
35 | #if @HAVE_WINT_T@ | |
36 | /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. | |
37 | Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before | |
38 | <wchar.h>. | |
39 | BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be | |
40 | included before <wchar.h>. */ | |
41 | # include <stddef.h> | |
42 | # include <stdio.h> | |
43 | # include <time.h> | |
44 | # include <wchar.h> | |
45 | #endif | |
46 | ||
47 | /* Include the original <wctype.h> if it exists. | |
48 | BeOS 5 has the functions but no <wctype.h>. */ | |
49 | /* The include_next requires a split double-inclusion guard. */ | |
50 | #if @HAVE_WCTYPE_H@ | |
51 | # @INCLUDE_NEXT@ @NEXT_WCTYPE_H@ | |
52 | #endif | |
53 | ||
54 | #ifndef _@GUARD_PREFIX@_WCTYPE_H | |
55 | #define _@GUARD_PREFIX@_WCTYPE_H | |
56 | ||
57 | /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ | |
58 | ||
59 | /* The definition of _GL_WARN_ON_USE is copied here. */ | |
60 | ||
61 | /* Solaris 2.6 <wctype.h> includes <widec.h> which includes <euc.h> which | |
62 | #defines a number of identifiers in the application namespace. Revert | |
63 | these #defines. */ | |
64 | #ifdef __sun | |
65 | # undef multibyte | |
66 | # undef eucw1 | |
67 | # undef eucw2 | |
68 | # undef eucw3 | |
69 | # undef scrw1 | |
70 | # undef scrw2 | |
71 | # undef scrw3 | |
72 | #endif | |
73 | ||
74 | /* Define wint_t and WEOF. (Also done in wchar.in.h.) */ | |
75 | #if !@HAVE_WINT_T@ && !defined wint_t | |
76 | # define wint_t int | |
77 | # ifndef WEOF | |
78 | # define WEOF -1 | |
79 | # endif | |
80 | #else | |
81 | /* MSVC defines wint_t as 'unsigned short' in <crtdefs.h>. | |
82 | This is too small: ISO C 99 section 7.24.1.(2) says that wint_t must be | |
83 | "unchanged by default argument promotions". Override it. */ | |
84 | # if defined _MSC_VER | |
85 | # if !GNULIB_defined_wint_t | |
86 | # include <crtdefs.h> | |
87 | typedef unsigned int rpl_wint_t; | |
88 | # undef wint_t | |
89 | # define wint_t rpl_wint_t | |
90 | # define GNULIB_defined_wint_t 1 | |
91 | # endif | |
92 | # endif | |
93 | # ifndef WEOF | |
94 | # define WEOF ((wint_t) -1) | |
95 | # endif | |
96 | #endif | |
97 | ||
98 | ||
99 | #if !GNULIB_defined_wctype_functions | |
100 | ||
101 | /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions. | |
102 | Linux libc5 has <wctype.h> and the functions but they are broken. | |
103 | Assume all 11 functions (all isw* except iswblank) are implemented the | |
104 | same way, or not at all. */ | |
105 | # if ! @HAVE_ISWCNTRL@ || @REPLACE_ISWCNTRL@ | |
106 | ||
107 | /* IRIX 5.3 has macros but no functions, its isw* macros refer to an | |
108 | undefined variable _ctmp_ and to <ctype.h> macros like _P, and they | |
109 | refer to system functions like _iswctype that are not in the | |
110 | standard C library. Rather than try to get ancient buggy | |
111 | implementations like this to work, just disable them. */ | |
112 | # undef iswalnum | |
113 | # undef iswalpha | |
114 | # undef iswblank | |
115 | # undef iswcntrl | |
116 | # undef iswdigit | |
117 | # undef iswgraph | |
118 | # undef iswlower | |
119 | # undef iswprint | |
120 | # undef iswpunct | |
121 | # undef iswspace | |
122 | # undef iswupper | |
123 | # undef iswxdigit | |
124 | # undef towlower | |
125 | # undef towupper | |
126 | ||
127 | /* Linux libc5 has <wctype.h> and the functions but they are broken. */ | |
128 | # if @REPLACE_ISWCNTRL@ | |
129 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
130 | # define iswalnum rpl_iswalnum | |
131 | # define iswalpha rpl_iswalpha | |
132 | # define iswblank rpl_iswblank | |
133 | # define iswcntrl rpl_iswcntrl | |
134 | # define iswdigit rpl_iswdigit | |
135 | # define iswgraph rpl_iswgraph | |
136 | # define iswlower rpl_iswlower | |
137 | # define iswprint rpl_iswprint | |
138 | # define iswpunct rpl_iswpunct | |
139 | # define iswspace rpl_iswspace | |
140 | # define iswupper rpl_iswupper | |
141 | # define iswxdigit rpl_iswxdigit | |
142 | # endif | |
143 | # endif | |
144 | # if @REPLACE_TOWLOWER@ | |
145 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
146 | # define towlower rpl_towlower | |
147 | # define towupper rpl_towupper | |
148 | # endif | |
149 | # endif | |
150 | ||
151 | static inline int | |
152 | # if @REPLACE_ISWCNTRL@ | |
153 | rpl_iswalnum | |
154 | # else | |
155 | iswalnum | |
156 | # endif | |
157 | (wint_t wc) | |
158 | { | |
159 | return ((wc >= '0' && wc <= '9') | |
160 | || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')); | |
161 | } | |
162 | ||
163 | static inline int | |
164 | # if @REPLACE_ISWCNTRL@ | |
165 | rpl_iswalpha | |
166 | # else | |
167 | iswalpha | |
168 | # endif | |
169 | (wint_t wc) | |
170 | { | |
171 | return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'; | |
172 | } | |
173 | ||
174 | static inline int | |
175 | # if @REPLACE_ISWCNTRL@ | |
176 | rpl_iswblank | |
177 | # else | |
178 | iswblank | |
179 | # endif | |
180 | (wint_t wc) | |
181 | { | |
182 | return wc == ' ' || wc == '\t'; | |
183 | } | |
184 | ||
185 | static inline int | |
186 | # if @REPLACE_ISWCNTRL@ | |
187 | rpl_iswcntrl | |
188 | # else | |
189 | iswcntrl | |
190 | # endif | |
191 | (wint_t wc) | |
192 | { | |
193 | return (wc & ~0x1f) == 0 || wc == 0x7f; | |
194 | } | |
195 | ||
196 | static inline int | |
197 | # if @REPLACE_ISWCNTRL@ | |
198 | rpl_iswdigit | |
199 | # else | |
200 | iswdigit | |
201 | # endif | |
202 | (wint_t wc) | |
203 | { | |
204 | return wc >= '0' && wc <= '9'; | |
205 | } | |
206 | ||
207 | static inline int | |
208 | # if @REPLACE_ISWCNTRL@ | |
209 | rpl_iswgraph | |
210 | # else | |
211 | iswgraph | |
212 | # endif | |
213 | (wint_t wc) | |
214 | { | |
215 | return wc >= '!' && wc <= '~'; | |
216 | } | |
217 | ||
218 | static inline int | |
219 | # if @REPLACE_ISWCNTRL@ | |
220 | rpl_iswlower | |
221 | # else | |
222 | iswlower | |
223 | # endif | |
224 | (wint_t wc) | |
225 | { | |
226 | return wc >= 'a' && wc <= 'z'; | |
227 | } | |
228 | ||
229 | static inline int | |
230 | # if @REPLACE_ISWCNTRL@ | |
231 | rpl_iswprint | |
232 | # else | |
233 | iswprint | |
234 | # endif | |
235 | (wint_t wc) | |
236 | { | |
237 | return wc >= ' ' && wc <= '~'; | |
238 | } | |
239 | ||
240 | static inline int | |
241 | # if @REPLACE_ISWCNTRL@ | |
242 | rpl_iswpunct | |
243 | # else | |
244 | iswpunct | |
245 | # endif | |
246 | (wint_t wc) | |
247 | { | |
248 | return (wc >= '!' && wc <= '~' | |
249 | && !((wc >= '0' && wc <= '9') | |
250 | || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'))); | |
251 | } | |
252 | ||
253 | static inline int | |
254 | # if @REPLACE_ISWCNTRL@ | |
255 | rpl_iswspace | |
256 | # else | |
257 | iswspace | |
258 | # endif | |
259 | (wint_t wc) | |
260 | { | |
261 | return (wc == ' ' || wc == '\t' | |
262 | || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r'); | |
263 | } | |
264 | ||
265 | static inline int | |
266 | # if @REPLACE_ISWCNTRL@ | |
267 | rpl_iswupper | |
268 | # else | |
269 | iswupper | |
270 | # endif | |
271 | (wint_t wc) | |
272 | { | |
273 | return wc >= 'A' && wc <= 'Z'; | |
274 | } | |
275 | ||
276 | static inline int | |
277 | # if @REPLACE_ISWCNTRL@ | |
278 | rpl_iswxdigit | |
279 | # else | |
280 | iswxdigit | |
281 | # endif | |
282 | (wint_t wc) | |
283 | { | |
284 | return ((wc >= '0' && wc <= '9') | |
285 | || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F')); | |
286 | } | |
287 | ||
288 | static inline wint_t | |
289 | # if @REPLACE_TOWLOWER@ | |
290 | rpl_towlower | |
291 | # else | |
292 | towlower | |
293 | # endif | |
294 | (wint_t wc) | |
295 | { | |
296 | return (wc >= 'A' && wc <= 'Z' ? wc - 'A' + 'a' : wc); | |
297 | } | |
298 | ||
299 | static inline wint_t | |
300 | # if @REPLACE_TOWLOWER@ | |
301 | rpl_towupper | |
302 | # else | |
303 | towupper | |
304 | # endif | |
305 | (wint_t wc) | |
306 | { | |
307 | return (wc >= 'a' && wc <= 'z' ? wc - 'a' + 'A' : wc); | |
308 | } | |
309 | ||
310 | # elif @GNULIB_ISWBLANK@ && (! @HAVE_ISWBLANK@ || @REPLACE_ISWBLANK@) | |
311 | /* Only the iswblank function is missing. */ | |
312 | ||
313 | # if @REPLACE_ISWBLANK@ | |
314 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
315 | # define iswblank rpl_iswblank | |
316 | # endif | |
317 | _GL_FUNCDECL_RPL (iswblank, int, (wint_t wc)); | |
318 | # else | |
319 | _GL_FUNCDECL_SYS (iswblank, int, (wint_t wc)); | |
320 | # endif | |
321 | ||
322 | # endif | |
323 | ||
324 | # if defined __MINGW32__ | |
325 | ||
326 | /* On native Windows, wchar_t is uint16_t, and wint_t is uint32_t. | |
327 | The functions towlower and towupper are implemented in the MSVCRT library | |
328 | to take a wchar_t argument and return a wchar_t result. mingw declares | |
329 | these functions to take a wint_t argument and return a wint_t result. | |
330 | This means that: | |
331 | 1. When the user passes an argument outside the range 0x0000..0xFFFF, the | |
332 | function will look only at the lower 16 bits. This is allowed according | |
333 | to POSIX. | |
334 | 2. The return value is returned in the lower 16 bits of the result register. | |
335 | The upper 16 bits are random: whatever happened to be in that part of the | |
336 | result register. We need to fix this by adding a zero-extend from | |
337 | wchar_t to wint_t after the call. */ | |
338 | ||
339 | static inline wint_t | |
340 | rpl_towlower (wint_t wc) | |
341 | { | |
342 | return (wint_t) (wchar_t) towlower (wc); | |
343 | } | |
344 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
345 | # define towlower rpl_towlower | |
346 | # endif | |
347 | ||
348 | static inline wint_t | |
349 | rpl_towupper (wint_t wc) | |
350 | { | |
351 | return (wint_t) (wchar_t) towupper (wc); | |
352 | } | |
353 | # if !(defined __cplusplus && defined GNULIB_NAMESPACE) | |
354 | # define towupper rpl_towupper | |
355 | # endif | |
356 | ||
357 | # endif /* __MINGW32__ */ | |
358 | ||
359 | # define GNULIB_defined_wctype_functions 1 | |
360 | #endif | |
361 | ||
362 | #if @REPLACE_ISWCNTRL@ | |
363 | _GL_CXXALIAS_RPL (iswalnum, int, (wint_t wc)); | |
364 | _GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc)); | |
365 | _GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc)); | |
366 | _GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc)); | |
367 | _GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc)); | |
368 | _GL_CXXALIAS_RPL (iswlower, int, (wint_t wc)); | |
369 | _GL_CXXALIAS_RPL (iswprint, int, (wint_t wc)); | |
370 | _GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc)); | |
371 | _GL_CXXALIAS_RPL (iswspace, int, (wint_t wc)); | |
372 | _GL_CXXALIAS_RPL (iswupper, int, (wint_t wc)); | |
373 | _GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc)); | |
374 | #else | |
375 | _GL_CXXALIAS_SYS (iswalnum, int, (wint_t wc)); | |
376 | _GL_CXXALIAS_SYS (iswalpha, int, (wint_t wc)); | |
377 | _GL_CXXALIAS_SYS (iswcntrl, int, (wint_t wc)); | |
378 | _GL_CXXALIAS_SYS (iswdigit, int, (wint_t wc)); | |
379 | _GL_CXXALIAS_SYS (iswgraph, int, (wint_t wc)); | |
380 | _GL_CXXALIAS_SYS (iswlower, int, (wint_t wc)); | |
381 | _GL_CXXALIAS_SYS (iswprint, int, (wint_t wc)); | |
382 | _GL_CXXALIAS_SYS (iswpunct, int, (wint_t wc)); | |
383 | _GL_CXXALIAS_SYS (iswspace, int, (wint_t wc)); | |
384 | _GL_CXXALIAS_SYS (iswupper, int, (wint_t wc)); | |
385 | _GL_CXXALIAS_SYS (iswxdigit, int, (wint_t wc)); | |
386 | #endif | |
387 | _GL_CXXALIASWARN (iswalnum); | |
388 | _GL_CXXALIASWARN (iswalpha); | |
389 | _GL_CXXALIASWARN (iswcntrl); | |
390 | _GL_CXXALIASWARN (iswdigit); | |
391 | _GL_CXXALIASWARN (iswgraph); | |
392 | _GL_CXXALIASWARN (iswlower); | |
393 | _GL_CXXALIASWARN (iswprint); | |
394 | _GL_CXXALIASWARN (iswpunct); | |
395 | _GL_CXXALIASWARN (iswspace); | |
396 | _GL_CXXALIASWARN (iswupper); | |
397 | _GL_CXXALIASWARN (iswxdigit); | |
398 | ||
399 | #if @GNULIB_ISWBLANK@ | |
400 | # if @REPLACE_ISWCNTRL@ || @REPLACE_ISWBLANK@ | |
401 | _GL_CXXALIAS_RPL (iswblank, int, (wint_t wc)); | |
402 | # else | |
403 | _GL_CXXALIAS_SYS (iswblank, int, (wint_t wc)); | |
404 | # endif | |
405 | _GL_CXXALIASWARN (iswblank); | |
406 | #endif | |
407 | ||
408 | #if !@HAVE_WCTYPE_T@ | |
409 | # if !GNULIB_defined_wctype_t | |
410 | typedef void * wctype_t; | |
411 | # define GNULIB_defined_wctype_t 1 | |
412 | # endif | |
413 | #endif | |
414 | ||
415 | /* Get a descriptor for a wide character property. */ | |
416 | #if @GNULIB_WCTYPE@ | |
417 | # if !@HAVE_WCTYPE_T@ | |
418 | _GL_FUNCDECL_SYS (wctype, wctype_t, (const char *name)); | |
419 | # endif | |
420 | _GL_CXXALIAS_SYS (wctype, wctype_t, (const char *name)); | |
421 | _GL_CXXALIASWARN (wctype); | |
422 | #elif defined GNULIB_POSIXCHECK | |
423 | # undef wctype | |
424 | # if HAVE_RAW_DECL_WCTYPE | |
425 | _GL_WARN_ON_USE (wctype, "wctype is unportable - " | |
426 | "use gnulib module wctype for portability"); | |
427 | # endif | |
428 | #endif | |
429 | ||
430 | /* Test whether a wide character has a given property. | |
431 | The argument WC must be either a wchar_t value or WEOF. | |
432 | The argument DESC must have been returned by the wctype() function. */ | |
433 | #if @GNULIB_ISWCTYPE@ | |
434 | # if !@HAVE_WCTYPE_T@ | |
435 | _GL_FUNCDECL_SYS (iswctype, int, (wint_t wc, wctype_t desc)); | |
436 | # endif | |
437 | _GL_CXXALIAS_SYS (iswctype, int, (wint_t wc, wctype_t desc)); | |
438 | _GL_CXXALIASWARN (iswctype); | |
439 | #elif defined GNULIB_POSIXCHECK | |
440 | # undef iswctype | |
441 | # if HAVE_RAW_DECL_ISWCTYPE | |
442 | _GL_WARN_ON_USE (iswctype, "iswctype is unportable - " | |
443 | "use gnulib module iswctype for portability"); | |
444 | # endif | |
445 | #endif | |
446 | ||
447 | #if @REPLACE_TOWLOWER@ || defined __MINGW32__ | |
448 | _GL_CXXALIAS_RPL (towlower, wint_t, (wint_t wc)); | |
449 | _GL_CXXALIAS_RPL (towupper, wint_t, (wint_t wc)); | |
450 | #else | |
451 | _GL_CXXALIAS_SYS (towlower, wint_t, (wint_t wc)); | |
452 | _GL_CXXALIAS_SYS (towupper, wint_t, (wint_t wc)); | |
453 | #endif | |
454 | _GL_CXXALIASWARN (towlower); | |
455 | _GL_CXXALIASWARN (towupper); | |
456 | ||
457 | #if !@HAVE_WCTRANS_T@ | |
458 | # if !GNULIB_defined_wctrans_t | |
459 | typedef void * wctrans_t; | |
460 | # define GNULIB_defined_wctrans_t 1 | |
461 | # endif | |
462 | #endif | |
463 | ||
464 | /* Get a descriptor for a wide character case conversion. */ | |
465 | #if @GNULIB_WCTRANS@ | |
466 | # if !@HAVE_WCTRANS_T@ | |
467 | _GL_FUNCDECL_SYS (wctrans, wctrans_t, (const char *name)); | |
468 | # endif | |
469 | _GL_CXXALIAS_SYS (wctrans, wctrans_t, (const char *name)); | |
470 | _GL_CXXALIASWARN (wctrans); | |
471 | #elif defined GNULIB_POSIXCHECK | |
472 | # undef wctrans | |
473 | # if HAVE_RAW_DECL_WCTRANS | |
474 | _GL_WARN_ON_USE (wctrans, "wctrans is unportable - " | |
475 | "use gnulib module wctrans for portability"); | |
476 | # endif | |
477 | #endif | |
478 | ||
479 | /* Perform a given case conversion on a wide character. | |
480 | The argument WC must be either a wchar_t value or WEOF. | |
481 | The argument DESC must have been returned by the wctrans() function. */ | |
482 | #if @GNULIB_TOWCTRANS@ | |
483 | # if !@HAVE_WCTRANS_T@ | |
484 | _GL_FUNCDECL_SYS (towctrans, wint_t, (wint_t wc, wctrans_t desc)); | |
485 | # endif | |
486 | _GL_CXXALIAS_SYS (towctrans, wint_t, (wint_t wc, wctrans_t desc)); | |
487 | _GL_CXXALIASWARN (towctrans); | |
488 | #elif defined GNULIB_POSIXCHECK | |
489 | # undef towctrans | |
490 | # if HAVE_RAW_DECL_TOWCTRANS | |
491 | _GL_WARN_ON_USE (towctrans, "towctrans is unportable - " | |
492 | "use gnulib module towctrans for portability"); | |
493 | # endif | |
494 | #endif | |
495 | ||
496 | ||
497 | #endif /* _@GUARD_PREFIX@_WCTYPE_H */ | |
498 | #endif /* _@GUARD_PREFIX@_WCTYPE_H */ |