Tizen 2.1 base
[platform/upstream/glib2.0.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
28 /**
29  * SECTION:gseekable
30  * @short_description: Stream seeking interface
31  * @include: gio/gio.h
32  * @see_also: #GInputStream, #GOutputStream
33  * 
34  * #GSeekable is implemented by streams (implementations of 
35  * #GInputStream or #GOutputStream) that support seeking.
36  * 
37  **/
38
39 typedef GSeekableIface GSeekableInterface;
40 G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
41
42 static void
43 g_seekable_default_init (GSeekableInterface *iface)
44 {
45 }
46
47 /**
48  * g_seekable_tell:
49  * @seekable: a #GSeekable.
50  * 
51  * Tells the current position within the stream.
52  * 
53  * Returns: the offset from the beginning of the buffer.
54  **/
55 goffset
56 g_seekable_tell (GSeekable *seekable)
57 {
58   GSeekableIface *iface;
59
60   g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
61
62   iface = G_SEEKABLE_GET_IFACE (seekable);
63
64   return (* iface->tell) (seekable);
65 }
66
67 /**
68  * g_seekable_can_seek:
69  * @seekable: a #GSeekable.
70  * 
71  * Tests if the stream supports the #GSeekableIface.
72  * 
73  * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
74  **/
75 gboolean
76 g_seekable_can_seek (GSeekable *seekable)
77 {
78   GSeekableIface *iface;
79   
80   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
81
82   iface = G_SEEKABLE_GET_IFACE (seekable);
83
84   return (* iface->can_seek) (seekable);
85 }
86
87 /**
88  * g_seekable_seek:
89  * @seekable: a #GSeekable.
90  * @offset: a #goffset.
91  * @type: a #GSeekType.
92  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
93  * @error: a #GError location to store the error occurring, or %NULL to 
94  * ignore.
95  * 
96  * Seeks in the stream by the given @offset, modified by @type.
97  *
98  * If @cancellable is not %NULL, then the operation can be cancelled by
99  * triggering the cancellable object from another thread. If the operation
100  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
101  * 
102  * Returns: %TRUE if successful. If an error
103  *     has occurred, this function will return %FALSE and set @error
104  *     appropriately if present.
105  **/
106 gboolean
107 g_seekable_seek (GSeekable     *seekable,
108                  goffset        offset,
109                  GSeekType      type,
110                  GCancellable  *cancellable,
111                  GError       **error)
112 {
113   GSeekableIface *iface;
114   
115   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
116
117   iface = G_SEEKABLE_GET_IFACE (seekable);
118
119   return (* iface->seek) (seekable, offset, type, cancellable, error);
120 }
121
122 /**
123  * g_seekable_can_truncate:
124  * @seekable: a #GSeekable.
125  * 
126  * Tests if the stream can be truncated.
127  * 
128  * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
129  **/
130 gboolean
131 g_seekable_can_truncate (GSeekable *seekable)
132 {
133   GSeekableIface *iface;
134   
135   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
136
137   iface = G_SEEKABLE_GET_IFACE (seekable);
138
139   return (* iface->can_truncate) (seekable);
140 }
141
142 /**
143  * g_seekable_truncate:
144  * @seekable: a #GSeekable.
145  * @offset: a #goffset.
146  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
147  * @error: a #GError location to store the error occurring, or %NULL to 
148  * ignore.
149  * 
150  * Truncates a stream with a given #offset. 
151  * 
152  * If @cancellable is not %NULL, then the operation can be cancelled by
153  * triggering the cancellable object from another thread. If the operation
154  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
155  * operation was partially finished when the operation was cancelled the
156  * partial result will be returned, without an error.
157  *
158  * Virtual: truncate_fn
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 }