/gio/gfilteroutputstream.c

https://github.com/rikaunite/gst-opera_glib · C · 312 lines · 200 code · 57 blank · 55 comment · 6 complexity · 2b9923825c8bed5c70b63bc2549b5e35 MD5 · raw file

  1. /* GIO - GLib Input, Output and Streaming Library
  2. *
  3. * Copyright (C) 2006-2007 Red Hat, Inc.
  4. *
  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.
  9. *
  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.
  14. *
  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.
  19. *
  20. * Author: Christian Kellner <gicmo@gnome.org>
  21. */
  22. #include "config.h"
  23. #include "gfilteroutputstream.h"
  24. #include "gsimpleasyncresult.h"
  25. #include "goutputstream.h"
  26. #include "glibintl.h"
  27. /**
  28. * SECTION:gfilteroutputstream
  29. * @short_description: Filter Output Stream
  30. * @include: gio/gio.h
  31. *
  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.
  36. */
  37. enum {
  38. PROP_0,
  39. PROP_BASE_STREAM,
  40. PROP_CLOSE_BASE
  41. };
  42. static void g_filter_output_stream_set_property (GObject *object,
  43. guint prop_id,
  44. const GValue *value,
  45. GParamSpec *pspec);
  46. static void g_filter_output_stream_get_property (GObject *object,
  47. guint prop_id,
  48. GValue *value,
  49. GParamSpec *pspec);
  50. static void g_filter_output_stream_dispose (GObject *object);
  51. static gssize g_filter_output_stream_write (GOutputStream *stream,
  52. const void *buffer,
  53. gsize count,
  54. GCancellable *cancellable,
  55. GError **error);
  56. static gboolean g_filter_output_stream_flush (GOutputStream *stream,
  57. GCancellable *cancellable,
  58. GError **error);
  59. static gboolean g_filter_output_stream_close (GOutputStream *stream,
  60. GCancellable *cancellable,
  61. GError **error);
  62. G_DEFINE_ABSTRACT_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
  63. #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
  64. G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
  65. typedef struct
  66. {
  67. gboolean close_base;
  68. } GFilterOutputStreamPrivate;
  69. static void
  70. g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
  71. {
  72. GObjectClass *object_class;
  73. GOutputStreamClass *ostream_class;
  74. object_class = G_OBJECT_CLASS (klass);
  75. object_class->get_property = g_filter_output_stream_get_property;
  76. object_class->set_property = g_filter_output_stream_set_property;
  77. object_class->dispose = g_filter_output_stream_dispose;
  78. ostream_class = G_OUTPUT_STREAM_CLASS (klass);
  79. ostream_class->write_fn = g_filter_output_stream_write;
  80. ostream_class->flush = g_filter_output_stream_flush;
  81. ostream_class->close_fn = g_filter_output_stream_close;
  82. g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
  83. g_object_class_install_property (object_class,
  84. PROP_BASE_STREAM,
  85. g_param_spec_object ("base-stream",
  86. P_("The Filter Base Stream"),
  87. P_("The underlying base stream on which the io ops will be done."),
  88. G_TYPE_OUTPUT_STREAM,
  89. G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
  90. G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
  91. g_object_class_install_property (object_class,
  92. PROP_CLOSE_BASE,
  93. g_param_spec_boolean ("close-base-stream",
  94. P_("Close Base Stream"),
  95. P_("If the base stream should be closed when the filter stream is closed."),
  96. TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
  97. G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
  98. }
  99. static void
  100. g_filter_output_stream_set_property (GObject *object,
  101. guint prop_id,
  102. const GValue *value,
  103. GParamSpec *pspec)
  104. {
  105. GFilterOutputStream *filter_stream;
  106. GObject *obj;
  107. filter_stream = G_FILTER_OUTPUT_STREAM (object);
  108. switch (prop_id)
  109. {
  110. case PROP_BASE_STREAM:
  111. obj = g_value_dup_object (value);
  112. filter_stream->base_stream = G_OUTPUT_STREAM (obj);
  113. break;
  114. case PROP_CLOSE_BASE:
  115. g_filter_output_stream_set_close_base_stream (filter_stream,
  116. g_value_get_boolean (value));
  117. break;
  118. default:
  119. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  120. break;
  121. }
  122. }
  123. static void
  124. g_filter_output_stream_get_property (GObject *object,
  125. guint prop_id,
  126. GValue *value,
  127. GParamSpec *pspec)
  128. {
  129. GFilterOutputStream *filter_stream;
  130. filter_stream = G_FILTER_OUTPUT_STREAM (object);
  131. switch (prop_id)
  132. {
  133. case PROP_BASE_STREAM:
  134. g_value_set_object (value, filter_stream->base_stream);
  135. break;
  136. case PROP_CLOSE_BASE:
  137. g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
  138. break;
  139. default:
  140. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  141. break;
  142. }
  143. }
  144. static void
  145. g_filter_output_stream_dispose (GObject *object)
  146. {
  147. GFilterOutputStream *stream;
  148. stream = G_FILTER_OUTPUT_STREAM (object);
  149. G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
  150. if (stream->base_stream)
  151. {
  152. g_object_unref (stream->base_stream);
  153. stream->base_stream = NULL;
  154. }
  155. }
  156. static void
  157. g_filter_output_stream_init (GFilterOutputStream *stream)
  158. {
  159. }
  160. /**
  161. * g_filter_output_stream_get_base_stream:
  162. * @stream: a #GFilterOutputStream.
  163. *
  164. * Gets the base stream for the filter stream.
  165. *
  166. * Returns: (transfer none): a #GOutputStream.
  167. **/
  168. GOutputStream *
  169. g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
  170. {
  171. g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
  172. return stream->base_stream;
  173. }
  174. /**
  175. * g_filter_output_stream_get_close_base_stream:
  176. * @stream: a #GFilterOutputStream.
  177. *
  178. * Returns whether the base stream will be closed when @stream is
  179. * closed.
  180. *
  181. * Return value: %TRUE if the base stream will be closed.
  182. **/
  183. gboolean
  184. g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
  185. {
  186. g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
  187. return GET_PRIVATE (stream)->close_base;
  188. }
  189. /**
  190. * g_filter_output_stream_set_close_base_stream:
  191. * @stream: a #GFilterOutputStream.
  192. * @close_base: %TRUE to close the base stream.
  193. *
  194. * Sets whether the base stream will be closed when @stream is closed.
  195. **/
  196. void
  197. g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
  198. gboolean close_base)
  199. {
  200. GFilterOutputStreamPrivate *priv;
  201. g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
  202. close_base = !!close_base;
  203. priv = GET_PRIVATE (stream);
  204. if (priv->close_base != close_base)
  205. {
  206. priv->close_base = close_base;
  207. g_object_notify (G_OBJECT (stream), "close-base-stream");
  208. }
  209. }
  210. static gssize
  211. g_filter_output_stream_write (GOutputStream *stream,
  212. const void *buffer,
  213. gsize count,
  214. GCancellable *cancellable,
  215. GError **error)
  216. {
  217. GFilterOutputStream *filter_stream;
  218. gssize nwritten;
  219. filter_stream = G_FILTER_OUTPUT_STREAM (stream);
  220. nwritten = g_output_stream_write (filter_stream->base_stream,
  221. buffer,
  222. count,
  223. cancellable,
  224. error);
  225. return nwritten;
  226. }
  227. static gboolean
  228. g_filter_output_stream_flush (GOutputStream *stream,
  229. GCancellable *cancellable,
  230. GError **error)
  231. {
  232. GFilterOutputStream *filter_stream;
  233. gboolean res;
  234. filter_stream = G_FILTER_OUTPUT_STREAM (stream);
  235. res = g_output_stream_flush (filter_stream->base_stream,
  236. cancellable,
  237. error);
  238. return res;
  239. }
  240. static gboolean
  241. g_filter_output_stream_close (GOutputStream *stream,
  242. GCancellable *cancellable,
  243. GError **error)
  244. {
  245. gboolean res = TRUE;
  246. if (GET_PRIVATE (stream)->close_base)
  247. {
  248. GFilterOutputStream *filter_stream;
  249. filter_stream = G_FILTER_OUTPUT_STREAM (stream);
  250. res = g_output_stream_close (filter_stream->base_stream,
  251. cancellable,
  252. error);
  253. }
  254. return res;
  255. }