Blame gio/gseekable.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gseekable.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gseekable
Packit ae235b
 * @short_description: Stream seeking interface
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GInputStream, #GOutputStream
Packit ae235b
 *
Packit ae235b
 * #GSeekable is implemented by streams (implementations of
Packit ae235b
 * #GInputStream or #GOutputStream) that support seeking.
Packit ae235b
 *
Packit ae235b
 * Seekable streams largely fall into two categories: resizable and
Packit ae235b
 * fixed-size.
Packit ae235b
 *
Packit ae235b
 * #GSeekable on fixed-sized streams is approximately the same as POSIX
Packit ae235b
 * lseek() on a block device (for example: attmepting to seek past the
Packit ae235b
 * end of the device is an error).  Fixed streams typically cannot be
Packit ae235b
 * truncated.
Packit ae235b
 *
Packit ae235b
 * #GSeekable on resizable streams is approximately the same as POSIX
Packit ae235b
 * lseek() on a normal file.  Seeking past the end and writing data will
Packit ae235b
 * usually cause the stream to resize by introducing zero bytes.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
typedef GSeekableIface GSeekableInterface;
Packit ae235b
G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_seekable_default_init (GSeekableInterface *iface)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_seekable_tell:
Packit ae235b
 * @seekable: a #GSeekable.
Packit ae235b
 * 
Packit ae235b
 * Tells the current position within the stream.
Packit ae235b
 * 
Packit ae235b
 * Returns: the offset from the beginning of the buffer.
Packit ae235b
 **/
Packit ae235b
goffset
Packit ae235b
g_seekable_tell (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  GSeekableIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
Packit ae235b
Packit ae235b
  iface = G_SEEKABLE_GET_IFACE (seekable);
Packit ae235b
Packit ae235b
  return (* iface->tell) (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_seekable_can_seek:
Packit ae235b
 * @seekable: a #GSeekable.
Packit ae235b
 * 
Packit ae235b
 * Tests if the stream supports the #GSeekableIface.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_seekable_can_seek (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  GSeekableIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
Packit ae235b
Packit ae235b
  iface = G_SEEKABLE_GET_IFACE (seekable);
Packit ae235b
Packit ae235b
  return (* iface->can_seek) (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_seekable_seek:
Packit ae235b
 * @seekable: a #GSeekable.
Packit ae235b
 * @offset: a #goffset.
Packit ae235b
 * @type: a #GSeekType.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 *
Packit ae235b
 * Seeks in the stream by the given @offset, modified by @type.
Packit ae235b
 *
Packit ae235b
 * Attempting to seek past the end of the stream will have different
Packit ae235b
 * results depending on if the stream is fixed-sized or resizable.  If
Packit ae235b
 * the stream is resizable then seeking past the end and then writing
Packit ae235b
 * will result in zeros filling the empty space.  Seeking past the end
Packit ae235b
 * of a resizable stream and reading will result in EOF.  Seeking past
Packit ae235b
 * the end of a fixed-sized stream will fail.
Packit ae235b
 *
Packit ae235b
 * Any operation that would result in a negative offset will fail.
Packit ae235b
 *
Packit ae235b
 * If @cancellable is not %NULL, then the operation can be cancelled by
Packit ae235b
 * triggering the cancellable object from another thread. If the operation
Packit ae235b
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if successful. If an error
Packit ae235b
 *     has occurred, this function will return %FALSE and set @error
Packit ae235b
 *     appropriately if present.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_seekable_seek (GSeekable     *seekable,
Packit ae235b
		 goffset        offset,
Packit ae235b
		 GSeekType      type,
Packit ae235b
		 GCancellable  *cancellable,
Packit ae235b
		 GError       **error)
Packit ae235b
{
Packit ae235b
  GSeekableIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
Packit ae235b
Packit ae235b
  iface = G_SEEKABLE_GET_IFACE (seekable);
Packit ae235b
Packit ae235b
  return (* iface->seek) (seekable, offset, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_seekable_can_truncate:
Packit ae235b
 * @seekable: a #GSeekable.
Packit ae235b
 * 
Packit ae235b
 * Tests if the length of the stream can be adjusted with
Packit ae235b
 * g_seekable_truncate().
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_seekable_can_truncate (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  GSeekableIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
Packit ae235b
Packit ae235b
  iface = G_SEEKABLE_GET_IFACE (seekable);
Packit ae235b
Packit ae235b
  return (* iface->can_truncate) (seekable);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_seekable_truncate: (virtual truncate_fn)
Packit ae235b
 * @seekable: a #GSeekable.
Packit ae235b
 * @offset: new length for @seekable, in bytes.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 * 
Packit ae235b
 * Sets the length of the stream to @offset. If the stream was previously
Packit ae235b
 * larger than @offset, the extra data is discarded. If the stream was
Packit ae235b
 * previouly shorter than @offset, it is extended with NUL ('\0') bytes.
Packit ae235b
 * 
Packit ae235b
 * If @cancellable is not %NULL, then the operation can be cancelled by
Packit ae235b
 * triggering the cancellable object from another thread. If the operation
Packit ae235b
 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
Packit ae235b
 * operation was partially finished when the operation was cancelled the
Packit ae235b
 * partial result will be returned, without an error.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if successful. If an error
Packit ae235b
 *     has occurred, this function will return %FALSE and set @error
Packit ae235b
 *     appropriately if present. 
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_seekable_truncate (GSeekable     *seekable,
Packit ae235b
		     goffset        offset,
Packit ae235b
		     GCancellable  *cancellable,
Packit ae235b
		     GError       **error)
Packit ae235b
{
Packit ae235b
  GSeekableIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
Packit ae235b
Packit ae235b
  iface = G_SEEKABLE_GET_IFACE (seekable);
Packit ae235b
Packit ae235b
  return (* iface->truncate_fn) (seekable, offset, cancellable, error);
Packit ae235b
}