1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
5 * Copyright © 2009 Codethink Limited
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gdatainputstream.h"
28 #include "gcancellable.h"
29 #include "gioenumtypes.h"
36 * SECTION:gdatainputstream
37 * @short_description: Data Input Stream
39 * @see_also: #GInputStream
41 * Data input stream implements #GInputStream and includes functions for
42 * reading structured data directly from a binary input stream.
46 struct _GDataInputStreamPrivate {
47 GDataStreamByteOrder byte_order;
48 GDataStreamNewlineType newline_type;
57 static void g_data_input_stream_set_property (GObject *object,
61 static void g_data_input_stream_get_property (GObject *object,
66 G_DEFINE_TYPE_WITH_PRIVATE (GDataInputStream,
68 G_TYPE_BUFFERED_INPUT_STREAM)
72 g_data_input_stream_class_init (GDataInputStreamClass *klass)
74 GObjectClass *object_class;
76 object_class = G_OBJECT_CLASS (klass);
77 object_class->get_property = g_data_input_stream_get_property;
78 object_class->set_property = g_data_input_stream_set_property;
81 * GDataInputStream:byte-order:
83 * The :byte-order property determines the byte ordering that
84 * is used when reading multi-byte entities (such as integers)
87 g_object_class_install_property (object_class,
89 g_param_spec_enum ("byte-order",
92 G_TYPE_DATA_STREAM_BYTE_ORDER,
93 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
94 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
97 * GDataInputStream:newline-type:
99 * The :newline-type property determines what is considered
100 * as a line ending when reading complete lines from the stream.
102 g_object_class_install_property (object_class,
104 g_param_spec_enum ("newline-type",
106 P_("The accepted types of line ending"),
107 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
108 G_DATA_STREAM_NEWLINE_TYPE_LF,
109 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
113 g_data_input_stream_set_property (GObject *object,
118 GDataInputStream *dstream;
120 dstream = G_DATA_INPUT_STREAM (object);
124 case PROP_BYTE_ORDER:
125 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
128 case PROP_NEWLINE_TYPE:
129 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 g_data_input_stream_get_property (GObject *object,
145 GDataInputStreamPrivate *priv;
146 GDataInputStream *dstream;
148 dstream = G_DATA_INPUT_STREAM (object);
149 priv = dstream->priv;
153 case PROP_BYTE_ORDER:
154 g_value_set_enum (value, priv->byte_order);
157 case PROP_NEWLINE_TYPE:
158 g_value_set_enum (value, priv->newline_type);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 g_data_input_stream_init (GDataInputStream *stream)
170 stream->priv = g_data_input_stream_get_instance_private (stream);
171 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
172 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
176 * g_data_input_stream_new:
177 * @base_stream: a #GInputStream.
179 * Creates a new data input stream for the @base_stream.
181 * Returns: a new #GDataInputStream.
184 g_data_input_stream_new (GInputStream *base_stream)
186 GDataInputStream *stream;
188 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
190 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
191 "base-stream", base_stream,
198 * g_data_input_stream_set_byte_order:
199 * @stream: a given #GDataInputStream.
200 * @order: a #GDataStreamByteOrder to set.
202 * This function sets the byte order for the given @stream. All subsequent
203 * reads from the @stream will be read in the given @order.
207 g_data_input_stream_set_byte_order (GDataInputStream *stream,
208 GDataStreamByteOrder order)
210 GDataInputStreamPrivate *priv;
212 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
216 if (priv->byte_order != order)
218 priv->byte_order = order;
220 g_object_notify (G_OBJECT (stream), "byte-order");
225 * g_data_input_stream_get_byte_order:
226 * @stream: a given #GDataInputStream.
228 * Gets the byte order for the data input stream.
230 * Returns: the @stream's current #GDataStreamByteOrder.
233 g_data_input_stream_get_byte_order (GDataInputStream *stream)
235 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
237 return stream->priv->byte_order;
241 * g_data_input_stream_set_newline_type:
242 * @stream: a #GDataInputStream.
243 * @type: the type of new line return as #GDataStreamNewlineType.
245 * Sets the newline type for the @stream.
247 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
248 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
249 * "CR LF", and this might block if there is no more data available.
253 g_data_input_stream_set_newline_type (GDataInputStream *stream,
254 GDataStreamNewlineType type)
256 GDataInputStreamPrivate *priv;
258 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
262 if (priv->newline_type != type)
264 priv->newline_type = type;
266 g_object_notify (G_OBJECT (stream), "newline-type");
271 * g_data_input_stream_get_newline_type:
272 * @stream: a given #GDataInputStream.
274 * Gets the current newline type for the @stream.
276 * Returns: #GDataStreamNewlineType for the given @stream.
278 GDataStreamNewlineType
279 g_data_input_stream_get_newline_type (GDataInputStream *stream)
281 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
283 return stream->priv->newline_type;
287 read_data (GDataInputStream *stream,
290 GCancellable *cancellable,
296 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
298 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
305 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
306 _("Unexpected early end-of-stream"));
311 /* This should always succeed, since it's in the buffer */
312 res = g_input_stream_read (G_INPUT_STREAM (stream),
315 g_warn_if_fail (res >= 0 && (gsize) res == size);
321 * g_data_input_stream_read_byte:
322 * @stream: a given #GDataInputStream.
323 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
324 * @error: #GError for error reporting.
326 * Reads an unsigned 8-bit/1-byte value from @stream.
328 * Returns: an unsigned 8-bit/1-byte value read from the @stream or `0`
329 * if an error occurred.
332 g_data_input_stream_read_byte (GDataInputStream *stream,
333 GCancellable *cancellable,
338 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
340 if (read_data (stream, &c, 1, cancellable, error))
348 * g_data_input_stream_read_int16:
349 * @stream: a given #GDataInputStream.
350 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
351 * @error: #GError for error reporting.
353 * Reads a 16-bit/2-byte value from @stream.
355 * In order to get the correct byte order for this read operation,
356 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
358 * Returns: a signed 16-bit/2-byte value read from @stream or `0` if
362 g_data_input_stream_read_int16 (GDataInputStream *stream,
363 GCancellable *cancellable,
368 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
370 if (read_data (stream, &v, 2, cancellable, error))
372 switch (stream->priv->byte_order)
374 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
375 v = GINT16_FROM_BE (v);
377 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
378 v = GINT16_FROM_LE (v);
380 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
392 * g_data_input_stream_read_uint16:
393 * @stream: a given #GDataInputStream.
394 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
395 * @error: #GError for error reporting.
397 * Reads an unsigned 16-bit/2-byte value from @stream.
399 * In order to get the correct byte order for this read operation,
400 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
402 * Returns: an unsigned 16-bit/2-byte value read from the @stream or `0` if
406 g_data_input_stream_read_uint16 (GDataInputStream *stream,
407 GCancellable *cancellable,
412 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
414 if (read_data (stream, &v, 2, cancellable, error))
416 switch (stream->priv->byte_order)
418 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
419 v = GUINT16_FROM_BE (v);
421 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
422 v = GUINT16_FROM_LE (v);
424 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
436 * g_data_input_stream_read_int32:
437 * @stream: a given #GDataInputStream.
438 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
439 * @error: #GError for error reporting.
441 * Reads a signed 32-bit/4-byte value from @stream.
443 * In order to get the correct byte order for this read operation,
444 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
446 * If @cancellable is not %NULL, then the operation can be cancelled by
447 * triggering the cancellable object from another thread. If the operation
448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
450 * Returns: a signed 32-bit/4-byte value read from the @stream or `0` if
454 g_data_input_stream_read_int32 (GDataInputStream *stream,
455 GCancellable *cancellable,
460 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
462 if (read_data (stream, &v, 4, cancellable, error))
464 switch (stream->priv->byte_order)
466 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
467 v = GINT32_FROM_BE (v);
469 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
470 v = GINT32_FROM_LE (v);
472 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
484 * g_data_input_stream_read_uint32:
485 * @stream: a given #GDataInputStream.
486 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
487 * @error: #GError for error reporting.
489 * Reads an unsigned 32-bit/4-byte value from @stream.
491 * In order to get the correct byte order for this read operation,
492 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
494 * If @cancellable is not %NULL, then the operation can be cancelled by
495 * triggering the cancellable object from another thread. If the operation
496 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
498 * Returns: an unsigned 32-bit/4-byte value read from the @stream or `0` if
502 g_data_input_stream_read_uint32 (GDataInputStream *stream,
503 GCancellable *cancellable,
508 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
510 if (read_data (stream, &v, 4, cancellable, error))
512 switch (stream->priv->byte_order)
514 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
515 v = GUINT32_FROM_BE (v);
517 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
518 v = GUINT32_FROM_LE (v);
520 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
532 * g_data_input_stream_read_int64:
533 * @stream: a given #GDataInputStream.
534 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
535 * @error: #GError for error reporting.
537 * Reads a 64-bit/8-byte value from @stream.
539 * In order to get the correct byte order for this read operation,
540 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
542 * If @cancellable is not %NULL, then the operation can be cancelled by
543 * triggering the cancellable object from another thread. If the operation
544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
546 * Returns: a signed 64-bit/8-byte value read from @stream or `0` if
550 g_data_input_stream_read_int64 (GDataInputStream *stream,
551 GCancellable *cancellable,
556 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
558 if (read_data (stream, &v, 8, cancellable, error))
560 switch (stream->priv->byte_order)
562 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
563 v = GINT64_FROM_BE (v);
565 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
566 v = GINT64_FROM_LE (v);
568 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
580 * g_data_input_stream_read_uint64:
581 * @stream: a given #GDataInputStream.
582 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
583 * @error: #GError for error reporting.
585 * Reads an unsigned 64-bit/8-byte value from @stream.
587 * In order to get the correct byte order for this read operation,
588 * see g_data_input_stream_get_byte_order().
590 * If @cancellable is not %NULL, then the operation can be cancelled by
591 * triggering the cancellable object from another thread. If the operation
592 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
594 * Returns: an unsigned 64-bit/8-byte read from @stream or `0` if
598 g_data_input_stream_read_uint64 (GDataInputStream *stream,
599 GCancellable *cancellable,
604 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
606 if (read_data (stream, &v, 8, cancellable, error))
608 switch (stream->priv->byte_order)
610 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
611 v = GUINT64_FROM_BE (v);
613 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
614 v = GUINT64_FROM_LE (v);
616 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
627 scan_for_newline (GDataInputStream *stream,
629 gboolean *last_saw_cr_out,
630 int *newline_len_out)
632 GBufferedInputStream *bstream;
633 GDataInputStreamPrivate *priv;
635 gsize start, end, peeked;
639 gsize available, checked;
640 gboolean last_saw_cr;
644 bstream = G_BUFFERED_INPUT_STREAM (stream);
646 checked = *checked_out;
647 last_saw_cr = *last_saw_cr_out;
652 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
654 peeked = end - start;
656 for (i = 0; checked < available && i < peeked; i++)
658 switch (priv->newline_type)
660 case G_DATA_STREAM_NEWLINE_TYPE_LF:
663 found_pos = start + i;
667 case G_DATA_STREAM_NEWLINE_TYPE_CR:
670 found_pos = start + i;
674 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
675 if (last_saw_cr && buffer[i] == 10)
677 found_pos = start + i - 1;
682 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
683 if (buffer[i] == 10) /* LF */
688 found_pos = start + i - 1;
694 found_pos = start + i;
698 else if (last_saw_cr)
700 /* Last was cr, this is not LF, end is CR */
701 found_pos = start + i - 1;
704 /* Don't check for CR here, instead look at last_saw_cr on next byte */
708 last_saw_cr = (buffer[i] == 13);
712 *newline_len_out = newline_len;
719 *checked_out = checked;
720 *last_saw_cr_out = last_saw_cr;
726 * g_data_input_stream_read_line:
727 * @stream: a given #GDataInputStream.
728 * @length: (out) (optional): a #gsize to get the length of the data read in.
729 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
730 * @error: #GError for error reporting.
732 * Reads a line from the data input stream. Note that no encoding
733 * checks or conversion is performed; the input is not guaranteed to
734 * be UTF-8, and may in fact have embedded NUL characters.
736 * If @cancellable is not %NULL, then the operation can be cancelled by
737 * triggering the cancellable object from another thread. If the operation
738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
740 * Returns: (nullable) (transfer full) (array zero-terminated=1) (element-type guint8):
741 * a NUL terminated byte array with the line that was read in
742 * (without the newlines). Set @length to a #gsize to get the length
743 * of the read line. On an error, it will return %NULL and @error
744 * will be set. If there's no content to read, it will still return
745 * %NULL, but @error won't be set.
748 g_data_input_stream_read_line (GDataInputStream *stream,
750 GCancellable *cancellable,
753 GBufferedInputStream *bstream;
755 gboolean last_saw_cr;
761 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
763 bstream = G_BUFFERED_INPUT_STREAM (stream);
769 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
771 if (g_buffered_input_stream_get_available (bstream) ==
772 g_buffered_input_stream_get_buffer_size (bstream))
773 g_buffered_input_stream_set_buffer_size (bstream,
774 2 * g_buffered_input_stream_get_buffer_size (bstream));
776 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
782 if (g_buffered_input_stream_get_available (bstream) == 0)
797 line = g_malloc (found_pos + newline_len + 1);
799 res = g_input_stream_read (G_INPUT_STREAM (stream),
801 found_pos + newline_len,
804 *length = (gsize)found_pos;
805 g_warn_if_fail (res == found_pos + newline_len);
812 * g_data_input_stream_read_line_utf8:
813 * @stream: a given #GDataInputStream.
814 * @length: (out) (optional): a #gsize to get the length of the data read in.
815 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
816 * @error: #GError for error reporting.
818 * Reads a UTF-8 encoded line from the data input stream.
820 * If @cancellable is not %NULL, then the operation can be cancelled by
821 * triggering the cancellable object from another thread. If the operation
822 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
824 * Returns: (nullable) (transfer full): a NUL terminated UTF-8 string
825 * with the line that was read in (without the newlines). Set
826 * @length to a #gsize to get the length of the read line. On an
827 * error, it will return %NULL and @error will be set. For UTF-8
828 * conversion errors, the set error domain is %G_CONVERT_ERROR. If
829 * there's no content to read, it will still return %NULL, but @error
835 g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
837 GCancellable *cancellable,
842 res = g_data_input_stream_read_line (stream, length, cancellable, error);
846 if (!g_utf8_validate (res, -1, NULL))
848 g_set_error_literal (error, G_CONVERT_ERROR,
849 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
850 _("Invalid byte sequence in conversion input"));
858 scan_for_chars (GDataInputStream *stream,
860 const char *stop_chars,
861 gsize stop_chars_len)
863 GBufferedInputStream *bstream;
865 gsize start, end, peeked;
867 gsize available, checked;
868 const char *stop_char;
869 const char *stop_end;
871 bstream = G_BUFFERED_INPUT_STREAM (stream);
872 stop_end = stop_chars + stop_chars_len;
874 checked = *checked_out;
877 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
879 peeked = end - start;
881 for (i = 0; checked < available && i < peeked; i++)
883 for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
885 if (buffer[i] == *stop_char)
892 *checked_out = checked;
897 * g_data_input_stream_read_until:
898 * @stream: a given #GDataInputStream.
899 * @stop_chars: characters to terminate the read.
900 * @length: (out) (optional): a #gsize to get the length of the data read in.
901 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
902 * @error: #GError for error reporting.
904 * Reads a string from the data input stream, up to the first
905 * occurrence of any of the stop characters.
907 * Note that, in contrast to g_data_input_stream_read_until_async(),
908 * this function consumes the stop character that it finds.
910 * Don't use this function in new code. Its functionality is
911 * inconsistent with g_data_input_stream_read_until_async(). Both
912 * functions will be marked as deprecated in a future release. Use
913 * g_data_input_stream_read_upto() instead, but note that that function
914 * does not consume the stop character.
916 * Returns: (transfer full): a string with the data that was read
917 * before encountering any of the stop characters. Set @length to
918 * a #gsize to get the length of the string. This function will
919 * return %NULL on an error.
920 * Deprecated: 2.56: Use g_data_input_stream_read_upto() instead, which has more
921 * consistent behaviour regarding the stop character.
924 g_data_input_stream_read_until (GDataInputStream *stream,
925 const gchar *stop_chars,
927 GCancellable *cancellable,
930 GBufferedInputStream *bstream;
933 bstream = G_BUFFERED_INPUT_STREAM (stream);
935 result = g_data_input_stream_read_upto (stream, stop_chars, -1,
936 length, cancellable, error);
938 /* If we're not at end of stream then we have a stop_char to consume. */
939 if (result != NULL && g_buffered_input_stream_get_available (bstream) > 0)
941 gsize res G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
944 res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
953 gboolean last_saw_cr;
957 gsize stop_chars_len;
959 } GDataInputStreamReadData;
962 g_data_input_stream_read_complete (GTask *task,
966 GDataInputStreamReadData *data = g_task_get_task_data (task);
967 GInputStream *stream = g_task_get_source_object (task);
970 if (read_length || skip_length)
974 data->length = read_length;
975 line = g_malloc (read_length + 1);
976 line[read_length] = '\0';
978 /* we already checked the buffer. this shouldn't fail. */
979 bytes = g_input_stream_read (stream, line, read_length, NULL, NULL);
980 g_assert_cmpint (bytes, ==, read_length);
982 bytes = g_input_stream_skip (stream, skip_length, NULL, NULL);
983 g_assert_cmpint (bytes, ==, skip_length);
986 g_task_return_pointer (task, line, g_free);
987 g_object_unref (task);
991 g_data_input_stream_read_line_ready (GObject *object,
992 GAsyncResult *result,
995 GTask *task = user_data;
996 GDataInputStreamReadData *data = g_task_get_task_data (task);
997 GBufferedInputStream *buffer = g_task_get_source_object (task);
1002 /* this is a callback. finish the async call. */
1004 GError *error = NULL;
1007 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
1014 g_task_return_error (task, error);
1015 g_object_unref (task);
1019 g_data_input_stream_read_complete (task, data->checked, 0);
1023 /* only proceed if we got more bytes... */
1026 if (data->stop_chars)
1028 found_pos = scan_for_chars (G_DATA_INPUT_STREAM (buffer),
1031 data->stop_chars_len);
1035 found_pos = scan_for_newline (G_DATA_INPUT_STREAM (buffer), &data->checked,
1036 &data->last_saw_cr, &newline_len);
1038 if (found_pos == -1)
1039 /* didn't find a full line; need to buffer some more bytes */
1043 size = g_buffered_input_stream_get_buffer_size (buffer);
1045 if (g_buffered_input_stream_get_available (buffer) == size)
1046 /* need to grow the buffer */
1047 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1050 g_buffered_input_stream_fill_async (buffer, -1,
1051 g_task_get_priority (task),
1052 g_task_get_cancellable (task),
1053 g_data_input_stream_read_line_ready,
1058 /* read the line and the EOL. no error is possible. */
1059 g_data_input_stream_read_complete (task, found_pos, newline_len);
1064 g_data_input_stream_read_data_free (gpointer user_data)
1066 GDataInputStreamReadData *data = user_data;
1068 g_free (data->stop_chars);
1069 g_slice_free (GDataInputStreamReadData, data);
1073 g_data_input_stream_read_async (GDataInputStream *stream,
1074 const gchar *stop_chars,
1075 gssize stop_chars_len,
1077 GCancellable *cancellable,
1078 GAsyncReadyCallback callback,
1081 GDataInputStreamReadData *data;
1083 gsize stop_chars_len_unsigned;
1085 data = g_slice_new0 (GDataInputStreamReadData);
1087 if (stop_chars_len < 0)
1088 stop_chars_len_unsigned = strlen (stop_chars);
1090 stop_chars_len_unsigned = (gsize) stop_chars_len;
1092 data->stop_chars = g_memdup2 (stop_chars, stop_chars_len_unsigned);
1093 data->stop_chars_len = stop_chars_len_unsigned;
1094 data->last_saw_cr = FALSE;
1096 task = g_task_new (stream, cancellable, callback, user_data);
1097 g_task_set_source_tag (task, g_data_input_stream_read_async);
1098 g_task_set_task_data (task, data, g_data_input_stream_read_data_free);
1099 g_task_set_priority (task, io_priority);
1101 g_data_input_stream_read_line_ready (NULL, NULL, task);
1105 g_data_input_stream_read_finish (GDataInputStream *stream,
1106 GAsyncResult *result,
1110 GTask *task = G_TASK (result);
1113 line = g_task_propagate_pointer (task, error);
1117 GDataInputStreamReadData *data = g_task_get_task_data (task);
1119 *length = data->length;
1126 * g_data_input_stream_read_line_async:
1127 * @stream: a given #GDataInputStream.
1128 * @io_priority: the [I/O priority][io-priority] of the request
1129 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1130 * @callback: (scope async) (closure user_data): callback to call when the request is satisfied.
1131 * @user_data: the data to pass to callback function.
1133 * The asynchronous version of g_data_input_stream_read_line(). It is
1134 * an error to have two outstanding calls to this function.
1136 * When the operation is finished, @callback will be called. You
1137 * can then call g_data_input_stream_read_line_finish() to get
1138 * the result of the operation.
1143 g_data_input_stream_read_line_async (GDataInputStream *stream,
1145 GCancellable *cancellable,
1146 GAsyncReadyCallback callback,
1149 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1150 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1152 g_data_input_stream_read_async (stream, NULL, 0, io_priority,
1153 cancellable, callback, user_data);
1157 * g_data_input_stream_read_until_async:
1158 * @stream: a given #GDataInputStream.
1159 * @stop_chars: characters to terminate the read.
1160 * @io_priority: the [I/O priority][io-priority] of the request
1161 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1162 * @callback: (scope async): callback to call when the request is satisfied.
1163 * @user_data: (closure): the data to pass to callback function.
1165 * The asynchronous version of g_data_input_stream_read_until().
1166 * It is an error to have two outstanding calls to this function.
1168 * Note that, in contrast to g_data_input_stream_read_until(),
1169 * this function does not consume the stop character that it finds. You
1170 * must read it for yourself.
1172 * When the operation is finished, @callback will be called. You
1173 * can then call g_data_input_stream_read_until_finish() to get
1174 * the result of the operation.
1176 * Don't use this function in new code. Its functionality is
1177 * inconsistent with g_data_input_stream_read_until(). Both functions
1178 * will be marked as deprecated in a future release. Use
1179 * g_data_input_stream_read_upto_async() instead.
1182 * Deprecated: 2.56: Use g_data_input_stream_read_upto_async() instead, which
1183 * has more consistent behaviour regarding the stop character.
1186 g_data_input_stream_read_until_async (GDataInputStream *stream,
1187 const gchar *stop_chars,
1189 GCancellable *cancellable,
1190 GAsyncReadyCallback callback,
1193 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1194 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1195 g_return_if_fail (stop_chars != NULL);
1197 g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
1198 cancellable, callback, user_data);
1202 * g_data_input_stream_read_line_finish:
1203 * @stream: a given #GDataInputStream.
1204 * @result: the #GAsyncResult that was provided to the callback.
1205 * @length: (out) (optional): a #gsize to get the length of the data read in.
1206 * @error: #GError for error reporting.
1208 * Finish an asynchronous call started by
1209 * g_data_input_stream_read_line_async(). Note the warning about
1210 * string encoding in g_data_input_stream_read_line() applies here as
1213 * Returns: (nullable) (transfer full) (array zero-terminated=1) (element-type guint8):
1214 * a NUL-terminated byte array with the line that was read in
1215 * (without the newlines). Set @length to a #gsize to get the length
1216 * of the read line. On an error, it will return %NULL and @error
1217 * will be set. If there's no content to read, it will still return
1218 * %NULL, but @error won't be set.
1223 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1224 GAsyncResult *result,
1228 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1230 return g_data_input_stream_read_finish (stream, result, length, error);
1234 * g_data_input_stream_read_line_finish_utf8:
1235 * @stream: a given #GDataInputStream.
1236 * @result: the #GAsyncResult that was provided to the callback.
1237 * @length: (out) (optional): a #gsize to get the length of the data read in.
1238 * @error: #GError for error reporting.
1240 * Finish an asynchronous call started by
1241 * g_data_input_stream_read_line_async().
1243 * Returns: (nullable) (transfer full): a string with the line that
1244 * was read in (without the newlines). Set @length to a #gsize to
1245 * get the length of the read line. On an error, it will return
1246 * %NULL and @error will be set. For UTF-8 conversion errors, the set
1247 * error domain is %G_CONVERT_ERROR. If there's no content to read,
1248 * it will still return %NULL, but @error won't be set.
1253 g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
1254 GAsyncResult *result,
1260 res = g_data_input_stream_read_line_finish (stream, result, length, error);
1264 if (!g_utf8_validate (res, -1, NULL))
1266 g_set_error_literal (error, G_CONVERT_ERROR,
1267 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1268 _("Invalid byte sequence in conversion input"));
1276 * g_data_input_stream_read_until_finish:
1277 * @stream: a given #GDataInputStream.
1278 * @result: the #GAsyncResult that was provided to the callback.
1279 * @length: (out) (optional): a #gsize to get the length of the data read in.
1280 * @error: #GError for error reporting.
1282 * Finish an asynchronous call started by
1283 * g_data_input_stream_read_until_async().
1287 * Returns: (transfer full): a string with the data that was read
1288 * before encountering any of the stop characters. Set @length to
1289 * a #gsize to get the length of the string. This function will
1290 * return %NULL on an error.
1291 * Deprecated: 2.56: Use g_data_input_stream_read_upto_finish() instead, which
1292 * has more consistent behaviour regarding the stop character.
1295 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1296 GAsyncResult *result,
1300 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1302 return g_data_input_stream_read_finish (stream, result, length, error);
1306 * g_data_input_stream_read_upto:
1307 * @stream: a #GDataInputStream
1308 * @stop_chars: characters to terminate the read
1309 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1311 * @length: (out) (optional): a #gsize to get the length of the data read in
1312 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1313 * @error: #GError for error reporting
1315 * Reads a string from the data input stream, up to the first
1316 * occurrence of any of the stop characters.
1318 * In contrast to g_data_input_stream_read_until(), this function
1319 * does not consume the stop character. You have to use
1320 * g_data_input_stream_read_byte() to get it before calling
1321 * g_data_input_stream_read_upto() again.
1323 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1326 * The returned string will always be nul-terminated on success.
1328 * Returns: (transfer full): a string with the data that was read
1329 * before encountering any of the stop characters. Set @length to
1330 * a #gsize to get the length of the string. This function will
1331 * return %NULL on an error
1336 g_data_input_stream_read_upto (GDataInputStream *stream,
1337 const gchar *stop_chars,
1338 gssize stop_chars_len,
1340 GCancellable *cancellable,
1343 GBufferedInputStream *bstream;
1348 gsize stop_chars_len_unsigned;
1350 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
1352 if (stop_chars_len < 0)
1353 stop_chars_len_unsigned = strlen (stop_chars);
1355 stop_chars_len_unsigned = (gsize) stop_chars_len;
1357 bstream = G_BUFFERED_INPUT_STREAM (stream);
1361 while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len_unsigned)) == -1)
1363 if (g_buffered_input_stream_get_available (bstream) ==
1364 g_buffered_input_stream_get_buffer_size (bstream))
1365 g_buffered_input_stream_set_buffer_size (bstream,
1366 2 * g_buffered_input_stream_get_buffer_size (bstream));
1368 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
1374 if (g_buffered_input_stream_get_available (bstream) == 0)
1382 found_pos = checked;
1388 data_until = g_malloc (found_pos + 1);
1390 res = g_input_stream_read (G_INPUT_STREAM (stream),
1395 *length = (gsize)found_pos;
1396 g_warn_if_fail (res == found_pos);
1397 data_until[found_pos] = 0;
1403 * g_data_input_stream_read_upto_async:
1404 * @stream: a #GDataInputStream
1405 * @stop_chars: characters to terminate the read
1406 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1408 * @io_priority: the [I/O priority][io-priority] of the request
1409 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1410 * @callback: (scope async): callback to call when the request is satisfied
1411 * @user_data: (closure): the data to pass to callback function
1413 * The asynchronous version of g_data_input_stream_read_upto().
1414 * It is an error to have two outstanding calls to this function.
1416 * In contrast to g_data_input_stream_read_until(), this function
1417 * does not consume the stop character. You have to use
1418 * g_data_input_stream_read_byte() to get it before calling
1419 * g_data_input_stream_read_upto() again.
1421 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1424 * When the operation is finished, @callback will be called. You
1425 * can then call g_data_input_stream_read_upto_finish() to get
1426 * the result of the operation.
1431 g_data_input_stream_read_upto_async (GDataInputStream *stream,
1432 const gchar *stop_chars,
1433 gssize stop_chars_len,
1435 GCancellable *cancellable,
1436 GAsyncReadyCallback callback,
1439 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1440 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1441 g_return_if_fail (stop_chars != NULL);
1443 g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
1444 cancellable, callback, user_data);
1448 * g_data_input_stream_read_upto_finish:
1449 * @stream: a #GDataInputStream
1450 * @result: the #GAsyncResult that was provided to the callback
1451 * @length: (out) (optional): a #gsize to get the length of the data read in
1452 * @error: #GError for error reporting
1454 * Finish an asynchronous call started by
1455 * g_data_input_stream_read_upto_async().
1457 * Note that this function does not consume the stop character. You
1458 * have to use g_data_input_stream_read_byte() to get it before calling
1459 * g_data_input_stream_read_upto_async() again.
1461 * The returned string will always be nul-terminated on success.
1463 * Returns: (transfer full): a string with the data that was read
1464 * before encountering any of the stop characters. Set @length to
1465 * a #gsize to get the length of the string. This function will
1466 * return %NULL on an error.
1471 g_data_input_stream_read_upto_finish (GDataInputStream *stream,
1472 GAsyncResult *result,
1476 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1478 return g_data_input_stream_read_finish (stream, result, length, error);