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"
35 * SECTION:gdatainputstream
36 * @short_description: Data Input Stream
38 * @see_also: #GInputStream
40 * Data input stream implements #GInputStream and includes functions for
41 * reading structured data directly from a binary input stream.
45 struct _GDataInputStreamPrivate {
46 GDataStreamByteOrder byte_order;
47 GDataStreamNewlineType newline_type;
56 static void g_data_input_stream_set_property (GObject *object,
60 static void g_data_input_stream_get_property (GObject *object,
65 G_DEFINE_TYPE (GDataInputStream,
67 G_TYPE_BUFFERED_INPUT_STREAM)
71 g_data_input_stream_class_init (GDataInputStreamClass *klass)
73 GObjectClass *object_class;
75 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
77 object_class = G_OBJECT_CLASS (klass);
78 object_class->get_property = g_data_input_stream_get_property;
79 object_class->set_property = g_data_input_stream_set_property;
82 * GDataStream:byte-order:
84 * The ::byte-order property determines the byte ordering that
85 * is used when reading multi-byte entities (such as integers)
88 g_object_class_install_property (object_class,
90 g_param_spec_enum ("byte-order",
93 G_TYPE_DATA_STREAM_BYTE_ORDER,
94 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
95 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
98 * GDataStream:newline-type:
100 * The :newline-type property determines what is considered
101 * as a line ending when reading complete lines from the stream.
103 g_object_class_install_property (object_class,
105 g_param_spec_enum ("newline-type",
107 P_("The accepted types of line ending"),
108 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
109 G_DATA_STREAM_NEWLINE_TYPE_LF,
110 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
114 g_data_input_stream_set_property (GObject *object,
119 GDataInputStream *dstream;
121 dstream = G_DATA_INPUT_STREAM (object);
125 case PROP_BYTE_ORDER:
126 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
129 case PROP_NEWLINE_TYPE:
130 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 g_data_input_stream_get_property (GObject *object,
146 GDataInputStreamPrivate *priv;
147 GDataInputStream *dstream;
149 dstream = G_DATA_INPUT_STREAM (object);
150 priv = dstream->priv;
154 case PROP_BYTE_ORDER:
155 g_value_set_enum (value, priv->byte_order);
158 case PROP_NEWLINE_TYPE:
159 g_value_set_enum (value, priv->newline_type);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 g_data_input_stream_init (GDataInputStream *stream)
171 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
172 G_TYPE_DATA_INPUT_STREAM,
173 GDataInputStreamPrivate);
175 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
176 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
180 * g_data_input_stream_new:
181 * @base_stream: a #GInputStream.
183 * Creates a new data input stream for the @base_stream.
185 * Returns: a new #GDataInputStream.
188 g_data_input_stream_new (GInputStream *base_stream)
190 GDataInputStream *stream;
192 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
194 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
195 "base-stream", base_stream,
202 * g_data_input_stream_set_byte_order:
203 * @stream: a given #GDataInputStream.
204 * @order: a #GDataStreamByteOrder to set.
206 * This function sets the byte order for the given @stream. All subsequent
207 * reads from the @stream will be read in the given @order.
211 g_data_input_stream_set_byte_order (GDataInputStream *stream,
212 GDataStreamByteOrder order)
214 GDataInputStreamPrivate *priv;
216 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
220 if (priv->byte_order != order)
222 priv->byte_order = order;
224 g_object_notify (G_OBJECT (stream), "byte-order");
229 * g_data_input_stream_get_byte_order:
230 * @stream: a given #GDataInputStream.
232 * Gets the byte order for the data input stream.
234 * Returns: the @stream's current #GDataStreamByteOrder.
237 g_data_input_stream_get_byte_order (GDataInputStream *stream)
239 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
241 return stream->priv->byte_order;
245 * g_data_input_stream_set_newline_type:
246 * @stream: a #GDataInputStream.
247 * @type: the type of new line return as #GDataStreamNewlineType.
249 * Sets the newline type for the @stream.
251 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
252 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
253 * "CR LF", and this might block if there is no more data availible.
257 g_data_input_stream_set_newline_type (GDataInputStream *stream,
258 GDataStreamNewlineType type)
260 GDataInputStreamPrivate *priv;
262 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
266 if (priv->newline_type != type)
268 priv->newline_type = type;
270 g_object_notify (G_OBJECT (stream), "newline-type");
275 * g_data_input_stream_get_newline_type:
276 * @stream: a given #GDataInputStream.
278 * Gets the current newline type for the @stream.
280 * Returns: #GDataStreamNewlineType for the given @stream.
282 GDataStreamNewlineType
283 g_data_input_stream_get_newline_type (GDataInputStream *stream)
285 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
287 return stream->priv->newline_type;
291 read_data (GDataInputStream *stream,
294 GCancellable *cancellable,
300 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
302 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
309 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
310 _("Unexpected early end-of-stream"));
315 /* This should always succeed, since it's in the buffer */
316 res = g_input_stream_read (G_INPUT_STREAM (stream),
319 g_warn_if_fail (res == size);
325 * g_data_input_stream_read_byte:
326 * @stream: a given #GDataInputStream.
327 * @cancellable: optional #GCancellable object, %NULL to ignore.
328 * @error: #GError for error reporting.
330 * Reads an unsigned 8-bit/1-byte value from @stream.
332 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
333 * if an error occurred.
336 g_data_input_stream_read_byte (GDataInputStream *stream,
337 GCancellable *cancellable,
342 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
344 if (read_data (stream, &c, 1, cancellable, error))
352 * g_data_input_stream_read_int16:
353 * @stream: a given #GDataInputStream.
354 * @cancellable: optional #GCancellable object, %NULL to ignore.
355 * @error: #GError for error reporting.
357 * Reads a 16-bit/2-byte value from @stream.
359 * In order to get the correct byte order for this read operation,
360 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
362 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
366 g_data_input_stream_read_int16 (GDataInputStream *stream,
367 GCancellable *cancellable,
372 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
374 if (read_data (stream, &v, 2, cancellable, error))
376 switch (stream->priv->byte_order)
378 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
379 v = GINT16_FROM_BE (v);
381 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
382 v = GINT16_FROM_LE (v);
384 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
396 * g_data_input_stream_read_uint16:
397 * @stream: a given #GDataInputStream.
398 * @cancellable: optional #GCancellable object, %NULL to ignore.
399 * @error: #GError for error reporting.
401 * Reads an unsigned 16-bit/2-byte value from @stream.
403 * In order to get the correct byte order for this read operation,
404 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
406 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
410 g_data_input_stream_read_uint16 (GDataInputStream *stream,
411 GCancellable *cancellable,
416 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
418 if (read_data (stream, &v, 2, cancellable, error))
420 switch (stream->priv->byte_order)
422 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
423 v = GUINT16_FROM_BE (v);
425 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
426 v = GUINT16_FROM_LE (v);
428 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
440 * g_data_input_stream_read_int32:
441 * @stream: a given #GDataInputStream.
442 * @cancellable: optional #GCancellable object, %NULL to ignore.
443 * @error: #GError for error reporting.
445 * Reads a signed 32-bit/4-byte value from @stream.
447 * In order to get the correct byte order for this read operation,
448 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
450 * If @cancellable is not %NULL, then the operation can be cancelled by
451 * triggering the cancellable object from another thread. If the operation
452 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
454 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
458 g_data_input_stream_read_int32 (GDataInputStream *stream,
459 GCancellable *cancellable,
464 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
466 if (read_data (stream, &v, 4, cancellable, error))
468 switch (stream->priv->byte_order)
470 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
471 v = GINT32_FROM_BE (v);
473 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
474 v = GINT32_FROM_LE (v);
476 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
488 * g_data_input_stream_read_uint32:
489 * @stream: a given #GDataInputStream.
490 * @cancellable: optional #GCancellable object, %NULL to ignore.
491 * @error: #GError for error reporting.
493 * Reads an unsigned 32-bit/4-byte value from @stream.
495 * In order to get the correct byte order for this read operation,
496 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
498 * If @cancellable is not %NULL, then the operation can be cancelled by
499 * triggering the cancellable object from another thread. If the operation
500 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
502 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
506 g_data_input_stream_read_uint32 (GDataInputStream *stream,
507 GCancellable *cancellable,
512 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
514 if (read_data (stream, &v, 4, cancellable, error))
516 switch (stream->priv->byte_order)
518 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
519 v = GUINT32_FROM_BE (v);
521 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
522 v = GUINT32_FROM_LE (v);
524 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
536 * g_data_input_stream_read_int64:
537 * @stream: a given #GDataInputStream.
538 * @cancellable: optional #GCancellable object, %NULL to ignore.
539 * @error: #GError for error reporting.
541 * Reads a 64-bit/8-byte value from @stream.
543 * In order to get the correct byte order for this read operation,
544 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
546 * If @cancellable is not %NULL, then the operation can be cancelled by
547 * triggering the cancellable object from another thread. If the operation
548 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
550 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
554 g_data_input_stream_read_int64 (GDataInputStream *stream,
555 GCancellable *cancellable,
560 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
562 if (read_data (stream, &v, 8, cancellable, error))
564 switch (stream->priv->byte_order)
566 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
567 v = GINT64_FROM_BE (v);
569 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
570 v = GINT64_FROM_LE (v);
572 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
584 * g_data_input_stream_read_uint64:
585 * @stream: a given #GDataInputStream.
586 * @cancellable: optional #GCancellable object, %NULL to ignore.
587 * @error: #GError for error reporting.
589 * Reads an unsigned 64-bit/8-byte value from @stream.
591 * In order to get the correct byte order for this read operation,
592 * see g_data_input_stream_get_byte_order().
594 * If @cancellable is not %NULL, then the operation can be cancelled by
595 * triggering the cancellable object from another thread. If the operation
596 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
598 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
602 g_data_input_stream_read_uint64 (GDataInputStream *stream,
603 GCancellable *cancellable,
608 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
610 if (read_data (stream, &v, 8, cancellable, error))
612 switch (stream->priv->byte_order)
614 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
615 v = GUINT64_FROM_BE (v);
617 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
618 v = GUINT64_FROM_LE (v);
620 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
631 scan_for_newline (GDataInputStream *stream,
633 gboolean *last_saw_cr_out,
634 int *newline_len_out)
636 GBufferedInputStream *bstream;
637 GDataInputStreamPrivate *priv;
639 gsize start, end, peeked;
643 gsize available, checked;
644 gboolean last_saw_cr;
648 bstream = G_BUFFERED_INPUT_STREAM (stream);
650 checked = *checked_out;
651 last_saw_cr = *last_saw_cr_out;
656 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
658 peeked = end - start;
660 for (i = 0; checked < available && i < peeked; i++)
662 switch (priv->newline_type)
664 case G_DATA_STREAM_NEWLINE_TYPE_LF:
667 found_pos = start + i;
671 case G_DATA_STREAM_NEWLINE_TYPE_CR:
674 found_pos = start + i;
678 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
679 if (last_saw_cr && buffer[i] == 10)
681 found_pos = start + i - 1;
686 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
687 if (buffer[i] == 10) /* LF */
692 found_pos = start + i - 1;
698 found_pos = start + i;
702 else if (last_saw_cr)
704 /* Last was cr, this is not LF, end is CR */
705 found_pos = start + i - 1;
708 /* Don't check for CR here, instead look at last_saw_cr on next byte */
712 last_saw_cr = (buffer[i] == 13);
716 *newline_len_out = newline_len;
723 *checked_out = checked;
724 *last_saw_cr_out = last_saw_cr;
730 * g_data_input_stream_read_line:
731 * @stream: a given #GDataInputStream.
732 * @length: a #gsize to get the length of the data read in.
733 * @cancellable: optional #GCancellable object, %NULL to ignore.
734 * @error: #GError for error reporting.
736 * Reads a line from the data input stream.
738 * If @cancellable is not %NULL, then the operation can be cancelled by
739 * triggering the cancellable object from another thread. If the operation
740 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
742 * Returns: a string with the line that was read in (without the newlines).
743 * Set @length to a #gsize to get the length of the read line.
744 * On an error, it will return %NULL and @error will be set. If there's no
745 * content to read, it will still return %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 scan_for_chars (GDataInputStream *stream,
814 const char *stop_chars)
816 GBufferedInputStream *bstream;
818 gsize start, end, peeked;
820 gsize available, checked;
821 const char *stop_char;
823 bstream = G_BUFFERED_INPUT_STREAM (stream);
825 checked = *checked_out;
828 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
830 peeked = end - start;
832 for (i = 0; checked < available && i < peeked; i++)
834 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
836 if (buffer[i] == *stop_char)
843 *checked_out = checked;
848 * g_data_input_stream_read_until:
849 * @stream: a given #GDataInputStream.
850 * @stop_chars: characters to terminate the read.
851 * @length: a #gsize to get the length of the data read in.
852 * @cancellable: optional #GCancellable object, %NULL to ignore.
853 * @error: #GError for error reporting.
855 * Reads a string from the data input stream, up to the first
856 * occurrence of any of the stop characters.
858 * Note that, in contrast to g_data_input_stream_read_until_async(),
859 * this function consumes the stop character that it finds.
861 * Returns: a string with the data that was read before encountering
862 * any of the stop characters. Set @length to a #gsize to get the length
863 * of the string. This function will return %NULL on an error.
866 g_data_input_stream_read_until (GDataInputStream *stream,
867 const gchar *stop_chars,
869 GCancellable *cancellable,
872 GBufferedInputStream *bstream;
879 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
881 bstream = G_BUFFERED_INPUT_STREAM (stream);
886 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
888 if (g_buffered_input_stream_get_available (bstream) ==
889 g_buffered_input_stream_get_buffer_size (bstream))
890 g_buffered_input_stream_set_buffer_size (bstream,
891 2 * g_buffered_input_stream_get_buffer_size (bstream));
893 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
899 if (g_buffered_input_stream_get_available (bstream) == 0)
914 data_until = g_malloc (found_pos + stop_char_len + 1);
916 res = g_input_stream_read (G_INPUT_STREAM (stream),
918 found_pos + stop_char_len,
921 *length = (gsize)found_pos;
922 g_warn_if_fail (res == found_pos + stop_char_len);
923 data_until[found_pos] = 0;
930 GDataInputStream *stream;
931 GSimpleAsyncResult *simple;
932 gboolean last_saw_cr;
935 GCancellable *cancellable;
940 } GDataInputStreamReadData;
943 g_data_input_stream_read_complete (GDataInputStreamReadData *data,
946 gboolean need_idle_dispatch)
948 if (read_length || skip_length)
952 data->length = read_length;
953 data->line = g_malloc (read_length + 1);
954 data->line[read_length] = '\0';
956 /* we already checked the buffer. this shouldn't fail. */
957 bytes = g_input_stream_read (G_INPUT_STREAM (data->stream),
958 data->line, read_length, NULL, NULL);
959 g_assert_cmpint (bytes, ==, read_length);
961 bytes = g_input_stream_skip (G_INPUT_STREAM (data->stream),
962 skip_length, NULL, NULL);
963 g_assert_cmpint (bytes, ==, skip_length);
966 if (need_idle_dispatch)
967 g_simple_async_result_complete_in_idle (data->simple);
969 g_simple_async_result_complete (data->simple);
971 g_object_unref (data->simple);
975 g_data_input_stream_read_line_ready (GObject *object,
976 GAsyncResult *result,
979 GDataInputStreamReadData *data = user_data;
984 /* this is a callback. finish the async call. */
986 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
987 GError *error = NULL;
990 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
997 g_simple_async_result_set_from_error (data->simple, error);
998 g_error_free (error);
1002 g_data_input_stream_read_complete (data, data->checked, 0, FALSE);
1006 /* only proceed if we got more bytes... */
1009 if (data->stop_chars)
1011 found_pos = scan_for_chars (data->stream,
1017 found_pos = scan_for_newline (data->stream, &data->checked,
1018 &data->last_saw_cr, &newline_len);
1020 if (found_pos == -1)
1021 /* didn't find a full line; need to buffer some more bytes */
1023 GBufferedInputStream *buffer = G_BUFFERED_INPUT_STREAM (data->stream);
1026 size = g_buffered_input_stream_get_buffer_size (buffer);
1028 if (g_buffered_input_stream_get_available (buffer) == size)
1029 /* need to grow the buffer */
1030 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1033 g_buffered_input_stream_fill_async (buffer, -1, data->io_priority,
1035 g_data_input_stream_read_line_ready,
1040 /* read the line and the EOL. no error is possible. */
1041 g_data_input_stream_read_complete (data, found_pos,
1042 newline_len, result == NULL);
1047 g_data_input_stream_read_data_free (gpointer user_data)
1049 GDataInputStreamReadData *data = user_data;
1051 /* we don't hold a ref to ->simple because it keeps a ref to us.
1052 * we are called because it is being finalized.
1055 g_free (data->stop_chars);
1056 if (data->cancellable)
1057 g_object_unref (data->cancellable);
1058 g_free (data->line);
1059 g_slice_free (GDataInputStreamReadData, data);
1063 g_data_input_stream_read_async (GDataInputStream *stream,
1064 const gchar *stop_chars,
1066 GCancellable *cancellable,
1067 GAsyncReadyCallback callback,
1069 gpointer source_tag)
1071 GDataInputStreamReadData *data;
1073 data = g_slice_new (GDataInputStreamReadData);
1074 data->stream = stream;
1076 g_object_ref (cancellable);
1077 data->cancellable = cancellable;
1078 data->stop_chars = g_strdup (stop_chars);
1079 data->io_priority = io_priority;
1080 data->last_saw_cr = FALSE;
1084 data->simple = g_simple_async_result_new (G_OBJECT (stream), callback,
1085 user_data, source_tag);
1086 g_simple_async_result_set_op_res_gpointer (data->simple, data,
1087 g_data_input_stream_read_data_free);
1088 g_data_input_stream_read_line_ready (NULL, NULL, data);
1092 g_data_input_stream_read_finish (GDataInputStream *stream,
1093 GAsyncResult *result,
1097 GDataInputStreamReadData *data;
1098 GSimpleAsyncResult *simple;
1101 simple = G_SIMPLE_ASYNC_RESULT (result);
1103 if (g_simple_async_result_propagate_error (simple, error))
1106 data = g_simple_async_result_get_op_res_gpointer (simple);
1112 *length = data->length;
1118 * g_data_input_stream_read_line_async:
1119 * @stream: a given #GDataInputStream.
1120 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1122 * @cancellable: optional #GCancellable object, %NULL to ignore.
1123 * @callback: callback to call when the request is satisfied.
1124 * @user_data: the data to pass to callback function.
1126 * The asynchronous version of g_data_input_stream_read_line(). It is
1127 * an error to have two outstanding calls to this function.
1129 * When the operation is finished, @callback will be called. You
1130 * can then call g_data_input_stream_read_line_finish() to get
1131 * the result of the operation.
1136 g_data_input_stream_read_line_async (GDataInputStream *stream,
1138 GCancellable *cancellable,
1139 GAsyncReadyCallback callback,
1142 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1143 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1145 g_data_input_stream_read_async (stream, NULL, io_priority,
1146 cancellable, callback, user_data,
1147 g_data_input_stream_read_line_async);
1151 * g_data_input_stream_read_until_async:
1152 * @stream: a given #GDataInputStream.
1153 * @stop_chars: characters to terminate the read.
1154 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1156 * @cancellable: optional #GCancellable object, %NULL to ignore.
1157 * @callback: callback to call when the request is satisfied.
1158 * @user_data: the data to pass to callback function.
1160 * The asynchronous version of g_data_input_stream_read_until().
1161 * It is an error to have two outstanding calls to this function.
1163 * Note that, in contrast to g_data_input_stream_read_until(),
1164 * this function does not consume the stop character that it finds. You
1165 * must read it for yourself.
1167 * When the operation is finished, @callback will be called. You
1168 * can then call g_data_input_stream_read_until_finish() to get
1169 * the result of the operation.
1174 g_data_input_stream_read_until_async (GDataInputStream *stream,
1175 const gchar *stop_chars,
1177 GCancellable *cancellable,
1178 GAsyncReadyCallback callback,
1181 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1182 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1183 g_return_if_fail (stop_chars != NULL);
1185 g_data_input_stream_read_async (stream, stop_chars, io_priority,
1186 cancellable, callback, user_data,
1187 g_data_input_stream_read_until_async);
1191 * g_data_input_stream_read_line_finish:
1192 * @stream: a given #GDataInputStream.
1193 * @result: the #GAsyncResult that was provided to the callback.
1194 * @length: a #gsize to get the length of the data read in.
1195 * @error: #GError for error reporting.
1197 * Finish an asynchronous call started by
1198 * g_data_input_stream_read_line_async().
1200 * Returns: a string with the line that was read in (without the newlines).
1201 * Set @length to a #gsize to get the length of the read line.
1202 * On an error, it will return %NULL and @error will be set. If there's no
1203 * content to read, it will still return %NULL, but @error won't be set.
1208 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1209 GAsyncResult *result,
1213 g_return_val_if_fail (
1214 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1215 g_data_input_stream_read_line_async), NULL);
1217 return g_data_input_stream_read_finish (stream, result, length, error);
1221 * g_data_input_stream_read_until_finish:
1222 * @stream: a given #GDataInputStream.
1223 * @result: the #GAsyncResult that was provided to the callback.
1224 * @length: a #gsize to get the length of the data read in.
1225 * @error: #GError for error reporting.
1227 * Finish an asynchronous call started by
1228 * g_data_input_stream_read_until_async().
1232 * Returns: a string with the data that was read before encountering
1233 * any of the stop characters. Set @length to a #gsize to get the length
1234 * of the string. This function will return %NULL on an error.
1237 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1238 GAsyncResult *result,
1242 g_return_val_if_fail (
1243 g_simple_async_result_is_valid (result, G_OBJECT (stream),
1244 g_data_input_stream_read_until_async), NULL);
1246 return g_data_input_stream_read_finish (stream, result, length, error);