diff options
-rw-r--r-- | camel/Makefile.am | 2 | ||||
-rw-r--r-- | camel/camel-stream-fs.c | 27 | ||||
-rw-r--r-- | camel/camel-stream-mem.c | 235 | ||||
-rw-r--r-- | camel/camel-stream-mem.h | 80 | ||||
-rw-r--r-- | camel/camel-stream.c | 27 | ||||
-rw-r--r-- | camel/camel-stream.h | 9 |
6 files changed, 378 insertions, 2 deletions
diff --git a/camel/Makefile.am b/camel/Makefile.am index 573f256d48..fc3f155726 100644 --- a/camel/Makefile.am +++ b/camel/Makefile.am @@ -27,6 +27,7 @@ libcamel_la_SOURCES = \ camel-store.c \ camel-stream.c \ camel-stream-fs.c \ + camel-stream-mem.c \ data-wrapper-repository.c \ gmime-content-field.c \ gmime-utils.c \ @@ -50,6 +51,7 @@ libcamelinclude_HEADERS = \ camel-store.h \ camel-stream.h \ camel-stream-fs.h \ + camel-stream-mem.h \ data-wrapper-repository.h \ gmime-content-field.h \ gmime-utils.h \ diff --git a/camel/camel-stream-fs.c b/camel/camel-stream-fs.c index 3216f6ffdf..0e006a28f3 100644 --- a/camel/camel-stream-fs.c +++ b/camel/camel-stream-fs.c @@ -41,10 +41,11 @@ 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_fs_class_init (CamelStreamClass *camel_stream_fs_class) +camel_stream_fs_class_init (CamelStreamFsClass *camel_stream_fs_class) { CamelStreamClass *camel_stream_class = CAMEL_STREAM_CLASS (camel_stream_fs_class); parent_class = gtk_type_class (gtk_object_get_type ()); @@ -58,6 +59,7 @@ camel_stream_fs_class_init (CamelStreamClass *camel_stream_fs_class) camel_stream_class->available = _available; camel_stream_class->eos = _eos; camel_stream_class->close = _close; + camel_stream_class->seek = _seek; } @@ -249,3 +251,26 @@ _close (CamelStream *stream) { close ((CAMEL_STREAM_FS (stream))->fd); } + + +static gint +_seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy) +{ + int whence; + switch (policy) { + case CAMEL_STREAM_SET: + whence = SEEK_SET; + break; + case CAMEL_STREAM_CUR: + whence = SEEK_CUR; + break; + case CAMEL_STREAM_END: + whence = SEEK_END; + break; + default: + return -1; + } + + + return lseek ((CAMEL_STREAM_FS (stream))->fd, offset, whence); +} 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; +} diff --git a/camel/camel-stream-mem.h b/camel/camel-stream-mem.h new file mode 100644 index 0000000000..1864512868 --- /dev/null +++ b/camel/camel-stream-mem.h @@ -0,0 +1,80 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-stream-mem.h :stream based on memory buffer */ + +/* + * + * 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 + */ + + +#ifndef CAMEL_STREAM_MEM_H +#define CAMEL_STREAM_MEM_H 1 + + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus }*/ + +#include <gtk/gtk.h> +#include <stdio.h> +#include "camel-stream.h" + +#define CAMEL_STREAM_MEM_TYPE (camel_stream_mem_get_type ()) +#define CAMEL_STREAM_MEM(obj) (GTK_CHECK_CAST((obj), CAMEL_STREAM_MEM_TYPE, CamelStreamMem)) +#define CAMEL_STREAM_MEM_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_STREAM_MEM_TYPE, CamelStreamMemClass)) +#define IS_CAMEL_STREAM_MEM(o) (GTK_CHECK_TYPE((o), CAMEL_STREAM_MEM_TYPE)) + +typedef enum +{ + CAMEL_STREAM_MEM_READ = 1, + CAMEL_STREAM_MEM_WRITE = 2 +} CamelStreamMemMode; + + +typedef struct +{ + CamelStream parent_object; + + GByteArray *buffer; + gint position; + CamelStreamMemMode mode; +} CamelStreamMem; + + + +typedef struct { + CamelStreamClass parent_class; + + /* Virtual methods */ + +} CamelStreamMemClass; + + + +/* Standard Gtk function */ +GtkType camel_stream_mem_get_type (void); + + +/* public methods */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_STREAM_MEM_H */ diff --git a/camel/camel-stream.c b/camel/camel-stream.c index c66a950b26..b63a64ddb7 100644 --- a/camel/camel-stream.c +++ b/camel/camel-stream.c @@ -42,6 +42,12 @@ default_camel_close (CamelStream *stream) /* nothing */ } +static gint +default_camel_seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy) +{ + /* nothing */ +} + static void camel_stream_class_init (CamelStreamClass *camel_stream_class) { @@ -55,6 +61,7 @@ camel_stream_class_init (CamelStreamClass *camel_stream_class) camel_stream_class->available = NULL; camel_stream_class->eos = NULL; camel_stream_class->close = default_camel_close; + camel_stream_class->seek = default_camel_seek; /* virtual method overload */ } @@ -169,6 +176,26 @@ camel_stream_close (CamelStream *stream) CS_CLASS (stream)->close (stream); } + +/** + * camel_stream_seek: + * @stream: a CamelStream object. + * @offset: offset value + * @policy: what to do with the offset + * + * + * + * Return value: new position, -1 if operation failed. + **/ +gint +camel_stream_seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy) +{ + return CS_CLASS (stream)->seek (stream, offset, policy); +} + + + + /***************** Utility functions ********************/ /** diff --git a/camel/camel-stream.h b/camel/camel-stream.h index 9818e492fb..4b4ea50b5b 100644 --- a/camel/camel-stream.h +++ b/camel/camel-stream.h @@ -38,7 +38,12 @@ extern "C" { #define CAMEL_STREAM_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_STREAM_TYPE, CamelStreamClass)) #define IS_CAMEL_STREAM(o) (GTK_CHECK_TYPE((o), CAMEL_STREAM_TYPE)) - +typedef enum +{ + CAMEL_STREAM_SET, + CAMEL_STREAM_CUR, + CAMEL_STREAM_END +} CamelStreamSeekPolicy; typedef struct { @@ -58,6 +63,7 @@ typedef struct { gint (*available) (CamelStream *stream); gboolean (*eos) (CamelStream *stream); void (*close) (CamelStream *stream); + gint (*seek) (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy); } CamelStreamClass; @@ -73,6 +79,7 @@ void camel_stream_flush (CamelStream *stream); gint camel_stream_available (CamelStream *stream); gboolean camel_stream_eos (CamelStream *stream); void camel_stream_close (CamelStream *stream); +gint camel_stream_seek (CamelStream *stream, gint offset, CamelStreamSeekPolicy policy); /* utility macros and funcs */ #define camel_stream_write_string(stream, string) camel_stream_write ((stream), (string), strlen (string)) |