1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-simple-data-wrapper.c : simple implementation of a data wrapper */
3 /* store the data in a glib byte array */
7 * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
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.
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.
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
25 #include "camel-simple-data-wrapper.h"
27 static CamelDataWrapperClass *parent_class=NULL;
29 /* Returns the class for a CamelDataWrapper */
30 #define CSDW_CLASS(so) CAMEL_SIMPLE_DATA_WRAPPER_CLASS (GTK_OBJECT(so)->klass)
32 static void _construct_from_stream (CamelDataWrapper *data_wrapper, CamelStream *stream, guint size);
33 static void _write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream);
36 camel_simple_data_wrapper_class_init (CamelSimpleDataWrapperClass *camel_simple_data_wrapper_class)
38 CamelDataWrapperClass *camel_data_wrapper_class = CAMEL_DATA_WRAPPER_CLASS (camel_simple_data_wrapper_class);
40 parent_class = gtk_type_class (camel_data_wrapper_get_type ());
41 /* virtual method definition */
43 /* virtual method overload */
44 camel_data_wrapper_class->write_to_stream = _write_to_stream;
45 camel_data_wrapper_class->construct_from_stream = _construct_from_stream;
54 camel_simple_data_wrapper_get_type (void)
56 static GtkType camel_simple_data_wrapper_type = 0;
58 if (!camel_simple_data_wrapper_type) {
59 GtkTypeInfo camel_simple_data_wrapper_info =
62 sizeof (CamelDataWrapper),
63 sizeof (CamelDataWrapperClass),
64 (GtkClassInitFunc) camel_simple_data_wrapper_class_init,
65 (GtkObjectInitFunc) NULL,
66 /* reserved_1 */ NULL,
67 /* reserved_2 */ NULL,
68 (GtkClassInitFunc) NULL,
71 camel_simple_data_wrapper_type = gtk_type_unique (camel_data_wrapper_get_type (), &camel_simple_data_wrapper_info);
74 return camel_simple_data_wrapper_type;
80 _write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream)
82 CamelSimpleDataWrapper *simple_data_wrapper = CAMEL_SIMPLE_DATA_WRAPPER (data_wrapper);
85 g_assert (data_wrapper);
87 g_assert (simple_data_wrapper->byte_array);
89 array = simple_data_wrapper->byte_array;
91 camel_stream_write (stream, (gchar *)array->data, array->len);
99 #define _CMSDW_TMP_BUF_SIZE 100
101 _construct_from_stream (CamelDataWrapper *data_wrapper, CamelStream *stream, guint size)
103 CamelSimpleDataWrapper *simple_data_wrapper = CAMEL_SIMPLE_DATA_WRAPPER (data_wrapper);
107 static gchar *tmp_buf;
111 g_assert (data_wrapper);
115 if (!tmp_buf) tmp_buf = g_new (gchar, _CMSDW_TMP_BUF_SIZE);
117 array = simple_data_wrapper->byte_array;
119 g_byte_array_free (array, FALSE);
121 array = g_byte_array_new();
122 nb_bytes_left = size;
125 nb_bytes_read = camel_stream_read (stream, tmp_buf,
126 MIN (_CMSDW_TMP_BUF_SIZE, nb_bytes_left));
127 nb_bytes_left -= nb_bytes_read;
128 if (nb_bytes_read) g_byte_array_append (array, tmp_buf, nb_bytes_read);
130 } while (nb_bytes_read && (nb_bytes_left>0));