Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gseekable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gseekable.h"
25 #include "glibintl.h"
26
27 #include "gioalias.h"
28
29 /**
30  * SECTION:gseekable
31  * @short_description: Stream seeking interface
32  * @include: gio/gio.h
33  * @see_also: #GInputStream, #GOutputStream
34  * 
35  * #GSeekable is implemented by streams (implementations of 
36  * #GInputStream or #GOutputStream) that support seeking.
37  * 
38  **/
39
40 typedef GSeekableIface GSeekableInterface;
41 G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
42
43 static void
44 g_seekable_default_init (GSeekableInterface *iface)
45 {
46 }
47
48 /**
49  * g_seekable_tell:
50  * @seekable: a #GSeekable.
51  * 
52  * Tells the current position within the stream.
53  * 
54  * Returns: the offset from the beginning of the buffer.
55  **/
56 goffset
57 g_seekable_tell (GSeekable *seekable)
58 {
59   GSeekableIface *iface;
60
61   g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
62
63   iface = G_SEEKABLE_GET_IFACE (seekable);
64
65   return (* iface->tell) (seekable);
66 }
67
68 /**
69  * g_seekable_can_seek:
70  * @seekable: a #GSeekable.
71  * 
72  * Tests if the stream supports the #GSeekableIface.
73  * 
74  * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
75  **/
76 gboolean
77 g_seekable_can_seek (GSeekable *seekable)
78 {
79   GSeekableIface *iface;
80   
81   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
82
83   iface = G_SEEKABLE_GET_IFACE (seekable);
84
85   return (* iface->can_seek) (seekable);
86 }
87
88 /**
89  * g_seekable_seek:
90  * @seekable: a #GSeekable.
91  * @offset: a #goffset.
92  * @type: a #GSeekType.
93  * @cancellable: optional #GCancellable object, %NULL to ignore. 
94  * @error: a #GError location to store the error occuring, or %NULL to 
95  * ignore.
96  * 
97  * Seeks in the stream by the given @offset, modified by @type.
98  *
99  * If @cancellable is not %NULL, then the operation can be cancelled by
100  * triggering the cancellable object from another thread. If the operation
101  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
102  * 
103  * Returns: %TRUE if successful. If an error
104  *     has occurred, this function will return %FALSE and set @error
105  *     appropriately if present.
106  **/
107 gboolean
108 g_seekable_seek (GSeekable     *seekable,
109                  goffset        offset,
110                  GSeekType      type,
111                  GCancellable  *cancellable,
112                  GError       **error)
113 {
114   GSeekableIface *iface;
115   
116   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
117
118   iface = G_SEEKABLE_GET_IFACE (seekable);
119
120   return (* iface->seek) (seekable, offset, type, cancellable, error);
121 }
122
123 /**
124  * g_seekable_can_truncate:
125  * @seekable: a #GSeekable.
126  * 
127  * Tests if the stream can be truncated.
128  * 
129  * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
130  **/
131 gboolean
132 g_seekable_can_truncate (GSeekable *seekable)
133 {
134   GSeekableIface *iface;
135   
136   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
137
138   iface = G_SEEKABLE_GET_IFACE (seekable);
139
140   return (* iface->can_truncate) (seekable);
141 }
142
143 /**
144  * g_seekable_truncate:
145  * @seekable: a #GSeekable.
146  * @offset: a #goffset.
147  * @cancellable: optional #GCancellable object, %NULL to ignore. 
148  * @error: a #GError location to store the error occuring, or %NULL to 
149  * ignore.
150  * 
151  * Truncates a stream with a given #offset. 
152  * 
153  * If @cancellable is not %NULL, then the operation can be cancelled by
154  * triggering the cancellable object from another thread. If the operation
155  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
156  * operation was partially finished when the operation was cancelled the
157  * partial result will be returned, without an error.
158  * 
159  * Returns: %TRUE if successful. If an error
160  *     has occurred, this function will return %FALSE and set @error
161  *     appropriately if present. 
162  **/
163 gboolean
164 g_seekable_truncate (GSeekable     *seekable,
165                      goffset        offset,
166                      GCancellable  *cancellable,
167                      GError       **error)
168 {
169   GSeekableIface *iface;
170   
171   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
172
173   iface = G_SEEKABLE_GET_IFACE (seekable);
174
175   return (* iface->truncate_fn) (seekable, offset, cancellable, error);
176 }
177
178 #define __G_SEEKABLE_C__
179 #include "gioaliasdef.c"