| 1 | #include "g_callback_input_stream.h" |
| 2 | |
| 3 | G_DEFINE_TYPE (GCallbackInputStream, g_callback_input_stream, G_TYPE_INPUT_STREAM); |
| 4 | |
| 5 | struct _GCallbackInputStreamPrivate { |
| 6 | GCallbackInputStreamReadFunc read_func; |
| 7 | GCallbackInputStreamCloseFunc close_func; |
| 8 | gpointer user_data; |
| 9 | }; |
| 10 | |
| 11 | static gssize g_callback_input_stream_read (GInputStream *stream, |
| 12 | void *buffer, |
| 13 | gsize count, |
| 14 | GCancellable *cancellable, |
| 15 | GError **error); |
| 16 | static gssize g_callback_input_stream_skip (GInputStream *stream, |
| 17 | gsize count, |
| 18 | GCancellable *cancellable, |
| 19 | GError **error); |
| 20 | static gboolean g_callback_input_stream_close (GInputStream *stream, |
| 21 | GCancellable *cancellable, |
| 22 | GError **error); |
| 23 | |
| 24 | |
| 25 | static void |
| 26 | g_callback_input_stream_finalize (GObject *object) |
| 27 | { |
| 28 | GCallbackInputStream *stream; |
| 29 | |
| 30 | stream = G_CALLBACK_INPUT_STREAM (object); |
| 31 | |
| 32 | G_OBJECT_CLASS (g_callback_input_stream_parent_class)->finalize (object); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | g_callback_input_stream_class_init (GCallbackInputStreamClass *klass) |
| 37 | { |
| 38 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 39 | GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass); |
| 40 | |
| 41 | g_type_class_add_private (klass, sizeof (GCallbackInputStreamPrivate)); |
| 42 | |
| 43 | gobject_class->finalize = g_callback_input_stream_finalize; |
| 44 | |
| 45 | stream_class->read_fn = g_callback_input_stream_read; |
| 46 | stream_class->skip = g_callback_input_stream_skip; |
| 47 | stream_class->close_fn = g_callback_input_stream_close; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | g_callback_input_stream_init (GCallbackInputStream *callback_stream) |
| 52 | { |
| 53 | callback_stream->priv = |
| 54 | G_TYPE_INSTANCE_GET_PRIVATE (callback_stream, |
| 55 | G_TYPE_CALLBACK_INPUT_STREAM, |
| 56 | GCallbackInputStreamPrivate); |
| 57 | } |
| 58 | |
| 59 | GInputStream* |
| 60 | g_callback_input_stream_new (GCallbackInputStreamReadFunc read_func, |
| 61 | GCallbackInputStreamCloseFunc close_func, |
| 62 | gpointer user_data) |
| 63 | { |
| 64 | GCallbackInputStream *stream; |
| 65 | |
| 66 | stream = g_object_new (G_TYPE_CALLBACK_INPUT_STREAM, NULL); |
| 67 | |
| 68 | stream->priv->read_func = read_func; |
| 69 | stream->priv->close_func = close_func; |
| 70 | stream->priv->user_data = user_data; |
| 71 | |
| 72 | return G_INPUT_STREAM (stream); |
| 73 | } |
| 74 | |
| 75 | static gssize |
| 76 | g_callback_input_stream_read (GInputStream *stream, |
| 77 | void *buffer, |
| 78 | gsize count, |
| 79 | GCancellable *cancellable, |
| 80 | GError **error) |
| 81 | { |
| 82 | GCallbackInputStream *callback_stream = G_CALLBACK_INPUT_STREAM (stream); |
| 83 | GCallbackInputStreamReadFunc read_func = callback_stream->priv->read_func; |
| 84 | gpointer user_data = callback_stream->priv->user_data; |
| 85 | |
| 86 | return read_func (buffer, count, cancellable, error, user_data); |
| 87 | } |
| 88 | |
| 89 | static gssize |
| 90 | g_callback_input_stream_skip (GInputStream *stream, |
| 91 | gsize count, |
| 92 | GCancellable *cancellable, |
| 93 | GError **error) |
| 94 | { |
| 95 | GCallbackInputStream *callback_stream = G_CALLBACK_INPUT_STREAM (stream); |
| 96 | GCallbackInputStreamReadFunc read_func = callback_stream->priv->read_func; |
| 97 | gpointer user_data = callback_stream->priv->user_data; |
| 98 | |
| 99 | return read_func (NULL, count, cancellable, error, user_data); |
| 100 | } |
| 101 | |
| 102 | static gboolean |
| 103 | g_callback_input_stream_close (GInputStream *stream, |
| 104 | GCancellable *cancellable, |
| 105 | GError **error) |
| 106 | { |
| 107 | GCallbackInputStream *callback_stream = G_CALLBACK_INPUT_STREAM (stream); |
| 108 | GCallbackInputStreamCloseFunc close_func = callback_stream->priv->close_func; |
| 109 | gpointer user_data = callback_stream->priv->user_data; |
| 110 | |
| 111 | callback_stream = G_CALLBACK_INPUT_STREAM (stream); |
| 112 | |
| 113 | if (callback_stream->priv->close_func) |
| 114 | return close_func (cancellable, error, user_data); |
| 115 | |
| 116 | return TRUE; |
| 117 | } |
| 118 | |
| 119 | #define __G_CALLBACK_INPUT_STREAM_C__ |
| 120 | |