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"
27 #include "gsimpleasyncresult.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 (GDataInputStream,
68 G_TYPE_BUFFERED_INPUT_STREAM)
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;
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)
89 g_object_class_install_property (object_class,
91 g_param_spec_enum ("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));
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,
106 g_param_spec_enum ("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));
115 g_data_input_stream_set_property (GObject *object,
120 GDataInputStream *dstream;
122 dstream = G_DATA_INPUT_STREAM (object);
126 case PROP_BYTE_ORDER:
127 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
130 case PROP_NEWLINE_TYPE:
131 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 g_data_input_stream_get_property (GObject *object,
147 GDataInputStreamPrivate *priv;
148 GDataInputStream *dstream;
150 dstream = G_DATA_INPUT_STREAM (object);
151 priv = dstream->priv;
155 case PROP_BYTE_ORDER:
156 g_value_set_enum (value, priv->byte_order);
159 case PROP_NEWLINE_TYPE:
160 g_value_set_enum (value, priv->newline_type);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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.
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,
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.
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));
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.
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 availible.
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));
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;
292 read_data (GDataInputStream *stream,
295 GCancellable *cancellable,
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),
310 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
311 _("Unexpected early end-of-stream"));
316 /* This should always succeed, since it's in the buffer */
317 res = g_input_stream_read (G_INPUT_STREAM (stream),
320 g_warn_if_fail (res == size);
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.
337 g_data_input_stream_read_byte (GDataInputStream *stream,
338 GCancellable *cancellable,
343 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
345 if (read_data (stream, &c, 1, cancellable, error))
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
367 g_data_input_stream_read_int16 (GDataInputStream *stream,
368 GCancellable *cancellable,
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);
382 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
383 v = GINT16_FROM_LE (v);
385 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
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
411 g_data_input_stream_read_uint16 (GDataInputStream *stream,
412 GCancellable *cancellable,
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);
426 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
427 v = GUINT16_FROM_LE (v);
429 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
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
459 g_data_input_stream_read_int32 (GDataInputStream *stream,
460 GCancellable *cancellable,
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);
474 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
475 v = GINT32_FROM_LE (v);
477 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
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
507 g_data_input_stream_read_uint32 (GDataInputStream *stream,
508 GCancellable *cancellable,
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);
522 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
523 v = GUINT32_FROM_LE (v);
525 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
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
555 g_data_input_stream_read_int64 (GDataInputStream *stream,
556 GCancellable *cancellable,
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);
570 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
571 v = GINT64_FROM_LE (v);
573 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
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
603 g_data_input_stream_read_uint64 (GDataInputStream *stream,
604 GCancellable *cancellable,
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);
618 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
619 v = GUINT64_FROM_LE (v);
621 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
632 scan_for_newline (GDataInputStream *stream,
634 gboolean *last_saw_cr_out,
635 int *newline_len_out)
637 GBufferedInputStream *bstream;
638 GDataInputStreamPrivate *priv;
640 gsize start, end, peeked;
644 gsize available, checked;
645 gboolean last_saw_cr;
649 bstream = G_BUFFERED_INPUT_STREAM (stream);
651 checked = *checked_out;
652 last_saw_cr = *last_saw_cr_out;
657 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
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:
668 found_pos = start + i;
672 case G_DATA_STREAM_NEWLINE_TYPE_CR:
675 found_pos = start + i;
679 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
680 if (last_saw_cr && buffer[i] == 10)
682 found_pos = start + i - 1;
687 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
688 if (buffer[i] == 10) /* LF */
693 found_pos = start + i - 1;
699 found_pos = start + i;
703 else if (last_saw_cr)
705 /* Last was cr, this is not LF, end is CR */
706 found_pos = start + i - 1;
709 /* Don't check for CR here, instead look at last_saw_cr on next byte */
713 last_saw_cr = (buffer[i] == 13);
717 *newline_len_out = newline_len;
724 *checked_out = checked;
725 *last_saw_cr_out = last_saw_cr;
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.
753 g_data_input_stream_read_line (GDataInputStream *stream,
755 GCancellable *cancellable,
758 GBufferedInputStream *bstream;
760 gboolean last_saw_cr;
766 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
768 bstream = G_BUFFERED_INPUT_STREAM (stream);
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);
787 if (g_buffered_input_stream_get_available (bstream) == 0)
802 line = g_malloc (found_pos + newline_len + 1);
804 res = g_input_stream_read (G_INPUT_STREAM (stream),
806 found_pos + newline_len,
809 *length = (gsize)found_pos;
810 g_warn_if_fail (res == found_pos + newline_len);
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.
837 g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
839 GCancellable *cancellable,
844 res = g_data_input_stream_read_line (stream, length, cancellable, error);
848 if (!g_utf8_validate (res, -1, NULL))
850 g_set_error_literal (error, G_CONVERT_ERROR,
851 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
852 _("Invalid byte sequence in conversion input"));
860 scan_for_chars (GDataInputStream *stream,
862 const char *stop_chars,
863 gssize stop_chars_len)
865 GBufferedInputStream *bstream;
867 gsize start, end, peeked;
869 gsize available, checked;
870 const char *stop_char;
871 const char *stop_end;
873 bstream = G_BUFFERED_INPUT_STREAM (stream);
874 stop_end = stop_chars + stop_chars_len;
876 checked = *checked_out;
879 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
881 peeked = end - start;
883 for (i = 0; checked < available && i < peeked; i++)
885 for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
887 if (buffer[i] == *stop_char)
894 *checked_out = checked;
899 * g_data_input_stream_read_until:
900 * @stream: a given #GDataInputStream.
901 * @stop_chars: characters to terminate the read.
902 * @length: (out): a #gsize to get the length of the data read in.
903 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
904 * @error: #GError for error reporting.
906 * Reads a string from the data input stream, up to the first
907 * occurrence of any of the stop characters.
909 * Note that, in contrast to g_data_input_stream_read_until_async(),
910 * this function consumes the stop character that it finds.
912 * Don't use this function in new code. Its functionality is
913 * inconsistent with g_data_input_stream_read_until_async(). Both
914 * functions will be marked as deprecated in a future release. Use
915 * g_data_input_stream_read_upto() instead, but note that that function
916 * does not consume the stop character.
918 * Returns: (transfer full): a string with the data that was read
919 * before encountering any of the stop characters. Set @length to
920 * a #gsize to get the length of the string. This function will
921 * return %NULL on an error.
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)
944 res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
953 GDataInputStream *stream;
954 GSimpleAsyncResult *simple;
955 gboolean last_saw_cr;
958 GCancellable *cancellable;
961 gssize stop_chars_len;
964 } GDataInputStreamReadData;
967 g_data_input_stream_read_complete (GDataInputStreamReadData *data,
970 gboolean need_idle_dispatch)
972 if (read_length || skip_length)
976 data->length = read_length;
977 data->line = g_malloc (read_length + 1);
978 data->line[read_length] = '\0';
980 /* we already checked the buffer. this shouldn't fail. */
981 bytes = g_input_stream_read (G_INPUT_STREAM (data->stream),
982 data->line, read_length, NULL, NULL);
983 g_assert_cmpint (bytes, ==, read_length);
985 bytes = g_input_stream_skip (G_INPUT_STREAM (data->stream),
986 skip_length, NULL, NULL);
987 g_assert_cmpint (bytes, ==, skip_length);
990 if (need_idle_dispatch)
991 g_simple_async_result_complete_in_idle (data->simple);
993 g_simple_async_result_complete (data->simple);
995 g_object_unref (data->simple);
999 g_data_input_stream_read_line_ready (GObject *object,
1000 GAsyncResult *result,
1003 GDataInputStreamReadData *data = user_data;
1008 /* this is a callback. finish the async call. */
1010 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
1011 GError *error = NULL;
1014 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
1021 g_simple_async_result_take_error (data->simple, error);
1025 g_data_input_stream_read_complete (data, data->checked, 0, FALSE);
1029 /* only proceed if we got more bytes... */
1032 if (data->stop_chars)
1034 found_pos = scan_for_chars (data->stream,
1037 data->stop_chars_len);
1041 found_pos = scan_for_newline (data->stream, &data->checked,
1042 &data->last_saw_cr, &newline_len);
1044 if (found_pos == -1)
1045 /* didn't find a full line; need to buffer some more bytes */
1047 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
1050 size = g_buffered_input_stream_get_buffer_size (buffer);
1052 if (g_buffered_input_stream_get_available (buffer) == size)
1053 /* need to grow the buffer */
1054 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1057 g_buffered_input_stream_fill_async (buffer, -1, data->io_priority,
1059 g_data_input_stream_read_line_ready,
1064 /* read the line and the EOL. no error is possible. */
1065 g_data_input_stream_read_complete (data, found_pos,
1066 newline_len, result == NULL);
1071 g_data_input_stream_read_data_free (gpointer user_data)
1073 GDataInputStreamReadData *data = user_data;
1075 /* we don't hold a ref to ->simple because it keeps a ref to us.
1076 * we are called because it is being finalized.
1079 g_free (data->stop_chars);
1080 if (data->cancellable)
1081 g_object_unref (data->cancellable);
1082 g_free (data->line);
1083 g_slice_free (GDataInputStreamReadData, data);
1087 g_data_input_stream_read_async (GDataInputStream *stream,
1088 const gchar *stop_chars,
1089 gssize stop_chars_len,
1091 GCancellable *cancellable,
1092 GAsyncReadyCallback callback,
1094 gpointer source_tag)
1096 GDataInputStreamReadData *data;
1098 data = g_slice_new (GDataInputStreamReadData);
1099 data->stream = stream;
1101 g_object_ref (cancellable);
1102 data->cancellable = cancellable;
1103 if (stop_chars_len == -1)
1104 stop_chars_len = strlen (stop_chars);
1105 data->stop_chars = g_memdup (stop_chars, stop_chars_len);
1106 data->stop_chars_len = stop_chars_len;
1107 data->io_priority = io_priority;
1108 data->last_saw_cr = FALSE;
1112 data->simple = g_simple_async_result_new (G_OBJECT (stream), callback,
1113 user_data, source_tag);
1114 g_simple_async_result_set_op_res_gpointer (data->simple, data,
1115 g_data_input_stream_read_data_free);
1116 g_data_input_stream_read_line_ready (NULL, NULL, data);
1120 g_data_input_stream_read_finish (GDataInputStream *stream,
1121 GAsyncResult *result,
1125 GDataInputStreamReadData *data;
1126 GSimpleAsyncResult *simple;
1129 simple = G_SIMPLE_ASYNC_RESULT (result);
1131 if (g_simple_async_result_propagate_error (simple, error))
1134 data = g_simple_async_result_get_op_res_gpointer (simple);
1140 *length = data->length;
1146 * g_data_input_stream_read_line_async:
1147 * @stream: a given #GDataInputStream.
1148 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1150 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1151 * @callback: (scope async): callback to call when the request is satisfied.
1152 * @user_data: (closure): the data to pass to callback function.
1154 * The asynchronous version of g_data_input_stream_read_line(). It is
1155 * an error to have two outstanding calls to this function.
1157 * When the operation is finished, @callback will be called. You
1158 * can then call g_data_input_stream_read_line_finish() to get
1159 * the result of the operation.
1164 g_data_input_stream_read_line_async (GDataInputStream *stream,
1166 GCancellable *cancellable,
1167 GAsyncReadyCallback callback,
1170 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1171 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1173 g_data_input_stream_read_async (stream, NULL, 0, io_priority,
1174 cancellable, callback, user_data,
1175 g_data_input_stream_read_line_async);
1179 * g_data_input_stream_read_until_async:
1180 * @stream: a given #GDataInputStream.
1181 * @stop_chars: characters to terminate the read.
1182 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1184 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1185 * @callback: (scope async): callback to call when the request is satisfied.
1186 * @user_data: (closure): the data to pass to callback function.
1188 * The asynchronous version of g_data_input_stream_read_until().
1189 * It is an error to have two outstanding calls to this function.
1191 * Note that, in contrast to g_data_input_stream_read_until(),
1192 * this function does not consume the stop character that it finds. You
1193 * must read it for yourself.
1195 * When the operation is finished, @callback will be called. You
1196 * can then call g_data_input_stream_read_until_finish() to get
1197 * the result of the operation.
1199 * Don't use this function in new code. Its functionality is
1200 * inconsistent with g_data_input_stream_read_until(). Both functions
1201 * will be marked as deprecated in a future release. Use
1202 * g_data_input_stream_read_upto_async() instead.
1207 g_data_input_stream_read_until_async (GDataInputStream *stream,
1208 const gchar *stop_chars,
1210 GCancellable *cancellable,
1211 GAsyncReadyCallback callback,
1214 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1215 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1216 g_return_if_fail (stop_chars != NULL);
1218 g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
1219 cancellable, callback, user_data,
1220 g_data_input_stream_read_until_async);
1224 * g_data_input_stream_read_line_finish:
1225 * @stream: a given #GDataInputStream.
1226 * @result: the #GAsyncResult that was provided to the callback.
1227 * @length: (out): a #gsize to get the length of the data read in.
1228 * @error: #GError for error reporting.
1230 * Finish an asynchronous call started by
1231 * g_data_input_stream_read_line_async(). Note the warning about
1232 * string encoding in g_data_input_stream_read_line() applies here as
1235 * Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
1236 * NUL-terminated byte array with the line that was read in
1237 * (without the newlines). Set @length to a #gsize to get the
1238 * length of the read line. On an error, it will return %NULL and
1239 * @error will be set. If there's no content to read, it will
1240 * still return %NULL, but @error won't be set.
1245 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1246 GAsyncResult *result,
1250 g_return_val_if_fail (
1251 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1252 g_data_input_stream_read_line_async), NULL);
1254 return g_data_input_stream_read_finish (stream, result, length, error);
1258 * g_data_input_stream_read_line_finish_utf8:
1259 * @stream: a given #GDataInputStream.
1260 * @result: the #GAsyncResult that was provided to the callback.
1261 * @length: (out): a #gsize to get the length of the data read in.
1262 * @error: #GError for error reporting.
1264 * Finish an asynchronous call started by
1265 * g_data_input_stream_read_line_async().
1267 * Returns: (transfer full): a string with the line that was read in
1268 * (without the newlines). Set @length to a #gsize to get the length
1269 * of the read line. On an error, it will return %NULL and @error
1270 * will be set. For UTF-8 conversion errors, the set error domain is
1271 * %G_CONVERT_ERROR. If there's no content to read, it will still
1272 * return %NULL, but @error won't be set.
1277 g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
1278 GAsyncResult *result,
1284 res = g_data_input_stream_read_line_finish (stream, result, length, error);
1285 if (!g_utf8_validate (res, -1, NULL))
1287 g_set_error_literal (error, G_CONVERT_ERROR,
1288 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1289 _("Invalid byte sequence in conversion input"));
1297 * g_data_input_stream_read_until_finish:
1298 * @stream: a given #GDataInputStream.
1299 * @result: the #GAsyncResult that was provided to the callback.
1300 * @length: (out): a #gsize to get the length of the data read in.
1301 * @error: #GError for error reporting.
1303 * Finish an asynchronous call started by
1304 * g_data_input_stream_read_until_async().
1308 * Returns: (transfer full): a string with the data that was read
1309 * before encountering any of the stop characters. Set @length to
1310 * a #gsize to get the length of the string. This function will
1311 * return %NULL on an error.
1314 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1315 GAsyncResult *result,
1319 g_return_val_if_fail (
1320 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1321 g_data_input_stream_read_until_async), NULL);
1323 return g_data_input_stream_read_finish (stream, result, length, error);
1327 * g_data_input_stream_read_upto:
1328 * @stream: a #GDataInputStream
1329 * @stop_chars: characters to terminate the read
1330 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1332 * @length: (out): a #gsize to get the length of the data read in
1333 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1334 * @error: #GError for error reporting
1336 * Reads a string from the data input stream, up to the first
1337 * occurrence of any of the stop characters.
1339 * In contrast to g_data_input_stream_read_until(), this function
1340 * does <emphasis>not</emphasis> consume the stop character. You have
1341 * to use g_data_input_stream_read_byte() to get it before calling
1342 * g_data_input_stream_read_upto() again.
1344 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1347 * Returns: (transfer full): a string with the data that was read
1348 * before encountering any of the stop characters. Set @length to
1349 * a #gsize to get the length of the string. This function will
1350 * return %NULL on an error
1355 g_data_input_stream_read_upto (GDataInputStream *stream,
1356 const gchar *stop_chars,
1357 gssize stop_chars_len,
1359 GCancellable *cancellable,
1362 GBufferedInputStream *bstream;
1368 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
1370 if (stop_chars_len < 0)
1371 stop_chars_len = strlen (stop_chars);
1373 bstream = G_BUFFERED_INPUT_STREAM (stream);
1377 while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
1379 if (g_buffered_input_stream_get_available (bstream) ==
1380 g_buffered_input_stream_get_buffer_size (bstream))
1381 g_buffered_input_stream_set_buffer_size (bstream,
1382 2 * g_buffered_input_stream_get_buffer_size (bstream));
1384 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
1390 if (g_buffered_input_stream_get_available (bstream) == 0)
1398 found_pos = checked;
1404 data_until = g_malloc (found_pos + 1);
1406 res = g_input_stream_read (G_INPUT_STREAM (stream),
1411 *length = (gsize)found_pos;
1412 g_warn_if_fail (res == found_pos);
1413 data_until[found_pos] = 0;
1419 * g_data_input_stream_read_upto_async:
1420 * @stream: a #GDataInputStream
1421 * @stop_chars: characters to terminate the read
1422 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1424 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1426 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1427 * @callback: (scope async): callback to call when the request is satisfied
1428 * @user_data: (closure): the data to pass to callback function
1430 * The asynchronous version of g_data_input_stream_read_upto().
1431 * It is an error to have two outstanding calls to this function.
1433 * In contrast to g_data_input_stream_read_until(), this function
1434 * does <emphasis>not</emphasis> consume the stop character. You have
1435 * to use g_data_input_stream_read_byte() to get it before calling
1436 * g_data_input_stream_read_upto() again.
1438 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1441 * When the operation is finished, @callback will be called. You
1442 * can then call g_data_input_stream_read_upto_finish() to get
1443 * the result of the operation.
1448 g_data_input_stream_read_upto_async (GDataInputStream *stream,
1449 const gchar *stop_chars,
1450 gssize stop_chars_len,
1452 GCancellable *cancellable,
1453 GAsyncReadyCallback callback,
1456 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1457 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1458 g_return_if_fail (stop_chars != NULL);
1460 g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
1461 cancellable, callback, user_data,
1462 g_data_input_stream_read_upto_async);
1466 * g_data_input_stream_read_upto_finish:
1467 * @stream: a #GDataInputStream
1468 * @result: the #GAsyncResult that was provided to the callback
1469 * @length: (out): a #gsize to get the length of the data read in
1470 * @error: #GError for error reporting
1472 * Finish an asynchronous call started by
1473 * g_data_input_stream_read_upto_async().
1475 * Note that this function does <emphasis>not</emphasis> consume the
1476 * stop character. You have to use g_data_input_stream_read_byte() to
1477 * get it before calling g_data_input_stream_read_upto_async() again.
1479 * Returns: (transfer full): a string with the data that was read
1480 * before encountering any of the stop characters. Set @length to
1481 * a #gsize to get the length of the string. This function will
1482 * return %NULL on an error.
1487 g_data_input_stream_read_upto_finish (GDataInputStream *stream,
1488 GAsyncResult *result,
1492 g_return_val_if_fail (
1493 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1494 g_data_input_stream_read_upto_async), NULL);
1496 return g_data_input_stream_read_finish (stream, result, length, error);