From 1c1106598f9162efbbc6df7622c93b02940a4743 Mon Sep 17 00:00:00 2001 From: bertrand Date: Sun, 30 May 1999 21:56:11 +0000 Subject: use new stream code instead of raw file * stuff. 1999-05-31 bertrand * tests/test2.c (main): use new stream code instead of raw file * stuff. * camel/gmime-utils.c (get_header_table_from_stream): new func. Will replace get_header_table_from_file and will be used to parse headers from files as well as from memory buffers. * camel/camel-stream-fs.c: CamelStream Subclass. File system based stream. svn path=/trunk/; revision=961 --- ChangeLog | 15 ++++ camel/Makefile.am | 2 + camel/camel-stream-fs.c | 234 ++++++++++++++++++++++++++++++++++++++++++++++++ camel/camel-stream-fs.h | 81 +++++++++++++++++ camel/camel-stream.c | 23 ++++- camel/camel-stream.h | 9 +- camel/gmime-utils.c | 10 +-- camel/gmime-utils.h | 4 +- tests/test2.c | 13 +-- 9 files changed, 372 insertions(+), 19 deletions(-) create mode 100644 camel/camel-stream-fs.c create mode 100644 camel/camel-stream-fs.h diff --git a/ChangeLog b/ChangeLog index 934b0142aa..fc95dbdeca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +1999-05-31 bertrand + + * tests/test2.c (main): + use new stream code instead of raw file * stuff. + + * camel/gmime-utils.c (get_header_table_from_stream): + new func. Will replace get_header_table_from_file and will + be used to parse headers from files as well as from + memory buffers. + + * camel/camel-stream-fs.c: + CamelStream Subclass. File system based + stream. + + 1999-05-30 bertrand * camel/camel-stream.h: new class. Represents an diff --git a/camel/Makefile.am b/camel/Makefile.am index 0713f735a2..9a88729c2e 100644 --- a/camel/Makefile.am +++ b/camel/Makefile.am @@ -22,6 +22,7 @@ libcamel_la_SOURCES = \ camel-session.c \ camel-store.c \ camel-stream.c \ + camel-stream-fs.c \ gmime-content-field.c \ gmime-utils.c \ gstring-util.c \ @@ -38,6 +39,7 @@ libcamelinclude_HEADERS = \ camel-session.h \ camel-store.h \ camel-stream.h \ + camel-stream-fs.h \ gmime-content-field.h \ gmime-utils.h \ gstring-util.h \ diff --git a/camel/camel-stream-fs.c b/camel/camel-stream-fs.c new file mode 100644 index 0000000000..0a42e5062f --- /dev/null +++ b/camel/camel-stream-fs.c @@ -0,0 +1,234 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-stream-fs.c : file system based stream */ + +/* inspired by gnome-stream-fs.c in bonobo by Miguel de Icaza */ +/* + * + * Copyright (C) 1999 Bertrand Guiheneuf . + * + * 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 "camel-stream-fs.h" +#include +#include +#include +#include + +static CamelStreamClass *parent_class=NULL; + + +/* Returns the class for a CamelMimeMessage */ +#define CS_CLASS(so) CAMEL_STREAM_FS_CLASS (GTK_OBJECT(so)->klass) + +static gint _read (CamelStream *stream, gchar *buffer, gint n); +static gint _write (CamelStream *stream, 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 void +camel_stream_fs_class_init (CamelStreamClass *camel_stream_fs_class) +{ + CamelStreamClass *camel_stream_class = CAMEL_STREAM_CLASS (camel_stream_fs_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; + +} + + + +GtkType +camel_stream_fs_get_type (void) +{ + static GtkType camel_stream_fs_type = 0; + + if (!camel_stream_fs_type) { + GtkTypeInfo camel_stream_fs_info = + { + "CamelStreamFs", + sizeof (CamelStreamFs), + sizeof (CamelStreamFsClass), + (GtkClassInitFunc) camel_stream_fs_class_init, + (GtkObjectInitFunc) NULL, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_stream_fs_type = gtk_type_unique (camel_stream_get_type (), &camel_stream_fs_info); + } + + return camel_stream_fs_type; +} + + +CamelStream * +camel_stream_fs_new_with_name (GString *name, CamelStreamFsMode mode) +{ + struct stat s; + int v, fd; + int flags; + CamelStreamFs *stream_fs; + + if (!name) return NULL; + + v = stat (name->str, &s); + + if (mode & CAMEL_STREAM_FS_READ) + if (mode & CAMEL_STREAM_FS_WRITE) flags = O_RDWR; + else flags = O_RDONLY; + else + if (mode & CAMEL_STREAM_FS_WRITE) flags = O_WRONLY; + else return NULL; + + if (mode & CAMEL_STREAM_FS_READ) + if (v == -1) return NULL; + + fd = open (name->str, flags); + + stream_fs = CAMEL_STREAM_FS (camel_stream_fs_new_with_fd (fd)); + stream_fs->name = name; + + return CAMEL_STREAM (stream_fs); + +} + +CamelStream * +camel_stream_fs_new_with_fd (int fd) +{ + CamelStreamFs *stream_fs; + + stream_fs = gtk_type_new (camel_stream_fs_get_type ()); + stream_fs->fd = fd; + return CAMEL_STREAM (stream_fs); +} + +/** + * _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) +{ + int v; + + do { + v = read ( (CAMEL_STREAM_FS (stream))->fd, buffer, n); + } while (v == -1 && errno == EINTR); + + return v; +} + + +/** + * _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, gchar *buffer, gint n) +{ + int v; + + do { + v = write ( (CAMEL_STREAM_FS (stream))->fd, buffer, n); + } while (v == -1 && errno == EINTR); + + return v; + +} + + + +/** + * _flush: flush pending changes + * @stream: the stream + * + * + **/ +static void +_flush (CamelStream *stream) +{ + fsync ((CAMEL_STREAM_FS (stream))->fd); +} + + + +/** + * _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) +{ + close ((CAMEL_STREAM_FS (stream))->fd); +} diff --git a/camel/camel-stream-fs.h b/camel/camel-stream-fs.h new file mode 100644 index 0000000000..046e90d26c --- /dev/null +++ b/camel/camel-stream-fs.h @@ -0,0 +1,81 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-stream-fs.h :stream based on unix filesystem */ + +/* + * + * Copyright (C) 1999 Bertrand Guiheneuf . + * + * 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_FS_H +#define CAMEL_STREAM_FS_H 1 + + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus }*/ + +#include +#include +#include "camel-stream.h" + +#define CAMEL_STREAM_FS_TYPE (camel_stream_fs_get_type ()) +#define CAMEL_STREAM_FS(obj) (GTK_CHECK_CAST((obj), CAMEL_STREAM_FS_TYPE, CamelStreamFs)) +#define CAMEL_STREAM_FS_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_STREAM_FS_TYPE, CamelStreamFsClass)) +#define IS_CAMEL_STREAM_FS(o) (GTK_CHECK_TYPE((o), CAMEL_STREAM_FS_TYPE)) + +typedef enum +{ + CAMEL_STREAM_FS_READ = 1, + CAMEL_STREAM_FS_WRITE = 2 +} CamelStreamFsMode; + + +typedef struct +{ + CamelStream parent_object; + GString *name; + int fd; + +} CamelStreamFs; + + + +typedef struct { + CamelStreamClass parent_class; + + /* Virtual methods */ + +} CamelStreamFsClass; + + + +/* Standard Gtk function */ +GtkType camel_stream_fs_get_type (void); + + +/* public methods */ +CamelStream *camel_stream_fs_new_with_name (GString *name, CamelStreamFsMode mode); +CamelStream *camel_stream_fs_new_with_fd (int fd); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_STREAM_FS_H */ diff --git a/camel/camel-stream.c b/camel/camel-stream.c index 9af57c5485..c993556fa0 100644 --- a/camel/camel-stream.c +++ b/camel/camel-stream.c @@ -30,7 +30,7 @@ static CamelStreamClass *parent_class=NULL; /* Returns the class for a CamelMimeMessage */ #define CS_CLASS(so) CAMEL_STREAM_CLASS (GTK_OBJECT(so)->klass) -static gint _read (CamelStream *stream, gchar **buffer, gint n); +static gint _read (CamelStream *stream, gchar *buffer, gint n); static gint _write (CamelStream *stream, gchar *buffer, gint n); static void _flush (CamelStream *stream); static gint _available (CamelStream *stream); @@ -66,7 +66,7 @@ camel_stream_get_type (void) if (!camel_stream_type) { GtkTypeInfo camel_stream_info = { - "CamelMimeMessage", + "CamelStream", sizeof (CamelStream), sizeof (CamelStreamClass), (GtkClassInitFunc) camel_stream_class_init, @@ -95,11 +95,16 @@ camel_stream_get_type (void) * Return value: number of bytes actually read. **/ static gint -_read (CamelStream *stream, gchar **buffer, gint n) +_read (CamelStream *stream, gchar *buffer, gint n) { } +gint +camel_stream_read (CamelStream *stream, gchar *buffer, gint n) +{ + CS_CLASS (stream)->read (stream, buffer, n); +} /** * _write: read bytes to a stream @@ -118,6 +123,12 @@ _write (CamelStream *stream, gchar *buffer, gint n) } +gint +camel_stream_write (CamelStream *stream, gchar *buffer, gint n) +{ + CS_CLASS (stream)->write (stream, buffer, n); +} + /** @@ -175,3 +186,9 @@ _close (CamelStream *stream) { } + +void +camel_stream_close (CamelStream *stream) +{ + CS_CLASS (stream)->close (stream); +} diff --git a/camel/camel-stream.h b/camel/camel-stream.h index e58cfd92ff..925d5583a0 100644 --- a/camel/camel-stream.h +++ b/camel/camel-stream.h @@ -34,8 +34,8 @@ extern "C" { #include #define CAMEL_STREAM_TYPE (camel_stream_get_type ()) -#define CAMEL_STREAM(obj) (GTK_CHECK_CAST((obj), CAMEL_STREAM_TYPE, CamelMimeMessage)) -#define CAMEL_STREAM_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_STREAM_TYPE, CamelMimeMessageClass)) +#define CAMEL_STREAM(obj) (GTK_CHECK_CAST((obj), CAMEL_STREAM_TYPE, CamelStream)) +#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)) @@ -52,7 +52,7 @@ typedef struct { GtkObjectClass parent_class; /* Virtual methods */ -gint (*read) (CamelStream *stream, gchar **buffer, gint n); +gint (*read) (CamelStream *stream, gchar *buffer, gint n); gint (*write) (CamelStream *stream, gchar *buffer, gint n); void (*flush) (CamelStream *stream); gint (*available) (CamelStream *stream); @@ -68,6 +68,9 @@ GtkType camel_stream_get_type (void); /* public methods */ +gint camel_stream_read (CamelStream *stream, gchar *buffer, gint n); +gint camel_stream_write (CamelStream *stream, gchar *buffer, gint n); +void camel_stream_close (CamelStream *stream); #ifdef __cplusplus } diff --git a/camel/gmime-utils.c b/camel/gmime-utils.c index b2d95e439e..591706adc6 100644 --- a/camel/gmime-utils.c +++ b/camel/gmime-utils.c @@ -172,9 +172,9 @@ get_header_table_from_file (FILE *file) GHashTable * -get_header_table_from_stream (GnomeStream *stream) +get_header_table_from_stream (CamelStream *stream) { - int next_char; + gchar next_char; gboolean crlf = FALSE; gboolean end_of_header_line = FALSE; @@ -184,7 +184,7 @@ get_header_table_from_stream (GnomeStream *stream) GHashTable *header_table; header_table = g_hash_table_new (g_string_hash, g_string_equal_for_hash); - //next_char = fgetc (file); + camel_stream_read (stream, &next_char, 1); do { header_line = g_string_new(""); end_of_header_line = FALSE; @@ -193,7 +193,7 @@ get_header_table_from_stream (GnomeStream *stream) /* read a whole header line */ do { switch (next_char) { - case EOF: + case -1: end_of_file=TRUE; end_of_header_line = TRUE; break; @@ -215,7 +215,7 @@ get_header_table_from_stream (GnomeStream *stream) /* if we have read a whole header line, we have also read the first character of the next line to be sure the crlf was not followed by a space or a tab char */ - //if (!end_of_header_line) next_char = fgetc (file); + if (!end_of_header_line) camel_stream_read (stream, &next_char, 1); } while ( !end_of_header_line ); if ( strlen(header_line->str) ) diff --git a/camel/gmime-utils.h b/camel/gmime-utils.h index 1c2097434f..428bb8afee 100644 --- a/camel/gmime-utils.h +++ b/camel/gmime-utils.h @@ -33,14 +33,14 @@ extern "C" { #include #include -#include +#include void gmime_write_header_pair_to_file (FILE* file, gchar* name, GString *value); void write_header_table_to_file (FILE *file, GHashTable *header_table); void write_header_with_glist_to_file (FILE *file, gchar *header_name, GList *header_values); GHashTable *get_header_table_from_file (FILE *file); -GHashTable *get_header_table_from_stream (GnomeStream *stream); +GHashTable *get_header_table_from_stream (CamelStream *stream); diff --git a/tests/test2.c b/tests/test2.c index 91e2fa0f2d..e79c5b79d3 100644 --- a/tests/test2.c +++ b/tests/test2.c @@ -6,6 +6,7 @@ #include "camel-mime-message.h" #include "camel-mime-part.h" #include "camel-stream.h" +#include "camel-stream-fs.h" void print_header_pair (gpointer key, gpointer value, gpointer user_data) { @@ -29,7 +30,6 @@ void print_header_pair (gpointer key, gpointer value, gpointer user_data) void main (int argc, char**argv) { - FILE *input_file; FILE *output_file; GHashTable *header_table; CamelMimeMessage *message; @@ -39,19 +39,20 @@ main (int argc, char**argv) camel_debug_level = FULL_DEBUG; message = camel_mime_message_new_with_session( (CamelSession *)NULL); - input_file = fopen ("mail.test", "r"); - /*stream = gnome_stream_fs_open (NULL, "mail.test", GNOME_Storage_READ);*/ - if (!input_file) { + /* input_file = fopen ("mail.test", "r"); */ + stream = camel_stream_fs_new_with_name (g_string_new ("mail.test"), CAMEL_STREAM_FS_READ); + if (!stream) { perror("could not open input file"); exit(2); } - header_table = get_header_table_from_file (input_file); + //header_table = get_header_table_from_file (input_file); + header_table = get_header_table_from_stream (stream); if (header_table) g_hash_table_foreach (header_table, print_header_pair, (gpointer)message); else printf("header is empty, no header line present\n"); - fclose (input_file); + camel_stream_close (stream); output_file = fopen ("mail2.test", "w"); if (!output_file) { -- cgit v1.2.3