Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-seekable-stream.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4  * Author:
5  *  Bertrand Guiheneuf <bertrand@helixcode.com>
6  *
7  * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of version 2 of the GNU Lesser General Public
11  * License as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "camel-seekable-stream.h"
29
30 static CamelStreamClass *parent_class = NULL;
31
32 /* Returns the class for a CamelSeekableStream */
33 #define CSS_CLASS(so) CAMEL_SEEKABLE_STREAM_CLASS (CAMEL_OBJECT_GET_CLASS(so))
34
35 static off_t seek        (CamelSeekableStream *stream, off_t offset,
36                           CamelStreamSeekPolicy policy);
37 static off_t stream_tell (CamelSeekableStream *stream);
38 static int   reset       (CamelStream *stream);
39 static int   set_bounds  (CamelSeekableStream *stream, off_t start, off_t end);
40
41 static void
42 camel_seekable_stream_class_init (CamelSeekableStreamClass *camel_seekable_stream_class)
43 {
44         CamelStreamClass *camel_stream_class =
45                 CAMEL_STREAM_CLASS (camel_seekable_stream_class);
46
47         parent_class = CAMEL_STREAM_CLASS( camel_type_get_global_classfuncs( CAMEL_STREAM_TYPE ) );
48
49         /* seekable stream methods */
50         camel_seekable_stream_class->seek = seek;
51         camel_seekable_stream_class->tell = stream_tell;
52         camel_seekable_stream_class->set_bounds = set_bounds;
53
54         /* camel stream methods overload */
55         camel_stream_class->reset = reset;
56 }
57
58 static void
59 camel_seekable_stream_init (void *o)
60 {
61         CamelSeekableStream *stream = (CamelSeekableStream *)o;
62
63         stream->bound_start = 0;
64         stream->bound_end = CAMEL_STREAM_UNBOUND;
65 }
66
67 CamelType
68 camel_seekable_stream_get_type (void)
69 {
70         static CamelType camel_seekable_stream_type = CAMEL_INVALID_TYPE;
71
72         if (camel_seekable_stream_type == CAMEL_INVALID_TYPE) {
73                 camel_seekable_stream_type = camel_type_register( CAMEL_STREAM_TYPE,
74                                                                   "CamelSeekableStream",
75                                                                   sizeof( CamelSeekableStream ),
76                                                                   sizeof( CamelSeekableStreamClass ),
77                                                                   (CamelObjectClassInitFunc) camel_seekable_stream_class_init,
78                                                                   NULL,
79                                                                   (CamelObjectInitFunc) camel_seekable_stream_init,
80                                                                   NULL );
81         }
82
83         return camel_seekable_stream_type;
84 }
85
86
87 static off_t
88 seek (CamelSeekableStream *stream, off_t offset,
89       CamelStreamSeekPolicy policy)
90 {
91         g_warning ("CamelSeekableStream::seek called on default "
92                    "implementation\n");
93         return -1;
94 }
95
96 /**
97  * camel_seekable_stream_seek:
98  * @stream: a #CamelStream object
99  * @offset: offset value
100  * @policy: what to do with the offset
101  *
102  * Seek to the specified position in @stream.
103  *
104  * If @policy is #CAMEL_STREAM_SET, seeks to @offset.
105  *
106  * If @policy is #CAMEL_STREAM_CUR, seeks to the current position plus
107  * @offset.
108  *
109  * If @policy is #CAMEL_STREAM_END, seeks to the end of the stream plus
110  * @offset.
111  *
112  * Regardless of @policy, the stream's final position will be clamped
113  * to the range specified by its lower and upper bounds, and the
114  * stream's eos state will be updated.
115  *
116  * Return value: new position, %-1 if operation failed.
117  **/
118 off_t
119 camel_seekable_stream_seek (CamelSeekableStream *stream, off_t offset,
120                             CamelStreamSeekPolicy policy)
121 {
122         g_return_val_if_fail (CAMEL_IS_SEEKABLE_STREAM (stream), -1);
123
124         return CSS_CLASS (stream)->seek (stream, offset, policy);
125 }
126
127
128 static off_t
129 stream_tell (CamelSeekableStream *stream)
130 {
131         return stream->position;
132 }
133
134 /**
135  * camel_seekable_stream_tell:
136  * @stream: a #CamelSeekableStream object
137  *
138  * Get the current position of a seekable stream.
139  *
140  * Returns the current position of the stream.
141  **/
142 off_t
143 camel_seekable_stream_tell (CamelSeekableStream *stream)
144 {
145         g_return_val_if_fail (CAMEL_IS_SEEKABLE_STREAM (stream), -1);
146
147         return CSS_CLASS (stream)->tell (stream);
148 }
149
150 static int
151 set_bounds (CamelSeekableStream *stream, off_t start, off_t end)
152 {
153         /* store the bounds */
154         stream->bound_start = start;
155         stream->bound_end = end;
156
157         if (start > stream->position)
158                 return camel_seekable_stream_seek (stream, start, CAMEL_STREAM_SET);
159
160         return 0;
161 }
162
163 /**
164  * camel_seekable_stream_set_bounds:
165  * @stream: a #CamelSeekableStream object
166  * @start: the first valid position
167  * @end: the first invalid position, or #CAMEL_STREAM_UNBOUND
168  *
169  * Set the range of valid data this stream is allowed to cover.  If
170  * there is to be no @end value, then @end should be set to
171  * #CAMEL_STREAM_UNBOUND.
172  *
173  * Returns %-1 on error.
174  **/
175 int
176 camel_seekable_stream_set_bounds (CamelSeekableStream *stream,
177                                   off_t start, off_t end)
178 {
179         g_return_val_if_fail (CAMEL_IS_SEEKABLE_STREAM (stream), -1);
180         g_return_val_if_fail (end == CAMEL_STREAM_UNBOUND || end >= start, -1);
181
182         return CSS_CLASS (stream)->set_bounds (stream, start, end);
183 }
184
185 /* a default implementation of reset for seekable streams */
186 static int
187 reset (CamelStream *stream)
188 {
189         CamelSeekableStream *seekable_stream;
190
191         seekable_stream = CAMEL_SEEKABLE_STREAM (stream);
192
193         return camel_seekable_stream_seek (seekable_stream,
194                                            seekable_stream->bound_start,
195                                            CAMEL_STREAM_SET);
196 }
197
198
199
200
201
202