2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Author: Christian Kellner <gicmo@gnome.org>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gmemoryinputstream.h"
|
|
|
|
#include "ginputstream.h"
|
|
|
|
#include "gseekable.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "gsimpleasyncresult.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gioerror.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "glibintl.h"
|
|
|
|
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gmemoryinputstream
|
2007-12-01 05:38:29 +01:00
|
|
|
* @short_description: Streaming input operations on memory chunks
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-28 07:43:33 +01:00
|
|
|
* @see_also: #GMemoryOutputStream
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* #GMemoryInputStream is a class for using arbitrary
|
|
|
|
* memory chunks as input for GIO streaming input operations.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
typedef struct _Chunk Chunk;
|
|
|
|
|
|
|
|
struct _Chunk {
|
|
|
|
guint8 *data;
|
|
|
|
gsize len;
|
|
|
|
GDestroyNotify destroy;
|
|
|
|
};
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
struct _GMemoryInputStreamPrivate {
|
2008-01-04 11:17:57 +01:00
|
|
|
GSList *chunks;
|
2007-11-26 17:13:05 +01:00
|
|
|
gsize len;
|
2008-01-04 11:17:57 +01:00
|
|
|
gsize pos;
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static gssize g_memory_input_stream_read (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static gssize g_memory_input_stream_skip (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static gboolean g_memory_input_stream_close (GInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static void g_memory_input_stream_read_async (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data);
|
|
|
|
static gssize g_memory_input_stream_read_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error);
|
|
|
|
static void g_memory_input_stream_skip_async (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellabl,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer datae);
|
|
|
|
static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error);
|
|
|
|
static void g_memory_input_stream_close_async (GInputStream *stream,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellabl,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer data);
|
|
|
|
static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
|
|
|
|
static goffset g_memory_input_stream_tell (GSeekable *seekable);
|
|
|
|
static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
|
|
|
|
static gboolean g_memory_input_stream_seek (GSeekable *seekable,
|
|
|
|
goffset offset,
|
|
|
|
GSeekType type,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
|
|
|
|
static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
|
|
|
|
goffset offset,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static void g_memory_input_stream_finalize (GObject *object);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
|
|
|
|
g_memory_input_stream_seekable_iface_init))
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
GInputStreamClass *istream_class;
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
|
|
|
|
|
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
object_class->finalize = g_memory_input_stream_finalize;
|
|
|
|
|
|
|
|
istream_class = G_INPUT_STREAM_CLASS (klass);
|
2007-12-05 11:38:03 +01:00
|
|
|
istream_class->read_fn = g_memory_input_stream_read;
|
2007-11-26 17:13:05 +01:00
|
|
|
istream_class->skip = g_memory_input_stream_skip;
|
2007-12-05 11:38:03 +01:00
|
|
|
istream_class->close_fn = g_memory_input_stream_close;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
istream_class->read_async = g_memory_input_stream_read_async;
|
|
|
|
istream_class->read_finish = g_memory_input_stream_read_finish;
|
|
|
|
istream_class->skip_async = g_memory_input_stream_skip_async;
|
|
|
|
istream_class->skip_finish = g_memory_input_stream_skip_finish;
|
|
|
|
istream_class->close_async = g_memory_input_stream_close_async;
|
|
|
|
istream_class->close_finish = g_memory_input_stream_close_finish;
|
|
|
|
}
|
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
static void
|
|
|
|
free_chunk (gpointer data,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
Chunk *chunk = data;
|
|
|
|
|
|
|
|
if (chunk->destroy)
|
|
|
|
chunk->destroy (chunk->data);
|
|
|
|
|
|
|
|
g_slice_free (Chunk, chunk);
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
|
|
|
g_memory_input_stream_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GMemoryInputStream *stream;
|
2008-01-04 11:17:57 +01:00
|
|
|
GMemoryInputStreamPrivate *priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
stream = G_MEMORY_INPUT_STREAM (object);
|
2008-01-04 11:17:57 +01:00
|
|
|
priv = stream->priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
g_slist_foreach (priv->chunks, free_chunk, NULL);
|
|
|
|
g_slist_free (priv->chunks);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-06-16 11:54:04 +02:00
|
|
|
G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
|
|
|
|
{
|
|
|
|
iface->tell = g_memory_input_stream_tell;
|
|
|
|
iface->can_seek = g_memory_input_stream_can_seek;
|
|
|
|
iface->seek = g_memory_input_stream_seek;
|
|
|
|
iface->can_truncate = g_memory_input_stream_can_truncate;
|
2007-12-05 11:38:03 +01:00
|
|
|
iface->truncate_fn = g_memory_input_stream_truncate;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_memory_input_stream_init (GMemoryInputStream *stream)
|
|
|
|
{
|
|
|
|
stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
|
|
|
|
G_TYPE_MEMORY_INPUT_STREAM,
|
|
|
|
GMemoryInputStreamPrivate);
|
|
|
|
}
|
|
|
|
|
2008-01-21 04:49:20 +01:00
|
|
|
/**
|
|
|
|
* g_memory_input_stream_new:
|
|
|
|
*
|
|
|
|
* Creates a new empty #GMemoryInputStream.
|
|
|
|
*
|
|
|
|
* Returns: a new #GInputStream
|
|
|
|
*/
|
2008-01-04 11:17:57 +01:00
|
|
|
GInputStream *
|
|
|
|
g_memory_input_stream_new (void)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-04 11:17:57 +01:00
|
|
|
GInputStream *stream;
|
|
|
|
|
|
|
|
stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
return stream;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-04 11:17:57 +01:00
|
|
|
* g_memory_input_stream_new_from_data:
|
2010-12-27 16:47:26 +01:00
|
|
|
* @data: (array length=len) (element-type guint8): input data
|
2008-01-04 11:17:57 +01:00
|
|
|
* @len: length of the data, may be -1 if @data is a nul-terminated string
|
2010-12-27 16:47:26 +01:00
|
|
|
* @destroy: (allow-none): function that is called to free @data, or %NULL
|
2007-12-09 16:51:12 +01:00
|
|
|
*
|
|
|
|
* Creates a new #GMemoryInputStream with data in memory of a given size.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Returns: new #GInputStream read from @data of @len bytes.
|
|
|
|
**/
|
|
|
|
GInputStream *
|
2008-01-04 11:17:57 +01:00
|
|
|
g_memory_input_stream_new_from_data (const void *data,
|
|
|
|
gssize len,
|
|
|
|
GDestroyNotify destroy)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GInputStream *stream;
|
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
stream = g_memory_input_stream_new ();
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2011-09-16 16:09:56 +02:00
|
|
|
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
|
|
|
|
data, len, destroy);
|
2008-01-04 11:17:57 +01:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2008-01-21 04:49:20 +01:00
|
|
|
/**
|
|
|
|
* g_memory_input_stream_add_data:
|
|
|
|
* @stream: a #GMemoryInputStream
|
2010-12-27 16:47:26 +01:00
|
|
|
* @data: (array length=len) (element-type guint8): input data
|
2008-01-21 04:49:20 +01:00
|
|
|
* @len: length of the data, may be -1 if @data is a nul-terminated string
|
2010-12-27 16:47:26 +01:00
|
|
|
* @destroy: (allow-none): function that is called to free @data, or %NULL
|
2008-01-21 04:49:20 +01:00
|
|
|
*
|
|
|
|
* Appends @data to data that can be read from the input stream
|
|
|
|
*/
|
2008-01-04 11:17:57 +01:00
|
|
|
void
|
|
|
|
g_memory_input_stream_add_data (GMemoryInputStream *stream,
|
|
|
|
const void *data,
|
|
|
|
gssize len,
|
|
|
|
GDestroyNotify destroy)
|
|
|
|
{
|
|
|
|
GMemoryInputStreamPrivate *priv;
|
|
|
|
Chunk *chunk;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
|
|
|
|
g_return_if_fail (data != NULL);
|
|
|
|
|
|
|
|
priv = stream->priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
if (len == -1)
|
|
|
|
len = strlen (data);
|
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
chunk = g_slice_new (Chunk);
|
|
|
|
chunk->data = (guint8 *)data;
|
|
|
|
chunk->len = len;
|
|
|
|
chunk->destroy = destroy;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
priv->chunks = g_slist_append (priv->chunks, chunk);
|
|
|
|
priv->len += chunk->len;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_read (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GMemoryInputStream *memory_stream;
|
2008-01-04 11:17:57 +01:00
|
|
|
GMemoryInputStreamPrivate *priv;
|
|
|
|
GSList *l;
|
|
|
|
Chunk *chunk;
|
|
|
|
gsize offset, start, rest, size;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
memory_stream = G_MEMORY_INPUT_STREAM (stream);
|
|
|
|
priv = memory_stream->priv;
|
|
|
|
|
|
|
|
count = MIN (count, priv->len - priv->pos);
|
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
offset = 0;
|
|
|
|
for (l = priv->chunks; l; l = l->next)
|
|
|
|
{
|
|
|
|
chunk = (Chunk *)l->data;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
if (offset + chunk->len > priv->pos)
|
|
|
|
break;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
offset += chunk->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = priv->pos - offset;
|
|
|
|
rest = count;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
for (; l && rest > 0; l = l->next)
|
|
|
|
{
|
|
|
|
chunk = (Chunk *)l->data;
|
|
|
|
size = MIN (rest, chunk->len - start);
|
|
|
|
|
2008-01-15 12:47:04 +01:00
|
|
|
memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
|
2008-01-04 11:17:57 +01:00
|
|
|
rest -= size;
|
|
|
|
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->pos += count;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-04 11:17:57 +01:00
|
|
|
return count;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_skip (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GMemoryInputStream *memory_stream;
|
|
|
|
GMemoryInputStreamPrivate *priv;
|
|
|
|
|
|
|
|
memory_stream = G_MEMORY_INPUT_STREAM (stream);
|
|
|
|
priv = memory_stream->priv;
|
|
|
|
|
|
|
|
count = MIN (count, priv->len - priv->pos);
|
|
|
|
priv->pos += count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_close (GInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_read_async (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *simple;
|
2011-12-01 13:10:25 +01:00
|
|
|
GError *error = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
gssize nread;
|
|
|
|
|
2011-12-01 13:10:25 +01:00
|
|
|
nread = G_INPUT_STREAM_GET_CLASS (stream)->read_fn (stream,
|
|
|
|
buffer,
|
|
|
|
count,
|
|
|
|
cancellable,
|
|
|
|
&error);
|
2007-11-26 17:13:05 +01:00
|
|
|
simple = g_simple_async_result_new (G_OBJECT (stream),
|
|
|
|
callback,
|
|
|
|
user_data,
|
|
|
|
g_memory_input_stream_read_async);
|
2011-12-01 13:10:25 +01:00
|
|
|
if (error)
|
|
|
|
g_simple_async_result_take_error (simple, error);
|
|
|
|
else
|
|
|
|
g_simple_async_result_set_op_res_gssize (simple, nread);
|
2007-11-26 17:13:05 +01:00
|
|
|
g_simple_async_result_complete_in_idle (simple);
|
|
|
|
g_object_unref (simple);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_read_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *simple;
|
|
|
|
gssize nread;
|
|
|
|
|
|
|
|
simple = G_SIMPLE_ASYNC_RESULT (result);
|
2007-12-10 15:07:42 +01:00
|
|
|
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
nread = g_simple_async_result_get_op_res_gssize (simple);
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_skip_async (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *simple;
|
|
|
|
gssize nskipped;
|
|
|
|
|
2011-11-30 10:32:42 +01:00
|
|
|
nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
|
2007-11-26 17:13:05 +01:00
|
|
|
simple = g_simple_async_result_new (G_OBJECT (stream),
|
2007-11-30 06:11:25 +01:00
|
|
|
callback,
|
|
|
|
user_data,
|
|
|
|
g_memory_input_stream_skip_async);
|
2007-11-26 17:13:05 +01:00
|
|
|
g_simple_async_result_set_op_res_gssize (simple, nskipped);
|
|
|
|
g_simple_async_result_complete_in_idle (simple);
|
|
|
|
g_object_unref (simple);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_skip_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *simple;
|
|
|
|
gssize nskipped;
|
|
|
|
|
|
|
|
simple = G_SIMPLE_ASYNC_RESULT (result);
|
2007-12-10 15:07:42 +01:00
|
|
|
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
nskipped = g_simple_async_result_get_op_res_gssize (simple);
|
|
|
|
return nskipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_close_async (GInputStream *stream,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *simple;
|
|
|
|
|
|
|
|
simple = g_simple_async_result_new (G_OBJECT (stream),
|
|
|
|
callback,
|
|
|
|
user_data,
|
|
|
|
g_memory_input_stream_close_async);
|
|
|
|
g_simple_async_result_complete_in_idle (simple);
|
|
|
|
g_object_unref (simple);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_close_finish (GInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static goffset
|
|
|
|
g_memory_input_stream_tell (GSeekable *seekable)
|
|
|
|
{
|
|
|
|
GMemoryInputStream *memory_stream;
|
2008-01-04 11:17:57 +01:00
|
|
|
GMemoryInputStreamPrivate *priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
memory_stream = G_MEMORY_INPUT_STREAM (seekable);
|
|
|
|
priv = memory_stream->priv;
|
|
|
|
|
|
|
|
return priv->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_seek (GSeekable *seekable,
|
|
|
|
goffset offset,
|
|
|
|
GSeekType type,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GMemoryInputStream *memory_stream;
|
2008-01-04 11:17:57 +01:00
|
|
|
GMemoryInputStreamPrivate *priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
goffset absolute;
|
|
|
|
|
|
|
|
memory_stream = G_MEMORY_INPUT_STREAM (seekable);
|
|
|
|
priv = memory_stream->priv;
|
|
|
|
|
2007-11-30 06:11:25 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
2007-11-26 17:13:05 +01:00
|
|
|
case G_SEEK_CUR:
|
|
|
|
absolute = priv->pos + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_SEEK_SET:
|
|
|
|
absolute = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_SEEK_END:
|
|
|
|
absolute = priv->len + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Invalid GSeekType supplied"));
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
return FALSE;
|
2007-11-30 06:11:25 +01:00
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
if (absolute < 0 || absolute > priv->len)
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Invalid seek request"));
|
2007-11-26 17:13:05 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->pos = absolute;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_memory_input_stream_can_truncate (GSeekable *seekable)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-30 06:11:25 +01:00
|
|
|
g_memory_input_stream_truncate (GSeekable *seekable,
|
|
|
|
goffset offset,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
_("Cannot truncate GMemoryInputStream"));
|
2007-11-26 17:13:05 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|