Merge branch '896-variant-type-docs' into 'master'
[glib.git] / gio / gfilteroutputstream.c
blob9d864932e4994845be784709a40875cbb198e893
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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Christian Kellner <gicmo@gnome.org>
21 #include "config.h"
22 #include "gfilteroutputstream.h"
23 #include "goutputstream.h"
24 #include "glibintl.h"
27 /**
28 * SECTION:gfilteroutputstream
29 * @short_description: Filter Output Stream
30 * @include: gio/gio.h
32 * Base class for output stream implementations that perform some
33 * kind of filtering operation on a base stream. Typical examples
34 * of filtering operations are character set conversion, compression
35 * and byte order flipping.
38 enum {
39 PROP_0,
40 PROP_BASE_STREAM,
41 PROP_CLOSE_BASE
44 static void g_filter_output_stream_set_property (GObject *object,
45 guint prop_id,
46 const GValue *value,
47 GParamSpec *pspec);
49 static void g_filter_output_stream_get_property (GObject *object,
50 guint prop_id,
51 GValue *value,
52 GParamSpec *pspec);
53 static void g_filter_output_stream_dispose (GObject *object);
56 static gssize g_filter_output_stream_write (GOutputStream *stream,
57 const void *buffer,
58 gsize count,
59 GCancellable *cancellable,
60 GError **error);
61 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
62 GCancellable *cancellable,
63 GError **error);
64 static gboolean g_filter_output_stream_close (GOutputStream *stream,
65 GCancellable *cancellable,
66 GError **error);
68 typedef struct
70 gboolean close_base;
71 } GFilterOutputStreamPrivate;
73 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
75 static void
76 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
78 GObjectClass *object_class;
79 GOutputStreamClass *ostream_class;
81 object_class = G_OBJECT_CLASS (klass);
82 object_class->get_property = g_filter_output_stream_get_property;
83 object_class->set_property = g_filter_output_stream_set_property;
84 object_class->dispose = g_filter_output_stream_dispose;
86 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
87 ostream_class->write_fn = g_filter_output_stream_write;
88 ostream_class->flush = g_filter_output_stream_flush;
89 ostream_class->close_fn = g_filter_output_stream_close;
91 g_object_class_install_property (object_class,
92 PROP_BASE_STREAM,
93 g_param_spec_object ("base-stream",
94 P_("The Filter Base Stream"),
95 P_("The underlying base stream on which the io ops will be done."),
96 G_TYPE_OUTPUT_STREAM,
97 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
98 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
100 g_object_class_install_property (object_class,
101 PROP_CLOSE_BASE,
102 g_param_spec_boolean ("close-base-stream",
103 P_("Close Base Stream"),
104 P_("If the base stream should be closed when the filter stream is closed."),
105 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
106 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
109 static void
110 g_filter_output_stream_set_property (GObject *object,
111 guint prop_id,
112 const GValue *value,
113 GParamSpec *pspec)
115 GFilterOutputStream *filter_stream;
116 GObject *obj;
118 filter_stream = G_FILTER_OUTPUT_STREAM (object);
120 switch (prop_id)
122 case PROP_BASE_STREAM:
123 obj = g_value_dup_object (value);
124 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
125 break;
127 case PROP_CLOSE_BASE:
128 g_filter_output_stream_set_close_base_stream (filter_stream,
129 g_value_get_boolean (value));
130 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 break;
139 static void
140 g_filter_output_stream_get_property (GObject *object,
141 guint prop_id,
142 GValue *value,
143 GParamSpec *pspec)
145 GFilterOutputStream *filter_stream;
146 GFilterOutputStreamPrivate *priv;
148 filter_stream = G_FILTER_OUTPUT_STREAM (object);
149 priv = g_filter_output_stream_get_instance_private (filter_stream);
151 switch (prop_id)
153 case PROP_BASE_STREAM:
154 g_value_set_object (value, filter_stream->base_stream);
155 break;
157 case PROP_CLOSE_BASE:
158 g_value_set_boolean (value, priv->close_base);
159 break;
161 default:
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 break;
168 static void
169 g_filter_output_stream_dispose (GObject *object)
171 GFilterOutputStream *stream;
173 stream = G_FILTER_OUTPUT_STREAM (object);
175 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
177 if (stream->base_stream)
179 g_object_unref (stream->base_stream);
180 stream->base_stream = NULL;
185 static void
186 g_filter_output_stream_init (GFilterOutputStream *stream)
191 * g_filter_output_stream_get_base_stream:
192 * @stream: a #GFilterOutputStream.
194 * Gets the base stream for the filter stream.
196 * Returns: (transfer none): a #GOutputStream.
198 GOutputStream *
199 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
201 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
203 return stream->base_stream;
207 * g_filter_output_stream_get_close_base_stream:
208 * @stream: a #GFilterOutputStream.
210 * Returns whether the base stream will be closed when @stream is
211 * closed.
213 * Returns: %TRUE if the base stream will be closed.
215 gboolean
216 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
218 GFilterOutputStreamPrivate *priv;
220 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
222 priv = g_filter_output_stream_get_instance_private (stream);
224 return priv->close_base;
228 * g_filter_output_stream_set_close_base_stream:
229 * @stream: a #GFilterOutputStream.
230 * @close_base: %TRUE to close the base stream.
232 * Sets whether the base stream will be closed when @stream is closed.
234 void
235 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
236 gboolean close_base)
238 GFilterOutputStreamPrivate *priv;
240 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
242 close_base = !!close_base;
244 priv = g_filter_output_stream_get_instance_private (stream);
246 if (priv->close_base != close_base)
248 priv->close_base = close_base;
249 g_object_notify (G_OBJECT (stream), "close-base-stream");
253 static gssize
254 g_filter_output_stream_write (GOutputStream *stream,
255 const void *buffer,
256 gsize count,
257 GCancellable *cancellable,
258 GError **error)
260 GFilterOutputStream *filter_stream;
261 gssize nwritten;
263 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
265 nwritten = g_output_stream_write (filter_stream->base_stream,
266 buffer,
267 count,
268 cancellable,
269 error);
271 return nwritten;
274 static gboolean
275 g_filter_output_stream_flush (GOutputStream *stream,
276 GCancellable *cancellable,
277 GError **error)
279 GFilterOutputStream *filter_stream;
280 gboolean res;
282 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
284 res = g_output_stream_flush (filter_stream->base_stream,
285 cancellable,
286 error);
288 return res;
291 static gboolean
292 g_filter_output_stream_close (GOutputStream *stream,
293 GCancellable *cancellable,
294 GError **error)
296 GFilterOutputStream *filter_stream = G_FILTER_OUTPUT_STREAM (stream);
297 GFilterOutputStreamPrivate *priv = g_filter_output_stream_get_instance_private (filter_stream);
298 gboolean res = TRUE;
300 if (priv->close_base)
302 res = g_output_stream_close (filter_stream->base_stream,
303 cancellable,
304 error);
307 return res;