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