annotate lib/mbfile.h @ 17632:86af85d364e1 default tip

unistd: port readlink to Mac OS X 10.3.9 * lib/unistd.in.h (_GL_INCLUDING_UNISTD_H): New macro, to work around self-include problem in Mac OS X 10.3.9 when combined with readlink module. Problem reported by Klaus Zietler in <http://bugs.gnu.org/16825>.
author Paul Eggert <eggert@penguin.cs.ucla.edu>
date Tue, 25 Feb 2014 11:16:27 -0800
parents 344018b6e5d7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Multibyte character I/O: macros for multi-byte encodings.
17587
344018b6e5d7 maint: update copyright
Eric Blake <eblake@redhat.com>
parents: 17484
diff changeset
2 Copyright (C) 2001, 2005, 2009-2014 Free Software Foundation, Inc.
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 6046
diff changeset
4 This program is free software: you can redistribute it and/or modify
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 6046
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 6046
diff changeset
7 (at your option) any later version.
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 6046
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Mitsuru Chinen <mchinen@yamato.ibm.com>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 and Bruno Haible <bruno@clisp.org>. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* The macros in this file implement multi-byte character input from a
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 stream.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 mb_file_t
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 is the type for multibyte character input stream, usable for variable
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 declarations.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 mbf_char_t
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 is the type for multibyte character or EOF, usable for variable
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 declarations.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 mbf_init (mbf, stream)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 initializes the MB_FILE for reading from stream.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 mbf_getc (mbc, mbf)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 reads the next multibyte character from mbf and stores it in mbc.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 mb_iseof (mbc)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 returns true if mbc represents the EOF value.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 Here are the function prototypes of the macros.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
42 extern void mbf_init (mb_file_t mbf, FILE *stream);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
43 extern void mbf_getc (mbf_char_t mbc, mb_file_t mbf);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
44 extern bool mb_iseof (const mbf_char_t mbc);
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #ifndef _MBFILE_H
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #define _MBFILE_H 1
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #include <assert.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #include <stdbool.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 #include <stdio.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 #include <string.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 <wchar.h>.
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 <wchar.h>. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 #include <stdio.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #include <time.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #include <wchar.h>
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #include "mbchar.h"
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
17484
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
65 #ifndef _GL_INLINE_HEADER_BEGIN
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
66 #error "Please include config.h first."
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
67 #endif
17102
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
68 _GL_INLINE_HEADER_BEGIN
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
69 #ifndef MBFILE_INLINE
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
70 # define MBFILE_INLINE _GL_INLINE
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
71 #endif
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
72
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 struct mbfile_multi {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 FILE *fp;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 bool eof_seen;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 bool have_pushback;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 mbstate_t state;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 unsigned int bufcount;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 char buf[MBCHAR_BUF_SIZE];
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 struct mbchar pushback;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 };
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
17102
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
83 MBFILE_INLINE void
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 size_t bytes;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 /* If EOF has already been seen, don't use getc. This matters if
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 mbf->fp is connected to an interactive tty. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 if (mbf->eof_seen)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 goto eof;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 /* Return character pushed back, if there is one. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 if (mbf->have_pushback)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 mb_copy (mbc, &mbf->pushback);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 mbf->have_pushback = false;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 return;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 /* Before using mbrtowc, we need at least one byte. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 if (mbf->bufcount == 0)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 int c = getc (mbf->fp);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 if (c == EOF)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
106 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
107 mbf->eof_seen = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
108 goto eof;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
109 }
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 mbf->buf[0] = (unsigned char) c;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 mbf->bufcount++;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 /* Handle most ASCII characters quickly, without calling mbrtowc(). */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 if (mbf->bufcount == 1 && mbsinit (&mbf->state) && is_basic (mbf->buf[0]))
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 /* These characters are part of the basic character set. ISO C 99
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
118 guarantees that their wide character code is identical to their
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
119 char code. */
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 mbc->wc = mbc->buf[0] = mbf->buf[0];
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 mbc->wc_valid = true;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 mbc->ptr = &mbc->buf[0];
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 mbc->bytes = 1;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 mbf->bufcount = 0;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 return;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 /* Use mbrtowc on an increasing number of bytes. Read only as many bytes
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 from mbf->fp as needed. This is needed to give reasonable interactive
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 behaviour when mbf->fp is connected to an interactive tty. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 for (;;)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 /* We don't know whether the 'mbrtowc' function updates the state when
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
134 it returns -2, - this is the ISO C 99 and glibc-2.2 behaviour - or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
135 not - amended ANSI C, glibc-2.1 and Solaris 2.7 behaviour. We
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
136 don't have an autoconf test for this, yet.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
137 The new behaviour would allow us to feed the bytes one by one into
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
138 mbrtowc. But the old behaviour forces us to feed all bytes since
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
139 the end of the last character into mbrtowc. Since we want to retry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
140 with more bytes when mbrtowc returns -2, we must backup the state
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
141 before calling mbrtowc, because implementations with the new
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
142 behaviour will clobber it. */
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 mbstate_t backup_state = mbf->state;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 bytes = mbrtowc (&mbc->wc, &mbf->buf[0], mbf->bufcount, &mbf->state);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 if (bytes == (size_t) -1)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
148 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
149 /* An invalid multibyte sequence was encountered. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
150 /* Return a single byte. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
151 bytes = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
152 mbc->wc_valid = false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
153 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
154 }
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 else if (bytes == (size_t) -2)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
156 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
157 /* An incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
158 mbf->state = backup_state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
159 if (mbf->bufcount == MBCHAR_BUF_SIZE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
160 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
161 /* An overlong incomplete multibyte sequence was encountered. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
162 /* Return a single byte. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
163 bytes = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
164 mbc->wc_valid = false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
165 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
166 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
167 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
168 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
169 /* Read one more byte and retry mbrtowc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
170 int c = getc (mbf->fp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
171 if (c == EOF)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
172 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
173 /* An incomplete multibyte character at the end. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
174 mbf->eof_seen = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
175 bytes = mbf->bufcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
176 mbc->wc_valid = false;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
177 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
178 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
179 mbf->buf[mbf->bufcount] = (unsigned char) c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
180 mbf->bufcount++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
181 }
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
184 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
185 if (bytes == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
186 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
187 /* A null wide character was encountered. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
188 bytes = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
189 assert (mbf->buf[0] == '\0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
190 assert (mbc->wc == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
191 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
192 mbc->wc_valid = true;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
193 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
194 }
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 /* Return the multibyte sequence mbf->buf[0..bytes-1]. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 mbc->ptr = &mbc->buf[0];
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 memcpy (&mbc->buf[0], &mbf->buf[0], bytes);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 mbc->bytes = bytes;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 mbf->bufcount -= bytes;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 if (mbf->bufcount > 0)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 /* It's not worth calling memmove() for so few bytes. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 unsigned int count = mbf->bufcount;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 char *p = &mbf->buf[0];
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 do
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
210 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
211 *p = *(p + bytes);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
212 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
213 }
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 while (--count > 0);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 return;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 eof:
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 /* An mbchar_t with bytes == 0 is used to indicate EOF. */
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 mbc->ptr = NULL;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 mbc->bytes = 0;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 mbc->wc_valid = false;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 return;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225
17102
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
226 MBFILE_INLINE void
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 mbfile_multi_ungetc (const struct mbchar *mbc, struct mbfile_multi *mbf)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 {
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 mb_copy (&mbf->pushback, mbc);
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 mbf->have_pushback = true;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 }
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 typedef struct mbfile_multi mb_file_t;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 typedef mbchar_t mbf_char_t;
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
237 #define mbf_init(mbf, stream) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
238 ((mbf).fp = (stream), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
239 (mbf).eof_seen = false, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
240 (mbf).have_pushback = false, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
241 memset (&(mbf).state, '\0', sizeof (mbstate_t)), \
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 (mbf).bufcount = 0)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 #define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 #define mbf_ungetc(mbc, mbf) mbfile_multi_ungetc (&(mbc), &(mbf))
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 #define mb_iseof(mbc) ((mbc).bytes == 0)
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249
17484
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
250 #ifndef _GL_INLINE_HEADER_BEGIN
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
251 #error "Please include config.h first."
1f9070ef79b0 headers: check that _GL_INLINE_HEADER_BEGIN is defined
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
252 #endif
17102
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
253 _GL_INLINE_HEADER_BEGIN
9e72d3927af1 binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
254
6046
455e151721e5 New module 'mbfile'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 #endif /* _MBFILE_H */