diff options
author | Bertrand Guiheneuf <bertrand@src.gnome.org> | 1999-08-02 05:36:42 +0800 |
---|---|---|
committer | Bertrand Guiheneuf <bertrand@src.gnome.org> | 1999-08-02 05:36:42 +0800 |
commit | c54abe884b175add134cc07e8fda31ffa65cac26 (patch) | |
tree | de32af6f25286f1a337d69dc20c7979b841788c5 /camel/camel-stream-mem.c | |
parent | 2c543492092832d71f1f677d52a42043c8787be7 (diff) | |
download | gsoc2013-evolution-c54abe884b175add134cc07e8fda31ffa65cac26.tar.gz gsoc2013-evolution-c54abe884b175add134cc07e8fda31ffa65cac26.tar.zst gsoc2013-evolution-c54abe884b175add134cc07e8fda31ffa65cac26.zip |
new memory buffer based stream.
* camel/camel-stream-mem.c:
* camel/camel-stream-mem.h:
new memory buffer based stream.
* camel/camel-stream-fs.c (_seek):
implementation for file system based stream.
* camel/camel-stream.c (camel_stream_seek):
new method.
* camel/camel-stream-fs.c (camel_stream_fs_class_init):
pass CamelStreamFsClass instead of CamelStreamClass.
svn path=/trunk/; revision=1056
Diffstat (limited to 'camel/camel-stream-mem.c')
-rw-r--r-- | camel/camel-stream-mem.c | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/camel/camel-stream-mem.c b/camel/camel-stream-mem.c new file mode 100644 index 0000000000..14eb9ec625 --- /dev/null +++ b/camel/camel-stream-mem.c @@ -0,0 +1,235 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-ofmemet: 8 -*- */ +/* camel-stream-mem.c : memory buffer based stream */ + +/* inspired by gnome-stream-mem.c in bonobo by Miguel de Icaza */ +/* + * + * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> . + * + * 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 <config.h> +#include "camel-stream-mem.h" +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include "camel-log.h" + +static CamelStreamClass *parent_class=NULL; + + +/* Returns the class for a CamelStreamMEM */ +#define CS_CLASS(so) CAMEL_STREAM_MEM_CLASS (GTK_OBJECT(so)->klass) + +static gint _read (CamelStream *stream, gchar *buffer, gint n); +static gint _write (CamelStream *stream, const gchar *buffer, gint n); +static void _flush (CamelStream *stream); +static gint _available (CamelStream *stream); +static gboolean _eos (CamelStream *stream); +static void _close (CamelStream *stream); +static gint _seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy); + + +static void +camel_stream_mem_class_init (CamelStreamMemClass *camel_stream_mem_class) +{ + CamelStreamClass *camel_stream_class = CAMEL_STREAM_CLASS (camel_stream_mem_class); + parent_class = gtk_type_class (gtk_object_get_type ()); + + /* virtual method definition */ + + /* virtual method overload */ + camel_stream_class->read = _read; + camel_stream_class->write = _write; + camel_stream_class->flush = _flush; + camel_stream_class->available = _available; + camel_stream_class->eos = _eos; + camel_stream_class->close = _close; + camel_stream_class->seek = _seek; + +} + +static void +camel_stream_mem_init (gpointer object, gpointer klass) +{ + CamelStreamMem *camel_stream_mem = CAMEL_STREAM_MEM (object); + camel_stream_mem->buffer = g_byte_array_new (); + camel_stream_mem->position = 0; +} + +GtkType +camel_stream_mem_get_type (void) +{ + static GtkType camel_stream_mem_type = 0; + + if (!camel_stream_mem_type) { + GtkTypeInfo camel_stream_mem_info = + { + "CamelStreamMem", + sizeof (CamelStreamMem), + sizeof (CamelStreamMemClass), + (GtkClassInitFunc) camel_stream_mem_class_init, + (GtkObjectInitFunc) camel_stream_mem_init, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_stream_mem_type = gtk_type_unique (camel_stream_get_type (), &camel_stream_mem_info); + } + + return camel_stream_mem_type; +} + + + + +/** + * _read: read bytes from a stream + * @stream: stream + * @buffer: buffer where bytes are stored + * @n: max number of bytes to read + * + * + * + * Return value: number of bytes actually read. + **/ +static gint +_read (CamelStream *stream, gchar *buffer, gint n) +{ + CamelStreamMem *camel_stream_mem = CAMEL_STREAM_MEM (stream); + gint nb_bytes_to_read; + + g_assert (stream); + nb_bytes_to_read = MIN (n, (camel_stream_mem->buffer)->len - camel_stream_mem->position); + if (nb_bytes_to_read) { + memcpy (buffer, (camel_stream_mem->buffer)->data + camel_stream_mem->position, nb_bytes_to_read); + camel_stream_mem->position += nb_bytes_to_read; + } + return nb_bytes_to_read; +} + + +/** + * _write: read bytes to a stream + * @stream: the stream + * @buffer: byte buffer + * @n: number of bytes to write + * + * + * + * Return value: the number of bytes actually written + * in the stream. + **/ +static gint +_write (CamelStream *stream, const gchar *buffer, gint n) +{ + CamelStreamMem *camel_stream_mem = CAMEL_STREAM_MEM (stream); + + g_assert (stream); + camel_stream_mem->buffer = g_byte_array_append (camel_stream_mem->buffer, (const guint8 *)buffer, n); + camel_stream_mem->position += n; + +} + + + +/** + * _flush: flush pending changes + * @stream: the stream + * + * + **/ +static void +_flush (CamelStream *stream) +{ + g_warning ("Not implemented yet"); +} + + + +/** + * _available: return the number of bytes available for reading + * @stream: the stream + * + * Return the number of bytes available without blocking. + * + * Return value: the number of bytes available + **/ +static gint +_available (CamelStream *stream) +{ + g_warning ("Not implemented yet"); +} + + +/** + * _eos: test if there are bytes left to read + * @stream: the stream + * + * + * + * Return value: true if all stream has been read + **/ +static gboolean +_eos (CamelStream *stream) +{ + g_warning ("Not implemented yet"); +} + + +/** + * _close: close a stream + * @stream: the stream + * + * + **/ +static void +_close (CamelStream *stream) +{ + g_warning ("Not implemented yet"); +} + + + +static gint +_seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy) +{ + gint position; + CamelStreamMem *camel_stream_mem = CAMEL_STREAM_MEM (stream); + + switch (policy) { + case CAMEL_STREAM_SET: + position = offset; + break; + case CAMEL_STREAM_CUR: + position = camel_stream_mem->position + offset; + break; + case CAMEL_STREAM_END: + position = (camel_stream_mem->buffer)->len + offset; + break; + default: + return -1; + } + + position = MIN (position, (camel_stream_mem->buffer)->len); + position = MAX (position, 0); + + camel_stream_mem->position = position; + + return position; +} |