GIO: add lots of annotations for Vala bindings
[platform/upstream/glib.git] / gio / gmemoryinputstream.c
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
23 #include "config.h"
24 #include "gmemoryinputstream.h"
25 #include "ginputstream.h"
26 #include "gseekable.h"
27 #include "string.h"
28 #include "gsimpleasyncresult.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gmemoryinputstream
35  * @short_description: Streaming input operations on memory chunks
36  * @include: gio/gio.h
37  * @see_also: #GMemoryOutputStream
38  *
39  * #GMemoryInputStream is a class for using arbitrary
40  * memory chunks as input for GIO streaming input operations.
41  *
42  */
43
44 typedef struct _Chunk Chunk;
45
46 struct _Chunk {
47   guint8         *data;
48   gsize           len;
49   GDestroyNotify  destroy;
50 };
51
52 struct _GMemoryInputStreamPrivate {
53   GSList *chunks;
54   gsize   len;
55   gsize   pos;
56 };
57
58 static gssize   g_memory_input_stream_read         (GInputStream         *stream,
59                                                     void                 *buffer,
60                                                     gsize                 count,
61                                                     GCancellable         *cancellable,
62                                                     GError              **error);
63 static gssize   g_memory_input_stream_skip         (GInputStream         *stream,
64                                                     gsize                 count,
65                                                     GCancellable         *cancellable,
66                                                     GError              **error);
67 static gboolean g_memory_input_stream_close        (GInputStream         *stream,
68                                                     GCancellable         *cancellable,
69                                                     GError              **error);
70 static void     g_memory_input_stream_read_async   (GInputStream         *stream,
71                                                     void                 *buffer,
72                                                     gsize                 count,
73                                                     int                   io_priority,
74                                                     GCancellable         *cancellable,
75                                                     GAsyncReadyCallback   callback,
76                                                     gpointer              user_data);
77 static gssize   g_memory_input_stream_read_finish  (GInputStream         *stream,
78                                                     GAsyncResult         *result,
79                                                     GError              **error);
80 static void     g_memory_input_stream_skip_async   (GInputStream         *stream,
81                                                     gsize                 count,
82                                                     int                   io_priority,
83                                                     GCancellable         *cancellabl,
84                                                     GAsyncReadyCallback   callback,
85                                                     gpointer              datae);
86 static gssize   g_memory_input_stream_skip_finish  (GInputStream         *stream,
87                                                     GAsyncResult         *result,
88                                                     GError              **error);
89 static void     g_memory_input_stream_close_async  (GInputStream         *stream,
90                                                     int                   io_priority,
91                                                     GCancellable         *cancellabl,
92                                                     GAsyncReadyCallback   callback,
93                                                     gpointer              data);
94 static gboolean g_memory_input_stream_close_finish (GInputStream         *stream,
95                                                     GAsyncResult         *result,
96                                                     GError              **error);
97
98 static void     g_memory_input_stream_seekable_iface_init (GSeekableIface  *iface);
99 static goffset  g_memory_input_stream_tell                (GSeekable       *seekable);
100 static gboolean g_memory_input_stream_can_seek            (GSeekable       *seekable);
101 static gboolean g_memory_input_stream_seek                (GSeekable       *seekable,
102                                                            goffset          offset,
103                                                            GSeekType        type,
104                                                            GCancellable    *cancellable,
105                                                            GError         **error);
106 static gboolean g_memory_input_stream_can_truncate        (GSeekable       *seekable);
107 static gboolean g_memory_input_stream_truncate            (GSeekable       *seekable,
108                                                            goffset          offset,
109                                                            GCancellable    *cancellable,
110                                                            GError         **error);
111 static void     g_memory_input_stream_finalize            (GObject         *object);
112
113 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
114                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
115                                                 g_memory_input_stream_seekable_iface_init))
116
117
118 static void
119 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
120 {
121   GObjectClass *object_class;
122   GInputStreamClass *istream_class;
123
124   g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
125
126   object_class = G_OBJECT_CLASS (klass);
127   object_class->finalize     = g_memory_input_stream_finalize;
128   
129   istream_class = G_INPUT_STREAM_CLASS (klass);
130   istream_class->read_fn  = g_memory_input_stream_read;
131   istream_class->skip  = g_memory_input_stream_skip;
132   istream_class->close_fn = g_memory_input_stream_close;
133
134   istream_class->read_async  = g_memory_input_stream_read_async;
135   istream_class->read_finish  = g_memory_input_stream_read_finish;
136   istream_class->skip_async  = g_memory_input_stream_skip_async;
137   istream_class->skip_finish  = g_memory_input_stream_skip_finish;
138   istream_class->close_async = g_memory_input_stream_close_async;
139   istream_class->close_finish = g_memory_input_stream_close_finish;
140 }
141
142 static void
143 free_chunk (gpointer data, 
144             gpointer user_data)
145 {
146   Chunk *chunk = data;
147
148   if (chunk->destroy)
149     chunk->destroy (chunk->data);
150
151   g_slice_free (Chunk, chunk);
152 }
153
154 static void
155 g_memory_input_stream_finalize (GObject *object)
156 {
157   GMemoryInputStream        *stream;
158   GMemoryInputStreamPrivate *priv;
159
160   stream = G_MEMORY_INPUT_STREAM (object);
161   priv = stream->priv;
162
163   g_slist_free_full (priv->chunks, free_chunk);
164
165   G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
166 }
167
168 static void
169 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
170 {
171   iface->tell         = g_memory_input_stream_tell;
172   iface->can_seek     = g_memory_input_stream_can_seek;
173   iface->seek         = g_memory_input_stream_seek;
174   iface->can_truncate = g_memory_input_stream_can_truncate;
175   iface->truncate_fn  = g_memory_input_stream_truncate;
176 }
177
178 static void
179 g_memory_input_stream_init (GMemoryInputStream *stream)
180 {
181   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
182                                               G_TYPE_MEMORY_INPUT_STREAM,
183                                               GMemoryInputStreamPrivate);
184 }
185
186 /**
187  * g_memory_input_stream_new:
188  *
189  * Creates a new empty #GMemoryInputStream. 
190  *
191  * Returns: a new #GInputStream
192  */
193 GInputStream *
194 g_memory_input_stream_new (void)
195 {
196   GInputStream *stream;
197
198   stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
199
200   return stream;
201 }
202
203 /**
204  * g_memory_input_stream_new_from_data:
205  * @data: (array length=len) (element-type guint8) (transfer full): input data
206  * @len: length of the data, may be -1 if @data is a nul-terminated string
207  * @destroy: (allow-none): function that is called to free @data, or %NULL
208  *
209  * Creates a new #GMemoryInputStream with data in memory of a given size.
210  * 
211  * Returns: new #GInputStream read from @data of @len bytes.
212  **/
213 GInputStream *
214 g_memory_input_stream_new_from_data (const void     *data, 
215                                      gssize          len,
216                                      GDestroyNotify  destroy)
217 {
218   GInputStream *stream;
219
220   stream = g_memory_input_stream_new ();
221
222   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
223                                   data, len, destroy);
224
225   return stream;
226 }
227
228 /**
229  * g_memory_input_stream_add_data:
230  * @stream: a #GMemoryInputStream
231  * @data: (array length=len) (element-type guint8) (transfer full): input data
232  * @len: length of the data, may be -1 if @data is a nul-terminated string
233  * @destroy: (allow-none): function that is called to free @data, or %NULL
234  *
235  * Appends @data to data that can be read from the input stream
236  */
237 void
238 g_memory_input_stream_add_data (GMemoryInputStream *stream,
239                                 const void         *data,
240                                 gssize              len,
241                                 GDestroyNotify      destroy)
242 {
243   GMemoryInputStreamPrivate *priv;
244   Chunk *chunk;
245  
246   g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
247   g_return_if_fail (data != NULL);
248
249   priv = stream->priv;
250
251   if (len == -1)
252     len = strlen (data);
253   
254   chunk = g_slice_new (Chunk);
255   chunk->data = (guint8 *)data;
256   chunk->len = len;
257   chunk->destroy = destroy;
258
259   priv->chunks = g_slist_append (priv->chunks, chunk);
260   priv->len += chunk->len;
261 }
262
263 static gssize
264 g_memory_input_stream_read (GInputStream  *stream,
265                             void          *buffer,
266                             gsize          count,
267                             GCancellable  *cancellable,
268                             GError       **error)
269 {
270   GMemoryInputStream *memory_stream;
271   GMemoryInputStreamPrivate *priv;
272   GSList *l;
273   Chunk *chunk;
274   gsize offset, start, rest, size;
275
276   memory_stream = G_MEMORY_INPUT_STREAM (stream);
277   priv = memory_stream->priv;
278
279   count = MIN (count, priv->len - priv->pos);
280
281   offset = 0;
282   for (l = priv->chunks; l; l = l->next) 
283     {
284       chunk = (Chunk *)l->data;
285
286       if (offset + chunk->len > priv->pos)
287         break;
288
289       offset += chunk->len;
290     }
291   
292   start = priv->pos - offset;
293   rest = count;
294
295   for (; l && rest > 0; l = l->next)
296     {
297       chunk = (Chunk *)l->data;
298       size = MIN (rest, chunk->len - start);
299
300       memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
301       rest -= size;
302
303       start = 0;
304     }
305
306   priv->pos += count;
307
308   return count;
309 }
310
311 static gssize
312 g_memory_input_stream_skip (GInputStream  *stream,
313                             gsize          count,
314                             GCancellable  *cancellable,
315                             GError       **error)
316 {
317   GMemoryInputStream *memory_stream;
318   GMemoryInputStreamPrivate *priv;
319
320   memory_stream = G_MEMORY_INPUT_STREAM (stream);
321   priv = memory_stream->priv;
322
323   count = MIN (count, priv->len - priv->pos);
324   priv->pos += count;
325
326   return count;
327 }
328
329 static gboolean
330 g_memory_input_stream_close (GInputStream  *stream,
331                              GCancellable  *cancellable,
332                              GError       **error)
333 {
334   return TRUE;
335 }
336
337 static void
338 g_memory_input_stream_read_async (GInputStream        *stream,
339                                   void                *buffer,
340                                   gsize                count,
341                                   int                  io_priority,
342                                   GCancellable        *cancellable,
343                                   GAsyncReadyCallback  callback,
344                                   gpointer             user_data)
345 {
346   GSimpleAsyncResult *simple;
347   GError *error = NULL;
348   gssize nread;
349
350   nread = G_INPUT_STREAM_GET_CLASS (stream)->read_fn (stream,
351                                                       buffer,
352                                                       count,
353                                                       cancellable,
354                                                       &error);
355   simple = g_simple_async_result_new (G_OBJECT (stream),
356                                       callback,
357                                       user_data,
358                                       g_memory_input_stream_read_async);
359   if (error)
360     g_simple_async_result_take_error (simple, error);
361   else
362     g_simple_async_result_set_op_res_gssize (simple, nread);
363   g_simple_async_result_complete_in_idle (simple);
364   g_object_unref (simple);
365 }
366
367 static gssize
368 g_memory_input_stream_read_finish (GInputStream  *stream,
369                                    GAsyncResult  *result,
370                                    GError       **error)
371 {
372   GSimpleAsyncResult *simple;
373   gssize nread;
374
375   simple = G_SIMPLE_ASYNC_RESULT (result);
376   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
377   
378   nread = g_simple_async_result_get_op_res_gssize (simple);
379   return nread;
380 }
381
382 static void
383 g_memory_input_stream_skip_async (GInputStream        *stream,
384                                   gsize                count,
385                                   int                  io_priority,
386                                   GCancellable        *cancellable,
387                                   GAsyncReadyCallback  callback,
388                                   gpointer             user_data)
389 {
390   GSimpleAsyncResult *simple;
391   gssize nskipped;
392
393   nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
394   simple = g_simple_async_result_new (G_OBJECT (stream),
395                                       callback,
396                                       user_data,
397                                       g_memory_input_stream_skip_async);
398   g_simple_async_result_set_op_res_gssize (simple, nskipped);
399   g_simple_async_result_complete_in_idle (simple);
400   g_object_unref (simple);
401 }
402
403 static gssize
404 g_memory_input_stream_skip_finish (GInputStream  *stream,
405                                    GAsyncResult  *result,
406                                    GError       **error)
407 {
408   GSimpleAsyncResult *simple;
409   gssize nskipped;
410
411   simple = G_SIMPLE_ASYNC_RESULT (result);
412   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
413   
414   nskipped = g_simple_async_result_get_op_res_gssize (simple);
415   return nskipped;
416 }
417
418 static void
419 g_memory_input_stream_close_async (GInputStream        *stream,
420                                    int                  io_priority,
421                                    GCancellable        *cancellable,
422                                    GAsyncReadyCallback  callback,
423                                    gpointer             user_data)
424 {
425   GSimpleAsyncResult *simple;
426   
427   simple = g_simple_async_result_new (G_OBJECT (stream),
428                                       callback,
429                                       user_data,
430                                       g_memory_input_stream_close_async);
431   g_simple_async_result_complete_in_idle (simple);
432   g_object_unref (simple);
433 }
434
435 static gboolean
436 g_memory_input_stream_close_finish (GInputStream  *stream,
437                                     GAsyncResult  *result,
438                                     GError       **error)
439 {
440   return TRUE;
441 }
442
443 static goffset
444 g_memory_input_stream_tell (GSeekable *seekable)
445 {
446   GMemoryInputStream *memory_stream;
447   GMemoryInputStreamPrivate *priv;
448
449   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
450   priv = memory_stream->priv;
451
452   return priv->pos;
453 }
454
455 static
456 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
457 {
458   return TRUE;
459 }
460
461 static gboolean
462 g_memory_input_stream_seek (GSeekable     *seekable,
463                             goffset        offset,
464                             GSeekType      type,
465                             GCancellable  *cancellable,
466                             GError       **error)
467 {
468   GMemoryInputStream *memory_stream;
469   GMemoryInputStreamPrivate *priv;
470   goffset absolute;
471
472   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
473   priv = memory_stream->priv;
474
475   switch (type) 
476     {
477     case G_SEEK_CUR:
478       absolute = priv->pos + offset;
479       break;
480
481     case G_SEEK_SET:
482       absolute = offset;
483       break;
484
485     case G_SEEK_END:
486       absolute = priv->len + offset;
487       break;
488   
489     default:
490       g_set_error_literal (error,
491                            G_IO_ERROR,
492                            G_IO_ERROR_INVALID_ARGUMENT,
493                            _("Invalid GSeekType supplied"));
494
495       return FALSE;
496     }
497
498   if (absolute < 0 || absolute > priv->len)
499     {
500       g_set_error_literal (error,
501                            G_IO_ERROR,
502                            G_IO_ERROR_INVALID_ARGUMENT,
503                            _("Invalid seek request"));
504       return FALSE;
505     }
506
507   priv->pos = absolute;
508
509   return TRUE;
510 }
511
512 static gboolean
513 g_memory_input_stream_can_truncate (GSeekable *seekable)
514 {
515   return FALSE;
516 }
517
518 static gboolean
519 g_memory_input_stream_truncate (GSeekable     *seekable,
520                                 goffset        offset,
521                                 GCancellable  *cancellable,
522                                 GError       **error)
523 {
524   g_set_error_literal (error,
525                        G_IO_ERROR,
526                        G_IO_ERROR_NOT_SUPPORTED,
527                        _("Cannot truncate GMemoryInputStream"));
528   return FALSE;
529 }