Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / gseekable.c
blob5237e7f2fb0d586a435b909778723f6793c78f4d
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include "gseekable.h"
23 #include "glibintl.h"
26 /**
27 * SECTION:gseekable
28 * @short_description: Stream seeking interface
29 * @include: gio/gio.h
30 * @see_also: #GInputStream, #GOutputStream
32 * #GSeekable is implemented by streams (implementations of
33 * #GInputStream or #GOutputStream) that support seeking.
35 * Seekable streams largely fall into two categories: resizable and
36 * fixed-size.
38 * #GSeekable on fixed-sized streams is approximately the same as POSIX
39 * lseek() on a block device (for example: attmepting to seek past the
40 * end of the device is an error). Fixed streams typically cannot be
41 * truncated.
43 * #GSeekable on resizable streams is approximately the same as POSIX
44 * lseek() on a normal file. Seeking past the end and writing data will
45 * usually cause the stream to resize by introducing zero bytes.
46 **/
48 typedef GSeekableIface GSeekableInterface;
49 G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
51 static void
52 g_seekable_default_init (GSeekableInterface *iface)
56 /**
57 * g_seekable_tell:
58 * @seekable: a #GSeekable.
60 * Tells the current position within the stream.
62 * Returns: the offset from the beginning of the buffer.
63 **/
64 goffset
65 g_seekable_tell (GSeekable *seekable)
67 GSeekableIface *iface;
69 g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
71 iface = G_SEEKABLE_GET_IFACE (seekable);
73 return (* iface->tell) (seekable);
76 /**
77 * g_seekable_can_seek:
78 * @seekable: a #GSeekable.
80 * Tests if the stream supports the #GSeekableIface.
82 * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
83 **/
84 gboolean
85 g_seekable_can_seek (GSeekable *seekable)
87 GSeekableIface *iface;
89 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
91 iface = G_SEEKABLE_GET_IFACE (seekable);
93 return (* iface->can_seek) (seekable);
96 /**
97 * g_seekable_seek:
98 * @seekable: a #GSeekable.
99 * @offset: a #goffset.
100 * @type: a #GSeekType.
101 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
102 * @error: a #GError location to store the error occurring, or %NULL to
103 * ignore.
105 * Seeks in the stream by the given @offset, modified by @type.
107 * Attempting to seek past the end of the stream will have different
108 * results depending on if the stream is fixed-sized or resizable. If
109 * the stream is resizable then seeking past the end and then writing
110 * will result in zeros filling the empty space. Seeking past the end
111 * of a resizable stream and reading will result in EOF. Seeking past
112 * the end of a fixed-sized stream will fail.
114 * Any operation that would result in a negative offset will fail.
116 * If @cancellable is not %NULL, then the operation can be cancelled by
117 * triggering the cancellable object from another thread. If the operation
118 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
120 * Returns: %TRUE if successful. If an error
121 * has occurred, this function will return %FALSE and set @error
122 * appropriately if present.
124 gboolean
125 g_seekable_seek (GSeekable *seekable,
126 goffset offset,
127 GSeekType type,
128 GCancellable *cancellable,
129 GError **error)
131 GSeekableIface *iface;
133 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
135 iface = G_SEEKABLE_GET_IFACE (seekable);
137 return (* iface->seek) (seekable, offset, type, cancellable, error);
141 * g_seekable_can_truncate:
142 * @seekable: a #GSeekable.
144 * Tests if the length of the stream can be adjusted with
145 * g_seekable_truncate().
147 * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
149 gboolean
150 g_seekable_can_truncate (GSeekable *seekable)
152 GSeekableIface *iface;
154 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
156 iface = G_SEEKABLE_GET_IFACE (seekable);
158 return (* iface->can_truncate) (seekable);
162 * g_seekable_truncate: (virtual truncate_fn)
163 * @seekable: a #GSeekable.
164 * @offset: new length for @seekable, in bytes.
165 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
166 * @error: a #GError location to store the error occurring, or %NULL to
167 * ignore.
169 * Sets the length of the stream to @offset. If the stream was previously
170 * larger than @offset, the extra data is discarded. If the stream was
171 * previouly shorter than @offset, it is extended with NUL ('\0') bytes.
173 * If @cancellable is not %NULL, then the operation can be cancelled by
174 * triggering the cancellable object from another thread. If the operation
175 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
176 * operation was partially finished when the operation was cancelled the
177 * partial result will be returned, without an error.
179 * Returns: %TRUE if successful. If an error
180 * has occurred, this function will return %FALSE and set @error
181 * appropriately if present.
183 gboolean
184 g_seekable_truncate (GSeekable *seekable,
185 goffset offset,
186 GCancellable *cancellable,
187 GError **error)
189 GSeekableIface *iface;
191 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
193 iface = G_SEEKABLE_GET_IFACE (seekable);
195 return (* iface->truncate_fn) (seekable, offset, cancellable, error);