Doc improvements
[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  = g_memory_input_stream_read;
123   istream_class->skip  = g_memory_input_stream_skip;
124   istream_class->close = 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     = 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:
169  * @free_data:
170  * 
171  **/
172 void
173 g_memory_input_stream_set_free_data (GMemoryInputStream *stream,
174                                      gboolean            free_data)
175 {
176   g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
177
178   stream->priv->free_data = free_data;
179 }
180
181 /**
182  * g_memory_input_stream_from_data:
183  * @data: input data.
184  * @len: length of the data.
185  * 
186  * Returns: new #GInputStream read from @data of @len bytes.
187  **/
188 GInputStream *
189 g_memory_input_stream_from_data (const void *data, 
190                                  gssize      len)
191 {
192   GInputStream *stream;
193   GMemoryInputStream *memory_stream;
194
195   g_return_val_if_fail (data != NULL, NULL);
196
197   stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
198   memory_stream = G_MEMORY_INPUT_STREAM (stream);
199
200   if (len == -1)
201     len = strlen (data);
202   
203   memory_stream->priv->buffer = (guint8 *)data;
204   memory_stream->priv->len = len;
205
206   return stream;
207 }
208
209 static gssize
210 g_memory_input_stream_read (GInputStream  *stream,
211                             void          *buffer,
212                             gsize          count,
213                             GCancellable  *cancellable,
214                             GError       **error)
215 {
216   GMemoryInputStream *memory_stream;
217   GMemoryInputStreamPrivate * priv;
218
219   memory_stream = G_MEMORY_INPUT_STREAM (stream);
220   priv = memory_stream->priv;
221
222   count = MIN (count, priv->len - priv->pos);
223   memcpy (buffer, priv->buffer + priv->pos, count);
224   priv->pos += count;
225
226   return count;
227 }
228
229 /**
230  * g_memory_input_stream_get_data:
231  * @stream:
232  * 
233  * Returns: 
234  **/
235 const void *
236 g_memory_input_stream_get_data (GMemoryInputStream *stream)
237 {
238   g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
239
240   return stream->priv->buffer;
241 }
242
243 /**
244  * g_memory_input_stream_get_data_size:
245  * @stream:
246  * 
247  * Returns: 
248  **/
249 gsize
250 g_memory_input_stream_get_data_size (GMemoryInputStream *stream)
251 {
252   g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), -1);
253
254   return stream->priv->len;
255 }
256
257 static gssize
258 g_memory_input_stream_skip (GInputStream  *stream,
259                             gsize          count,
260                             GCancellable  *cancellable,
261                             GError       **error)
262 {
263   GMemoryInputStream *memory_stream;
264   GMemoryInputStreamPrivate *priv;
265
266   memory_stream = G_MEMORY_INPUT_STREAM (stream);
267   priv = memory_stream->priv;
268
269   count = MIN (count, priv->len - priv->pos);
270   priv->pos += count;
271
272   return count;
273  
274
275 }
276
277 static gboolean
278 g_memory_input_stream_close (GInputStream  *stream,
279                              GCancellable  *cancellable,
280                              GError       **error)
281 {
282   return TRUE;
283 }
284
285 static void
286 g_memory_input_stream_read_async (GInputStream        *stream,
287                                   void                *buffer,
288                                   gsize                count,
289                                   int                  io_priority,
290                                   GCancellable        *cancellable,
291                                   GAsyncReadyCallback  callback,
292                                   gpointer             user_data)
293 {
294   GSimpleAsyncResult *simple;
295   gssize nread;
296
297   nread =  g_memory_input_stream_read (stream,  buffer, count, cancellable, NULL);
298   simple = g_simple_async_result_new (G_OBJECT (stream),
299                                       callback,
300                                       user_data,
301                                       g_memory_input_stream_read_async);
302   g_simple_async_result_set_op_res_gssize (simple, nread);
303   g_simple_async_result_complete_in_idle (simple);
304   g_object_unref (simple);
305 }
306
307 static gssize
308 g_memory_input_stream_read_finish (GInputStream  *stream,
309                                    GAsyncResult  *result,
310                                    GError       **error)
311 {
312   GSimpleAsyncResult *simple;
313   gssize nread;
314
315   simple = G_SIMPLE_ASYNC_RESULT (result);
316   g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
317   
318   nread = g_simple_async_result_get_op_res_gssize (simple);
319   return nread;
320 }
321
322 static void
323 g_memory_input_stream_skip_async (GInputStream        *stream,
324                                   gsize                count,
325                                   int                  io_priority,
326                                   GCancellable        *cancellable,
327                                   GAsyncReadyCallback  callback,
328                                   gpointer             user_data)
329 {
330   GSimpleAsyncResult *simple;
331   gssize nskipped;
332
333   nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
334   simple = g_simple_async_result_new (G_OBJECT (stream),
335                                       callback,
336                                       user_data,
337                                       g_memory_input_stream_skip_async);
338   g_simple_async_result_set_op_res_gssize (simple, nskipped);
339   g_simple_async_result_complete_in_idle (simple);
340   g_object_unref (simple);
341 }
342
343 static gssize
344 g_memory_input_stream_skip_finish (GInputStream  *stream,
345                                    GAsyncResult  *result,
346                                    GError       **error)
347 {
348   GSimpleAsyncResult *simple;
349   gssize nskipped;
350
351   simple = G_SIMPLE_ASYNC_RESULT (result);
352   g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
353   
354   nskipped = g_simple_async_result_get_op_res_gssize (simple);
355   return nskipped;
356 }
357
358 static void
359 g_memory_input_stream_close_async (GInputStream        *stream,
360                                    int                  io_priority,
361                                    GCancellable        *cancellable,
362                                    GAsyncReadyCallback  callback,
363                                    gpointer             user_data)
364 {
365   GSimpleAsyncResult *simple;
366   
367   simple = g_simple_async_result_new (G_OBJECT (stream),
368                                       callback,
369                                       user_data,
370                                       g_memory_input_stream_close_async);
371   g_simple_async_result_complete_in_idle (simple);
372   g_object_unref (simple);
373 }
374
375 static gboolean
376 g_memory_input_stream_close_finish (GInputStream  *stream,
377                                     GAsyncResult  *result,
378                                     GError       **error)
379 {
380   return TRUE;
381 }
382
383 static goffset
384 g_memory_input_stream_tell (GSeekable *seekable)
385 {
386   GMemoryInputStream *memory_stream;
387   GMemoryInputStreamPrivate * priv;
388
389   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
390   priv = memory_stream->priv;
391
392   return priv->pos;
393 }
394
395 static
396 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
397 {
398   return TRUE;
399 }
400
401 static gboolean
402 g_memory_input_stream_seek (GSeekable     *seekable,
403                             goffset        offset,
404                             GSeekType      type,
405                             GCancellable  *cancellable,
406                             GError       **error)
407 {
408   GMemoryInputStream *memory_stream;
409   GMemoryInputStreamPrivate * priv;
410   goffset absolute;
411
412   memory_stream = G_MEMORY_INPUT_STREAM (seekable);
413   priv = memory_stream->priv;
414
415   switch (type) 
416     {
417     case G_SEEK_CUR:
418       absolute = priv->pos + offset;
419       break;
420
421     case G_SEEK_SET:
422       absolute = offset;
423       break;
424
425     case G_SEEK_END:
426       absolute = priv->len + offset;
427       break;
428   
429     default:
430       g_set_error (error,
431                    G_IO_ERROR,
432                    G_IO_ERROR_INVALID_ARGUMENT,
433                    "Invalid GSeekType supplied");
434
435       return FALSE;
436     }
437
438   if (absolute < 0 || absolute > priv->len)
439     {
440       g_set_error (error,
441                    G_IO_ERROR,
442                    G_IO_ERROR_INVALID_ARGUMENT,
443                    "Invalid seek request");
444       return FALSE;
445     }
446
447   priv->pos = absolute;
448
449   return TRUE;
450 }
451
452 static gboolean
453 g_memory_input_stream_can_truncate (GSeekable *seekable)
454 {
455   return FALSE;
456 }
457
458 static gboolean
459 g_memory_input_stream_truncate (GSeekable     *seekable,
460                                 goffset        offset,
461                                 GCancellable  *cancellable,
462                                 GError       **error)
463 {
464   g_set_error (error,
465                G_IO_ERROR,
466                G_IO_ERROR_NOT_SUPPORTED,
467                "Cannot seek on GMemoryInputStream");
468   return FALSE;
469 }
470
471 #define __G_MEMORY_INPUT_STREAM_C__
472 #include "gioaliasdef.c"