/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */ /* camel-stream.c : abstract class for a stream */ /* * Author: * Bertrand Guiheneuf * * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com) * * 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 the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ #include #include "camel-stream.h" static CamelObjectClass *parent_class = NULL; /* Returns the class for a CamelStream */ #define CS_CLASS(so) CAMEL_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so)) /* default implementations, do very little */ static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n) { return 0; } static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n) { return n; } static int stream_close (CamelStream *stream) { return 0; } static int stream_flush (CamelStream *stream) { return 0; } static gboolean stream_eos (CamelStream *stream) { return stream->eos; } static int stream_reset (CamelStream *stream) { return 0; } static void camel_stream_class_init (CamelStreamClass *camel_stream_class) { parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE ); /* virtual method definition */ camel_stream_class->read = stream_read; camel_stream_class->write = stream_write; camel_stream_class->close = stream_close; camel_stream_class->flush = stream_flush; camel_stream_class->eos = stream_eos; camel_stream_class->reset = stream_reset; } CamelType camel_stream_get_type (void) { static CamelType camel_stream_type = CAMEL_INVALID_TYPE; if (camel_stream_type == CAMEL_INVALID_TYPE) { camel_stream_type = camel_type_register( CAMEL_OBJECT_TYPE, "CamelStream", sizeof( CamelStream ), sizeof( CamelStreamClass ), (CamelObjectClassInitFunc) camel_stream_class_init, NULL, NULL, NULL ); } return camel_stream_type; } /** * camel_stream_read: * @stream: a CamelStream. * @buffer: buffer where bytes pulled from the stream are stored. * @n: max number of bytes to read. * * Read at most @n bytes from the @stream object and stores them * in the buffer pointed at by @buffer. * * Return value: number of bytes actually read, or -1 on error and * set errno. **/ ssize_t camel_stream_read (CamelStream *stream, char *buffer, size_t n) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); g_return_val_if_fail (n == 0 || buffer, -1); return CS_CLASS (stream)->read (stream, buffer, n); } /** * camel_stream_write: * @stream: a CamelStream object. * @buffer: buffer to write. * @n: number of bytes to write * * Write @n bytes from the buffer pointed at by @buffer into @stream. * * Return value: the number of bytes actually written to the stream, * or -1 on error. **/ ssize_t camel_stream_write (CamelStream *stream, const char *buffer, size_t n) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); g_return_val_if_fail (n == 0 || buffer, -1); return CS_CLASS (stream)->write (stream, buffer, n); } /** * camel_stream_flush: * @stream: a CamelStream object * * Flushes the contents of the stream to its backing store. Only meaningful * on writable streams. * * Return value: -1 on error. **/ int camel_stream_flush (CamelStream *stream) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); return CS_CLASS (stream)->flush (stream); } /** * camel_stream_close: * @stream: * * Close a stream. * * Return value: -1 on error. **/ int camel_stream_close (CamelStream *stream) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); return CS_CLASS (stream)->close (stream); } /** * camel_stream_eos: * @stream: a CamelStream object * * Test if there are bytes left to read on the @stream object. * * Return value: %TRUE if all the contents on the stream has been read, or * %FALSE if information is still available. **/ gboolean camel_stream_eos (CamelStream *stream) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), TRUE); return CS_CLASS (stream)->eos (stream); } /** * camel_stream_reset: reset a stream * @stream: the stream object * * Reset a stream. That is, put it in a state where it can be read * from the beginning again. Not all streams in Camel are seekable, * but they must all be resettable. * * Return value: -1 on error. **/ int camel_stream_reset (CamelStream *stream) { g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); return CS_CLASS (stream)->reset (stream); } /***************** Utility functions ********************/ /** * camel_stream_write_string: * @stream: a stream object * @string: a string * * Writes the string to the stream. * * Return value: the number of characters output, -1 on error. **/ ssize_t camel_stream_write_string (CamelStream *stream, const char *string) { return camel_stream_write (stream, string, strlen (string)); } /** * camel_stream_printf: * @stream: a stream object * @fmt: a printf-style format string * * This printfs the given data to @stream. * * Return value: the number of characters output, -1 on error. **/ ssize_t camel_stream_printf (CamelStream *stream, const char *fmt, ... ) { va_list args; char *string; ssize_t ret; g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); va_start (args, fmt); string = g_strdup_vprintf (fmt, args); va_end (args); if (!string) return -1; ret = camel_stream_write (stream, string, strlen (string)); g_free (string); return ret; } /** * camel_stream_write_to_stream: * @stream: Source CamelStream. * @output_stream: Destination CamelStream. * * Write all of a stream (until eos) into another stream, in a blocking * fashion. * * Return value: Returns -1 on error, or the number of bytes succesfully * copied across streams. **/ ssize_t camel_stream_write_to_stream (CamelStream *stream, CamelStream *output_stream) { char tmp_buf[4096]; ssize_t total = 0; ssize_t nb_read; ssize_t nb_written; g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); g_return_val_if_fail (CAMEL_IS_STREAM (output_stream), -1); while (!camel_stream_eos (stream)) { nb_read = camel_stream_read (stream, tmp_buf, sizeof (tmp_buf)); if (nb_read < 0) return -1; else if (nb_read > 0) { nb_written = 0; while (nb_written < nb_read) { ssize_t len = camel_stream_write (output_stream, tmp_buf + nb_written, nb_read - nb_written); if (len < 0) return -1; nb_written += len; } total += nb_written; } } return total; } me/log/math/mathomatic?h=gstreamer&id=25efc752b6345b882f22c70021378fcf4d080c94&showmsg=1'>Expand)AuthorAgeFilesLines * Update to 15.7.2ehaupt2011-12-272-3/+3 * Update to 15.7.1ehaupt2011-11-192-3/+3 * Update to 15.7.0ehaupt2011-11-032-3/+3 * Update to 15.6.5ehaupt2011-10-042-3/+3 * Update to 15.6.4ehaupt2011-08-242-3/+3 * - Update to 15.6.3ehaupt2011-08-052-4/+5 * Update to 15.6.2ehaupt2011-06-132-3/+3 * Update to 15.6.1ehaupt2011-06-062-3/+3 * Update to 15.6.0ehaupt2011-05-012-3/+3 * Update to 15.5.3ehaupt2011-04-202-4/+3 * Fix install targetehaupt2011-03-221-1/+2 * Update to 15.5.2ehaupt2011-03-222-3/+3 * Update to 15.5.1ehaupt2011-03-112-3/+3 * Update to 15.5.0ehaupt2011-02-262-3/+3 * Update to 15.4.4ehaupt2011-02-152-3/+3 * Update to 15.4.3ehaupt2011-02-072-3/+3 * Update to 15.4.2ehaupt2011-01-282-3/+3 * Update to 15.4.1 and add LICENSEehaupt2011-01-232-3/+4 * Update to 15.4.0ehaupt2011-01-132-3/+3 * Update to 15.3.7ehaupt2010-12-272-3/+3 * Update to 15.3.6ehaupt2010-12-232-3/+3 * Update to 15.3.5ehaupt2010-12-012-3/+3 * Update to 15.3.4ehaupt2010-11-152-3/+3 * Update to 15.3.3ehaupt2010-11-092-3/+3 * Update to 15.3.2ehaupt2010-10-312-4/+3 * Update to 15.3.1ehaupt2010-10-242-4/+4 * Update to 15.3.0ehaupt2010-10-112-4/+4 * Update to 15.2.2ehaupt2010-09-282-4/+4 * Update to 15.2.1ehaupt2010-09-012-4/+4 * Update to 15.2.0ehaupt2010-08-242-4/+4 * Update to 15.1.6ehaupt2010-08-102-4/+4 * Update to 15.1.5ehaupt2010-07-222-4/+4 * Update to 15.1.4ehaupt2010-06-212-4/+4 * Update to 15.1.3ehaupt2010-06-072-4/+4 * Update to 15.1.2ehaupt2010-06-062-4/+4 * Update to 15.1.1ehaupt2010-05-252-4/+4 * Update to 15.1.0ehaupt2010-05-172-6/+8 * Update to 15.0.8ehaupt2010-05-022-5/+19 * 15.0.7ehaupt2010-04-162-4/+4 * Update to 15.0.6ehaupt2010-03-272-4/+4 * Update to 15.0.5ehaupt2010-01-282-4/+4 * Update to 15.0.4ehaupt2010-01-202-8/+4 * - Introduce regression-test targetehaupt2010-01-091-1/+5 * Update to 15.0.3ehaupt2010-01-092-4/+4 * Update to 15.0.2ehaupt2010-01-062-4/+4 * Update to 15.0.1ehaupt2009-12-182-5/+4 * - Update post-patch sectionehaupt2009-12-152-3/+3 * Update to 15.0.0ehaupt2009-12-112-4/+4 * Update to 14.6.3ehaupt2009-11-292-4/+4 * Update to 14.6.0ehaupt2009-11-083-7/+21 * Update to 14.5.7ehaupt2009-11-032-4/+4 * Update to 14.5.6ehaupt2009-10-152-4/+4 * Update to 14.5.5ehaupt2009-09-212-4/+4 * Update to 14.5.3ehaupt2009-08-082-4/+4 * Update to 14.5.2ehaupt2009-07-272-4/+4 * Update to 14.5.1ehaupt2009-07-062-4/+4 * Update to 14.5.0ehaupt2009-06-262-4/+4 * Update to 14.4.5ehaupt2009-06-222-4/+4 * Update to 14.4.3ehaupt2009-05-282-4/+4 * Update to 14.4.1ehaupt2009-05-032-4/+4 * Update to 14.4.0ehaupt2009-04-212-4/+4 * Update to 14.3.6ehaupt2009-03-312-4/+4 * Update to 14.3.5ehaupt2009-03-282-4/+4 * Mark MAKE_JOBS_SAFEehaupt2009-03-271-0/+1 * Update to 14.3.4ehaupt2009-03-092-4/+4 * Update to 14.3.1ehaupt2009-02-022-4/+4 * Update to 14.3.0ehaupt2009-01-292-4/+4 * - Update to 14.2.7miwi2008-12-232-4/+4 * Update to 14.2.5ehaupt2008-12-122-4/+4 * Update to 14.2.3ehaupt2008-11-202-4/+4 * Update to 14.2.2ehaupt2008-11-062-4/+4 * Update to 14.2.1ehaupt2008-10-162-4/+4 * Update to 14.2.0ehaupt2008-09-262-4/+4 * Update to 14.1.6ehaupt2008-09-072-4/+4 * Update to 14.1.5ehaupt2008-08-262-4/+4 * Update to 14.1.4ehaupt2008-08-162-4/+4 * Update to 14.1.3ehaupt2008-08-152-4/+4 * Update to 14.1.2ehaupt2008-08-122-4/+4 * Update to 14.1.1ehaupt2008-08-082-4/+4 * Update to 14.0.8ehaupt2008-07-202-5/+5 * Update to 14.0.7ehaupt2008-07-082-4/+4 * Update to 14.0.6ehaupt2008-06-262-4/+4 * Update to 14.0.5ehaupt2008-06-192-4/+4 * Update to 14.0.4ehaupt2008-06-022-4/+4 * Update to 14.0.3ehaupt2008-05-202-4/+4 * Update to 14.0.2ehaupt2008-05-092-4/+4 * Update to 14.0.1ehaupt2008-04-302-4/+4 * Update to 14.0.0ehaupt2008-04-252-4/+4 * Update to 12.9.2ehaupt2008-04-212-4/+4 * Update to 12.9.1ehaupt2008-03-312-4/+4 * Use MASTER_SITE_CRITICALehaupt2008-03-281-2/+1 * Update to 12.9.0ehaupt2008-03-182-4/+4 * Update to 12.8.9ehaupt2008-03-082-4/+4 * Update to 12.8.8ehaupt2008-02-262-4/+4 * Update to 12.8.7ehaupt2008-02-182-4/+4 * Update to 12.8.6ehaupt2008-02-062-4/+4 * Update to 12.8.5ehaupt2008-01-262-4/+4 * Update to 12.8.4ehaupt2008-01-092-4/+4 * Update to 12.8.3ehaupt2007-12-262-4/+4 * Update to 12.8.2ehaupt2007-12-132-4/+4 * Update to 12.8.1ehaupt2007-12-132-4/+4 * Update to 12.8.0ehaupt2007-12-122-4/+4 * Update to 12.7.8ehaupt2007-10-202-4/+4 * Update to 12.7.7ehaupt2007-10-022-4/+4 * Update to 12.7.6ehaupt2007-09-162-4/+4 * Update to 12.7.5ehaupt2007-08-222-4/+4 * Update to 12.7.4ehaupt2007-07-292-4/+4 * Update to 12.7.3ehaupt2007-06-232-4/+4 * Update to 12.7.2ehaupt2007-06-082-4/+4 * Update to 12.7.1ehaupt2007-06-032-4/+4 * Update to 12.6.12ehaupt2007-04-212-4/+4 * Update to 12.6.11ehaupt2007-03-242-4/+4 * Update to 12.6.10ehaupt2007-02-282-4/+4 * Update to 12.6.9ehaupt2007-02-052-4/+4 * Update to 12.6.8ehaupt2006-12-202-4/+4 * Update to 12.6.7ehaupt2006-11-102-4/+4 * Update to 12.6.6ehaupt2006-10-312-4/+4 * Update to 12.6.4ehaupt2006-09-262-4/+4 * Update to 12.6.3ehaupt2006-09-082-4/+4 * Update to 12.6.2ehaupt2006-08-252-4/+4 * Update to 12.6.1ehaupt2006-08-152-4/+4 * - Update to 12.5.22ehaupt2006-07-293-6/+6 * Update to 12.5.21ehaupt2006-07-212-4/+4 * Update to 12.5.19ehaupt2006-07-042-4/+4 * - Update to 12.5.18ehaupt2006-06-223-6/+5 * Update to 12.5.17ehaupt2006-06-162-4/+4 * Add an additional mirror.ehaupt2006-06-011-1/+3 * - Update to 12.5.16ehaupt2006-05-112-6/+5 * - Update to 12.5.15 [1]ehaupt2006-05-082-4/+6 * - Update to 12.5.14pav2006-04-302-4/+4 * upgrade to 12.5.11ijliao2006-03-202-4/+4 * upgrade to 12.5.8ijliao2006-02-152-4/+4 * - Update to 12.5.6pav2006-01-232-6/+5 * Remove maintainer at his request.linimon2006-01-231-1/+1 * SHA256ifyedwin2006-01-221-0/+1 * Remove a feature which no longer exists from pkg-descr.jylefort2006-01-071-1/+0 * - Update to 12.5.0 [1]jylefort2005-10-243-29/+14 * - Update to 12.4.11garga2005-10-083-6/+15 * upgrade to 12.3.2ijliao2005-05-183-9/+9