mbfile.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Multibyte character I/O: macros for multi-byte encodings.
  2. Copyright (C) 2001, 2005, 2009-2020 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 3 of the License, or
  6. (at your option) 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, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Mitsuru Chinen <mchinen@yamato.ibm.com>
  14. and Bruno Haible <bruno@clisp.org>. */
  15. /* The macros in this file implement multi-byte character input from a
  16. stream.
  17. mb_file_t
  18. is the type for multibyte character input stream, usable for variable
  19. declarations.
  20. mbf_char_t
  21. is the type for multibyte character or EOF, usable for variable
  22. declarations.
  23. mbf_init (mbf, stream)
  24. initializes the MB_FILE for reading from stream.
  25. mbf_getc (mbc, mbf)
  26. reads the next multibyte character from mbf and stores it in mbc.
  27. mb_iseof (mbc)
  28. returns true if mbc represents the EOF value.
  29. Here are the function prototypes of the macros.
  30. extern void mbf_init (mb_file_t mbf, FILE *stream);
  31. extern void mbf_getc (mbf_char_t mbc, mb_file_t mbf);
  32. extern bool mb_iseof (const mbf_char_t mbc);
  33. */
  34. #ifndef _MBFILE_H
  35. #define _MBFILE_H 1
  36. #include <assert.h>
  37. #include <stdbool.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
  41. <wchar.h>.
  42. BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
  43. <wchar.h>. */
  44. #include <stdio.h>
  45. #include <time.h>
  46. #include <wchar.h>
  47. #include "mbchar.h"
  48. #ifndef _GL_INLINE_HEADER_BEGIN
  49. #error "Please include config.h first."
  50. #endif
  51. _GL_INLINE_HEADER_BEGIN
  52. #ifndef MBFILE_INLINE
  53. # define MBFILE_INLINE _GL_INLINE
  54. #endif
  55. struct mbfile_multi {
  56. FILE *fp;
  57. bool eof_seen;
  58. bool have_pushback;
  59. mbstate_t state;
  60. unsigned int bufcount;
  61. char buf[MBCHAR_BUF_SIZE];
  62. struct mbchar pushback;
  63. };
  64. MBFILE_INLINE void
  65. mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
  66. {
  67. size_t bytes;
  68. /* If EOF has already been seen, don't use getc. This matters if
  69. mbf->fp is connected to an interactive tty. */
  70. if (mbf->eof_seen)
  71. goto eof;
  72. /* Return character pushed back, if there is one. */
  73. if (mbf->have_pushback)
  74. {
  75. mb_copy (mbc, &mbf->pushback);
  76. mbf->have_pushback = false;
  77. return;
  78. }
  79. /* Before using mbrtowc, we need at least one byte. */
  80. if (mbf->bufcount == 0)
  81. {
  82. int c = getc (mbf->fp);
  83. if (c == EOF)
  84. {
  85. mbf->eof_seen = true;
  86. goto eof;
  87. }
  88. mbf->buf[0] = (unsigned char) c;
  89. mbf->bufcount++;
  90. }
  91. /* Handle most ASCII characters quickly, without calling mbrtowc(). */
  92. if (mbf->bufcount == 1 && mbsinit (&mbf->state) && is_basic (mbf->buf[0]))
  93. {
  94. /* These characters are part of the basic character set. ISO C 99
  95. guarantees that their wide character code is identical to their
  96. char code. */
  97. mbc->wc = mbc->buf[0] = mbf->buf[0];
  98. mbc->wc_valid = true;
  99. mbc->ptr = &mbc->buf[0];
  100. mbc->bytes = 1;
  101. mbf->bufcount = 0;
  102. return;
  103. }
  104. /* Use mbrtowc on an increasing number of bytes. Read only as many bytes
  105. from mbf->fp as needed. This is needed to give reasonable interactive
  106. behaviour when mbf->fp is connected to an interactive tty. */
  107. for (;;)
  108. {
  109. /* We don't know whether the 'mbrtowc' function updates the state when
  110. it returns -2, - this is the ISO C 99 and glibc-2.2 behaviour - or
  111. not - amended ANSI C, glibc-2.1 and Solaris 2.7 behaviour. We
  112. don't have an autoconf test for this, yet.
  113. The new behaviour would allow us to feed the bytes one by one into
  114. mbrtowc. But the old behaviour forces us to feed all bytes since
  115. the end of the last character into mbrtowc. Since we want to retry
  116. with more bytes when mbrtowc returns -2, we must backup the state
  117. before calling mbrtowc, because implementations with the new
  118. behaviour will clobber it. */
  119. mbstate_t backup_state = mbf->state;
  120. bytes = mbrtowc (&mbc->wc, &mbf->buf[0], mbf->bufcount, &mbf->state);
  121. if (bytes == (size_t) -1)
  122. {
  123. /* An invalid multibyte sequence was encountered. */
  124. /* Return a single byte. */
  125. bytes = 1;
  126. mbc->wc_valid = false;
  127. break;
  128. }
  129. else if (bytes == (size_t) -2)
  130. {
  131. /* An incomplete multibyte character. */
  132. mbf->state = backup_state;
  133. if (mbf->bufcount == MBCHAR_BUF_SIZE)
  134. {
  135. /* An overlong incomplete multibyte sequence was encountered. */
  136. /* Return a single byte. */
  137. bytes = 1;
  138. mbc->wc_valid = false;
  139. break;
  140. }
  141. else
  142. {
  143. /* Read one more byte and retry mbrtowc. */
  144. int c = getc (mbf->fp);
  145. if (c == EOF)
  146. {
  147. /* An incomplete multibyte character at the end. */
  148. mbf->eof_seen = true;
  149. bytes = mbf->bufcount;
  150. mbc->wc_valid = false;
  151. break;
  152. }
  153. mbf->buf[mbf->bufcount] = (unsigned char) c;
  154. mbf->bufcount++;
  155. }
  156. }
  157. else
  158. {
  159. if (bytes == 0)
  160. {
  161. /* A null wide character was encountered. */
  162. bytes = 1;
  163. assert (mbf->buf[0] == '\0');
  164. assert (mbc->wc == 0);
  165. }
  166. mbc->wc_valid = true;
  167. break;
  168. }
  169. }
  170. /* Return the multibyte sequence mbf->buf[0..bytes-1]. */
  171. mbc->ptr = &mbc->buf[0];
  172. memcpy (&mbc->buf[0], &mbf->buf[0], bytes);
  173. mbc->bytes = bytes;
  174. mbf->bufcount -= bytes;
  175. if (mbf->bufcount > 0)
  176. {
  177. /* It's not worth calling memmove() for so few bytes. */
  178. unsigned int count = mbf->bufcount;
  179. char *p = &mbf->buf[0];
  180. do
  181. {
  182. *p = *(p + bytes);
  183. p++;
  184. }
  185. while (--count > 0);
  186. }
  187. return;
  188. eof:
  189. /* An mbchar_t with bytes == 0 is used to indicate EOF. */
  190. mbc->ptr = NULL;
  191. mbc->bytes = 0;
  192. mbc->wc_valid = false;
  193. return;
  194. }
  195. MBFILE_INLINE void
  196. mbfile_multi_ungetc (const struct mbchar *mbc, struct mbfile_multi *mbf)
  197. {
  198. mb_copy (&mbf->pushback, mbc);
  199. mbf->have_pushback = true;
  200. }
  201. typedef struct mbfile_multi mb_file_t;
  202. typedef mbchar_t mbf_char_t;
  203. #define mbf_init(mbf, stream) \
  204. ((mbf).fp = (stream), \
  205. (mbf).eof_seen = false, \
  206. (mbf).have_pushback = false, \
  207. memset (&(mbf).state, '\0', sizeof (mbstate_t)), \
  208. (mbf).bufcount = 0)
  209. #define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
  210. #define mbf_ungetc(mbc, mbf) mbfile_multi_ungetc (&(mbc), &(mbf))
  211. #define mb_iseof(mbc) ((mbc).bytes == 0)
  212. _GL_INLINE_HEADER_END
  213. #endif /* _MBFILE_H */