Fix up GIO docs
[platform/upstream/glib.git] / gio / gconverterinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 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: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "gconverterinputstream.h"
28 #include "gsimpleasyncresult.h"
29 #include "gcancellable.h"
30 #include "gioenumtypes.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33
34 #include "gioalias.h"
35
36 /**
37  * SECTION:gconverterinputstream
38  * @short_description: Converter Input Stream
39  * @include: gio/gio.h
40  * @see_also: #GInputStream, #GConverter
41  *
42  * Converter input stream implements #GInputStream and allows
43  * conversion of data of various types during reading.
44  *
45  **/
46
47 #define INITIAL_BUFFER_SIZE 4096
48
49 typedef struct {
50   char *data;
51   gsize start;
52   gsize end;
53   gsize size;
54 } Buffer;
55
56 struct _GConverterInputStreamPrivate {
57   gboolean at_input_end;
58   gboolean finished;
59   GConverter *converter;
60   Buffer input_buffer;
61   Buffer converted_buffer;
62 };
63
64 enum {
65   PROP_0,
66   PROP_CONVERTER
67 };
68
69 static void   g_converter_input_stream_set_property (GObject       *object,
70                                                      guint          prop_id,
71                                                      const GValue  *value,
72                                                      GParamSpec    *pspec);
73 static void   g_converter_input_stream_get_property (GObject       *object,
74                                                      guint          prop_id,
75                                                      GValue        *value,
76                                                      GParamSpec    *pspec);
77 static void   g_converter_input_stream_finalize     (GObject       *object);
78 static gssize g_converter_input_stream_read         (GInputStream  *stream,
79                                                      void          *buffer,
80                                                      gsize          count,
81                                                      GCancellable  *cancellable,
82                                                      GError       **error);
83
84 G_DEFINE_TYPE (GConverterInputStream,
85                g_converter_input_stream,
86                G_TYPE_FILTER_INPUT_STREAM)
87
88 static void
89 g_converter_input_stream_class_init (GConverterInputStreamClass *klass)
90 {
91   GObjectClass *object_class;
92   GInputStreamClass *istream_class;
93
94   g_type_class_add_private (klass, sizeof (GConverterInputStreamPrivate));
95
96   object_class = G_OBJECT_CLASS (klass);
97   object_class->get_property = g_converter_input_stream_get_property;
98   object_class->set_property = g_converter_input_stream_set_property;
99   object_class->finalize     = g_converter_input_stream_finalize;
100
101   istream_class = G_INPUT_STREAM_CLASS (klass);
102   istream_class->read_fn = g_converter_input_stream_read;
103
104   g_object_class_install_property (object_class,
105                                    PROP_CONVERTER,
106                                    g_param_spec_object ("converter",
107                                                         P_("Converter"),
108                                                         P_("The converter object"),
109                                                         G_TYPE_CONVERTER,
110                                                         G_PARAM_READWRITE|
111                                                         G_PARAM_CONSTRUCT_ONLY|
112                                                         G_PARAM_STATIC_STRINGS));
113
114 }
115
116 static void
117 g_converter_input_stream_finalize (GObject *object)
118 {
119   GConverterInputStreamPrivate *priv;
120   GConverterInputStream        *stream;
121
122   stream = G_CONVERTER_INPUT_STREAM (object);
123   priv = stream->priv;
124
125   g_free (priv->input_buffer.data);
126   g_free (priv->converted_buffer.data);
127   if (priv->converter)
128     g_object_unref (priv->converter);
129
130   G_OBJECT_CLASS (g_converter_input_stream_parent_class)->finalize (object);
131 }
132
133 static void
134 g_converter_input_stream_set_property (GObject      *object,
135                                        guint         prop_id,
136                                        const GValue *value,
137                                        GParamSpec   *pspec)
138 {
139   GConverterInputStream *cstream;
140
141   cstream = G_CONVERTER_INPUT_STREAM (object);
142
143    switch (prop_id)
144     {
145     case PROP_CONVERTER:
146       cstream->priv->converter = g_value_dup_object (value);
147       break;
148
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152     }
153
154 }
155
156 static void
157 g_converter_input_stream_get_property (GObject    *object,
158                                        guint       prop_id,
159                                        GValue     *value,
160                                        GParamSpec *pspec)
161 {
162   GConverterInputStreamPrivate *priv;
163   GConverterInputStream        *cstream;
164
165   cstream = G_CONVERTER_INPUT_STREAM (object);
166   priv = cstream->priv;
167
168   switch (prop_id)
169     {
170     case PROP_CONVERTER:
171       g_value_set_object (value, priv->converter);
172       break;
173
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177     }
178
179 }
180 static void
181 g_converter_input_stream_init (GConverterInputStream *stream)
182 {
183   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
184                                               G_TYPE_CONVERTER_INPUT_STREAM,
185                                               GConverterInputStreamPrivate);
186 }
187
188 /**
189  * g_converter_input_stream_new:
190  * @base_stream: a #GInputStream
191  * @converter: a #GConverter
192  *
193  * Creates a new converter input stream for the @base_stream.
194  *
195  * Returns: a new #GInputStream.
196  **/
197 GInputStream *
198 g_converter_input_stream_new (GInputStream *base_stream,
199                               GConverter   *converter)
200 {
201   GInputStream *stream;
202
203   g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
204
205   stream = g_object_new (G_TYPE_CONVERTER_INPUT_STREAM,
206                          "base-stream", base_stream,
207                          "converter", converter,
208                          NULL);
209
210   return stream;
211 }
212
213 static gsize
214 buffer_available (Buffer *buffer)
215 {
216   return buffer->end - buffer->start;
217 }
218
219 static gsize
220 buffer_tailspace (Buffer *buffer)
221 {
222   return buffer->size - buffer->end;
223 }
224
225 static char *
226 buffer_data (Buffer *buffer)
227 {
228   return buffer->data + buffer->start;
229 }
230
231 static void
232 buffer_consumed (Buffer *buffer,
233                  gsize count)
234 {
235   buffer->start += count;
236   if (buffer->start == buffer->end)
237     buffer->start = buffer->end = 0;
238 }
239
240 static void
241 buffer_read (Buffer *buffer,
242              char *dest,
243              gsize count)
244 {
245   memcpy (dest, buffer->data + buffer->start, count);
246   buffer_consumed (buffer, count);
247 }
248
249 static void
250 compact_buffer (Buffer *buffer)
251 {
252   gsize in_buffer;
253
254   in_buffer = buffer_available (buffer);
255   memmove (buffer->data,
256            buffer->data + buffer->start,
257            in_buffer);
258   buffer->end -= buffer->start;
259   buffer->start = 0;
260 }
261
262 static void
263 grow_buffer (Buffer *buffer)
264 {
265   char *data;
266   gsize size, in_buffer;
267
268   if (buffer->size == 0)
269     size = INITIAL_BUFFER_SIZE;
270   else
271     size = buffer->size * 2;
272
273   data = g_malloc (size);
274   in_buffer = buffer_available (buffer);
275
276   memcpy (data,
277           buffer->data + buffer->start,
278           in_buffer);
279   g_free (buffer->data);
280   buffer->data = data;
281   buffer->end -= buffer->start;
282   buffer->start = 0;
283   buffer->size = size;
284 }
285
286 static void
287 buffer_ensure_space (Buffer *buffer,
288                      gsize at_least_size)
289 {
290   gsize in_buffer, left_to_fill;
291
292   in_buffer = buffer_available (buffer);
293
294   if (in_buffer >= at_least_size)
295     return;
296
297   left_to_fill = buffer_tailspace (buffer);
298
299   if (in_buffer + left_to_fill >= at_least_size)
300     {
301       /* We fit in remaining space at end */
302       /* If the copy is small, compact now anyway so we can fill more */
303       if (in_buffer < 256)
304         compact_buffer (buffer);
305     }
306   else if (buffer->size >= at_least_size)
307     {
308       /* We fit, but only if we compact */
309       compact_buffer (buffer);
310     }
311   else
312     {
313       /* Need to grow buffer */
314       while (buffer->size < at_least_size)
315         grow_buffer (buffer);
316     }
317 }
318
319 static gssize
320 fill_input_buffer (GConverterInputStream  *stream,
321                    gsize                   at_least_size,
322                    GCancellable           *cancellable,
323                    GError                **error)
324 {
325   GConverterInputStreamPrivate *priv;
326   GInputStream *base_stream;
327   gssize nread;
328
329   priv = stream->priv;
330
331   buffer_ensure_space (&priv->input_buffer, at_least_size);
332
333   base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
334   nread = g_input_stream_read (base_stream,
335                                priv->input_buffer.data + priv->input_buffer.end,
336                                buffer_tailspace (&priv->input_buffer),
337                                cancellable,
338                                error);
339
340   if (nread > 0)
341     priv->input_buffer.end += nread;
342
343   return nread;
344 }
345
346
347 static gssize
348 g_converter_input_stream_read (GInputStream *stream,
349                                void         *buffer,
350                                gsize         count,
351                                GCancellable *cancellable,
352                                GError      **error)
353 {
354   GConverterInputStream *cstream;
355   GConverterInputStreamPrivate *priv;
356   gsize available, total_bytes_read;
357   gssize nread;
358   GConverterResult res;
359   gsize bytes_read;
360   gsize bytes_written;
361   GError *my_error;
362   GError *my_error2;
363
364   cstream = G_CONVERTER_INPUT_STREAM (stream);
365   priv = cstream->priv;
366
367   available = buffer_available (&priv->converted_buffer);
368
369   if (available > 0 &&
370       count <= available)
371     {
372       /* Converted data available, return that */
373       buffer_read (&priv->converted_buffer, buffer, count);
374       return count;
375     }
376
377   /* Full request not available, read all currently available and request
378      refill/conversion for more */
379
380   buffer_read (&priv->converted_buffer, buffer, available);
381
382   total_bytes_read = available;
383   count -= available;
384
385   /* If there is no data to convert, and no pre-converted data,
386      do some i/o for more input */
387   if (buffer_available (&priv->input_buffer) == 0 &&
388       total_bytes_read == 0 &&
389       !priv->at_input_end)
390     {
391       nread = fill_input_buffer (cstream, count, cancellable, error);
392       if (nread < 0)
393         return -1;
394       if (nread == 0)
395         priv->at_input_end = TRUE;
396     }
397
398   /* First try to convert any available data (or state) directly to the user buffer: */
399   if (!priv->finished)
400     {
401       my_error = NULL;
402       res = g_converter_convert (priv->converter,
403                                  buffer_data (&priv->input_buffer),
404                                  buffer_available (&priv->input_buffer),
405                                  buffer, count,
406                                  priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
407                                  &bytes_read,
408                                  &bytes_written,
409                                  &my_error);
410       if (res != G_CONVERTER_ERROR)
411         {
412           total_bytes_read += bytes_written;
413           buffer_consumed (&priv->input_buffer, bytes_read);
414           if (res == G_CONVERTER_FINISHED)
415             priv->finished = TRUE; /* We're done converting */
416         }
417       else if (total_bytes_read == 0 &&
418                !g_error_matches (my_error,
419                                  G_IO_ERROR,
420                                  G_IO_ERROR_PARTIAL_INPUT) &&
421                !g_error_matches (my_error,
422                                  G_IO_ERROR,
423                                  G_IO_ERROR_NO_SPACE))
424         {
425           /* No previously read data and no "special" error, return error */
426           g_propagate_error (error, my_error);
427           return -1;
428         }
429       else
430         g_error_free (my_error);
431     }
432
433   /* We had some pre-converted data and/or we converted directly to the
434      user buffer */
435   if (total_bytes_read > 0)
436     return total_bytes_read;
437
438   /* If there is no more to convert, return EOF */
439   if (priv->finished)
440     {
441       g_assert (buffer_available (&priv->converted_buffer) == 0);
442       return 0;
443     }
444
445   /* There was "complexity" in the straight-to-buffer conversion,
446    * convert to our own buffer and write from that.
447    * At this point we didn't produce any data into @buffer.
448    */
449
450   /* Ensure we have *some* initial target space */
451   buffer_ensure_space (&priv->converted_buffer, count);
452
453   while (TRUE)
454     {
455       g_assert (!priv->finished);
456
457       /* Try to convert to our buffer */
458       my_error = NULL;
459       res = g_converter_convert (priv->converter,
460                                  buffer_data (&priv->input_buffer),
461                                  buffer_available (&priv->input_buffer),
462                                  buffer_data (&priv->converted_buffer),
463                                  buffer_tailspace (&priv->converted_buffer),
464                                  priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
465                                  &bytes_read,
466                                  &bytes_written,
467                                  &my_error);
468       if (res != G_CONVERTER_ERROR)
469         {
470           priv->converted_buffer.end += bytes_written;
471           buffer_consumed (&priv->input_buffer, bytes_read);
472
473           /* Maybe we consumed without producing any output */
474           if (buffer_available (&priv->converted_buffer) == 0 && res != G_CONVERTER_FINISHED)
475             continue; /* Convert more */
476
477           if (res == G_CONVERTER_FINISHED)
478             priv->finished = TRUE;
479
480           total_bytes_read = MIN (count, buffer_available (&priv->converted_buffer));
481           buffer_read (&priv->converted_buffer, buffer, total_bytes_read);
482
483           g_assert (priv->finished || total_bytes_read > 0);
484
485           return total_bytes_read;
486         }
487
488       /* There was some kind of error filling our buffer */
489
490       if (g_error_matches (my_error,
491                            G_IO_ERROR,
492                            G_IO_ERROR_PARTIAL_INPUT) &&
493           !priv->at_input_end)
494         {
495           /* Need more data */
496           my_error2 = NULL;
497           res = fill_input_buffer (cstream,
498                                    buffer_available (&priv->input_buffer) + 4096,
499                                    cancellable,
500                                    &my_error2);
501           if (res < 0)
502             {
503               /* Can't read any more data, return that error */
504               g_error_free (my_error);
505               g_propagate_error (error, my_error2);
506               return -1;
507             }
508           else if (res == 0)
509             {
510               /* End of file, try INPUT_AT_END */
511               priv->at_input_end = TRUE;
512             }
513           g_error_free (my_error);
514           continue;
515         }
516
517       if (g_error_matches (my_error,
518                            G_IO_ERROR,
519                            G_IO_ERROR_NO_SPACE))
520         {
521           /* Need more destination space, grow it
522            * Note: if we actually grow the buffer (as opposed to compacting it),
523            * this will double the size, not just add one byte. */
524           buffer_ensure_space (&priv->converted_buffer,
525                                priv->converted_buffer.size + 1);
526           g_error_free (my_error);
527           continue;
528         }
529
530       /* Any other random error, return it */
531       g_propagate_error (error, my_error);
532       return -1;
533     }
534
535   g_assert_not_reached ();
536 }
537
538 /**
539  * g_converter_input_stream_get_converter:
540  * @converter_stream: a #GConverterInputStream
541  *
542  * Gets the #GConverter that is used by @converter_stream.
543  *
544  * Returns: the converter of the converter input stream
545  *
546  * Since: 2.24
547  */
548 GConverter *
549 g_converter_input_stream_get_converter (GConverterInputStream *converter_stream)
550 {
551   return converter_stream->priv->converter;
552 }
553
554 #define __G_CONVERTER_INPUT_STREAM_C__
555 #include "gioaliasdef.c"