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 * 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>
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 * GDataStream: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 * GDataStream: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 == size);
321 * g_data_input_stream_read_byte:
322 * @stream: a given #GDataInputStream.
323 * @cancellable: (allow-none): 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: (allow-none): 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: (allow-none): 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: (allow-none): 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: (allow-none): 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: (allow-none): 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: (allow-none): 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): a #gsize to get the length of the data read in.
729 * @cancellable: (allow-none): 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: (transfer full) (array zero-terminated=1) (element-type guint8): a
741 * NUL terminated byte array with the line that was read in (without
742 * the newlines). Set @length to a #gsize to get the length of the
743 * read line. On an error, it will return %NULL and @error will be
744 * set. If there's no content to read, it will still return %NULL,
745 * 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): a #gsize to get the length of the data read in.
815 * @cancellable: (allow-none): 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: (transfer full): a NUL terminated UTF-8 string with the
825 * line that was read in (without the newlines). Set @length to a
826 * #gsize to get the length of the read line. On an error, it will
827 * return %NULL and @error will be set. For UTF-8 conversion errors,
828 * the set error domain is %G_CONVERT_ERROR. If there's no content to
829 * read, it will still return %NULL, but @error won't be set.
834 g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
836 GCancellable *cancellable,
841 res = g_data_input_stream_read_line (stream, length, cancellable, error);
845 if (!g_utf8_validate (res, -1, NULL))
847 g_set_error_literal (error, G_CONVERT_ERROR,
848 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
849 _("Invalid byte sequence in conversion input"));
857 scan_for_chars (GDataInputStream *stream,
859 const char *stop_chars,
860 gssize stop_chars_len)
862 GBufferedInputStream *bstream;
864 gsize start, end, peeked;
866 gsize available, checked;
867 const char *stop_char;
868 const char *stop_end;
870 bstream = G_BUFFERED_INPUT_STREAM (stream);
871 stop_end = stop_chars + stop_chars_len;
873 checked = *checked_out;
876 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
878 peeked = end - start;
880 for (i = 0; checked < available && i < peeked; i++)
882 for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
884 if (buffer[i] == *stop_char)
891 *checked_out = checked;
896 * g_data_input_stream_read_until:
897 * @stream: a given #GDataInputStream.
898 * @stop_chars: characters to terminate the read.
899 * @length: (out): a #gsize to get the length of the data read in.
900 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
901 * @error: #GError for error reporting.
903 * Reads a string from the data input stream, up to the first
904 * occurrence of any of the stop characters.
906 * Note that, in contrast to g_data_input_stream_read_until_async(),
907 * this function consumes the stop character that it finds.
909 * Don't use this function in new code. Its functionality is
910 * inconsistent with g_data_input_stream_read_until_async(). Both
911 * functions will be marked as deprecated in a future release. Use
912 * g_data_input_stream_read_upto() instead, but note that that function
913 * does not consume the stop character.
915 * Returns: (transfer full): a string with the data that was read
916 * before encountering any of the stop characters. Set @length to
917 * a #gsize to get the length of the string. This function will
918 * return %NULL on an error.
921 g_data_input_stream_read_until (GDataInputStream *stream,
922 const gchar *stop_chars,
924 GCancellable *cancellable,
927 GBufferedInputStream *bstream;
930 bstream = G_BUFFERED_INPUT_STREAM (stream);
932 result = g_data_input_stream_read_upto (stream, stop_chars, -1,
933 length, cancellable, error);
935 /* If we're not at end of stream then we have a stop_char to consume. */
936 if (result != NULL && g_buffered_input_stream_get_available (bstream) > 0)
941 res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
950 gboolean last_saw_cr;
954 gssize stop_chars_len;
956 } GDataInputStreamReadData;
959 g_data_input_stream_read_complete (GTask *task,
963 GDataInputStreamReadData *data = g_task_get_task_data (task);
964 GInputStream *stream = g_task_get_source_object (task);
967 if (read_length || skip_length)
971 data->length = read_length;
972 line = g_malloc (read_length + 1);
973 line[read_length] = '\0';
975 /* we already checked the buffer. this shouldn't fail. */
976 bytes = g_input_stream_read (stream, line, read_length, NULL, NULL);
977 g_assert_cmpint (bytes, ==, read_length);
979 bytes = g_input_stream_skip (stream, skip_length, NULL, NULL);
980 g_assert_cmpint (bytes, ==, skip_length);
983 g_task_return_pointer (task, line, g_free);
984 g_object_unref (task);
988 g_data_input_stream_read_line_ready (GObject *object,
989 GAsyncResult *result,
992 GTask *task = user_data;
993 GDataInputStreamReadData *data = g_task_get_task_data (task);
994 GBufferedInputStream *buffer = g_task_get_source_object (task);
999 /* this is a callback. finish the async call. */
1001 GError *error = NULL;
1004 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
1011 g_task_return_error (task, error);
1012 g_object_unref (task);
1016 g_data_input_stream_read_complete (task, data->checked, 0);
1020 /* only proceed if we got more bytes... */
1023 if (data->stop_chars)
1025 found_pos = scan_for_chars (G_DATA_INPUT_STREAM (buffer),
1028 data->stop_chars_len);
1032 found_pos = scan_for_newline (G_DATA_INPUT_STREAM (buffer), &data->checked,
1033 &data->last_saw_cr, &newline_len);
1035 if (found_pos == -1)
1036 /* didn't find a full line; need to buffer some more bytes */
1040 size = g_buffered_input_stream_get_buffer_size (buffer);
1042 if (g_buffered_input_stream_get_available (buffer) == size)
1043 /* need to grow the buffer */
1044 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1047 g_buffered_input_stream_fill_async (buffer, -1,
1048 g_task_get_priority (task),
1049 g_task_get_cancellable (task),
1050 g_data_input_stream_read_line_ready,
1055 /* read the line and the EOL. no error is possible. */
1056 g_data_input_stream_read_complete (task, found_pos, newline_len);
1061 g_data_input_stream_read_data_free (gpointer user_data)
1063 GDataInputStreamReadData *data = user_data;
1065 g_free (data->stop_chars);
1066 g_slice_free (GDataInputStreamReadData, data);
1070 g_data_input_stream_read_async (GDataInputStream *stream,
1071 const gchar *stop_chars,
1072 gssize stop_chars_len,
1074 GCancellable *cancellable,
1075 GAsyncReadyCallback callback,
1078 GDataInputStreamReadData *data;
1081 data = g_slice_new0 (GDataInputStreamReadData);
1082 if (stop_chars_len == -1)
1083 stop_chars_len = strlen (stop_chars);
1084 data->stop_chars = g_memdup (stop_chars, stop_chars_len);
1085 data->stop_chars_len = stop_chars_len;
1086 data->last_saw_cr = FALSE;
1088 task = g_task_new (stream, cancellable, callback, user_data);
1089 g_task_set_task_data (task, data, g_data_input_stream_read_data_free);
1090 g_task_set_priority (task, io_priority);
1092 g_data_input_stream_read_line_ready (NULL, NULL, task);
1096 g_data_input_stream_read_finish (GDataInputStream *stream,
1097 GAsyncResult *result,
1101 GTask *task = G_TASK (result);
1104 line = g_task_propagate_pointer (task, error);
1108 GDataInputStreamReadData *data = g_task_get_task_data (task);
1110 *length = data->length;
1117 * g_data_input_stream_read_line_async:
1118 * @stream: a given #GDataInputStream.
1119 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1121 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1122 * @callback: (scope async): callback to call when the request is satisfied.
1123 * @user_data: (closure): the data to pass to callback function.
1125 * The asynchronous version of g_data_input_stream_read_line(). It is
1126 * an error to have two outstanding calls to this function.
1128 * When the operation is finished, @callback will be called. You
1129 * can then call g_data_input_stream_read_line_finish() to get
1130 * the result of the operation.
1135 g_data_input_stream_read_line_async (GDataInputStream *stream,
1137 GCancellable *cancellable,
1138 GAsyncReadyCallback callback,
1141 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1142 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1144 g_data_input_stream_read_async (stream, NULL, 0, io_priority,
1145 cancellable, callback, user_data);
1149 * g_data_input_stream_read_until_async:
1150 * @stream: a given #GDataInputStream.
1151 * @stop_chars: characters to terminate the read.
1152 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1154 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1155 * @callback: (scope async): callback to call when the request is satisfied.
1156 * @user_data: (closure): the data to pass to callback function.
1158 * The asynchronous version of g_data_input_stream_read_until().
1159 * It is an error to have two outstanding calls to this function.
1161 * Note that, in contrast to g_data_input_stream_read_until(),
1162 * this function does not consume the stop character that it finds. You
1163 * must read it for yourself.
1165 * When the operation is finished, @callback will be called. You
1166 * can then call g_data_input_stream_read_until_finish() to get
1167 * the result of the operation.
1169 * Don't use this function in new code. Its functionality is
1170 * inconsistent with g_data_input_stream_read_until(). Both functions
1171 * will be marked as deprecated in a future release. Use
1172 * g_data_input_stream_read_upto_async() instead.
1177 g_data_input_stream_read_until_async (GDataInputStream *stream,
1178 const gchar *stop_chars,
1180 GCancellable *cancellable,
1181 GAsyncReadyCallback callback,
1184 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1185 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1186 g_return_if_fail (stop_chars != NULL);
1188 g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
1189 cancellable, callback, user_data);
1193 * g_data_input_stream_read_line_finish:
1194 * @stream: a given #GDataInputStream.
1195 * @result: the #GAsyncResult that was provided to the callback.
1196 * @length: (out): a #gsize to get the length of the data read in.
1197 * @error: #GError for error reporting.
1199 * Finish an asynchronous call started by
1200 * g_data_input_stream_read_line_async(). Note the warning about
1201 * string encoding in g_data_input_stream_read_line() applies here as
1204 * Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
1205 * NUL-terminated byte array with the line that was read in
1206 * (without the newlines). Set @length to a #gsize to get the
1207 * length of the read line. On an error, it will return %NULL and
1208 * @error will be set. If there's no content to read, it will
1209 * still return %NULL, but @error won't be set.
1214 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1215 GAsyncResult *result,
1219 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1221 return g_data_input_stream_read_finish (stream, result, length, error);
1225 * g_data_input_stream_read_line_finish_utf8:
1226 * @stream: a given #GDataInputStream.
1227 * @result: the #GAsyncResult that was provided to the callback.
1228 * @length: (out): a #gsize to get the length of the data read in.
1229 * @error: #GError for error reporting.
1231 * Finish an asynchronous call started by
1232 * g_data_input_stream_read_line_async().
1234 * Returns: (transfer full): a string with the line that was read in
1235 * (without the newlines). Set @length to a #gsize to get the length
1236 * of the read line. On an error, it will return %NULL and @error
1237 * will be set. For UTF-8 conversion errors, the set error domain is
1238 * %G_CONVERT_ERROR. If there's no content to read, it will still
1239 * return %NULL, but @error won't be set.
1244 g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
1245 GAsyncResult *result,
1251 res = g_data_input_stream_read_line_finish (stream, result, length, error);
1255 if (!g_utf8_validate (res, -1, NULL))
1257 g_set_error_literal (error, G_CONVERT_ERROR,
1258 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1259 _("Invalid byte sequence in conversion input"));
1267 * g_data_input_stream_read_until_finish:
1268 * @stream: a given #GDataInputStream.
1269 * @result: the #GAsyncResult that was provided to the callback.
1270 * @length: (out): a #gsize to get the length of the data read in.
1271 * @error: #GError for error reporting.
1273 * Finish an asynchronous call started by
1274 * g_data_input_stream_read_until_async().
1278 * Returns: (transfer full): a string with the data that was read
1279 * before encountering any of the stop characters. Set @length to
1280 * a #gsize to get the length of the string. This function will
1281 * return %NULL on an error.
1284 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1285 GAsyncResult *result,
1289 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1291 return g_data_input_stream_read_finish (stream, result, length, error);
1295 * g_data_input_stream_read_upto:
1296 * @stream: a #GDataInputStream
1297 * @stop_chars: characters to terminate the read
1298 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1300 * @length: (out): a #gsize to get the length of the data read in
1301 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1302 * @error: #GError for error reporting
1304 * Reads a string from the data input stream, up to the first
1305 * occurrence of any of the stop characters.
1307 * In contrast to g_data_input_stream_read_until(), this function
1308 * does <emphasis>not</emphasis> consume the stop character. You have
1309 * to use g_data_input_stream_read_byte() to get it before calling
1310 * g_data_input_stream_read_upto() again.
1312 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1315 * Returns: (transfer full): a string with the data that was read
1316 * before encountering any of the stop characters. Set @length to
1317 * a #gsize to get the length of the string. This function will
1318 * return %NULL on an error
1323 g_data_input_stream_read_upto (GDataInputStream *stream,
1324 const gchar *stop_chars,
1325 gssize stop_chars_len,
1327 GCancellable *cancellable,
1330 GBufferedInputStream *bstream;
1336 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
1338 if (stop_chars_len < 0)
1339 stop_chars_len = strlen (stop_chars);
1341 bstream = G_BUFFERED_INPUT_STREAM (stream);
1345 while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
1347 if (g_buffered_input_stream_get_available (bstream) ==
1348 g_buffered_input_stream_get_buffer_size (bstream))
1349 g_buffered_input_stream_set_buffer_size (bstream,
1350 2 * g_buffered_input_stream_get_buffer_size (bstream));
1352 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
1358 if (g_buffered_input_stream_get_available (bstream) == 0)
1366 found_pos = checked;
1372 data_until = g_malloc (found_pos + 1);
1374 res = g_input_stream_read (G_INPUT_STREAM (stream),
1379 *length = (gsize)found_pos;
1380 g_warn_if_fail (res == found_pos);
1381 data_until[found_pos] = 0;
1387 * g_data_input_stream_read_upto_async:
1388 * @stream: a #GDataInputStream
1389 * @stop_chars: characters to terminate the read
1390 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1392 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1394 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1395 * @callback: (scope async): callback to call when the request is satisfied
1396 * @user_data: (closure): the data to pass to callback function
1398 * The asynchronous version of g_data_input_stream_read_upto().
1399 * It is an error to have two outstanding calls to this function.
1401 * In contrast to g_data_input_stream_read_until(), this function
1402 * does <emphasis>not</emphasis> consume the stop character. You have
1403 * to use g_data_input_stream_read_byte() to get it before calling
1404 * g_data_input_stream_read_upto() again.
1406 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1409 * When the operation is finished, @callback will be called. You
1410 * can then call g_data_input_stream_read_upto_finish() to get
1411 * the result of the operation.
1416 g_data_input_stream_read_upto_async (GDataInputStream *stream,
1417 const gchar *stop_chars,
1418 gssize stop_chars_len,
1420 GCancellable *cancellable,
1421 GAsyncReadyCallback callback,
1424 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1425 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1426 g_return_if_fail (stop_chars != NULL);
1428 g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
1429 cancellable, callback, user_data);
1433 * g_data_input_stream_read_upto_finish:
1434 * @stream: a #GDataInputStream
1435 * @result: the #GAsyncResult that was provided to the callback
1436 * @length: (out): a #gsize to get the length of the data read in
1437 * @error: #GError for error reporting
1439 * Finish an asynchronous call started by
1440 * g_data_input_stream_read_upto_async().
1442 * Note that this function does <emphasis>not</emphasis> consume the
1443 * stop character. You have to use g_data_input_stream_read_byte() to
1444 * get it before calling g_data_input_stream_read_upto_async() again.
1446 * Returns: (transfer full): a string with the data that was read
1447 * before encountering any of the stop characters. Set @length to
1448 * a #gsize to get the length of the string. This function will
1449 * return %NULL on an error.
1454 g_data_input_stream_read_upto_finish (GDataInputStream *stream,
1455 GAsyncResult *result,
1459 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1461 return g_data_input_stream_read_finish (stream, result, length, error);