Implemented base64 encoder based on CamelStreams. Should the
[platform/upstream/evolution-data-server.git] / camel / camel-stream.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-stream.c : abstract class for a stream */
3
4
5 /* 
6  *
7  * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
8  *
9  * This program is free software; you can redistribute it and/or 
10  * modify it under the terms of the GNU General Public License as 
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */
24 #include <config.h>
25 #include "camel-stream.h"
26
27 static CamelStreamClass *parent_class = NULL;
28
29
30 /* Returns the class for a CamelMimeMessage */
31 #define CS_CLASS(so) CAMEL_STREAM_CLASS (GTK_OBJECT(so)->klass)
32
33 static void
34 default_camel_flush (CamelStream *stream)
35 {
36         /* nothing */
37 }
38
39 static void
40 default_camel_close (CamelStream *stream)
41 {
42         /* nothing */
43 }
44
45 static void
46 camel_stream_class_init (CamelStreamClass *camel_stream_class)
47 {
48
49         parent_class = gtk_type_class (gtk_object_get_type ());
50
51         /* virtual method definition */
52         camel_stream_class->read = NULL;
53         camel_stream_class->write = NULL;
54         camel_stream_class->flush = default_camel_flush;
55         camel_stream_class->available = NULL;
56         camel_stream_class->eos = NULL; 
57         camel_stream_class->close = default_camel_close;
58
59         /* virtual method overload */
60 }
61
62 GtkType
63 camel_stream_get_type (void)
64 {
65         static GtkType camel_stream_type = 0;
66         
67         if (!camel_stream_type) {
68                 GtkTypeInfo camel_stream_info = 
69                 {
70                         "CamelStream",
71                         sizeof (CamelStream),
72                         sizeof (CamelStreamClass),
73                         (GtkClassInitFunc) camel_stream_class_init,
74                         (GtkObjectInitFunc) NULL,
75                                 /* reserved_1 */ NULL,
76                                 /* reserved_2 */ NULL,
77                         (GtkClassInitFunc) NULL,
78                 };
79                 
80                 camel_stream_type = gtk_type_unique (gtk_object_get_type (), &camel_stream_info);
81         }
82         
83         return camel_stream_type;
84 }
85
86 /**
87  * camel_stream_read: 
88  * @stream: a CamelStream.
89  * @buffer: buffer where bytes pulled from the stream are stored.
90  * @n: max number of bytes to read.
91  * 
92  * Read at most @n bytes from the @stream object and stores them
93  * in the buffer pointed at by @buffer.
94  * 
95  * Return value: number of bytes actually read.
96  **/
97 gint 
98 camel_stream_read (CamelStream *stream, gchar *buffer, gint n)
99 {
100         CS_CLASS (stream)->read (stream, buffer, n);
101 }
102
103 /**
104  * camel_stream_write: 
105  * @stream: a CamelStream object.
106  * @buffer: buffer to write.
107  * @n: number of bytes to write
108  *
109  * Write @n bytes from the buffer pointed at by @buffer into @stream.
110  *
111  * Return value: the number of bytes actually written
112  *  in the stream.
113  **/
114 gint
115 camel_stream_write (CamelStream *stream, gchar *buffer, gint n)
116 {
117         return CS_CLASS (stream)->write (stream, buffer, n);
118 }
119
120 /**
121  * camel_stream_flush:
122  * @stream: a CamelStream object
123  * 
124  * Flushes the contents of the stream to its backing store.
125  **/
126 void
127 camel_stream_flush (CamelStream *stream)
128 {
129         return CS_CLASS (stream)->flush (stream);
130 }
131
132 /**
133  * camel_stream_available: 
134  * @stream: a CamelStream object
135  * 
136  * Return value: the number of bytes available.
137  **/
138 static gint 
139 camel_stream_available (CamelStream *stream)
140 {
141         return CS_CLASS (stream)->available (stream);
142 }
143
144 /**
145  * camle_stream_eos: 
146  * @stream: a CamelStream object
147  * 
148  * Test if there are bytes left to read on the @stream object.
149  * 
150  * Return value: %TRUE if all the contents on the stream has been read, or
151  * %FALSE if information is still available.
152  **/
153 static gboolean
154 camel_stream_eos (CamelStream *stream)
155 {
156         return CS_CLASS (stream)->eos (stream);
157 }
158
159
160 /**
161  * camel_stram_close: 
162  * @stream: a CamelStream object.
163  * 
164  * Close the @stream object.
165  **/
166 void
167 camel_stream_close (CamelStream *stream)
168 {
169         CS_CLASS (stream)->close (stream);
170 }
171
172 /***************** Utility functions ********************/
173
174 /**
175  * came_stream_write_strings:
176  * @stream: a CamelStream object.
177  * @...: A %NULL terminated list of strings.
178  *
179  * This is a utility function that writes the list of
180  * strings into the @stream object.
181  */
182 void
183 camel_stream_write_strings (CamelStream *stream, ... )
184 {
185         va_list args;
186         char *string;
187         
188         va_start(args, stream);
189         string = va_arg (args, char *);
190         
191         while (string) {
192                 camel_stream_write_string (stream, string);
193                 string = va_arg (args, char *);
194         }
195         va_end (args);
196 }