use new stream code instead of raw file * stuff.
authorbertrand <Bertrand.Guiheneuf@inria.fr>
Sun, 30 May 1999 21:56:09 +0000 (21:56 +0000)
committerBertrand Guiheneuf <bertrand@src.gnome.org>
Sun, 30 May 1999 21:56:09 +0000 (21:56 +0000)
1999-05-31  bertrand  <Bertrand.Guiheneuf@inria.fr>

* 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.

camel/Makefile.am
camel/camel-stream-fs.c [new file with mode: 0644]
camel/camel-stream-fs.h [new file with mode: 0644]
camel/camel-stream.c
camel/camel-stream.h
camel/gmime-utils.c
camel/gmime-utils.h

index 0713f73..9a88729 100644 (file)
@@ -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 (file)
index 0000000..0a42e50
--- /dev/null
@@ -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 <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 "camel-stream-fs.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+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 (file)
index 0000000..046e90d
--- /dev/null
@@ -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 <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_FS_H
+#define CAMEL_STREAM_FS_H 1
+
+
+#ifdef __cplusplus
+extern "C" {
+#pragma }
+#endif /* __cplusplus }*/
+
+#include <gtk/gtk.h>
+#include <stdio.h>
+#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 */
index 9af57c5..c993556 100644 (file)
@@ -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);
+}
index e58cfd9..925d558 100644 (file)
@@ -34,8 +34,8 @@ extern "C" {
 #include <gtk/gtk.h>
 
 #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
 }
index b2d95e4..591706a 100644 (file)
@@ -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) ) 
index 1c20974..428bb8a 100644 (file)
@@ -33,14 +33,14 @@ extern "C" {
 
 #include <glib.h>
 #include <stdio.h>
-#include <bonobo/gnome-stream.h>
+#include <camel-stream.h>
 
 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);