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