~ubuntu-branches/ubuntu/karmic/libtinymail/karmic

« back to all changes in this revision

Viewing changes to libtinymail/tny-stream.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-10-12 11:21:12 UTC
  • Revision ID: james.westby@ubuntu.com-20071012112112-fod9fs7yrooxjr7i
Tags: upstream-0.0.2
ImportĀ upstreamĀ versionĀ 0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libtinymail - The Tiny Mail base library
 
2
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with self library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <tny-stream.h>
 
23
 
 
24
 
 
25
/**
 
26
 * tny_stream_write_to_stream:
 
27
 * @self: a #TnyStream object
 
28
 * @output: a #TnyStream object
 
29
 * 
 
30
 * Write @self to @output in an efficient way
 
31
 *
 
32
 * Return value: the number of bytes written to the output stream, or -1 on
 
33
 * error along with setting errno.
 
34
 *
 
35
 **/
 
36
gssize
 
37
tny_stream_write_to_stream (TnyStream *self, TnyStream *output)
 
38
{
 
39
#ifdef DBC /* require */
 
40
        g_assert (TNY_IS_STREAM (self));
 
41
        g_assert (output);
 
42
        g_assert (TNY_IS_STREAM (output));
 
43
        g_assert (TNY_STREAM_GET_IFACE (self)->write_to_stream_func != NULL);
 
44
#endif
 
45
 
 
46
        return TNY_STREAM_GET_IFACE (self)->write_to_stream_func (self, output);
 
47
}
 
48
 
 
49
/**
 
50
 * tny_stream_read:
 
51
 * @self: a #TnyStream object
 
52
 * @buffer: a buffer that is at least n in size
 
53
 * @n: the max amount of bytes to read from self and to write into buffer
 
54
 * 
 
55
 * Read n bytes @self and write it into @buffer. It's your responsibility
 
56
 * to pass a buffer that is large enough to hold n bytes.
 
57
 *
 
58
 * Return value: the number of bytes actually read, or -1 on error and 
 
59
 * set errno.
 
60
 *
 
61
 **/
 
62
gssize
 
63
tny_stream_read  (TnyStream *self, char *buffer, gsize n)
 
64
{
 
65
#ifdef DBC /* require */
 
66
        g_assert (TNY_IS_STREAM (self));
 
67
        g_assert (TNY_STREAM_GET_IFACE (self)->read_func  != NULL);
 
68
#endif
 
69
 
 
70
        return TNY_STREAM_GET_IFACE (self)->read_func (self, buffer, n);
 
71
}
 
72
 
 
73
/**
 
74
 * tny_stream_write:
 
75
 * @self: a #TnyStream object
 
76
 * @buffer: a buffer that has at least n bytes
 
77
 * @n: the amount of bytes to read from buffer and to write to self
 
78
 * 
 
79
 * Write n bytes of @buffer into @self. It's your responsibility to pass
 
80
 * a buffer that has at least n bytes.
 
81
 *
 
82
 * Return value: the number of bytes written to the stream, or -1 on error 
 
83
 * along with setting errno.
 
84
 *
 
85
 **/
 
86
gssize
 
87
tny_stream_write (TnyStream *self, const char *buffer, gsize n)
 
88
{
 
89
#ifdef DBC /* require */
 
90
        g_assert (TNY_IS_STREAM (self));
 
91
        g_assert (TNY_STREAM_GET_IFACE (self)->write_func  != NULL);
 
92
#endif
 
93
 
 
94
        return TNY_STREAM_GET_IFACE (self)->write_func (self, buffer, n);
 
95
}
 
96
 
 
97
 
 
98
/**
 
99
 * tny_stream_flush:
 
100
 * @self: a #TnyStream object
 
101
 * 
 
102
 * Flushes any buffered data to the stream's backing store. 
 
103
 *
 
104
 * Return value: 0 on success or -1 on fail along with setting errno.
 
105
 *
 
106
 **/
 
107
gint
 
