| 1 | #include "g_callback_output_stream.h" |
| 2 | |
| 3 | G_DEFINE_TYPE (GCallbackOutputStream, g_callback_output_stream, G_TYPE_OUTPUT_STREAM); |
| 4 | |
| 5 | struct _GCallbackOutputStreamPrivate { |
| 6 | GCallbackOutputStreamWriteFunc write_func; |
| 7 | GCallbackOutputStreamFlushFunc flush_func; |
| 8 | GCallbackOutputStreamCloseFunc close_func; |
| 9 | gpointer user_data; |
| 10 | }; |
| 11 | |
| 12 | static gssize g_callback_output_stream_write (GOutputStream *stream, |
| 13 | void const *buffer, |
| 14 | gsize count, |
| 15 | GCancellable *cancellable, |
| 16 | GError **error); |
| 17 | static gboolean g_callback_output_stream_flush (GOutputStream *stream, |
| 18 | GCancellable *cancellable, |
| 19 | GError **error); |
| 20 | static gboolean g_callback_output_stream_close (GOutputStream *stream, |
| 21 | GCancellable *cancellable, |
| 22 | GError **error); |
| 23 | |
| 24 | |
| 25 | static void |
| 26 | g_callback_output_stream_finalize (GObject *object) |
| 27 | { |
| 28 | GCallbackOutputStream *stream; |
| 29 | |
| 30 | stream = G_CALLBACK_OUTPUT_STREAM (object); |
| 31 | |
| 32 | G_OBJECT_CLASS (g_callback_output_stream_parent_class)->finalize (object); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | g_callback_output_stream_class_init (GCallbackOutputStreamClass *klass) |
| 37 | { |
| 38 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 39 | GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass); |
| 40 | |
| 41 | g_type_class_add_private (klass, sizeof (GCallbackOutputStreamPrivate)); |
| 42 | |
| 43 | gobject_class->finalize = g_callback_output_stream_finalize; |
| 44 | |
| 45 | stream_class->write_fn = g_callback_output_stream_write; |
| 46 | stream_class->flush = g_callback_output_stream_flush; |
| 47 | stream_class->close_fn = g_callback_output_stream_close; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | g_callback_output_stream_init (GCallbackOutputStream *callback_stream) |
| 52 | { |
| 53 | callback_stream->priv = |
| 54 | G_TYPE_INSTANCE_GET_PRIVATE (callback_stream, |
| 55 | G_TYPE_CALLBACK_OUTPUT_STREAM, |
| 56 | GCallbackOutputStreamPrivate); |
| 57 | } |
| 58 | |
| 59 | GOutputStream* |
| 60 | g_callback_output_stream_new (GCallbackOutputStreamWriteFunc write_func, |
| 61 | GCallbackOutputStreamFlushFunc flush_func, |
| 62 | GCallbackOutputStreamCloseFunc close_func, |
| 63 | gpointer user_data) |
| 64 | { |
| 65 | GCallbackOutputStream *stream; |
| 66 | |
| 67 | stream = g_object_new (G_TYPE_CALLBACK_OUTPUT_STREAM, NULL); |
| 68 | |
| 69 | stream->priv->write_func = write_func; |
| 70 | stream->priv->flush_func = flush_func; |
| 71 | stream->priv->close_func = close_func; |
| 72 | stream->priv->user_data = user_data; |
| 73 | |
| 74 | return G_OUTPUT_STREAM (stream); |
| 75 | } |
| 76 | |
| 77 | static gssize |
| 78 | g_callback_output_stream_write (GOutputStream *stream, |
| 79 | const void *buffer, |
| 80 | gsize count, |
| 81 | GCancellable *cancellable, |
| 82 | GError **error) |
| 83 | { |
| 84 | GCallbackOutputStream *callback_stream = G_CALLBACK_OUTPUT_STREAM (stream); |
| 85 | GCallbackOutputStreamWriteFunc write_func = callback_stream->priv->write_func; |
| 86 | gpointer user_data = callback_stream->priv->user_data; |
| 87 | |
| 88 | return write_func (buffer, count, cancellable, error, user_data); |
| 89 | } |
| 90 | |
| 91 | static gboolean |
| 92 | g_callback_output_stream_flush (GOutputStream *stream, |
| 93 | GCancellable *cancellable, |
| 94 | GError **error) |
| 95 | { |
| 96 | GCallbackOutputStream *callback_stream = G_CALLBACK_OUTPUT_STREAM (stream); |
| 97 | GCallbackOutputStreamFlushFunc flush_func = callback_stream->priv->flush_func; |
| 98 | gpointer user_data = callback_stream->priv->user_data; |
| 99 | |
| 100 | if (callback_stream->priv->flush_func) |
| 101 | return flush_func (cancellable, error, user_data); |
| 102 | |
| 103 | return TRUE; |
| 104 | } |
| 105 | |
| 106 | static gboolean |
| 107 | g_callback_output_stream_close (GOutputStream *stream, |
| 108 | GCancellable *cancellable, |
| 109 | GError **error) |
| 110 | { |
| 111 | GCallbackOutputStream *callback_stream = G_CALLBACK_OUTPUT_STREAM (stream); |
| 112 | GCallbackOutputStreamCloseFunc close_func = callback_stream->priv->close_func; |
| 113 | gpointer user_data = callback_stream->priv->user_data; |
| 114 | |
| 115 | callback_stream = G_CALLBACK_OUTPUT_STREAM (stream); |
| 116 | |
| 117 | if (callback_stream->priv->close_func) |
| 118 | return close_func (cancellable, error, user_data); |
| 119 | |
| 120 | return TRUE; |
| 121 | } |
| 122 | |
| 123 | #define __G_CALLBACK_OUTPUT_STREAM_C__ |
| 124 | |