Add a test for the previous fix
[glib.git] / gio / gfilteroutputstream.c
blob6f32ce958f93d92e24e909a5985bd781510fb28e
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 "gfilteroutputstream.h"
25 #include "goutputstream.h"
26 #include "glibintl.h"
29 /**
30 * SECTION:gfilteroutputstream
31 * @short_description: Filter Output Stream
32 * @include: gio/gio.h
34 * Base class for output 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.
40 enum {
41 PROP_0,
42 PROP_BASE_STREAM,
43 PROP_CLOSE_BASE
46 static void g_filter_output_stream_set_property (GObject *object,
47 guint prop_id,
48 const GValue *value,
49 GParamSpec *pspec);
51 static void g_filter_output_stream_get_property (GObject *object,
52 guint prop_id,
53 GValue *value,
54 GParamSpec *pspec);
55 static void g_filter_output_stream_dispose (GObject *object);
58 static gssize g_filter_output_stream_write (GOutputStream *stream,
59 const void *buffer,
60 gsize count,
61 GCancellable *cancellable,
62 GError **error);
63 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
64 GCancellable *cancellable,
65 GError **error);
66 static gboolean g_filter_output_stream_close (GOutputStream *stream,
67 GCancellable *cancellable,
68 GError **error);
70 G_DEFINE_ABSTRACT_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
72 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
73 G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
75 typedef struct
77 gboolean close_base;
78 } GFilterOutputStreamPrivate;
80 static void
81 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
83 GObjectClass *object_class;
84 GOutputStreamClass *ostream_class;
86 object_class = G_OBJECT_CLASS (klass);
87 object_class->get_property = g_filter_output_stream_get_property;
88 object_class->set_property = g_filter_output_stream_set_property;
89 object_class->dispose = g_filter_output_stream_dispose;
91 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
92 ostream_class->write_fn = g_filter_output_stream_write;
93 ostream_class->flush = g_filter_output_stream_flush;
94 ostream_class->close_fn = g_filter_output_stream_close;
96 g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
98 g_object_class_install_property (object_class,
99 PROP_BASE_STREAM,
100 g_param_spec_object ("base-stream",
101 P_("The Filter Base Stream"),
102 P_("The underlying base stream on which the io ops will be done."),
103 G_TYPE_OUTPUT_STREAM,
104 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
105 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
107 g_object_class_install_property (object_class,
108 PROP_CLOSE_BASE,
109 g_param_spec_boolean ("close-base-stream",
110 P_("Close Base Stream"),
111 P_("If the base stream should be closed when the filter stream is closed."),
112 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
113 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
116 static void
117 g_filter_output_stream_set_property (GObject *object,
118 guint prop_id,
119 const GValue *value,
120 GParamSpec *pspec)
122 GFilterOutputStream *filter_stream;
123 GObject *obj;
125 filter_stream = G_FILTER_OUTPUT_STREAM (object);
127 switch (prop_id)
129 case PROP_BASE_STREAM:
130 obj = g_value_dup_object (value);
131 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
132 break;
134 case PROP_CLOSE_BASE:
135 g_filter_output_stream_set_close_base_stream (filter_stream,
136 g_value_get_boolean (value));
137 break;
139 default:
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 break;
146 static void
147 g_filter_output_stream_get_property (GObject *object,
148 guint prop_id,
149 GValue *value,
150 GParamSpec *pspec)
152 GFilterOutputStream *filter_stream;
154 filter_stream = G_FILTER_OUTPUT_STREAM (object);
156 switch (prop_id)
158 case PROP_BASE_STREAM:
159 g_value_set_object (value, filter_stream->base_stream);
160 break;
162 case PROP_CLOSE_BASE:
163 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
164 break;
166 default:
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 break;
173 static void
174 g_filter_output_stream_dispose (GObject *object)
176 GFilterOutputStream *stream;
178 stream = G_FILTER_OUTPUT_STREAM (object);
180 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
182 if (stream->base_stream)
184 g_object_unref (stream->base_stream);
185 stream->base_stream = NULL;
190 static void
191 g_filter_output_stream_init (GFilterOutputStream *stream)
196 * g_filter_output_stream_get_base_stream:
197 * @stream: a #GFilterOutputStream.
199 * Gets the base stream for the filter stream.
201 * Returns: (transfer none): a #GOutputStream.
203 GOutputStream *
204 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
206 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
208 return stream->base_stream;
212 * g_filter_output_stream_get_close_base_stream:
213 * @stream: a #GFilterOutputStream.
215 * Returns whether the base stream will be closed when @stream is
216 * closed.
218 * Return value: %TRUE if the base stream will be closed.
220 gboolean
221 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
223 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
225 return GET_PRIVATE (stream)->close_base;
229 * g_filter_output_stream_set_close_base_stream:
230 * @stream: a #GFilterOutputStream.
231 * @close_base: %TRUE to close the base stream.
233 * Sets whether the base stream will be closed when @stream is closed.
235 void
236 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
237 gboolean close_base)
239 GFilterOutputStreamPrivate *priv;
241 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
243 close_base = !!close_base;
245 priv = GET_PRIVATE (stream);
247 if (priv->close_base != close_base)
249 priv->close_base = close_base;
250 g_object_notify (G_OBJECT (stream), "close-base-stream");
254 static gssize
255 g_filter_output_stream_write (GOutputStream *stream,
256 const void *buffer,
257 gsize count,
258 GCancellable *cancellable,
259 GError **error)
261 GFilterOutputStream *filter_stream;
262 gssize nwritten;
264 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
266 nwritten = g_output_stream_write (filter_stream->base_stream,
267 buffer,
268 count,
269 cancellable,
270 error);
272 return nwritten;
275 static gboolean
276 g_filter_output_stream_flush (GOutputStream *stream,
277 GCancellable *cancellable,
278 GError **error)
280 GFilterOutputStream *filter_stream;
281 gboolean res;
283 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
285 res = g_output_stream_flush (filter_stream->base_stream,
286 cancellable,
287 error);
289 return res;
292 static gboolean
293 g_filter_output_stream_close (GOutputStream *stream,
294 GCancellable *cancellable,
295 GError **error)
297 gboolean res = TRUE;
299 if (GET_PRIVATE (stream)->close_base)
301 GFilterOutputStream *filter_stream;
303 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
305 res = g_output_stream_close (filter_stream->base_stream,
306 cancellable,
307 error);
310 return res;