regex: unicode: Update to Unicode 6.1.0
[glib.git] / gio / gfilterinputstream.c
blob890fb6e65050ee1b4e230c8d47e36b4aa4b32f7d
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Christian Kellner <gicmo@gnome.org>
23 #include "config.h"
24 #include "gfilterinputstream.h"
25 #include "ginputstream.h"
26 #include "glibintl.h"
29 /**
30 * SECTION:gfilterinputstream
31 * @short_description: Filter Input Stream
32 * @include: gio/gio.h
34 * Base class for input stream implementations that perform some
35 * kind of filtering operation on a base stream. Typical examples
36 * of filtering operations are character set conversion, compression
37 * and byte order flipping.
38 **/
40 enum {
41 PROP_0,
42 PROP_BASE_STREAM,
43 PROP_CLOSE_BASE
46 static void g_filter_input_stream_set_property (GObject *object,
47 guint prop_id,
48 const GValue *value,
49 GParamSpec *pspec);
51 static void g_filter_input_stream_get_property (GObject *object,
52 guint prop_id,
53 GValue *value,
54 GParamSpec *pspec);
55 static void g_filter_input_stream_finalize (GObject *object);
58 static gssize g_filter_input_stream_read (GInputStream *stream,
59 void *buffer,
60 gsize count,
61 GCancellable *cancellable,
62 GError **error);
63 static gssize g_filter_input_stream_skip (GInputStream *stream,
64 gsize count,
65 GCancellable *cancellable,
66 GError **error);
67 static gboolean g_filter_input_stream_close (GInputStream *stream,
68 GCancellable *cancellable,
69 GError **error);
71 G_DEFINE_ABSTRACT_TYPE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
73 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
74 G_TYPE_FILTER_INPUT_STREAM, GFilterInputStreamPrivate)
76 typedef struct
78 gboolean close_base;
79 } GFilterInputStreamPrivate;
81 static void
82 g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
84 GObjectClass *object_class;
85 GInputStreamClass *istream_class;
87 object_class = G_OBJECT_CLASS (klass);
88 object_class->get_property = g_filter_input_stream_get_property;
89 object_class->set_property = g_filter_input_stream_set_property;
90 object_class->finalize = g_filter_input_stream_finalize;
92 istream_class = G_INPUT_STREAM_CLASS (klass);
93 istream_class->read_fn = g_filter_input_stream_read;
94 istream_class->skip = g_filter_input_stream_skip;
95 istream_class->close_fn = g_filter_input_stream_close;
97 g_type_class_add_private (klass, sizeof (GFilterInputStreamPrivate));
99 g_object_class_install_property (object_class,
100 PROP_BASE_STREAM,
101 g_param_spec_object ("base-stream",
102 P_("The Filter Base Stream"),
103 P_("The underlying base stream on which the io ops will be done."),
104 G_TYPE_INPUT_STREAM,
105 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
106 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
108 g_object_class_install_property (object_class,
109 PROP_CLOSE_BASE,
110 g_param_spec_boolean ("close-base-stream",
111 P_("Close Base Stream"),
112 P_("If the base stream should be closed when the filter stream is closed."),
113 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
114 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
117 static void
118 g_filter_input_stream_set_property (GObject *object,
119 guint prop_id,
120 const GValue *value,
121 GParamSpec *pspec)
123 GFilterInputStream *filter_stream;
124 GObject *obj;
126 filter_stream = G_FILTER_INPUT_STREAM (object);
128 switch (prop_id)
130 case PROP_BASE_STREAM:
131 obj = g_value_dup_object (value);
132 filter_stream->base_stream = G_INPUT_STREAM (obj);
133 break;
135 case PROP_CLOSE_BASE:
136 g_filter_input_stream_set_close_base_stream (filter_stream,
137 g_value_get_boolean (value));
138 break;
140 default:
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 break;
147 static void
148 g_filter_input_stream_get_property (GObject *object,
149 guint prop_id,
150 GValue *value,
151 GParamSpec *pspec)
153 GFilterInputStream *filter_stream;
155 filter_stream = G_FILTER_INPUT_STREAM (object);
157 switch (prop_id)
159 case PROP_BASE_STREAM:
160 g_value_set_object (value, filter_stream->base_stream);
161 break;
163 case PROP_CLOSE_BASE:
164 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
165 break;
167 default:
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 break;
174 static void
175 g_filter_input_stream_finalize (GObject *object)
177 GFilterInputStream *stream;
179 stream = G_FILTER_INPUT_STREAM (object);
181 g_object_unref (stream->base_stream);
183 G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
186 static void
187 g_filter_input_stream_init (GFilterInputStream *stream)
193 * g_filter_input_stream_get_base_stream:
194 * @stream: a #GFilterInputStream.
196 * Gets the base stream for the filter stream.
198 * Returns: (transfer none): a #GInputStream.
200 GInputStream *
201 g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
203 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
205 return stream->base_stream;
209 * g_filter_input_stream_get_close_base_stream:
210 * @stream: a #GFilterInputStream.
212 * Returns whether the base stream will be closed when @stream is
213 * closed.
215 * Return value: %TRUE if the base stream will be closed.
217 gboolean
218 g_filter_input_stream_get_close_base_stream (GFilterInputStream *stream)
220 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), FALSE);
222 return GET_PRIVATE (stream)->close_base;
226 * g_filter_input_stream_set_close_base_stream:
227 * @stream: a #GFilterInputStream.
228 * @close_base: %TRUE to close the base stream.
230 * Sets whether the base stream will be closed when @stream is closed.
232 void
233 g_filter_input_stream_set_close_base_stream (GFilterInputStream *stream,
234 gboolean close_base)
236 GFilterInputStreamPrivate *priv;
238 g_return_if_fail (G_IS_FILTER_INPUT_STREAM (stream));
240 close_base = !!close_base;
242 priv = GET_PRIVATE (stream);
244 if (priv->close_base != close_base)
246 priv->close_base = close_base;
247 g_object_notify (G_OBJECT (stream), "close-base-stream");
251 static gssize
252 g_filter_input_stream_read (GInputStream *stream,
253 void *buffer,
254 gsize count,
255 GCancellable *cancellable,
256 GError **error)
258 GFilterInputStream *filter_stream;
259 GInputStream *base_stream;
260 gssize nread;
262 filter_stream = G_FILTER_INPUT_STREAM (stream);
263 base_stream = filter_stream->base_stream;
265 nread = g_input_stream_read (base_stream,
266 buffer,
267 count,
268 cancellable,
269 error);
271 return nread;
274 static gssize
275 g_filter_input_stream_skip (GInputStream *stream,
276 gsize count,
277 GCancellable *cancellable,
278 GError **error)
280 GFilterInputStream *filter_stream;
281 GInputStream *base_stream;
282 gssize nskipped;
284 filter_stream = G_FILTER_INPUT_STREAM (stream);
285 base_stream = filter_stream->base_stream;
287 nskipped = g_input_stream_skip (base_stream,
288 count,
289 cancellable,
290 error);
291 return nskipped;
294 static gboolean
295 g_filter_input_stream_close (GInputStream *stream,
296 GCancellable *cancellable,
297 GError **error)
299 gboolean res = TRUE;
301 if (GET_PRIVATE (stream)->close_base)
303 GFilterInputStream *filter_stream;
304 GInputStream *base_stream;
306 filter_stream = G_FILTER_INPUT_STREAM (stream);
307 base_stream = filter_stream->base_stream;
309 res = g_input_stream_close (base_stream,
310 cancellable,
311 error);
314 return res;