1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 #line 1 "fd-ostream.oo.c"
4 /* Output stream referring to a file descriptor.
5 Copyright (C) 2006-2007, 2015 Free Software Foundation, Inc.
6 Written by Bruno Haible <bruno@clisp.org>, 2006.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "fd-ostream.h"
32 #include "full-write.h"
36 #define _(str) gettext (str)
38 #line 39 "fd-ostream.c"
40 #define fd_ostream_representation any_ostream_representation
42 #include "fd_ostream.priv.h"
44 const typeinfo_t fd_ostream_typeinfo = { "fd_ostream" };
46 static const typeinfo_t * const fd_ostream_superclasses[] =
47 { fd_ostream_SUPERCLASSES };
49 #define super ostream_vtable
51 #line 43 "fd-ostream.oo.c"
55 /* Implementation of ostream_t methods. */
58 fd_ostream__write_mem (fd_ostream_t stream, const void *data, size_t len)
62 if (stream->buffer != NULL)
65 assert (stream->avail > 0);
66 #if 0 /* unoptimized */
69 size_t n = (len <= stream->avail ? len : stream->avail);
72 memcpy (stream->buffer + BUFSIZE - stream->avail, data, n);
73 data = (char *) data + n;
77 if (stream->avail == 0)
79 if (full_write (stream->fd, stream->buffer, BUFSIZE) < BUFSIZE)
80 error (EXIT_FAILURE, errno, _("error writing to %s"),
82 stream->avail = BUFSIZE;
87 if (len < stream->avail)
89 /* Move the data into the buffer. */
90 memcpy (stream->buffer + BUFSIZE - stream->avail, data, len);
95 /* Split the data into:
96 - a first chunk, which is added to the buffer and output,
97 - a series of chunks of size BUFSIZE, which can be output
98 directly, without going through the buffer, and
99 - a last chunk, which is copied to the buffer. */
100 size_t n = stream->avail;
101 memcpy (stream->buffer + BUFSIZE - stream->avail, data, n);
102 data = (char *) data + n;
104 if (full_write (stream->fd, stream->buffer, BUFSIZE) < BUFSIZE)
105 error (EXIT_FAILURE, errno, _("error writing to %s"),
108 while (len >= BUFSIZE)
110 if (full_write (stream->fd, data, BUFSIZE) < BUFSIZE)
111 error (EXIT_FAILURE, errno, _("error writing to %s"),
113 data = (char *) data + BUFSIZE;
118 memcpy (stream->buffer, data, len);
119 stream->avail = BUFSIZE - len;
122 assert (stream->avail > 0);
127 if (full_write (stream->fd, data, len) < len)
128 error (EXIT_FAILURE, errno, _("error writing to %s"),
135 fd_ostream__flush (fd_ostream_t stream)
137 if (stream->buffer != NULL && stream->avail < BUFSIZE)
139 size_t filled = BUFSIZE - stream->avail;
140 if (full_write (stream->fd, stream->buffer, filled) < filled)
141 error (EXIT_FAILURE, errno, _("error writing to %s"), stream->filename);
142 stream->avail = BUFSIZE;
147 fd_ostream__free (fd_ostream_t stream)
149 fd_ostream_flush (stream);
150 free (stream->filename);
157 fd_ostream_create (int fd, const char *filename, bool buffered)
159 fd_ostream_t stream =
160 (struct fd_ostream_representation *)
161 xmalloc (sizeof (struct fd_ostream_representation)
162 + (buffered ? BUFSIZE : 0));
164 stream->base.vtable = &fd_ostream_vtable;
166 stream->filename = xstrdup (filename);
170 (char *) (void *) stream + sizeof (struct fd_ostream_representation);
171 stream->avail = BUFSIZE;
174 stream->buffer = NULL;
179 #line 180 "fd-ostream.c"
181 const struct fd_ostream_implementation fd_ostream_vtable =
183 fd_ostream_superclasses,
184 sizeof (fd_ostream_superclasses) / sizeof (fd_ostream_superclasses[0]),
185 sizeof (struct fd_ostream_representation),
186 fd_ostream__write_mem,
193 /* Define the functions that invoke the methods. */
196 fd_ostream_write_mem (fd_ostream_t first_arg, const void *data, size_t len)
198 const struct fd_ostream_implementation *vtable =
199 ((struct fd_ostream_representation_header *) (struct fd_ostream_representation *) first_arg)->vtable;
200 vtable->write_mem (first_arg,data,len);
204 fd_ostream_flush (fd_ostream_t first_arg)
206 const struct fd_ostream_implementation *vtable =
207 ((struct fd_ostream_representation_header *) (struct fd_ostream_representation *) first_arg)->vtable;
208 vtable->flush (first_arg);
212 fd_ostream_free (fd_ostream_t first_arg)
214 const struct fd_ostream_implementation *vtable =
215 ((struct fd_ostream_representation_header *) (struct fd_ostream_representation *) first_arg)->vtable;
216 vtable->free (first_arg);