GApplication: allow handles_commandline and service
[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
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  * Seekable streams largely fall into two categories: resizable and
38  * fixed-size.
39  *
40  * #GSeekable on fixed-sized streams is approximately the same as POSIX
41  * lseek() on a block device (for example: attmepting to seek past the
42  * end of the device is an error).  Fixed streams typically cannot be
43  * truncated.
44  *
45  * #GSeekable on resizable streams is approximately the same as POSIX
46  * lseek() on a normal file.  Seeking past the end and writing data will
47  * usually cause the stream to resize by introducing zero bytes.
48  **/
49
50 typedef GSeekableIface GSeekableInterface;
51 G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
52
53 static void
54 g_seekable_default_init (GSeekableInterface *iface)
55 {
56 }
57
58 /**
59  * g_seekable_tell:
60  * @seekable: a #GSeekable.
61  * 
62  * Tells the current position within the stream.
63  * 
64  * Returns: the offset from the beginning of the buffer.
65  **/
66 goffset
67 g_seekable_tell (GSeekable *seekable)
68 {
69   GSeekableIface *iface;
70
71   g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
72
73   iface = G_SEEKABLE_GET_IFACE (seekable);
74
75   return (* iface->tell) (seekable);
76 }
77
78 /**
79  * g_seekable_can_seek:
80  * @seekable: a #GSeekable.
81  * 
82  * Tests if the stream supports the #GSeekableIface.
83  * 
84  * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
85  **/
86 gboolean
87 g_seekable_can_seek (GSeekable *seekable)
88 {
89   GSeekableIface *iface;
90   
91   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
92
93   iface = G_SEEKABLE_GET_IFACE (seekable);
94
95   return (* iface->can_seek) (seekable);
96 }
97
98 /**
99  * g_seekable_seek:
100  * @seekable: a #GSeekable.
101  * @offset: a #goffset.
102  * @type: a #GSeekType.
103  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
104  * @error: a #GError location to store the error occurring, or %NULL to
105  * ignore.
106  *
107  * Seeks in the stream by the given @offset, modified by @type.
108  *
109  * Attempting to seek past the end of the stream will have different
110  * results depending on if the stream is fixed-sized or resizable.  If
111  * the stream is resizable then seeking past the end and then writing
112  * will result in zeros filling the empty space.  Seeking past the end
113  * of a resizable stream and reading will result in EOF.  Seeking past
114  * the end of a fixed-sized stream will fail.
115  *
116  * Any operation that would result in a negative offset will fail.
117  *
118  * If @cancellable is not %NULL, then the operation can be cancelled by
119  * triggering the cancellable object from another thread. If the operation
120  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
121  * 
122  * Returns: %TRUE if successful. If an error
123  *     has occurred, this function will return %FALSE and set @error
124  *     appropriately if present.
125  **/
126 gboolean
127 g_seekable_seek (GSeekable     *seekable,
128                  goffset        offset,
129                  GSeekType      type,
130                  GCancellable  *cancellable,
131                  GError       **error)
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->seek) (seekable, offset, type, cancellable, error);
140 }
141
142 /**
143  * g_seekable_can_truncate:
144  * @seekable: a #GSeekable.
145  * 
146  * Tests if the stream can be truncated.
147  * 
148  * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
149  **/
150 gboolean
151 g_seekable_can_truncate (GSeekable *seekable)
152 {
153   GSeekableIface *iface;
154   
155   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
156
157   iface = G_SEEKABLE_GET_IFACE (seekable);
158
159   return (* iface->can_truncate) (seekable);
160 }
161
162 /**
163  * g_seekable_truncate:
164  * @seekable: a #GSeekable.
165  * @offset: a #goffset.
166  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
167  * @error: a #GError location to store the error occurring, or %NULL to 
168  * ignore.
169  * 
170  * Truncates a stream with a given #offset. 
171  * 
172  * If @cancellable is not %NULL, then the operation can be cancelled by
173  * triggering the cancellable object from another thread. If the operation
174  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
175  * operation was partially finished when the operation was cancelled the
176  * partial result will be returned, without an error.
177  *
178  * Virtual: truncate_fn
179  * Returns: %TRUE if successful. If an error
180  *     has occurred, this function will return %FALSE and set @error
181  *     appropriately if present. 
182  **/
183 gboolean
184 g_seekable_truncate (GSeekable     *seekable,
185                      goffset        offset,
186                      GCancellable  *cancellable,
187                      GError       **error)
188 {
189   GSeekableIface *iface;
190   
191   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
192
193   iface = G_SEEKABLE_GET_IFACE (seekable);
194
195   return (* iface->truncate_fn) (seekable, offset, cancellable, error);
196 }