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 "gfilterinputstream.h"
|
|
|
|
#include "ginputstream.h"
|
2009-01-21 15:09:56 +01:00
|
|
|
#include "gsimpleasyncresult.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:gfilterinputstream
|
2007-11-28 07:43:33 +01:00
|
|
|
* @short_description: Filter Input Stream
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2010-08-13 22:04:04 +02:00
|
|
|
* Base class for input stream implementations that perform some
|
|
|
|
* kind of filtering operation on a base stream. Typical examples
|
|
|
|
* of filtering operations are character set conversion, compression
|
|
|
|
* and byte order flipping.
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
2009-01-21 15:09:56 +01:00
|
|
|
PROP_BASE_STREAM,
|
|
|
|
PROP_CLOSE_BASE
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void g_filter_input_stream_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
|
|
|
static void g_filter_input_stream_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void g_filter_input_stream_finalize (GObject *object);
|
|
|
|
|
|
|
|
|
|
|
|
static gssize g_filter_input_stream_read (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static gssize g_filter_input_stream_skip (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
static gboolean g_filter_input_stream_close (GInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error);
|
|
|
|
|
2010-08-16 16:21:38 +02:00
|
|
|
G_DEFINE_ABSTRACT_TYPE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
#define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
|
|
|
|
G_TYPE_FILTER_INPUT_STREAM, GFilterInputStreamPrivate)
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gboolean close_base;
|
2009-12-07 22:00:51 +01:00
|
|
|
GAsyncReadyCallback outstanding_callback;
|
|
|
|
gpointer outstanding_user_data;
|
2009-01-21 15:09:56 +01:00
|
|
|
} GFilterInputStreamPrivate;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
GInputStreamClass *istream_class;
|
|
|
|
|
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
object_class->get_property = g_filter_input_stream_get_property;
|
|
|
|
object_class->set_property = g_filter_input_stream_set_property;
|
|
|
|
object_class->finalize = g_filter_input_stream_finalize;
|
|
|
|
|
|
|
|
istream_class = G_INPUT_STREAM_CLASS (klass);
|
2007-12-05 11:38:03 +01:00
|
|
|
istream_class->read_fn = g_filter_input_stream_read;
|
2007-11-26 17:13:05 +01:00
|
|
|
istream_class->skip = g_filter_input_stream_skip;
|
2007-12-05 11:38:03 +01:00
|
|
|
istream_class->close_fn = g_filter_input_stream_close;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
g_type_class_add_private (klass, sizeof (GFilterInputStreamPrivate));
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_BASE_STREAM,
|
|
|
|
g_param_spec_object ("base-stream",
|
|
|
|
P_("The Filter Base Stream"),
|
2009-02-06 15:08:19 +01:00
|
|
|
P_("The underlying base stream on which the io ops will be done."),
|
2007-11-26 17:13:05 +01:00
|
|
|
G_TYPE_INPUT_STREAM,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
|
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_CLOSE_BASE,
|
|
|
|
g_param_spec_boolean ("close-base-stream",
|
|
|
|
P_("Close Base Stream"),
|
2009-02-06 15:08:19 +01:00
|
|
|
P_("If the base stream should be closed when the filter stream is closed."),
|
2011-04-29 16:14:17 +02:00
|
|
|
TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
|
2009-01-21 15:09:56 +01:00
|
|
|
G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_filter_input_stream_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GFilterInputStream *filter_stream;
|
|
|
|
GObject *obj;
|
|
|
|
|
|
|
|
filter_stream = G_FILTER_INPUT_STREAM (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_BASE_STREAM:
|
|
|
|
obj = g_value_dup_object (value);
|
|
|
|
filter_stream->base_stream = G_INPUT_STREAM (obj);
|
|
|
|
break;
|
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
case PROP_CLOSE_BASE:
|
|
|
|
g_filter_input_stream_set_close_base_stream (filter_stream,
|
|
|
|
g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_filter_input_stream_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GFilterInputStream *filter_stream;
|
|
|
|
|
|
|
|
filter_stream = G_FILTER_INPUT_STREAM (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_BASE_STREAM:
|
|
|
|
g_value_set_object (value, filter_stream->base_stream);
|
|
|
|
break;
|
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
case PROP_CLOSE_BASE:
|
|
|
|
g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
|
|
|
|
break;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_filter_input_stream_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GFilterInputStream *stream;
|
|
|
|
|
|
|
|
stream = G_FILTER_INPUT_STREAM (object);
|
|
|
|
|
|
|
|
g_object_unref (stream->base_stream);
|
|
|
|
|
2008-06-16 11:54:04 +02:00
|
|
|
G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_filter_input_stream_init (GFilterInputStream *stream)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_filter_input_stream_get_base_stream:
|
|
|
|
* @stream: a #GFilterInputStream.
|
|
|
|
*
|
2007-11-28 07:01:13 +01:00
|
|
|
* Gets the base stream for the filter stream.
|
|
|
|
*
|
2010-06-10 20:02:15 +02:00
|
|
|
* Returns: (transfer none): a #GInputStream.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
GInputStream *
|
|
|
|
g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
|
|
|
|
|
|
|
|
return stream->base_stream;
|
|
|
|
}
|
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
/**
|
|
|
|
* g_filter_input_stream_get_close_base_stream:
|
|
|
|
* @stream: a #GFilterInputStream.
|
|
|
|
*
|
|
|
|
* Returns whether the base stream will be closed when @stream is
|
|
|
|
* closed.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if the base stream will be closed.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_filter_input_stream_get_close_base_stream (GFilterInputStream *stream)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), FALSE);
|
|
|
|
|
|
|
|
return GET_PRIVATE (stream)->close_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_filter_input_stream_set_close_base_stream:
|
|
|
|
* @stream: a #GFilterInputStream.
|
|
|
|
* @close_base: %TRUE to close the base stream.
|
|
|
|
*
|
|
|
|
* Sets whether the base stream will be closed when @stream is closed.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
g_filter_input_stream_set_close_base_stream (GFilterInputStream *stream,
|
|
|
|
gboolean close_base)
|
|
|
|
{
|
|
|
|
GFilterInputStreamPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_FILTER_INPUT_STREAM (stream));
|
|
|
|
|
|
|
|
close_base = !!close_base;
|
|
|
|
|
|
|
|
priv = GET_PRIVATE (stream);
|
|
|
|
|
|
|
|
if (priv->close_base != close_base)
|
|
|
|
{
|
|
|
|
priv->close_base = close_base;
|
|
|
|
g_object_notify (G_OBJECT (stream), "close-base-stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static gssize
|
2007-11-29 08:17:59 +01:00
|
|
|
g_filter_input_stream_read (GInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GFilterInputStream *filter_stream;
|
|
|
|
GInputStream *base_stream;
|
|
|
|
gssize nread;
|
|
|
|
|
|
|
|
filter_stream = G_FILTER_INPUT_STREAM (stream);
|
|
|
|
base_stream = filter_stream->base_stream;
|
|
|
|
|
|
|
|
nread = g_input_stream_read (base_stream,
|
|
|
|
buffer,
|
|
|
|
count,
|
|
|
|
cancellable,
|
|
|
|
error);
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
2007-11-29 08:17:59 +01:00
|
|
|
g_filter_input_stream_skip (GInputStream *stream,
|
|
|
|
gsize count,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GFilterInputStream *filter_stream;
|
|
|
|
GInputStream *base_stream;
|
|
|
|
gssize nskipped;
|
|
|
|
|
|
|
|
filter_stream = G_FILTER_INPUT_STREAM (stream);
|
|
|
|
base_stream = filter_stream->base_stream;
|
|
|
|
|
|
|
|
nskipped = g_input_stream_skip (base_stream,
|
|
|
|
count,
|
|
|
|
cancellable,
|
|
|
|
error);
|
|
|
|
return nskipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_filter_input_stream_close (GInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
2009-01-21 15:09:56 +01:00
|
|
|
gboolean res = TRUE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
if (GET_PRIVATE (stream)->close_base)
|
|
|
|
{
|
|
|
|
GFilterInputStream *filter_stream;
|
|
|
|
GInputStream *base_stream;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2009-01-21 15:09:56 +01:00
|
|
|
filter_stream = G_FILTER_INPUT_STREAM (stream);
|
|
|
|
base_stream = filter_stream->base_stream;
|
|
|
|
|
|
|
|
res = g_input_stream_close (base_stream,
|
|
|
|
cancellable,
|
|
|
|
error);
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|