108
tny_stream_flush (TnyStream *self)
 
109
{
 
110
#ifdef DBC /* require */
 
111
        g_assert (TNY_IS_STREAM (self));
 
112
        g_assert (TNY_STREAM_GET_IFACE (self)->flush_func  != NULL);
 
113
#endif
 
114
 
 
115
        return TNY_STREAM_GET_IFACE (self)->flush_func (self);
 
116
}
 
117
 
 
118
/**
 
119
 * tny_stream_close:
 
120
 * @self: a #TnyStream object
 
121
 * 
 
122
 * Closes the stream
 
123
 *
 
124
 * Return value: 0 on success or -1 on fail.
 
125
 **/
 
126
gint
 
127
tny_stream_close (TnyStream *self)
 
128
{
 
129
#ifdef DBC /* require */
 
130
        g_assert (TNY_IS_STREAM (self));
 
131
        g_assert (TNY_STREAM_GET_IFACE (self)->close_func  != NULL);
 
132
#endif
 
133
 
 
134
        return TNY_STREAM_GET_IFACE (self)->close_func (self);
 
135
}
 
136
 
 
137
 
 
138
/**
 
139
 * tny_stream_is_eos:
 
140
 * @self: a #TnyStream object
 
141
 * 
 
142
 * Tests if there are bytes left to read on @self.
 
143
 *
 
144
 * Return value: TRUE on EOS or FALSE otherwise.
 
145
 *
 
146
 **/
 
147
gboolean
 
148
tny_stream_is_eos   (TnyStream *self)
 
149
{
 
150
#ifdef DBC /* require */
 
151
        g_assert (TNY_IS_STREAM (self));
 
152
        g_assert (TNY_STREAM_GET_IFACE (self)->is_eos_func  != NULL);
 
153
#endif
 
154
 
 
155
        return TNY_STREAM_GET_IFACE (self)->is_eos_func (self);
 
156
}
 
157
 
 
158
/**
 
159
 * tny_stream_reset:
 
160
 * @self: a #TnyStream object
 
161
 * 
 
162
 * Resets @self. That is, put it in a state where it can be read from 
 
163
 * the beginning again.
 
164
 *
 
165
 * Return value: 0 on success or -1 on error along with setting errno.
 
166
 *
 
167
 **/
 
168
gint
 
169
tny_stream_reset (TnyStream *self)
 
170
{
 
171
#ifdef DBC /* require */
 
172
        g_assert (TNY_IS_STREAM (self));
 
173
        g_assert (TNY_STREAM_GET_IFACE (self)->reset_func  != NULL);
 
174
#endif
 
175
 
 
176
        return TNY_STREAM_GET_IFACE (self)->reset_func (self);
 
177
}
 
178
 
 
179
static void
 
180
tny_stream_base_init (gpointer g_class)
 
181
{
 
182
        static gboolean initialized = FALSE;
 
183
 
 
184
        if (!initialized) {
 
185
                /* create interface signals here. */
 
186
                initialized = TRUE;
 
187
        }
 
188
}
 
189
 
 
190
GType
 
191
tny_stream_get_type (void)
 
192
{
 
193
        static GType type = 0;
 
194
 
 
195
        if (G_UNLIKELY(type == 0))
 
196
        {
 
197
                static const GTypeInfo info = 
 
198
                {
 
199
                  sizeof (TnyStreamIface),
 
200
                  tny_stream_base_init,   /* base_init */
 
201
                  NULL,   /* base_finalize */
 
202
                  NULL,   /* class_init */
 
203
                  NULL,   /* class_finalize */
 
204
                  NULL,   /* class_data */
 
205
                  0,
 
206
                  0,      /* n_preallocs */
 
207
                  NULL,   /* instance_init */
 
208
                  NULL
 
209
                };
 
210
                type = g_type_register_static (G_TYPE_INTERFACE, 
 
211
                        "TnyStream", &info, 0);
 
212
        }
 
213
 
 
214
        return type;
 
215
}
 
216
 
 
217