2d81fa62a99ff8c13e35af56c49af43b144f4e28
[platform/upstream/evolution-data-server.git] / camel / camel-simple-data-wrapper.c
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                                   */
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
25 #include "camel-simple-data-wrapper.h"
26
27 static  CamelDataWrapperClass *parent_class=NULL;
28
29 /* Returns the class for a CamelDataWrapper */
30 #define CSDW_CLASS(so) CAMEL_SIMPLE_DATA_WRAPPER_CLASS (GTK_OBJECT(so)->klass)
31
32 static void _construct_from_stream (CamelDataWrapper *data_wrapper, CamelStream *stream, guint size);
33 static void _write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream);
34
35 static void
36 camel_simple_data_wrapper_class_init (CamelSimpleDataWrapperClass *camel_simple_data_wrapper_class)
37 {
38         CamelDataWrapperClass *camel_data_wrapper_class = CAMEL_DATA_WRAPPER_CLASS (camel_simple_data_wrapper_class);
39         
40         parent_class = gtk_type_class (camel_data_wrapper_get_type ());
41         /* virtual method definition */
42
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;
46 }
47
48
49
50
51
52
53 GtkType
54 camel_simple_data_wrapper_get_type (void)
55 {
56         static GtkType camel_simple_data_wrapper_type = 0;
57         
58         if (!camel_simple_data_wrapper_type)    {
59                 GtkTypeInfo camel_simple_data_wrapper_info =    
60                 {
61                         "CamelDataWrapper",
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,
69                 };
70                 
71                 camel_simple_data_wrapper_type = gtk_type_unique (camel_data_wrapper_get_type (), &camel_simple_data_wrapper_info);
72         }
73         
74         return camel_simple_data_wrapper_type;
75 }
76
77
78
79 static void
80 _write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream)
81 {
82         CamelSimpleDataWrapper *simple_data_wrapper = CAMEL_SIMPLE_DATA_WRAPPER (data_wrapper);
83         GByteArray *array;
84
85         g_assert (data_wrapper);
86         g_assert (stream);
87         g_assert (simple_data_wrapper->byte_array);
88
89         array = simple_data_wrapper->byte_array;
90         if (array->len)
91                 camel_stream_write (stream, (gchar *)array->data, array->len);
92
93         return;
94 }
95
96
97
98
99 #define _CMSDW_TMP_BUF_SIZE 100
100 static void
101 _construct_from_stream (CamelDataWrapper *data_wrapper, CamelStream *stream, guint size)
102 {
103         CamelSimpleDataWrapper *simple_data_wrapper = CAMEL_SIMPLE_DATA_WRAPPER (data_wrapper);
104         guint current_index;
105         guint nb_bytes_read;
106         guint nb_bytes_left;
107         static gchar *tmp_buf;
108         GByteArray *array;
109                 
110
111         g_assert (data_wrapper);
112         g_assert (stream);
113         
114         if (!size) return;
115         if (!tmp_buf) tmp_buf = g_new (gchar, _CMSDW_TMP_BUF_SIZE);
116
117         array = simple_data_wrapper->byte_array;
118         if (array)
119                 g_byte_array_free (array, FALSE);
120         
121         array = g_byte_array_new();
122         nb_bytes_left = size;
123         do {
124                 
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);
129                 
130         } while (nb_bytes_read && (nb_bytes_left>0));
131         
132 }