Documentation updates
[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 "glibintl.h"
30
31 #include "gioalias.h"
32
33 /**
34  * SECTION:gmemoryinputstream
35  * @short_description: Streaming input operations on memory chunks
36  * @include: 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_foreach (priv->chunks, free_chunk, NULL);
164   g_slist_free (priv->chunks);
165
166   if (G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize)
167     (*G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize) (object);
168 }
169
170 static void
171 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
172 {
173   iface->tell         = g_memory_input_stream_tell;
174   iface->can_seek     = g_memory_input_stream_can_seek;
175   iface->seek         = g_memory_input_stream_seek;
176   iface->can_truncate = g_memory_input_stream_can_truncate;
177   iface->truncate_fn  = g_memory_input_stream_truncate;
178 }
179
180 static void
181 g_memory_input_stream_init (GMemoryInputStream *stream)
182 {
183   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
184                                               G_TYPE_MEMORY_INPUT_STREAM,
185                                               GMemoryInputStreamPrivate);
186 }
187
188 /**
189  * g_memory_input_stream_new:
190  *
191  * Creates a new empty #GMemoryInputStream. 
192  *
193  * Returns: a new #GInputStream
194  */
195 GInputStream *
196 g_memory_input_stream_new (void)
197 {
198   GInputStream *stream;
199
200   stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
201
202   return stream;
203 }
204
205 /**
206  * g_memory_input_stream_new_from_data:
207  * @data: input data
208  * @len: length of the data, may be -1 if @data is a nul-terminated string
209  * @destroy: function that is called to free @data, or %NULL
210  *
211  * Creates a new #GMemoryInputStream with data in memory of a given size.
212  * 
213  * Returns: new #GInputStream read from @data of @len bytes.
214  **/
215 GInputStream *
216 g_memory_input_stream_new_from_data (const void     *data, 
217                                      gssize          len,
218                                      GDestroyNotify  destroy)
219 {
220   GInputStream *stream;
221
222   stream = g_memory_input_stream_new ();
223
224   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
225                                   data, len, destroy);
226
227   return stream;
228 }
229
230 /**
231  * g_memory_input_stream_add_data:
232  * @stream: a #GMemoryInputStream
233  * @data: input data
234  * @len: length of the data, may be -1 if @data is a nul-terminated string
235  * @destroy: function that is called to free @data, or %NULL
236  *
237  * Appends @data to data that can be read from the input stream
238  */
239 void
240 g_memory_input_stream_add_data (GMemoryInputStream *stream,
241                                 const void         *data,
242                                 gssize              len,
243                                 GDestroyNotify      destroy)
244 {
245   GMemoryInputStreamPrivate *priv;
246   Chunk *chunk;
247  
248   g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
249   g_return_if_fail (data != NULL);
250
251   priv = stream->priv;
252
253   if (len == -1)
254     len = strlen (data);
255   
256   chunk = g_slice_new (Chunk);
257   chunk->data = (guint8 *)data;
258   chunk->len = len;
259   chunk->destroy = destroy;
260
261   priv->chunks = g_slist_append (priv->chunks, chunk);
262   priv->len += chunk->len;
263 }
264
265 static gssize
266 g_memory_input_stream_read (GInputStream  *stream,
267                             void          *buffer,
268                             gsize          count,
269                             GCancellable  *cancellable,
270                             GError       **error)
271 {
272   GMemoryInputStream *memory_stream;
273   GMemoryInputStreamPrivate *priv;
274   GSList *l;
275   Chunk *chunk;
276   gsize offset, start, rest, size;
277
278   memory_stream = G_MEMORY_INPUT_STREAM (stream);
279   priv = memory_stream->priv;
280
281   count = MIN (count, priv->len - priv->pos);
282
283   offset = 0;
284   for (l = priv->chunks; l; l = l->next) 
285     {
286       chunk = (Chunk *)l->data;
287
288       if (offset + chunk->len > priv->pos)
289         break;
290
291       offset += chunk->len;
292     }
293   
294   start = priv->pos - offset;
295   rest = count;
296
297   for (; l && rest > 0; l = l->next)
298     {
299       chunk = (Chunk *)l->data;
300       size = MIN (rest, chunk->len - start);
301
302       memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
303       rest -= size;
304
305       start = 0;
306     }
307
308   priv->pos += count;
309
310   return count;
311 }
312
313 static gssize
314 g_memory_input_stream_skip (GInputStream  *stream,
315                             gsize          count,
316                             GCancellable  *cancellable,
317                             GError       **error)
318 {
319   GMemoryInputStream *memory_stream;
320   GMemoryInputStreamPrivate *priv;
321
322   memory_stream = G_MEMORY_INPUT_STREAM (stream);
323   priv = memory_stream->priv;
324
325   count = MIN (count, priv->len - priv->pos);
326   priv->pos += count;
327
328   return count;
329 }
330
331 static gboolean
332 g_memory_input_stream_close (GInputStream  *stream,
333                              GCancellable  *cancellable,
334                              GError       **error)
335 {
336   return TRUE;
337 }
338
339 static void
340 g_memory_input_stream_read_async (GInputStream        *stream,
341                                   void                *buffer,
342                                   gsize                count,
343                                   int                  io_priority,
344                                   GCancellable        *cancellable,
345                                   GAsyncReadyCallback  callback,
346                                   gpointer             user_data)
347 {
348   GSimpleAsyncResult *simple;
349   gssize nread;
350
351   nread = g_memory_input_stream_read (stream, buffer, count, cancellable, NULL);
352   simple = g_simple_async_result_new (G_OBJECT (stream),
353                                       callback,
354                                       user_data,
355                                       g_memory_input_stream_read_async);
356   g_simple_async_result_set_op_res_gssize (simple, nread);
357   g_simple_async_result_complete_in_idle (simple);
358   g_object_unref (simple);
359 }
360
361 static gssize
362 g_memory_input_stream_read_finish (GInputStream  *stream,
363                                    GAsyncResult  *result,
364                                    GError       **error)
365 {
366   GSimpleAsyncResult *simple;
367   gssize nread;
368
369   simple = G_SIMPLE_ASYNC_RESULT (result);
370   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
371   
372   nread = g_simple_async_result_get_op_res_gssize (simple);
373   return nread;
374 }
375
376 static void
377 g_memory_input_stream_skip_async (GInputStream        *stream,
378                                   gsize                count,
379                                   int                  io_priority,
380                                   GCancellable        *cancellable,
381                                   GAsyncReadyCallback  callback,
382                                   gpointer             user_data)
383 {
384   GSimpleAsyncResult *simple;
385   gssize nskipped;
386
387   nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
388   simple = g_simple_async_result_new (G_OBJECT (stream),
389                                       callback,
390                                       user_data,
391                                       g_memory_input_stream_skip_async);
392   g_simple_async_result_set_op_res_gssize (simple, nskipped);
393   g_simple_async_result_complete_in_idle (simple);
394   g_object_unref (simple);
395 }
396
397 static gssize
398 g_memory_input_stream_skip_finish (GInputStream  *stream,
399                                    GAsyncResult  *result,
400                                    GError       **error)
401 {
402   GSimpleAsyncResult *simple;
403   gssize nskipped;
404
405   simple = G_SIMPLE_ASYNC_RESULT (result);
406   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
407   
408   nskipped = g_simple_async_result_get_op_res_gssize (simple);
409   return nskipped;
410 }
411
412 static void
413 g_memory_input_stream_close_async (GInputStream        *stream,
414                                    int                  io_priority,
415                                    GCancellable        *cancellable,
416                                    GAsyncReadyCallback  callback,
417                                    gpointer             user_data)
418 {
419   GSimpleAsyncResult *simple;
420   
421   simple = g_simple_async_result_new (G_OBJECT (stream),
422                                       callback,
423                                       user_data,
424                                       g_memory_input_stream_close_async);
425   g_simple_async_result_complete_in_idle (simple);
426   g_object_unref (simple);
427 }
428
429 static gboolean
430 g_memory_input_stream_close_finish (GInputStream  *stream,
431                                     GAsyncResult  *result,
432                                     GError       **error)
433 {
434   return TRUE;
435 }
436
437 static goffset
438 g_memory_input_stream_tell (GSeekable *seekable)
439 {
440   GMemoryInputStream *memory_stream;
441   GMemoryInputStreamPrivate *priv;
442
443   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
444   priv = memory_stream->priv;
445
446   return priv->pos;
447 }
448
449 static
450 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
451 {
452   return TRUE;
453 }
454
455 static gboolean
456 g_memory_input_stream_seek (GSeekable     *seekable,
457                             goffset        offset,
458                             GSeekType      type,
459                             GCancellable  *cancellable,
460                             GError       **error)
461 {
462   GMemoryInputStream *memory_stream;
463   GMemoryInputStreamPrivate *priv;
464   goffset absolute;
465
466   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
467   priv = memory_stream->priv;
468
469   switch (type) 
470     {
471     case G_SEEK_CUR:
472       absolute = priv->pos + offset;
473       break;
474
475     case G_SEEK_SET:
476       absolute = offset;
477       break;
478
479     case G_SEEK_END:
480       absolute = priv->len + offset;
481       break;
482   
483     default:
484       g_set_error (error,
485                    G_IO_ERROR,
486                    G_IO_ERROR_INVALID_ARGUMENT,
487                    _("Invalid GSeekType supplied"));
488
489       return FALSE;
490     }
491
492   if (absolute < 0 || absolute > priv->len)
493     {
494       g_set_error (error,
495                    G_IO_ERROR,
496                    G_IO_ERROR_INVALID_ARGUMENT,
497                    _("Invalid seek request"));
498       return FALSE;
499     }
500
501   priv->pos = absolute;
502
503   return TRUE;
504 }
505
506 static gboolean
507 g_memory_input_stream_can_truncate (GSeekable *seekable)
508 {
509   return FALSE;
510 }
511
512 static gboolean
513 g_memory_input_stream_truncate (GSeekable     *seekable,
514                                 goffset        offset,
515                                 GCancellable  *cancellable,
516                                 GError       **error)
517 {
518   g_set_error (error,
519                G_IO_ERROR,
520                G_IO_ERROR_NOT_SUPPORTED,
521                _("Cannot truncate GMemoryInputStream"));
522   return FALSE;
523 }
524
525 #define __G_MEMORY_INPUT_STREAM_C__
526 #include "gioaliasdef.c"