regex: unicode: Update to Unicode 6.1.0
[glib.git] / gio / gdatainputstream.c
blobea26aeb36a5058c508df7edc4538544b1e1a4e3d
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
5 * Copyright © 2009 Codethink Limited
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "config.h"
26 #include "gdatainputstream.h"
27 #include "gsimpleasyncresult.h"
28 #include "gcancellable.h"
29 #include "gioenumtypes.h"
30 #include "gioerror.h"
31 #include "glibintl.h"
33 #include <string.h>
35 /**
36 * SECTION:gdatainputstream
37 * @short_description: Data Input Stream
38 * @include: gio/gio.h
39 * @see_also: #GInputStream
41 * Data input stream implements #GInputStream and includes functions for
42 * reading structured data directly from a binary input stream.
44 **/
46 struct _GDataInputStreamPrivate {
47 GDataStreamByteOrder byte_order;
48 GDataStreamNewlineType newline_type;
51 enum {
52 PROP_0,
53 PROP_BYTE_ORDER,
54 PROP_NEWLINE_TYPE
57 static void g_data_input_stream_set_property (GObject *object,
58 guint prop_id,
59 const GValue *value,
60 GParamSpec *pspec);
61 static void g_data_input_stream_get_property (GObject *object,
62 guint prop_id,
63 GValue *value,
64 GParamSpec *pspec);
66 G_DEFINE_TYPE (GDataInputStream,
67 g_data_input_stream,
68 G_TYPE_BUFFERED_INPUT_STREAM)
71 static void
72 g_data_input_stream_class_init (GDataInputStreamClass *klass)
74 GObjectClass *object_class;
76 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
78 object_class = G_OBJECT_CLASS (klass);
79 object_class->get_property = g_data_input_stream_get_property;
80 object_class->set_property = g_data_input_stream_set_property;
82 /**
83 * GDataStream:byte-order:
85 * The ::byte-order property determines the byte ordering that
86 * is used when reading multi-byte entities (such as integers)
87 * from the stream.
88 */
89 g_object_class_install_property (object_class,
90 PROP_BYTE_ORDER,
91 g_param_spec_enum ("byte-order",
92 P_("Byte order"),
93 P_("The byte order"),
94 G_TYPE_DATA_STREAM_BYTE_ORDER,
95 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
96 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
98 /**
99 * GDataStream:newline-type:
101 * The :newline-type property determines what is considered
102 * as a line ending when reading complete lines from the stream.
104 g_object_class_install_property (object_class,
105 PROP_NEWLINE_TYPE,
106 g_param_spec_enum ("newline-type",
107 P_("Newline type"),
108 P_("The accepted types of line ending"),
109 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
110 G_DATA_STREAM_NEWLINE_TYPE_LF,
111 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
114 static void
115 g_data_input_stream_set_property (GObject *object,
116 guint prop_id,
117 const GValue *value,
118 GParamSpec *pspec)
120 GDataInputStream *dstream;
122 dstream = G_DATA_INPUT_STREAM (object);
124 switch (prop_id)
126 case PROP_BYTE_ORDER:
127 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
128 break;
130 case PROP_NEWLINE_TYPE:
131 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
132 break;
134 default:
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 break;
141 static void
142 g_data_input_stream_get_property (GObject *object,
143 guint prop_id,
144 GValue *value,
145 GParamSpec *pspec)
147 GDataInputStreamPrivate *priv;
148 GDataInputStream *dstream;
150 dstream = G_DATA_INPUT_STREAM (object);
151 priv = dstream->priv;
153 switch (prop_id)
155 case PROP_BYTE_ORDER:
156 g_value_set_enum (value, priv->byte_order);
157 break;
159 case PROP_NEWLINE_TYPE:
160 g_value_set_enum (value, priv->newline_type);
161 break;
163 default:
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 break;
169 static void
170 g_data_input_stream_init (GDataInputStream *stream)
172 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
173 G_TYPE_DATA_INPUT_STREAM,
174 GDataInputStreamPrivate);
176 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
177 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
181 * g_data_input_stream_new:
182 * @base_stream: a #GInputStream.
184 * Creates a new data input stream for the @base_stream.
186 * Returns: a new #GDataInputStream.
188 GDataInputStream *
189 g_data_input_stream_new (GInputStream *base_stream)
191 GDataInputStream *stream;
193 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
195 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
196 "base-stream", base_stream,
197 NULL);
199 return stream;
203 * g_data_input_stream_set_byte_order:
204 * @stream: a given #GDataInputStream.
205 * @order: a #GDataStreamByteOrder to set.
207 * This function sets the byte order for the given @stream. All subsequent
208 * reads from the @stream will be read in the given @order.
211 void
212 g_data_input_stream_set_byte_order (GDataInputStream *stream,
213 GDataStreamByteOrder order)
215 GDataInputStreamPrivate *priv;
217 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
219 priv = stream->priv;
221 if (priv->byte_order != order)
223 priv->byte_order = order;
225 g_object_notify (G_OBJECT (stream), "byte-order");
230 * g_data_input_stream_get_byte_order:
231 * @stream: a given #GDataInputStream.
233 * Gets the byte order for the data input stream.
235 * Returns: the @stream's current #GDataStreamByteOrder.
237 GDataStreamByteOrder
238 g_data_input_stream_get_byte_order (GDataInputStream *stream)
240 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
242 return stream->priv->byte_order;
246 * g_data_input_stream_set_newline_type:
247 * @stream: a #GDataInputStream.
248 * @type: the type of new line return as #GDataStreamNewlineType.
250 * Sets the newline type for the @stream.
252 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
253 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
254 * "CR LF", and this might block if there is no more data available.
257 void
258 g_data_input_stream_set_newline_type (GDataInputStream *stream,
259 GDataStreamNewlineType type)
261 GDataInputStreamPrivate *priv;
263 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
265 priv = stream->priv;
267 if (priv->newline_type != type)
269 priv->newline_type = type;
271 g_object_notify (G_OBJECT (stream), "newline-type");
276 * g_data_input_stream_get_newline_type:
277 * @stream: a given #GDataInputStream.
279 * Gets the current newline type for the @stream.
281 * Returns: #GDataStreamNewlineType for the given @stream.
283 GDataStreamNewlineType
284 g_data_input_stream_get_newline_type (GDataInputStream *stream)
286 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
288 return stream->priv->newline_type;
291 static gboolean
292 read_data (GDataInputStream *stream,
293 void *buffer,
294 gsize size,
295 GCancellable *cancellable,
296 GError **error)
298 gsize available;
299 gssize res;
301 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
303 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
304 size - available,
305 cancellable, error);
306 if (res < 0)
307 return FALSE;
308 if (res == 0)
310 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
311 _("Unexpected early end-of-stream"));
312 return FALSE;
316 /* This should always succeed, since it's in the buffer */
317 res = g_input_stream_read (G_INPUT_STREAM (stream),
318 buffer, size,
319 NULL, NULL);
320 g_warn_if_fail (res == size);
321 return TRUE;
326 * g_data_input_stream_read_byte:
327 * @stream: a given #GDataInputStream.
328 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
329 * @error: #GError for error reporting.
331 * Reads an unsigned 8-bit/1-byte value from @stream.
333 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
334 * if an error occurred.
336 guchar
337 g_data_input_stream_read_byte (GDataInputStream *stream,
338 GCancellable *cancellable,
339 GError **error)
341 guchar c;
343 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
345 if (read_data (stream, &c, 1, cancellable, error))
346 return c;
348 return 0;
353 * g_data_input_stream_read_int16:
354 * @stream: a given #GDataInputStream.
355 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
356 * @error: #GError for error reporting.
358 * Reads a 16-bit/2-byte value from @stream.
360 * In order to get the correct byte order for this read operation,
361 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
363 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
364 * an error occurred.
366 gint16
367 g_data_input_stream_read_int16 (GDataInputStream *stream,
368 GCancellable *cancellable,
369 GError **error)
371 gint16 v;
373 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
375 if (read_data (stream, &v, 2, cancellable, error))
377 switch (stream->priv->byte_order)
379 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
380 v = GINT16_FROM_BE (v);
381 break;
382 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
383 v = GINT16_FROM_LE (v);
384 break;
385 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
386 default:
387 break;
389 return v;
392 return 0;
397 * g_data_input_stream_read_uint16:
398 * @stream: a given #GDataInputStream.
399 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
400 * @error: #GError for error reporting.
402 * Reads an unsigned 16-bit/2-byte value from @stream.
404 * In order to get the correct byte order for this read operation,
405 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
407 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
408 * an error occurred.
410 guint16
411 g_data_input_stream_read_uint16 (GDataInputStream *stream,
412 GCancellable *cancellable,
413 GError **error)
415 guint16 v;
417 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
419 if (read_data (stream, &v, 2, cancellable, error))
421 switch (stream->priv->byte_order)
423 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
424 v = GUINT16_FROM_BE (v);
425 break;
426 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
427 v = GUINT16_FROM_LE (v);
428 break;
429 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
430 default:
431 break;
433 return v;
436 return 0;
441 * g_data_input_stream_read_int32:
442 * @stream: a given #GDataInputStream.
443 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
444 * @error: #GError for error reporting.
446 * Reads a signed 32-bit/4-byte value from @stream.
448 * In order to get the correct byte order for this read operation,
449 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
451 * If @cancellable is not %NULL, then the operation can be cancelled by
452 * triggering the cancellable object from another thread. If the operation
453 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
455 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
456 * an error occurred.
458 gint32
459 g_data_input_stream_read_int32 (GDataInputStream *stream,
460 GCancellable *cancellable,
461 GError **error)
463 gint32 v;
465 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
467 if (read_data (stream, &v, 4, cancellable, error))
469 switch (stream->priv->byte_order)
471 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
472 v = GINT32_FROM_BE (v);
473 break;
474 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
475 v = GINT32_FROM_LE (v);
476 break;
477 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
478 default:
479 break;
481 return v;
484 return 0;
489 * g_data_input_stream_read_uint32:
490 * @stream: a given #GDataInputStream.
491 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
492 * @error: #GError for error reporting.
494 * Reads an unsigned 32-bit/4-byte value from @stream.
496 * In order to get the correct byte order for this read operation,
497 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
499 * If @cancellable is not %NULL, then the operation can be cancelled by
500 * triggering the cancellable object from another thread. If the operation
501 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
503 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
504 * an error occurred.
506 guint32
507 g_data_input_stream_read_uint32 (GDataInputStream *stream,
508 GCancellable *cancellable,
509 GError **error)
511 guint32 v;
513 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
515 if (read_data (stream, &v, 4, cancellable, error))
517 switch (stream->priv->byte_order)
519 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
520 v = GUINT32_FROM_BE (v);
521 break;
522 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
523 v = GUINT32_FROM_LE (v);
524 break;
525 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
526 default:
527 break;
529 return v;
532 return 0;
537 * g_data_input_stream_read_int64:
538 * @stream: a given #GDataInputStream.
539 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
540 * @error: #GError for error reporting.
542 * Reads a 64-bit/8-byte value from @stream.
544 * In order to get the correct byte order for this read operation,
545 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
547 * If @cancellable is not %NULL, then the operation can be cancelled by
548 * triggering the cancellable object from another thread. If the operation
549 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
551 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
552 * an error occurred.
554 gint64
555 g_data_input_stream_read_int64 (GDataInputStream *stream,
556 GCancellable *cancellable,
557 GError **error)
559 gint64 v;
561 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
563 if (read_data (stream, &v, 8, cancellable, error))
565 switch (stream->priv->byte_order)
567 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
568 v = GINT64_FROM_BE (v);
569 break;
570 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
571 v = GINT64_FROM_LE (v);
572 break;
573 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
574 default:
575 break;
577 return v;
580 return 0;
585 * g_data_input_stream_read_uint64:
586 * @stream: a given #GDataInputStream.
587 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
588 * @error: #GError for error reporting.
590 * Reads an unsigned 64-bit/8-byte value from @stream.
592 * In order to get the correct byte order for this read operation,
593 * see g_data_input_stream_get_byte_order().
595 * If @cancellable is not %NULL, then the operation can be cancelled by
596 * triggering the cancellable object from another thread. If the operation
597 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
599 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
600 * an error occurred.
602 guint64
603 g_data_input_stream_read_uint64 (GDataInputStream *stream,
604 GCancellable *cancellable,
605 GError **error)
607 guint64 v;
609 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
611 if (read_data (stream, &v, 8, cancellable, error))
613 switch (stream->priv->byte_order)
615 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
616 v = GUINT64_FROM_BE (v);
617 break;
618 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
619 v = GUINT64_FROM_LE (v);
620 break;
621 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
622 default:
623 break;
625 return v;
628 return 0;
631 static gssize
632 scan_for_newline (GDataInputStream *stream,
633 gsize *checked_out,
634 gboolean *last_saw_cr_out,
635 int *newline_len_out)
637 GBufferedInputStream *bstream;
638 GDataInputStreamPrivate *priv;
639 const char *buffer;
640 gsize start, end, peeked;
641 int i;
642 gssize found_pos;
643 int newline_len;
644 gsize available, checked;
645 gboolean last_saw_cr;
647 priv = stream->priv;
649 bstream = G_BUFFERED_INPUT_STREAM (stream);
651 checked = *checked_out;
652 last_saw_cr = *last_saw_cr_out;
653 found_pos = -1;
654 newline_len = 0;
656 start = checked;
657 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
658 end = available;
659 peeked = end - start;
661 for (i = 0; checked < available && i < peeked; i++)
663 switch (priv->newline_type)
665 case G_DATA_STREAM_NEWLINE_TYPE_LF:
666 if (buffer[i] == 10)
668 found_pos = start + i;
669 newline_len = 1;
671 break;
672 case G_DATA_STREAM_NEWLINE_TYPE_CR:
673 if (buffer[i] == 13)
675 found_pos = start + i;
676 newline_len = 1;
678 break;
679 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
680 if (last_saw_cr && buffer[i] == 10)
682 found_pos = start + i - 1;
683 newline_len = 2;
685 break;
686 default:
687 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
688 if (buffer[i] == 10) /* LF */
690 if (last_saw_cr)
692 /* CR LF */
693 found_pos = start + i - 1;
694 newline_len = 2;
696 else
698 /* LF */
699 found_pos = start + i;
700 newline_len = 1;
703 else if (last_saw_cr)
705 /* Last was cr, this is not LF, end is CR */
706 found_pos = start + i - 1;
707 newline_len = 1;
709 /* Don't check for CR here, instead look at last_saw_cr on next byte */
710 break;
713 last_saw_cr = (buffer[i] == 13);
715 if (found_pos != -1)
717 *newline_len_out = newline_len;
718 return found_pos;
722 checked = end;
724 *checked_out = checked;
725 *last_saw_cr_out = last_saw_cr;
726 return -1;
731 * g_data_input_stream_read_line:
732 * @stream: a given #GDataInputStream.
733 * @length: (out): a #gsize to get the length of the data read in.
734 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
735 * @error: #GError for error reporting.
737 * Reads a line from the data input stream. Note that no encoding
738 * checks or conversion is performed; the input is not guaranteed to
739 * be UTF-8, and may in fact have embedded NUL characters.
741 * If @cancellable is not %NULL, then the operation can be cancelled by
742 * triggering the cancellable object from another thread. If the operation
743 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
745 * Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
746 * NUL terminated byte array with the line that was read in (without
747 * the newlines). Set @length to a #gsize to get the length of the
748 * read line. On an error, it will return %NULL and @error will be
749 * set. If there's no content to read, it will still return %NULL,
750 * but @error won't be set.
752 char *
753 g_data_input_stream_read_line (GDataInputStream *stream,
754 gsize *length,
755 GCancellable *cancellable,
756 GError **error)
758 GBufferedInputStream *bstream;
759 gsize checked;
760 gboolean last_saw_cr;
761 gssize found_pos;
762 gssize res;
763 int newline_len;
764 char *line;
766 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
768 bstream = G_BUFFERED_INPUT_STREAM (stream);
770 newline_len = 0;
771 checked = 0;
772 last_saw_cr = FALSE;
774 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
776 if (g_buffered_input_stream_get_available (bstream) ==
777 g_buffered_input_stream_get_buffer_size (bstream))
778 g_buffered_input_stream_set_buffer_size (bstream,
779 2 * g_buffered_input_stream_get_buffer_size (bstream));
781 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
782 if (res < 0)
783 return NULL;
784 if (res == 0)
786 /* End of stream */
787 if (g_buffered_input_stream_get_available (bstream) == 0)
789 if (length)
790 *length = 0;
791 return NULL;
793 else
795 found_pos = checked;
796 newline_len = 0;
797 break;
802 line = g_malloc (found_pos + newline_len + 1);
804 res = g_input_stream_read (G_INPUT_STREAM (stream),
805 line,
806 found_pos + newline_len,
807 NULL, NULL);
808 if (length)
809 *length = (gsize)found_pos;
810 g_warn_if_fail (res == found_pos + newline_len);
811 line[found_pos] = 0;
813 return line;
817 * g_data_input_stream_read_line_utf8:
818 * @stream: a given #GDataInputStream.
819 * @length: (out): a #gsize to get the length of the data read in.
820 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
821 * @error: #GError for error reporting.
823 * Reads a UTF-8 encoded line from the data input stream.
825 * If @cancellable is not %NULL, then the operation can be cancelled by
826 * triggering the cancellable object from another thread. If the operation
827 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
829 * Returns: (transfer full): a NUL terminated UTF-8 string with the
830 * line that was read in (without the newlines). Set @length to a
831 * #gsize to get the length of the read line. On an error, it will
832 * return %NULL and @error will be set. For UTF-8 conversion errors,
833 * the set error domain is %G_CONVERT_ERROR. If there's no content to
834 * read, it will still return %NULL, but @error won't be set.
836 * Since: 2.30
838 char *
839 g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
840 gsize *length,
841 GCancellable *cancellable,
842 GError **error)
844 char *res;
846 res = g_data_input_stream_read_line (stream, length, cancellable, error);
847 if (!res)
848 return NULL;
850 if (!g_utf8_validate (res, -1, NULL))
852 g_set_error_literal (error, G_CONVERT_ERROR,
853 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
854 _("Invalid byte sequence in conversion input"));
855 g_free (res);
856 return NULL;
858 return res;
861 static gssize
862 scan_for_chars (GDataInputStream *stream,
863 gsize *checked_out,
864 const char *stop_chars,
865 gssize stop_chars_len)
867 GBufferedInputStream *bstream;
868 const char *buffer;
869 gsize start, end, peeked;
870 int i;
871 gsize available, checked;
872 const char *stop_char;
873 const char *stop_end;
875 bstream = G_BUFFERED_INPUT_STREAM (stream);
876 stop_end = stop_chars + stop_chars_len;
878 checked = *checked_out;
880 start = checked;
881 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
882 end = available;
883 peeked = end - start;
885 for (i = 0; checked < available && i < peeked; i++)
887 for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
889 if (buffer[i] == *stop_char)
890 return (start + i);
894 checked = end;
896 *checked_out = checked;
897 return -1;
901 * g_data_input_stream_read_until:
902 * @stream: a given #GDataInputStream.
903 * @stop_chars: characters to terminate the read.
904 * @length: (out): a #gsize to get the length of the data read in.
905 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
906 * @error: #GError for error reporting.
908 * Reads a string from the data input stream, up to the first
909 * occurrence of any of the stop characters.
911 * Note that, in contrast to g_data_input_stream_read_until_async(),
912 * this function consumes the stop character that it finds.
914 * Don't use this function in new code. Its functionality is
915 * inconsistent with g_data_input_stream_read_until_async(). Both
916 * functions will be marked as deprecated in a future release. Use
917 * g_data_input_stream_read_upto() instead, but note that that function
918 * does not consume the stop character.
920 * Returns: (transfer full): a string with the data that was read
921 * before encountering any of the stop characters. Set @length to
922 * a #gsize to get the length of the string. This function will
923 * return %NULL on an error.
925 char *
926 g_data_input_stream_read_until (GDataInputStream *stream,
927 const gchar *stop_chars,
928 gsize *length,
929 GCancellable *cancellable,
930 GError **error)
932 GBufferedInputStream *bstream;
933 gchar *result;
935 bstream = G_BUFFERED_INPUT_STREAM (stream);
937 result = g_data_input_stream_read_upto (stream, stop_chars, -1,
938 length, cancellable, error);
940 /* If we're not at end of stream then we have a stop_char to consume. */
941 if (result != NULL && g_buffered_input_stream_get_available (bstream) > 0)
943 gsize res;
944 gchar b;
946 res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
947 g_assert (res == 1);
950 return result;
953 typedef struct
955 GDataInputStream *stream;
956 GSimpleAsyncResult *simple;
957 gboolean last_saw_cr;
958 gsize checked;
959 gint io_priority;
960 GCancellable *cancellable;
962 gchar *stop_chars;
963 gssize stop_chars_len;
964 gchar *line;
965 gsize length;
966 } GDataInputStreamReadData;
968 static void
969 g_data_input_stream_read_complete (GDataInputStreamReadData *data,
970 gsize read_length,
971 gsize skip_length,
972 gboolean need_idle_dispatch)
974 if (read_length || skip_length)
976 gssize bytes;
978 data->length = read_length;
979 data->line = g_malloc (read_length + 1);
980 data->line[read_length] = '\0';
982 /* we already checked the buffer. this shouldn't fail. */
983 bytes = g_input_stream_read (G_INPUT_STREAM (data->stream),
984 data->line, read_length, NULL, NULL);
985 g_assert_cmpint (bytes, ==, read_length);
987 bytes = g_input_stream_skip (G_INPUT_STREAM (data->stream),
988 skip_length, NULL, NULL);
989 g_assert_cmpint (bytes, ==, skip_length);
992 if (need_idle_dispatch)
993 g_simple_async_result_complete_in_idle (data->simple);
994 else
995 g_simple_async_result_complete (data->simple);
997 g_object_unref (data->simple);
1000 static void
1001 g_data_input_stream_read_line_ready (GObject *object,
1002 GAsyncResult *result,
1003 gpointer user_data)
1005 GDataInputStreamReadData *data = user_data;
1006 gssize found_pos;
1007 gint newline_len;
1009 if (result)
1010 /* this is a callback. finish the async call. */
1012 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
1013 GError *error = NULL;
1014 gssize bytes;
1016 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
1018 if (bytes <= 0)
1020 if (bytes < 0)
1021 /* stream error. */
1023 g_simple_async_result_take_error (data->simple, error);
1024 data->checked = 0;
1027 g_data_input_stream_read_complete (data, data->checked, 0, FALSE);
1028 return;
1031 /* only proceed if we got more bytes... */
1034 if (data->stop_chars)
1036 found_pos = scan_for_chars (data->stream,
1037 &data->checked,
1038 data->stop_chars,
1039 data->stop_chars_len);
1040 newline_len = 0;
1042 else
1043 found_pos = scan_for_newline (data->stream, &data->checked,
1044 &data->last_saw_cr, &newline_len);
1046 if (found_pos == -1)
1047 /* didn't find a full line; need to buffer some more bytes */
1049 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
1050 gsize size;
1052 size = g_buffered_input_stream_get_buffer_size (buffer);
1054 if (g_buffered_input_stream_get_available (buffer) == size)
1055 /* need to grow the buffer */
1056 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1058 /* try again */
1059 g_buffered_input_stream_fill_async (buffer, -1, data->io_priority,
1060 data->cancellable,
1061 g_data_input_stream_read_line_ready,
1062 user_data);
1064 else
1066 /* read the line and the EOL. no error is possible. */
1067 g_data_input_stream_read_complete (data, found_pos,
1068 newline_len, result == NULL);
1072 static void
1073 g_data_input_stream_read_data_free (gpointer user_data)
1075 GDataInputStreamReadData *data = user_data;
1077 /* we don't hold a ref to ->simple because it keeps a ref to us.
1078 * we are called because it is being finalized.
1081 g_free (data->stop_chars);
1082 if (data->cancellable)
1083 g_object_unref (data->cancellable);
1084 g_free (data->line);
1085 g_slice_free (GDataInputStreamReadData, data);
1088 static void
1089 g_data_input_stream_read_async (GDataInputStream *stream,
1090 const gchar *stop_chars,
1091 gssize stop_chars_len,
1092 gint io_priority,
1093 GCancellable *cancellable,
1094 GAsyncReadyCallback callback,
1095 gpointer user_data,
1096 gpointer source_tag)
1098 GDataInputStreamReadData *data;
1100 data = g_slice_new (GDataInputStreamReadData);
1101 data->stream = stream;
1102 if (cancellable)
1103 g_object_ref (cancellable);
1104 data->cancellable = cancellable;
1105 if (stop_chars_len == -1)
1106 stop_chars_len = strlen (stop_chars);
1107 data->stop_chars = g_memdup (stop_chars, stop_chars_len);
1108 data->stop_chars_len = stop_chars_len;
1109 data->io_priority = io_priority;
1110 data->last_saw_cr = FALSE;
1111 data->checked = 0;
1112 data->line = NULL;
1114 data->simple = g_simple_async_result_new (G_OBJECT (stream), callback,
1115 user_data, source_tag);
1116 g_simple_async_result_set_op_res_gpointer (data->simple, data,
1117 g_data_input_stream_read_data_free);
1118 g_data_input_stream_read_line_ready (NULL, NULL, data);
1121 static gchar *
1122 g_data_input_stream_read_finish (GDataInputStream *stream,
1123 GAsyncResult *result,
1124 gsize *length,
1125 GError **error)
1127 GDataInputStreamReadData *data;
1128 GSimpleAsyncResult *simple;
1129 gchar *line;
1131 simple = G_SIMPLE_ASYNC_RESULT (result);
1133 if (g_simple_async_result_propagate_error (simple, error))
1134 return NULL;
1136 data = g_simple_async_result_get_op_res_gpointer (simple);
1138 line = data->line;
1139 data->line = NULL;
1141 if (length && line)
1142 *length = data->length;
1144 return line;
1148 * g_data_input_stream_read_line_async:
1149 * @stream: a given #GDataInputStream.
1150 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1151 * of the request.
1152 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1153 * @callback: (scope async): callback to call when the request is satisfied.
1154 * @user_data: (closure): the data to pass to callback function.
1156 * The asynchronous version of g_data_input_stream_read_line(). It is
1157 * an error to have two outstanding calls to this function.
1159 * When the operation is finished, @callback will be called. You
1160 * can then call g_data_input_stream_read_line_finish() to get
1161 * the result of the operation.
1163 * Since: 2.20
1165 void
1166 g_data_input_stream_read_line_async (GDataInputStream *stream,
1167 gint io_priority,
1168 GCancellable *cancellable,
1169 GAsyncReadyCallback callback,
1170 gpointer user_data)
1172 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1173 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1175 g_data_input_stream_read_async (stream, NULL, 0, io_priority,
1176 cancellable, callback, user_data,
1177 g_data_input_stream_read_line_async);
1181 * g_data_input_stream_read_until_async:
1182 * @stream: a given #GDataInputStream.
1183 * @stop_chars: characters to terminate the read.
1184 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1185 * of the request.
1186 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1187 * @callback: (scope async): callback to call when the request is satisfied.
1188 * @user_data: (closure): the data to pass to callback function.
1190 * The asynchronous version of g_data_input_stream_read_until().
1191 * It is an error to have two outstanding calls to this function.
1193 * Note that, in contrast to g_data_input_stream_read_until(),
1194 * this function does not consume the stop character that it finds. You
1195 * must read it for yourself.
1197 * When the operation is finished, @callback will be called. You
1198 * can then call g_data_input_stream_read_until_finish() to get
1199 * the result of the operation.
1201 * Don't use this function in new code. Its functionality is
1202 * inconsistent with g_data_input_stream_read_until(). Both functions
1203 * will be marked as deprecated in a future release. Use
1204 * g_data_input_stream_read_upto_async() instead.
1206 * Since: 2.20
1208 void
1209 g_data_input_stream_read_until_async (GDataInputStream *stream,
1210 const gchar *stop_chars,
1211 gint io_priority,
1212 GCancellable *cancellable,
1213 GAsyncReadyCallback callback,
1214 gpointer user_data)
1216 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1217 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1218 g_return_if_fail (stop_chars != NULL);
1220 g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
1221 cancellable, callback, user_data,
1222 g_data_input_stream_read_until_async);
1226 * g_data_input_stream_read_line_finish:
1227 * @stream: a given #GDataInputStream.
1228 * @result: the #GAsyncResult that was provided to the callback.
1229 * @length: (out): a #gsize to get the length of the data read in.
1230 * @error: #GError for error reporting.
1232 * Finish an asynchronous call started by
1233 * g_data_input_stream_read_line_async(). Note the warning about
1234 * string encoding in g_data_input_stream_read_line() applies here as
1235 * well.
1237 * Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
1238 * NUL-terminated byte array with the line that was read in
1239 * (without the newlines). Set @length to a #gsize to get the
1240 * length of the read line. On an error, it will return %NULL and
1241 * @error will be set. If there's no content to read, it will
1242 * still return %NULL, but @error won't be set.
1244 * Since: 2.20
1246 gchar *
1247 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1248 GAsyncResult *result,
1249 gsize *length,
1250 GError **error)
1252 g_return_val_if_fail (
1253 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1254 g_data_input_stream_read_line_async), NULL);
1256 return g_data_input_stream_read_finish (stream, result, length, error);
1260 * g_data_input_stream_read_line_finish_utf8:
1261 * @stream: a given #GDataInputStream.
1262 * @result: the #GAsyncResult that was provided to the callback.
1263 * @length: (out): a #gsize to get the length of the data read in.
1264 * @error: #GError for error reporting.
1266 * Finish an asynchronous call started by
1267 * g_data_input_stream_read_line_async().
1269 * Returns: (transfer full): a string with the line that was read in
1270 * (without the newlines). Set @length to a #gsize to get the length
1271 * of the read line. On an error, it will return %NULL and @error
1272 * will be set. For UTF-8 conversion errors, the set error domain is
1273 * %G_CONVERT_ERROR. If there's no content to read, it will still
1274 * return %NULL, but @error won't be set.
1276 * Since: 2.30
1278 gchar *
1279 g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
1280 GAsyncResult *result,
1281 gsize *length,
1282 GError **error)
1284 gchar *res;
1286 res = g_data_input_stream_read_line_finish (stream, result, length, error);
1287 if (!res)
1288 return NULL;
1290 if (!g_utf8_validate (res, -1, NULL))
1292 g_set_error_literal (error, G_CONVERT_ERROR,
1293 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1294 _("Invalid byte sequence in conversion input"));
1295 g_free (res);
1296 return NULL;
1298 return res;
1302 * g_data_input_stream_read_until_finish:
1303 * @stream: a given #GDataInputStream.
1304 * @result: the #GAsyncResult that was provided to the callback.
1305 * @length: (out): a #gsize to get the length of the data read in.
1306 * @error: #GError for error reporting.
1308 * Finish an asynchronous call started by
1309 * g_data_input_stream_read_until_async().
1311 * Since: 2.20
1313 * Returns: (transfer full): a string with the data that was read
1314 * before encountering any of the stop characters. Set @length to
1315 * a #gsize to get the length of the string. This function will
1316 * return %NULL on an error.
1318 gchar *
1319 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1320 GAsyncResult *result,
1321 gsize *length,
1322 GError **error)
1324 g_return_val_if_fail (
1325 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1326 g_data_input_stream_read_until_async), NULL);
1328 return g_data_input_stream_read_finish (stream, result, length, error);
1332 * g_data_input_stream_read_upto:
1333 * @stream: a #GDataInputStream
1334 * @stop_chars: characters to terminate the read
1335 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1336 * nul-terminated
1337 * @length: (out): a #gsize to get the length of the data read in
1338 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1339 * @error: #GError for error reporting
1341 * Reads a string from the data input stream, up to the first
1342 * occurrence of any of the stop characters.
1344 * In contrast to g_data_input_stream_read_until(), this function
1345 * does <emphasis>not</emphasis> consume the stop character. You have
1346 * to use g_data_input_stream_read_byte() to get it before calling
1347 * g_data_input_stream_read_upto() again.
1349 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1350 * specified.
1352 * Returns: (transfer full): a string with the data that was read
1353 * before encountering any of the stop characters. Set @length to
1354 * a #gsize to get the length of the string. This function will
1355 * return %NULL on an error
1357 * Since: 2.26
1359 char *
1360 g_data_input_stream_read_upto (GDataInputStream *stream,
1361 const gchar *stop_chars,
1362 gssize stop_chars_len,
1363 gsize *length,
1364 GCancellable *cancellable,
1365 GError **error)
1367 GBufferedInputStream *bstream;
1368 gsize checked;
1369 gssize found_pos;
1370 gssize res;
1371 char *data_until;
1373 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
1375 if (stop_chars_len < 0)
1376 stop_chars_len = strlen (stop_chars);
1378 bstream = G_BUFFERED_INPUT_STREAM (stream);
1380 checked = 0;
1382 while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
1384 if (g_buffered_input_stream_get_available (bstream) ==
1385 g_buffered_input_stream_get_buffer_size (bstream))
1386 g_buffered_input_stream_set_buffer_size (bstream,
1387 2 * g_buffered_input_stream_get_buffer_size (bstream));
1389 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
1390 if (res < 0)
1391 return NULL;
1392 if (res == 0)
1394 /* End of stream */
1395 if (g_buffered_input_stream_get_available (bstream) == 0)
1397 if (length)
1398 *length = 0;
1399 return NULL;
1401 else
1403 found_pos = checked;
1404 break;
1409 data_until = g_malloc (found_pos + 1);
1411 res = g_input_stream_read (G_INPUT_STREAM (stream),
1412 data_until,
1413 found_pos,
1414 NULL, NULL);
1415 if (length)
1416 *length = (gsize)found_pos;
1417 g_warn_if_fail (res == found_pos);
1418 data_until[found_pos] = 0;
1420 return data_until;
1424 * g_data_input_stream_read_upto_async:
1425 * @stream: a #GDataInputStream
1426 * @stop_chars: characters to terminate the read
1427 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1428 * nul-terminated
1429 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1430 * of the request.
1431 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1432 * @callback: (scope async): callback to call when the request is satisfied
1433 * @user_data: (closure): the data to pass to callback function
1435 * The asynchronous version of g_data_input_stream_read_upto().
1436 * It is an error to have two outstanding calls to this function.
1438 * In contrast to g_data_input_stream_read_until(), this function
1439 * does <emphasis>not</emphasis> consume the stop character. You have
1440 * to use g_data_input_stream_read_byte() to get it before calling
1441 * g_data_input_stream_read_upto() again.
1443 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1444 * specified.
1446 * When the operation is finished, @callback will be called. You
1447 * can then call g_data_input_stream_read_upto_finish() to get
1448 * the result of the operation.
1450 * Since: 2.26
1452 void
1453 g_data_input_stream_read_upto_async (GDataInputStream *stream,
1454 const gchar *stop_chars,
1455 gssize stop_chars_len,
1456 gint io_priority,
1457 GCancellable *cancellable,
1458 GAsyncReadyCallback callback,
1459 gpointer user_data)
1461 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1462 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1463 g_return_if_fail (stop_chars != NULL);
1465 g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
1466 cancellable, callback, user_data,
1467 g_data_input_stream_read_upto_async);
1471 * g_data_input_stream_read_upto_finish:
1472 * @stream: a #GDataInputStream
1473 * @result: the #GAsyncResult that was provided to the callback
1474 * @length: (out): a #gsize to get the length of the data read in
1475 * @error: #GError for error reporting
1477 * Finish an asynchronous call started by
1478 * g_data_input_stream_read_upto_async().
1480 * Note that this function does <emphasis>not</emphasis> consume the
1481 * stop character. You have to use g_data_input_stream_read_byte() to
1482 * get it before calling g_data_input_stream_read_upto_async() again.
1484 * Returns: (transfer full): a string with the data that was read
1485 * before encountering any of the stop characters. Set @length to
1486 * a #gsize to get the length of the string. This function will
1487 * return %NULL on an error.
1489 * Since: 2.24
1491 gchar *
1492 g_data_input_stream_read_upto_finish (GDataInputStream *stream,
1493 GAsyncResult *result,
1494 gsize *length,
1495 GError **error)
1497 g_return_val_if_fail (
1498 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1499 g_data_input_stream_read_upto_async), NULL);
1501 return g_data_input_stream_read_finish (stream, result, length, error);