Add some more cases to the app-id unit tests
[glib.git] / gio / gseekable.c
blobc1f6789708a545885dcae316d4d99e17ca412e2f
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 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 stream can be truncated.
146 * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
148 gboolean
149 g_seekable_can_truncate (GSeekable *seekable)
151 GSeekableIface *iface;
153 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
155 iface = G_SEEKABLE_GET_IFACE (seekable);
157 return (* iface->can_truncate) (seekable);
161 * g_seekable_truncate:
162 * @seekable: a #GSeekable.
163 * @offset: a #goffset.
164 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
165 * @error: a #GError location to store the error occurring, or %NULL to
166 * ignore.
168 * Truncates a stream with a given #offset.
170 * If @cancellable is not %NULL, then the operation can be cancelled by
171 * triggering the cancellable object from another thread. If the operation
172 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
173 * operation was partially finished when the operation was cancelled the
174 * partial result will be returned, without an error.
176 * Virtual: truncate_fn
177 * Returns: %TRUE if successful. If an error
178 * has occurred, this function will return %FALSE and set @error
179 * appropriately if present.
181 gboolean
182 g_seekable_truncate (GSeekable *seekable,
183 goffset offset,
184 GCancellable *cancellable,
185 GError **error)
187 GSeekableIface *iface;
189 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
191 iface = G_SEEKABLE_GET_IFACE (seekable);
193 return (* iface->truncate_fn) (seekable, offset, cancellable, error);