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 (C) 1999-2008 Novell, Inc. (www.novell.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-debug.h"
33 #include "camel-stream.h"
35 G_DEFINE_ABSTRACT_TYPE (CamelStream, camel_stream, CAMEL_TYPE_OBJECT)
38 stream_read (CamelStream *stream,
41 GCancellable *cancellable,
48 stream_write (CamelStream *stream,
51 GCancellable *cancellable,
58 stream_close (CamelStream *stream,
59 GCancellable *cancellable,
66 stream_flush (CamelStream *stream,
67 GCancellable *cancellable,
74 stream_eos (CamelStream *stream)
80 camel_stream_class_init (CamelStreamClass *class)
82 class->read = stream_read;
83 class->write = stream_write;
84 class->close = stream_close;
85 class->flush = stream_flush;
86 class->eos = stream_eos;
90 camel_stream_init (CamelStream *stream)
96 * @stream: a #CamelStream object.
97 * @buffer: output buffer
98 * @n: max number of bytes to read.
99 * @cancellable: optional #GCancellable object, or %NULL
100 * @error: return location for a #GError, or %NULL
102 * Attempts to read up to @len bytes from @stream into @buf.
104 * Returns: the number of bytes actually read, or %-1 on error and set
108 camel_stream_read (CamelStream *stream,
111 GCancellable *cancellable,
114 CamelStreamClass *class;
117 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
118 g_return_val_if_fail (n == 0 || buffer, -1);
120 class = CAMEL_STREAM_GET_CLASS (stream);
121 g_return_val_if_fail (class->read != NULL, -1);
123 n_bytes = class->read (stream, buffer, n, cancellable, error);
124 CAMEL_CHECK_GERROR (stream, read, n_bytes >= 0, error);
130 * camel_stream_write:
131 * @stream: a #CamelStream object
132 * @buffer: buffer to write.
133 * @n: number of bytes to write
134 * @cancellable: optional #GCancellable object, or %NULL
135 * @error: return location for a #GError, or %NULL
137 * Attempts to write up to @n bytes of @buffer into @stream.
139 * Returns: the number of bytes written to the stream, or %-1 on error
140 * along with setting errno.
143 camel_stream_write (CamelStream *stream,
146 GCancellable *cancellable,
149 CamelStreamClass *class;
152 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
153 g_return_val_if_fail (n == 0 || buffer, -1);
155 class = CAMEL_STREAM_GET_CLASS (stream);
156 g_return_val_if_fail (class->write != NULL, -1);
158 n_bytes = class->write (stream, buffer, n, cancellable, error);
159 CAMEL_CHECK_GERROR (stream, write, n_bytes >= 0, error);
165 * camel_stream_flush:
166 * @stream: a #CamelStream object
167 * @cancellable: optional #GCancellable object, or %NULL
168 * @error: return location for a #GError, or %NULL
170 * Flushes any buffered data to the stream's backing store. Only
171 * meaningful for writable streams.
173 * Returns: %0 on success or %-1 on fail along with setting @error
176 camel_stream_flush (CamelStream *stream,
177 GCancellable *cancellable,
180 CamelStreamClass *class;
183 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
185 class = CAMEL_STREAM_GET_CLASS (stream);
186 g_return_val_if_fail (class->flush != NULL, -1);
188 retval = class->flush (stream, cancellable, error);
189 CAMEL_CHECK_GERROR (stream, flush, retval == 0, error);
195 * camel_stream_close:
196 * @stream: a #CamelStream object
197 * @cancellable: optional #GCancellable object, or %NULL
198 * @error: return location for a #GError, or %NULL
202 * Returns: %0 on success or %-1 on error.
205 camel_stream_close (CamelStream *stream,
206 GCancellable *cancellable,
209 CamelStreamClass *class;
212 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
214 class = CAMEL_STREAM_GET_CLASS (stream);
215 g_return_val_if_fail (class->close != NULL, -1);
217 retval = class->close (stream, cancellable, error);
218 CAMEL_CHECK_GERROR (stream, close, retval == 0, error);
225 * @stream: a #CamelStream object
227 * Tests if there are bytes left to read on the @stream object.
229 * Returns: %TRUE on EOS or %FALSE otherwise.
232 camel_stream_eos (CamelStream *stream)
234 CamelStreamClass *class;
236 g_return_val_if_fail (CAMEL_IS_STREAM (stream), TRUE);
238 class = CAMEL_STREAM_GET_CLASS (stream);
239 g_return_val_if_fail (class->eos != NULL, TRUE);
241 return class->eos (stream);
244 /***************** Utility functions ********************/
247 * camel_stream_write_string:
248 * @stream: a #CamelStream object
250 * @error: return location for a #GError, or %NULL
252 * Writes the string to the stream.
254 * Returns: the number of characters written or %-1 on error.
257 camel_stream_write_string (CamelStream *stream,
259 GCancellable *cancellable,
262 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
263 g_return_val_if_fail (string != NULL, -1);
265 return camel_stream_write (
266 stream, string, strlen (string), cancellable, error);
270 * camel_stream_write_to_stream:
271 * @stream: source #CamelStream object
272 * @output_stream: destination #CamelStream object
273 * @cancellable: optional #GCancellable object, or %NULL
274 * @error: return location for a #GError, or %NULL
276 * Write all of a stream (until eos) into another stream, in a
279 * Returns: %-1 on error, or the number of bytes succesfully
280 * copied across streams.
283 camel_stream_write_to_stream (CamelStream *stream,
284 CamelStream *output_stream,
285 GCancellable *cancellable,
293 g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1);
294 g_return_val_if_fail (CAMEL_IS_STREAM (output_stream), -1);
296 while (!camel_stream_eos (stream)) {
297 nb_read = camel_stream_read (
298 stream, tmp_buf, sizeof (tmp_buf),
302 else if (nb_read > 0) {
305 while (nb_written < nb_read) {
306 gssize len = camel_stream_write (
308 tmp_buf + nb_written,
309 nb_read - nb_written,