wchar.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_POSIX_WCHAR_H
  19. #define GRUB_POSIX_WCHAR_H 1
  20. #include <grub/charset.h>
  21. #define wint_t grub_posix_wint_t
  22. #define wchar_t grub_posix_wchar_t
  23. #define mbstate_t grub_posix_mbstate_t
  24. /* UCS-4. */
  25. typedef grub_int32_t wint_t;
  26. enum
  27. {
  28. WEOF = -1
  29. };
  30. /* UCS-4. */
  31. typedef grub_int32_t wchar_t;
  32. typedef struct mbstate {
  33. grub_uint32_t code;
  34. int count;
  35. } mbstate_t;
  36. /* UTF-8. */
  37. #define MB_CUR_MAX 4
  38. #define MB_LEN_MAX 4
  39. static inline size_t
  40. mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
  41. {
  42. const char *ptr;
  43. if (!s)
  44. {
  45. pwc = 0;
  46. s = "";
  47. n = 1;
  48. }
  49. if (pwc)
  50. *pwc = 0;
  51. for (ptr = s; ptr < s + n; ptr++)
  52. {
  53. if (!grub_utf8_process (*ptr, &ps->code, &ps->count))
  54. return -1;
  55. if (ps->count)
  56. continue;
  57. if (pwc)
  58. *pwc = ps->code;
  59. if (ps->code == 0)
  60. return 0;
  61. return ptr - s + 1;
  62. }
  63. return -2;
  64. }
  65. static inline int
  66. mbsinit(const mbstate_t *ps)
  67. {
  68. return ps->count == 0;
  69. }
  70. static inline size_t
  71. wcrtomb (char *s, wchar_t wc, mbstate_t *ps __attribute__ ((unused)))
  72. {
  73. if (s == 0)
  74. return 1;
  75. return grub_encode_utf8_character ((grub_uint8_t *) s,
  76. (grub_uint8_t *) s + MB_LEN_MAX,
  77. wc);
  78. }
  79. static inline wint_t btowc (int c)
  80. {
  81. if (c & ~0x7f)
  82. return WEOF;
  83. return c;
  84. }
  85. static inline int
  86. wcscoll (const wchar_t *s1, const wchar_t *s2)
  87. {
  88. while (*s1 && *s2)
  89. {
  90. if (*s1 != *s2)
  91. break;
  92. s1++;
  93. s2++;
  94. }
  95. if (*s1 < *s2)
  96. return -1;
  97. if (*s1 > *s2)
  98. return +1;
  99. return 0;
  100. }
  101. #endif