wctype_.h 3.7 KB

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