Use g_set_error_literal where appropriate. Patch from bug #535947.
[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
41 static void g_seekable_base_init (gpointer g_class);
42
43
44 GType
45 g_seekable_get_type (void)
46 {
47   static GType seekable_type = 0;
48
49   if (!seekable_type)
50     {
51       static const GTypeInfo seekable_info =
52       {
53         sizeof (GSeekableIface), /* class_size */
54         g_seekable_base_init,   /* base_init */
55         NULL,           /* base_finalize */
56         NULL,
57         NULL,           /* class_finalize */
58         NULL,           /* class_data */
59         0,
60         0,              /* n_preallocs */
61         NULL
62       };
63
64       seekable_type =
65         g_type_register_static (G_TYPE_INTERFACE, I_("GSeekable"),
66                                 &seekable_info, 0);
67
68       g_type_interface_add_prerequisite (seekable_type, G_TYPE_OBJECT);
69     }
70
71   return seekable_type;
72 }
73
74 static void
75 g_seekable_base_init (gpointer g_class)
76 {
77 }
78
79 /**
80  * g_seekable_tell:
81  * @seekable: a #GSeekable.
82  * 
83  * Tells the current position within the stream.
84  * 
85  * Returns: the offset from the beginning of the buffer.
86  **/
87 goffset
88 g_seekable_tell (GSeekable *seekable)
89 {
90   GSeekableIface *iface;
91
92   g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
93
94   iface = G_SEEKABLE_GET_IFACE (seekable);
95
96   return (* iface->tell) (seekable);
97 }
98
99 /**
100  * g_seekable_can_seek:
101  * @seekable: a #GSeekable.
102  * 
103  * Tests if the stream supports the #GSeekableIface.
104  * 
105  * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
106  **/
107 gboolean
108 g_seekable_can_seek (GSeekable *seekable)
109 {
110   GSeekableIface *iface;
111   
112   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
113
114   iface = G_SEEKABLE_GET_IFACE (seekable);
115
116   return (* iface->can_seek) (seekable);
117 }
118
119 /**
120  * g_seekable_seek:
121  * @seekable: a #GSeekable.
122  * @offset: a #goffset.
123  * @type: a #GSeekType.
124  * @cancellable: optional #GCancellable object, %NULL to ignore. 
125  * @error: a #GError location to store the error occuring, or %NULL to 
126  * ignore.
127  * 
128  * Seeks in the stream by the given @offset, modified by @type.
129  *
130  * If @cancellable is not %NULL, then the operation can be cancelled by
131  * triggering the cancellable object from another thread. If the operation
132  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
133  * 
134  * Returns: %TRUE if successful. If an error
135  *     has occurred, this function will return %FALSE and set @error
136  *     appropriately if present.
137  **/
138 gboolean
139 g_seekable_seek (GSeekable     *seekable,
140                  goffset        offset,
141                  GSeekType      type,
142                  GCancellable  *cancellable,
143                  GError       **error)
144 {
145   GSeekableIface *iface;
146   
147   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
148
149   iface = G_SEEKABLE_GET_IFACE (seekable);
150
151   return (* iface->seek) (seekable, offset, type, cancellable, error);
152 }
153
154 /**
155  * g_seekable_can_truncate:
156  * @seekable: a #GSeekable.
157  * 
158  * Tests if the stream can be truncated.
159  * 
160  * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
161  **/
162 gboolean
163 g_seekable_can_truncate (GSeekable *seekable)
164 {
165   GSeekableIface *iface;
166   
167   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
168
169   iface = G_SEEKABLE_GET_IFACE (seekable);
170
171   return (* iface->can_truncate) (seekable);
172 }
173
174 /**
175  * g_seekable_truncate:
176  * @seekable: a #GSeekable.
177  * @offset: a #goffset.
178  * @cancellable: optional #GCancellable object, %NULL to ignore. 
179  * @error: a #GError location to store the error occuring, or %NULL to 
180  * ignore.
181  * 
182  * Truncates a stream with a given #offset. 
183  * 
184  * If @cancellable is not %NULL, then the operation can be cancelled by
185  * triggering the cancellable object from another thread. If the operation
186  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
187  * operation was partially finished when the operation was cancelled the
188  * partial result will be returned, without an error.
189  * 
190  * Returns: %TRUE if successful. If an error
191  *     has occurred, this function will return %FALSE and set @error
192  *     appropriately if present. 
193  **/
194 gboolean
195 g_seekable_truncate (GSeekable     *seekable,
196                      goffset        offset,
197                      GCancellable  *cancellable,
198                      GError       **error)
199 {
200   GSeekableIface *iface;
201   
202   g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
203
204   iface = G_SEEKABLE_GET_IFACE (seekable);
205
206   return (* iface->truncate_fn) (seekable, offset, cancellable, error);
207 }
208
209 #define __G_SEEKABLE_C__
210 #include "gioaliasdef.c"