mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-15 03:58:04 +02:00
Make GDataOutputStream implement GSeekable
https://bugzilla.gnome.org/show_bug.cgi?id=673034
This commit is contained in:
parent
43895e3089
commit
a44e801983
@ -23,7 +23,9 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "gdataoutputstream.h"
|
#include "gdataoutputstream.h"
|
||||||
|
#include "gseekable.h"
|
||||||
#include "gioenumtypes.h"
|
#include "gioenumtypes.h"
|
||||||
|
#include "gioerror.h"
|
||||||
#include "glibintl.h"
|
#include "glibintl.h"
|
||||||
|
|
||||||
|
|
||||||
@ -58,9 +60,25 @@ static void g_data_output_stream_get_property (GObject *object,
|
|||||||
GValue *value,
|
GValue *value,
|
||||||
GParamSpec *pspec);
|
GParamSpec *pspec);
|
||||||
|
|
||||||
G_DEFINE_TYPE (GDataOutputStream,
|
static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
|
||||||
g_data_output_stream,
|
static goffset g_data_output_stream_tell (GSeekable *seekable);
|
||||||
G_TYPE_FILTER_OUTPUT_STREAM)
|
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
|
static void
|
||||||
@ -147,6 +165,16 @@ g_data_output_stream_init (GDataOutputStream *stream)
|
|||||||
stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
|
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:
|
* g_data_output_stream_new:
|
||||||
* @base_stream: a #GOutputStream.
|
* @base_stream: a #GOutputStream.
|
||||||
@ -500,3 +528,77 @@ g_data_output_stream_put_string (GDataOutputStream *stream,
|
|||||||
&bytes_written,
|
&bytes_written,
|
||||||
cancellable, error);
|
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);
|
||||||
|
}
|
||||||
|
@ -298,6 +298,198 @@ test_read_int (void)
|
|||||||
g_free (buffer);
|
g_free (buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_seek (void)
|
||||||
|
{
|
||||||
|
GDataOutputStream *stream;
|
||||||
|
GMemoryOutputStream *base_stream;
|
||||||
|
GSeekable *seekable;
|
||||||
|
GError *error;
|
||||||
|
guchar *stream_data;
|
||||||
|
gsize len;
|
||||||
|
gboolean res;
|
||||||
|
|
||||||
|
len = 8;
|
||||||
|
|
||||||
|
/* create objects */
|
||||||
|
stream_data = g_malloc0 (len);
|
||||||
|
base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, NULL, NULL));
|
||||||
|
stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
|
||||||
|
g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||||
|
seekable = G_SEEKABLE (stream);
|
||||||
|
g_assert (!g_seekable_can_truncate (seekable));
|
||||||
|
error = NULL;
|
||||||
|
|
||||||
|
/* Write */
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 0);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
|
||||||
|
|
||||||
|
/* Forward relative seek */
|
||||||
|
res = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0x89AB, NULL, &error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 8);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
g_assert_cmpint (stream_data[4], ==, 0x00);
|
||||||
|
g_assert_cmpint (stream_data[5], ==, 0x00);
|
||||||
|
g_assert_cmpint (stream_data[6], ==, 0x89);
|
||||||
|
g_assert_cmpint (stream_data[7], ==, 0xAB);
|
||||||
|
|
||||||
|
/* Backward relative seek */
|
||||||
|
res = g_seekable_seek (seekable, -3, G_SEEK_CUR, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 5);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0xCDEF, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 7);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
g_assert_cmpint (stream_data[4], ==, 0x00);
|
||||||
|
g_assert_cmpint (stream_data[5], ==, 0xCD);
|
||||||
|
g_assert_cmpint (stream_data[6], ==, 0xEF);
|
||||||
|
g_assert_cmpint (stream_data[7], ==, 0xAB);
|
||||||
|
|
||||||
|
/* From start */
|
||||||
|
res = g_seekable_seek (seekable, 4, G_SEEK_SET, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0xFEDC, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
g_assert_cmpint (stream_data[4], ==, 0xFE);
|
||||||
|
g_assert_cmpint (stream_data[5], ==, 0xDC);
|
||||||
|
g_assert_cmpint (stream_data[6], ==, 0xEF);
|
||||||
|
g_assert_cmpint (stream_data[7], ==, 0xAB);
|
||||||
|
|
||||||
|
/* From end */
|
||||||
|
res = g_seekable_seek (seekable, -4, G_SEEK_END, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0xBA87, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
g_assert_cmpint (stream_data[4], ==, 0xBA);
|
||||||
|
g_assert_cmpint (stream_data[5], ==, 0x87);
|
||||||
|
g_assert_cmpint (stream_data[6], ==, 0xEF);
|
||||||
|
g_assert_cmpint (stream_data[7], ==, 0xAB);
|
||||||
|
|
||||||
|
g_object_unref (stream);
|
||||||
|
g_object_unref (base_stream);
|
||||||
|
g_free (stream_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_truncate (void)
|
||||||
|
{
|
||||||
|
GDataOutputStream *stream;
|
||||||
|
GMemoryOutputStream *base_stream;
|
||||||
|
GSeekable *seekable;
|
||||||
|
GError *error;
|
||||||
|
guchar *stream_data;
|
||||||
|
gsize len;
|
||||||
|
gboolean res;
|
||||||
|
|
||||||
|
len = 8;
|
||||||
|
|
||||||
|
/* Create objects */
|
||||||
|
stream_data = g_malloc0 (len);
|
||||||
|
base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
|
||||||
|
stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
|
||||||
|
g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||||
|
seekable = G_SEEKABLE (stream);
|
||||||
|
error = NULL;
|
||||||
|
g_assert (g_seekable_can_truncate (seekable));
|
||||||
|
|
||||||
|
/* Write */
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
res = g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
|
||||||
|
/* Truncate at position */
|
||||||
|
res = g_seekable_truncate (seekable, 4, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
|
||||||
|
/* Truncate beyond position */
|
||||||
|
res = g_seekable_truncate (seekable, 6, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
g_assert_cmpint (stream_data[2], ==, 0x45);
|
||||||
|
g_assert_cmpint (stream_data[3], ==, 0x67);
|
||||||
|
|
||||||
|
/* Truncate before position */
|
||||||
|
res = g_seekable_truncate (seekable, 2, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (res);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
|
||||||
|
g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
|
||||||
|
g_assert_cmpint (stream_data[0], ==, 0x01);
|
||||||
|
g_assert_cmpint (stream_data[1], ==, 0x23);
|
||||||
|
|
||||||
|
g_object_unref (stream);
|
||||||
|
g_object_unref (base_stream);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc,
|
main (int argc,
|
||||||
char *argv[])
|
char *argv[])
|
||||||
@ -310,6 +502,8 @@ main (int argc,
|
|||||||
g_test_add_func ("/data-output-stream/write-lines-CR", test_read_lines_CR);
|
g_test_add_func ("/data-output-stream/write-lines-CR", test_read_lines_CR);
|
||||||
g_test_add_func ("/data-output-stream/write-lines-CR-LF", test_read_lines_CR_LF);
|
g_test_add_func ("/data-output-stream/write-lines-CR-LF", test_read_lines_CR_LF);
|
||||||
g_test_add_func ("/data-output-stream/write-int", test_read_int);
|
g_test_add_func ("/data-output-stream/write-int", test_read_int);
|
||||||
|
g_test_add_func ("/data-output-stream/seek", test_seek);
|
||||||
|
g_test_add_func ("/data-output-stream/truncate", test_truncate);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user