mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-20 07:38:54 +02:00
Make GDataOutputStream implement GSeekable
https://bugzilla.gnome.org/show_bug.cgi?id=673034
This commit is contained in:
committed by
Alexander Larsson
parent
43895e3089
commit
a44e801983
@@ -23,7 +23,9 @@
|
||||
#include "config.h"
|
||||
#include <string.h>
|
||||
#include "gdataoutputstream.h"
|
||||
#include "gseekable.h"
|
||||
#include "gioenumtypes.h"
|
||||
#include "gioerror.h"
|
||||
#include "glibintl.h"
|
||||
|
||||
|
||||
@@ -58,9 +60,25 @@ static void g_data_output_stream_get_property (GObject *object,
|
||||
GValue *value,
|
||||
GParamSpec *pspec);
|
||||
|
||||
G_DEFINE_TYPE (GDataOutputStream,
|
||||
g_data_output_stream,
|
||||
G_TYPE_FILTER_OUTPUT_STREAM)
|
||||
static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
|
||||
static goffset g_data_output_stream_tell (GSeekable *seekable);
|
||||
static gboolean g_data_output_stream_can_seek (GSeekable *seekable);
|
||||
static gboolean g_data_output_stream_seek (GSeekable *seekable,
|
||||
goffset offset,
|
||||
GSeekType type,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
static gboolean g_data_output_stream_can_truncate (GSeekable *seekable);
|
||||
static gboolean g_data_output_stream_truncate (GSeekable *seekable,
|
||||
goffset offset,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (GDataOutputStream,
|
||||
g_data_output_stream,
|
||||
G_TYPE_FILTER_OUTPUT_STREAM,
|
||||
G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
|
||||
g_data_output_stream_seekable_iface_init))
|
||||
|
||||
|
||||
static void
|
||||
@@ -147,6 +165,16 @@ g_data_output_stream_init (GDataOutputStream *stream)
|
||||
stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
|
||||
}
|
||||
|
||||
static void
|
||||
g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
|
||||
{
|
||||
iface->tell = g_data_output_stream_tell;
|
||||
iface->can_seek = g_data_output_stream_can_seek;
|
||||
iface->seek = g_data_output_stream_seek;
|
||||
iface->can_truncate = g_data_output_stream_can_truncate;
|
||||
iface->truncate_fn = g_data_output_stream_truncate;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_data_output_stream_new:
|
||||
* @base_stream: a #GOutputStream.
|
||||
@@ -500,3 +528,77 @@ g_data_output_stream_put_string (GDataOutputStream *stream,
|
||||
&bytes_written,
|
||||
cancellable, error);
|
||||
}
|
||||
|
||||
static goffset
|
||||
g_data_output_stream_tell (GSeekable *seekable)
|
||||
{
|
||||
GOutputStream *base_stream;
|
||||
GSeekable *base_stream_seekable;
|
||||
|
||||
base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
|
||||
if (!G_IS_SEEKABLE (base_stream))
|
||||
return 0;
|
||||
base_stream_seekable = G_SEEKABLE (base_stream);
|
||||
return g_seekable_tell (base_stream_seekable);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_data_output_stream_can_seek (GSeekable *seekable)
|
||||
{
|
||||
GOutputStream *base_stream;
|
||||
|
||||
base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
|
||||
return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_data_output_stream_seek (GSeekable *seekable,
|
||||
goffset offset,
|
||||
GSeekType type,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
GOutputStream *base_stream;
|
||||
GSeekable *base_stream_seekable;
|
||||
|
||||
base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
|
||||
if (!G_IS_SEEKABLE (base_stream))
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
_("Seek not supported on base stream"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
base_stream_seekable = G_SEEKABLE (base_stream);
|
||||
return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_data_output_stream_can_truncate (GSeekable *seekable)
|
||||
{
|
||||
GOutputStream *base_stream;
|
||||
|
||||
base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
|
||||
return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_data_output_stream_truncate (GSeekable *seekable,
|
||||
goffset offset,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
GOutputStream *base_stream;
|
||||
GSeekable *base_stream_seekable;
|
||||
|
||||
base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
|
||||
if (!G_IS_SEEKABLE (base_stream))
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
_("Truncate not supported on base stream"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
base_stream_seekable = G_SEEKABLE (base_stream);
|
||||
return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);
|
||||
}
|
||||
|
Reference in New Issue
Block a user