/* Multibyte character I/O: macros for multi-byte encodings.
- Copyright (C) 2001, 2005 Free Software Foundation, Inc.
+ Copyright (C) 2001, 2005, 2009-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Here are the function prototypes of the macros.
- extern void mbf_init (mb_file_t mbf, FILE *stream);
- extern void mbf_getc (mbf_char_t mbc, mb_file_t mbf);
- extern bool mb_iseof (const mbf_char_t mbc);
+ extern void mbf_init (mb_file_t mbf, FILE *stream);
+ extern void mbf_getc (mbf_char_t mbc, mb_file_t mbf);
+ extern bool mb_iseof (const mbf_char_t mbc);
*/
#ifndef _MBFILE_H
#include "mbchar.h"
+#ifndef _GL_INLINE_HEADER_BEGIN
+ #error "Please include config.h first."
+#endif
+_GL_INLINE_HEADER_BEGIN
+#ifndef MBFILE_INLINE
+# define MBFILE_INLINE _GL_INLINE
+#endif
+
struct mbfile_multi {
FILE *fp;
bool eof_seen;
struct mbchar pushback;
};
-static inline void
+MBFILE_INLINE void
mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
{
size_t bytes;
{
int c = getc (mbf->fp);
if (c == EOF)
- {
- mbf->eof_seen = true;
- goto eof;
- }
+ {
+ mbf->eof_seen = true;
+ goto eof;
+ }
mbf->buf[0] = (unsigned char) c;
mbf->bufcount++;
}
if (mbf->bufcount == 1 && mbsinit (&mbf->state) && is_basic (mbf->buf[0]))
{
/* These characters are part of the basic character set. ISO C 99
- guarantees that their wide character code is identical to their
- char code. */
+ guarantees that their wide character code is identical to their
+ char code. */
mbc->wc = mbc->buf[0] = mbf->buf[0];
mbc->wc_valid = true;
mbc->ptr = &mbc->buf[0];
for (;;)
{
/* We don't know whether the 'mbrtowc' function updates the state when
- it returns -2, - this is the ISO C 99 and glibc-2.2 behaviour - or
- not - amended ANSI C, glibc-2.1 and Solaris 2.7 behaviour. We
- don't have an autoconf test for this, yet.
- The new behaviour would allow us to feed the bytes one by one into
- mbrtowc. But the old behaviour forces us to feed all bytes since
- the end of the last character into mbrtowc. Since we want to retry
- with more bytes when mbrtowc returns -2, we must backup the state
- before calling mbrtowc, because implementations with the new
- behaviour will clobber it. */
+ it returns -2, - this is the ISO C 99 and glibc-2.2 behaviour - or
+ not - amended ANSI C, glibc-2.1 and Solaris 2.7 behaviour. We
+ don't have an autoconf test for this, yet.
+ The new behaviour would allow us to feed the bytes one by one into
+ mbrtowc. But the old behaviour forces us to feed all bytes since
+ the end of the last character into mbrtowc. Since we want to retry
+ with more bytes when mbrtowc returns -2, we must backup the state
+ before calling mbrtowc, because implementations with the new
+ behaviour will clobber it. */
mbstate_t backup_state = mbf->state;
bytes = mbrtowc (&mbc->wc, &mbf->buf[0], mbf->bufcount, &mbf->state);
if (bytes == (size_t) -1)
- {
- /* An invalid multibyte sequence was encountered. */
- /* Return a single byte. */
- bytes = 1;
- mbc->wc_valid = false;
- break;
- }
+ {
+ /* An invalid multibyte sequence was encountered. */
+ /* Return a single byte. */
+ bytes = 1;
+ mbc->wc_valid = false;
+ break;
+ }
else if (bytes == (size_t) -2)
- {
- /* An incomplete multibyte character. */
- mbf->state = backup_state;
- if (mbf->bufcount == MBCHAR_BUF_SIZE)
- {
- /* An overlong incomplete multibyte sequence was encountered. */
- /* Return a single byte. */
- bytes = 1;
- mbc->wc_valid = false;
- break;
- }
- else
- {
- /* Read one more byte and retry mbrtowc. */
- int c = getc (mbf->fp);
- if (c == EOF)
- {
- /* An incomplete multibyte character at the end. */
- mbf->eof_seen = true;
- bytes = mbf->bufcount;
- mbc->wc_valid = false;
- break;
- }
- mbf->buf[mbf->bufcount] = (unsigned char) c;
- mbf->bufcount++;
- }
+ {
+ /* An incomplete multibyte character. */
+ mbf->state = backup_state;
+ if (mbf->bufcount == MBCHAR_BUF_SIZE)
+ {
+ /* An overlong incomplete multibyte sequence was encountered. */
+ /* Return a single byte. */
+ bytes = 1;
+ mbc->wc_valid = false;
+ break;
+ }
+ else
+ {
+ /* Read one more byte and retry mbrtowc. */
+ int c = getc (mbf->fp);
+ if (c == EOF)
+ {
+ /* An incomplete multibyte character at the end. */
+ mbf->eof_seen = true;
+ bytes = mbf->bufcount;
+ mbc->wc_valid = false;
+ break;
+ }
+ mbf->buf[mbf->bufcount] = (unsigned char) c;
+ mbf->bufcount++;
+ }
}
else
- {
- if (bytes == 0)
- {
- /* A null wide character was encountered. */
- bytes = 1;
- assert (mbf->buf[0] == '\0');
- assert (mbc->wc == 0);
- }
- mbc->wc_valid = true;
- break;
- }
+ {
+ if (bytes == 0)
+ {
+ /* A null wide character was encountered. */
+ bytes = 1;
+ assert (mbf->buf[0] == '\0');
+ assert (mbc->wc == 0);
+ }
+ mbc->wc_valid = true;
+ break;
+ }
}
/* Return the multibyte sequence mbf->buf[0..bytes-1]. */
char *p = &mbf->buf[0];
do
- {
- *p = *(p + bytes);
- p++;
- }
+ {
+ *p = *(p + bytes);
+ p++;
+ }
while (--count > 0);
}
return;
return;
}
-static inline void
+MBFILE_INLINE void
mbfile_multi_ungetc (const struct mbchar *mbc, struct mbfile_multi *mbf)
{
mb_copy (&mbf->pushback, mbc);
typedef mbchar_t mbf_char_t;
-#define mbf_init(mbf, stream) \
- ((mbf).fp = (stream), \
- (mbf).eof_seen = false, \
- (mbf).have_pushback = false, \
- memset (&(mbf).state, '\0', sizeof (mbstate_t)), \
+#define mbf_init(mbf, stream) \
+ ((mbf).fp = (stream), \
+ (mbf).eof_seen = false, \
+ (mbf).have_pushback = false, \
+ memset (&(mbf).state, '\0', sizeof (mbstate_t)), \
(mbf).bufcount = 0)
#define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
#define mb_iseof(mbc) ((mbc).bytes == 0)
+#ifndef _GL_INLINE_HEADER_BEGIN
+ #error "Please include config.h first."
+#endif
+_GL_INLINE_HEADER_BEGIN
+
#endif /* _MBFILE_H */