1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Bertrand Guiheneuf <bertrand@helixcode.com>
7 * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include "camel-seekable-stream.h"
30 static CamelStreamClass *parent_class = NULL;
32 /* Returns the class for a CamelSeekableStream */
33 #define CSS_CLASS(so) CAMEL_SEEKABLE_STREAM_CLASS (CAMEL_OBJECT_GET_CLASS(so))
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);
42 camel_seekable_stream_class_init (CamelSeekableStreamClass *camel_seekable_stream_class)
44 CamelStreamClass *camel_stream_class =
45 CAMEL_STREAM_CLASS (camel_seekable_stream_class);
47 parent_class = CAMEL_STREAM_CLASS( camel_type_get_global_classfuncs( CAMEL_STREAM_TYPE ) );
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;
54 /* camel stream methods overload */
55 camel_stream_class->reset = reset;
59 camel_seekable_stream_init (void *o)
61 CamelSeekableStream *stream = (CamelSeekableStream *)o;
63 stream->bound_start = 0;
64 stream->bound_end = CAMEL_STREAM_UNBOUND;
68 camel_seekable_stream_get_type (void)
70 static CamelType camel_seekable_stream_type = CAMEL_INVALID_TYPE;
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,
79 (CamelObjectInitFunc) camel_seekable_stream_init,
83 return camel_seekable_stream_type;
88 seek (CamelSeekableStream *stream, off_t offset,
89 CamelStreamSeekPolicy policy)
91 g_warning ("CamelSeekableStream::seek called on default "
97 * camel_seekable_stream_seek:
98 * @stream: a #CamelStream object
99 * @offset: offset value
100 * @policy: what to do with the offset
102 * Seek to the specified position in @stream.
104 * If @policy is #CAMEL_STREAM_SET, seeks to @offset.
106 * If @policy is #CAMEL_STREAM_CUR, seeks to the current position plus
109 * If @policy is #CAMEL_STREAM_END, seeks to the end of the stream plus
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.
116 * Return value: new position, %-1 if operation failed.
119 camel_seekable_stream_seek (CamelSeekableStream *stream, off_t offset,
120 CamelStreamSeekPolicy policy)
122 g_return_val_if_fail (CAMEL_IS_SEEKABLE_STREAM (stream), -1);
124 return CSS_CLASS (stream)->seek (stream, offset, policy);
129 stream_tell (CamelSeekableStream *stream)
131 return stream->position;
135 * camel_seekable_stream_tell:
136 * @stream: a #CamelSeekableStream object
138 * Get the current position of a seekable stream.
140 * Returns the current position of the stream.
143 camel_seekable_stream_tell (CamelSeekableStream *stream)
145 g_return_val_if_fail (CAMEL_IS_SEEKABLE_STREAM (stream), -1);
147 return CSS_CLASS (stream)->tell (stream);
151 set_bounds (CamelSeekableStream *stream, off_t start, off_t end)
153 /* store the bounds */
154 stream->bound_start = start;
155 stream->bound_end = end;
157 if (start > stream->position)
158 return camel_seekable_stream_seek (stream, start, CAMEL_STREAM_SET);
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
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.
173 * Returns %-1 on error.
176 camel_seekable_stream_set_bounds (CamelSeekableStream *stream,
177 off_t start, off_t end)
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);
182 return CSS_CLASS (stream)->set_bounds (stream, start, end);
185 /* a default implementation of reset for seekable streams */
187 reset (CamelStream *stream)
189 CamelSeekableStream *seekable_stream;
191 seekable_stream = CAMEL_SEEKABLE_STREAM (stream);
193 return camel_seekable_stream_seek (seekable_stream,
194 seekable_stream->bound_start,