1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-stream.c : abstract class for a stream */
6 * Bertrand Guiheneuf <bertrand@helixcode.com>
8 * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU Lesser General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include "camel-stream.h"
34 static CamelObjectClass *parent_class = NULL;
36 /* Returns the class for a CamelStream */
37 #define CS_CLASS(so) CAMEL_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so))
39 /* default implementations, do very little */
40 static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n) { return 0; }
41 static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n) { return n; }
42 static int stream_close (CamelStream *stream) { return 0; }
43 static int stream_flush (CamelStream *stream) { return 0; }
44 static gboolean stream_eos (CamelStream *stream) { return stream->eos; }
45 static int stream_reset (CamelStream *stream) { return 0; }
48 camel_stream_class_init (CamelStreamClass *camel_stream_class)
50 parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );
52 /* virtual method definition */
53 camel_stream_class->read = stream_read;
54 camel_stream_class->write = stream_write;
55 camel_stream_class->close = stream_close;
56 camel_stream_class->flush = stream_flush;
57 camel_stream_class->eos = stream_eos;
58 camel_stream_class->reset = stream_reset;
62 camel_stream_get_type (void)
64 static CamelType camel_stream_type = CAMEL_INVALID_TYPE;
66 if (camel_stream_type == CAMEL_INVALID_TYPE) {
67 camel_stream_type = camel_type_register( CAMEL_OBJECT_TYPE,
69 sizeof( CamelStream ),
70 sizeof( CamelStreamClass ),
71 (CamelObjectClassInitFunc) camel_stream_class_init,
77 return camel_stream_type;
83 * @stream: a #CamelStream object.
84 * @buffer: output buffer
85 * @n: max number of bytes to read.
87 * Attempts to read up to @len bytes from @stream into @buf.
89 * Returns the number of bytes actually read, or %-1 on error and set
93 camel_stream_read (CamelStream *stream, char *buffer, size_t n)
95 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
96 g_return_val_if_fail (n == 0 || buffer, -1);
98 return (CS_CLASS (stream)->read) (stream, buffer, n);
103 * camel_stream_write:
104 * @stream: a #CamelStream object
105 * @buffer: buffer to write.
106 * @n: number of bytes to write
108 * Attempts to write up to @n bytes of @buffer into @stream.
110 * Returns the number of bytes written to the stream, or %-1 on error
111 * along with setting errno.
114 camel_stream_write (CamelStream *stream, const char *buffer, size_t n)
116 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
117 g_return_val_if_fail (n == 0 || buffer, -1);
119 return CS_CLASS (stream)->write (stream, buffer, n);
124 * camel_stream_flush:
125 * @stream: a #CamelStream object
127 * Flushes any buffered data to the stream's backing store. Only
128 * meaningful for writable streams.
130 * Returns %0 on success or %-1 on fail along with setting errno.
133 camel_stream_flush (CamelStream *stream)
135 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
137 return CS_CLASS (stream)->flush (stream);
142 * camel_stream_close:
143 * @stream: a #CamelStream object
147 * Returns %0 on success or %-1 on error.
150 camel_stream_close (CamelStream *stream)
152 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
154 return CS_CLASS (stream)->close (stream);
160 * @stream: a #CamelStream object
162 * Tests if there are bytes left to read on the @stream object.
164 * Returns %TRUE on EOS or %FALSE otherwise.
167 camel_stream_eos (CamelStream *stream)
169 g_return_val_if_fail (CAMEL_IS_STREAM (stream), TRUE);
171 return CS_CLASS (stream)->eos (stream);
176 * camel_stream_reset:
177 * @stream: a #CamelStream object
179 * Resets the stream. That is, put it in a state where it can be read
180 * from the beginning again. Not all streams in Camel are seekable,
181 * but they must all be resettable.
183 * Returns %0 on success or %-1 on error along with setting errno.
186 camel_stream_reset (CamelStream *stream)
188 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
190 return CS_CLASS (stream)->reset (stream);
193 /***************** Utility functions ********************/
196 * camel_stream_write_string:
197 * @stream: a #CamelStream object
200 * Writes the string to the stream.
202 * Returns the number of characters written or %-1 on error.
205 camel_stream_write_string (CamelStream *stream, const char *string)
207 return camel_stream_write (stream, string, strlen (string));
212 * camel_stream_printf:
213 * @stream: a #CamelStream object
214 * @fmt: a printf-style format string
216 * Write formatted output to a stream.
218 * Returns the number of characters written or %-1 on error.
221 camel_stream_printf (CamelStream *stream, const char *fmt, ... )
227 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
229 va_start (args, fmt);
230 string = g_strdup_vprintf (fmt, args);
236 ret = camel_stream_write (stream, string, strlen (string));
243 * camel_stream_write_to_stream:
244 * @stream: source #CamelStream object
245 * @output_stream: destination #CamelStream object
247 * Write all of a stream (until eos) into another stream, in a
250 * Returns %-1 on error, or the number of bytes succesfully
251 * copied across streams.
254 camel_stream_write_to_stream (CamelStream *stream, CamelStream *output_stream)
261 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
262 g_return_val_if_fail (CAMEL_IS_STREAM (output_stream), -1);
264 while (!camel_stream_eos (stream)) {
265 nb_read = camel_stream_read (stream, tmp_buf, sizeof (tmp_buf));
268 else if (nb_read > 0) {
271 while (nb_written < nb_read) {
272 ssize_t len = camel_stream_write (output_stream, tmp_buf + nb_written,
273 nb_read - nb_written);