1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gdatainputstream.h"
26 #include "gioenumtypes.h"
32 * SECTION:gdatainputstream
33 * @short_description: Data Input Stream
35 * @see_also: #GInputStream
37 * Data input stream implements #GInputStream and includes functions for
38 * reading structured data directly from a binary input stream.
42 struct _GDataInputStreamPrivate {
43 GDataStreamByteOrder byte_order;
44 GDataStreamNewlineType newline_type;
53 static void g_data_input_stream_set_property (GObject *object,
57 static void g_data_input_stream_get_property (GObject *object,
62 G_DEFINE_TYPE (GDataInputStream,
64 G_TYPE_BUFFERED_INPUT_STREAM)
68 g_data_input_stream_class_init (GDataInputStreamClass *klass)
70 GObjectClass *object_class;
72 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
74 object_class = G_OBJECT_CLASS (klass);
75 object_class->get_property = g_data_input_stream_get_property;
76 object_class->set_property = g_data_input_stream_set_property;
79 * GDataStream:byte-order:
81 * The ::byte-order property determines the byte ordering that
82 * is used when reading multi-byte entities (such as integers)
85 g_object_class_install_property (object_class,
87 g_param_spec_enum ("byte-order",
90 G_TYPE_DATA_STREAM_BYTE_ORDER,
91 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
92 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
95 * GDataStream:newline-type:
97 * The :newline-type property determines what is considered
98 * as a line ending when reading complete lines from the stream.
100 g_object_class_install_property (object_class,
102 g_param_spec_enum ("newline-type",
104 P_("The accepted types of line ending"),
105 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
106 G_DATA_STREAM_NEWLINE_TYPE_LF,
107 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
111 g_data_input_stream_set_property (GObject *object,
116 GDataInputStreamPrivate *priv;
117 GDataInputStream *dstream;
119 dstream = G_DATA_INPUT_STREAM (object);
120 priv = dstream->priv;
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_TYPE_INSTANCE_GET_PRIVATE (stream,
171 G_TYPE_DATA_INPUT_STREAM,
172 GDataInputStreamPrivate);
174 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
175 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
179 * g_data_input_stream_new:
180 * @base_stream: a #GInputStream.
182 * Creates a new data input stream for the @base_stream.
184 * Returns: a new #GDataInputStream.
187 g_data_input_stream_new (GInputStream *base_stream)
189 GDataInputStream *stream;
191 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
193 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
194 "base-stream", base_stream,
201 * g_data_input_stream_set_byte_order:
202 * @stream: a given #GDataInputStream.
203 * @order: a #GDataStreamByteOrder to set.
205 * This function sets the byte order for the given @stream. All subsequent
206 * reads from the @stream will be read in the given @order.
210 g_data_input_stream_set_byte_order (GDataInputStream *stream,
211 GDataStreamByteOrder order)
213 GDataInputStreamPrivate *priv;
215 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
219 if (priv->byte_order != order)
221 priv->byte_order = order;
223 g_object_notify (G_OBJECT (stream), "byte-order");
228 * g_data_input_stream_get_byte_order:
229 * @stream: a given #GDataInputStream.
231 * Gets the byte order for the data input stream.
233 * Returns: the @stream's current #GDataStreamByteOrder.
236 g_data_input_stream_get_byte_order (GDataInputStream *stream)
238 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
240 return stream->priv->byte_order;
244 * g_data_input_stream_set_newline_type:
245 * @stream: a #GDataInputStream.
246 * @type: the type of new line return as #GDataStreamNewlineType.
248 * Sets the newline type for the @stream.
250 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
251 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
252 * "CR LF", and this might block if there is no more data availible.
256 g_data_input_stream_set_newline_type (GDataInputStream *stream,
257 GDataStreamNewlineType type)
259 GDataInputStreamPrivate *priv;
261 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
265 if (priv->newline_type != type)
267 priv->newline_type = type;
269 g_object_notify (G_OBJECT (stream), "newline-type");
274 * g_data_input_stream_get_newline_type:
275 * @stream: a given #GDataInputStream.
277 * Gets the current newline type for the @stream.
279 * Returns: #GDataStreamNewlineType for the given @stream.
281 GDataStreamNewlineType
282 g_data_input_stream_get_newline_type (GDataInputStream *stream)
284 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
286 return stream->priv->newline_type;
290 read_data (GDataInputStream *stream,
293 GCancellable *cancellable,
299 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
301 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
308 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
309 _("Unexpected early end-of-stream"));
314 /* This should always succeed, since its in the buffer */
315 res = g_input_stream_read (G_INPUT_STREAM (stream),
318 g_warn_if_fail (res == size);
324 * g_data_input_stream_read_byte:
325 * @stream: a given #GDataInputStream.
326 * @cancellable: optional #GCancellable object, %NULL to ignore.
327 * @error: #GError for error reporting.
329 * Reads an unsigned 8-bit/1-byte value from @stream.
331 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
332 * if an error occurred.
335 g_data_input_stream_read_byte (GDataInputStream *stream,
336 GCancellable *cancellable,
341 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
343 if (read_data (stream, &c, 1, cancellable, error))
351 * g_data_input_stream_read_int16:
352 * @stream: a given #GDataInputStream.
353 * @cancellable: optional #GCancellable object, %NULL to ignore.
354 * @error: #GError for error reporting.
356 * Reads a 16-bit/2-byte value from @stream.
358 * In order to get the correct byte order for this read operation,
359 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
361 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
365 g_data_input_stream_read_int16 (GDataInputStream *stream,
366 GCancellable *cancellable,
371 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
373 if (read_data (stream, &v, 2, cancellable, error))
375 switch (stream->priv->byte_order)
377 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
378 v = GINT16_FROM_BE (v);
380 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
381 v = GINT16_FROM_LE (v);
383 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
395 * g_data_input_stream_read_uint16:
396 * @stream: a given #GDataInputStream.
397 * @cancellable: optional #GCancellable object, %NULL to ignore.
398 * @error: #GError for error reporting.
400 * Reads an unsigned 16-bit/2-byte value from @stream.
402 * In order to get the correct byte order for this read operation,
403 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
405 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
409 g_data_input_stream_read_uint16 (GDataInputStream *stream,
410 GCancellable *cancellable,
415 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
417 if (read_data (stream, &v, 2, cancellable, error))
419 switch (stream->priv->byte_order)
421 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
422 v = GUINT16_FROM_BE (v);
424 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
425 v = GUINT16_FROM_LE (v);
427 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
439 * g_data_input_stream_read_int32:
440 * @stream: a given #GDataInputStream.
441 * @cancellable: optional #GCancellable object, %NULL to ignore.
442 * @error: #GError for error reporting.
444 * Reads a signed 32-bit/4-byte value from @stream.
446 * In order to get the correct byte order for this read operation,
447 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
449 * If @cancellable is not %NULL, then the operation can be cancelled by
450 * triggering the cancellable object from another thread. If the operation
451 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
453 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
457 g_data_input_stream_read_int32 (GDataInputStream *stream,
458 GCancellable *cancellable,
463 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
465 if (read_data (stream, &v, 4, cancellable, error))
467 switch (stream->priv->byte_order)
469 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
470 v = GINT32_FROM_BE (v);
472 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
473 v = GINT32_FROM_LE (v);
475 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
487 * g_data_input_stream_read_uint32:
488 * @stream: a given #GDataInputStream.
489 * @cancellable: optional #GCancellable object, %NULL to ignore.
490 * @error: #GError for error reporting.
492 * Reads an unsigned 32-bit/4-byte value from @stream.
494 * In order to get the correct byte order for this read operation,
495 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
497 * If @cancellable is not %NULL, then the operation can be cancelled by
498 * triggering the cancellable object from another thread. If the operation
499 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
501 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
505 g_data_input_stream_read_uint32 (GDataInputStream *stream,
506 GCancellable *cancellable,
511 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
513 if (read_data (stream, &v, 4, cancellable, error))
515 switch (stream->priv->byte_order)
517 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
518 v = GUINT32_FROM_BE (v);
520 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
521 v = GUINT32_FROM_LE (v);
523 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
535 * g_data_input_stream_read_int64:
536 * @stream: a given #GDataInputStream.
537 * @cancellable: optional #GCancellable object, %NULL to ignore.
538 * @error: #GError for error reporting.
540 * Reads a 64-bit/8-byte value from @stream.
542 * In order to get the correct byte order for this read operation,
543 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
545 * If @cancellable is not %NULL, then the operation can be cancelled by
546 * triggering the cancellable object from another thread. If the operation
547 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
549 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
553 g_data_input_stream_read_int64 (GDataInputStream *stream,
554 GCancellable *cancellable,
559 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
561 if (read_data (stream, &v, 8, cancellable, error))
563 switch (stream->priv->byte_order)
565 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
566 v = GINT64_FROM_BE (v);
568 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
569 v = GINT64_FROM_LE (v);
571 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
583 * g_data_input_stream_read_uint64:
584 * @stream: a given #GDataInputStream.
585 * @cancellable: optional #GCancellable object, %NULL to ignore.
586 * @error: #GError for error reporting.
588 * Reads an unsigned 64-bit/8-byte value from @stream.
590 * In order to get the correct byte order for this read operation,
591 * see g_data_stream_get_byte_order().
593 * If @cancellable is not %NULL, then the operation can be cancelled by
594 * triggering the cancellable object from another thread. If the operation
595 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
597 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
601 g_data_input_stream_read_uint64 (GDataInputStream *stream,
602 GCancellable *cancellable,
607 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
609 if (read_data (stream, &v, 8, cancellable, error))
611 switch (stream->priv->byte_order)
613 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
614 v = GUINT64_FROM_BE (v);
616 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
617 v = GUINT64_FROM_LE (v);
619 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
630 scan_for_newline (GDataInputStream *stream,
632 gboolean *last_saw_cr_out,
633 int *newline_len_out)
635 GBufferedInputStream *bstream;
636 GDataInputStreamPrivate *priv;
638 gsize start, end, peeked;
642 gsize available, checked;
643 gboolean last_saw_cr;
647 bstream = G_BUFFERED_INPUT_STREAM (stream);
649 checked = *checked_out;
650 last_saw_cr = *last_saw_cr_out;
655 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
657 peeked = end - start;
659 for (i = 0; checked < available && i < peeked; i++)
661 switch (priv->newline_type)
663 case G_DATA_STREAM_NEWLINE_TYPE_LF:
666 found_pos = start + i;
670 case G_DATA_STREAM_NEWLINE_TYPE_CR:
673 found_pos = start + i;
677 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
678 if (last_saw_cr && buffer[i] == 10)
680 found_pos = start + i - 1;
685 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
686 if (buffer[i] == 10) /* LF */
691 found_pos = start + i - 1;
697 found_pos = start + i;
701 else if (last_saw_cr)
703 /* Last was cr, this is not LF, end is CR */
704 found_pos = start + i - 1;
707 /* Don't check for CR here, instead look at last_saw_cr on next byte */
711 last_saw_cr = (buffer[i] == 13);
715 *newline_len_out = newline_len;
722 *checked_out = checked;
723 *last_saw_cr_out = last_saw_cr;
729 * g_data_input_stream_read_line:
730 * @stream: a given #GDataInputStream.
731 * @length: a #gsize to get the length of the data read in.
732 * @cancellable: optional #GCancellable object, %NULL to ignore.
733 * @error: #GError for error reporting.
735 * Reads a line from the data input stream.
737 * If @cancellable is not %NULL, then the operation can be cancelled by
738 * triggering the cancellable object from another thread. If the operation
739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
741 * Returns: a string with the line that was read in (including the newlines).
742 * Set @length to a #gsize to get the length of the read line. Returns %NULL on an error.
745 g_data_input_stream_read_line (GDataInputStream *stream,
747 GCancellable *cancellable,
750 GBufferedInputStream *bstream;
752 gboolean last_saw_cr;
758 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
760 bstream = G_BUFFERED_INPUT_STREAM (stream);
766 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
768 if (g_buffered_input_stream_get_available (bstream) ==
769 g_buffered_input_stream_get_buffer_size (bstream))
770 g_buffered_input_stream_set_buffer_size (bstream,
771 2 * g_buffered_input_stream_get_buffer_size (bstream));
773 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
779 if (g_buffered_input_stream_get_available (bstream) == 0)
794 line = g_malloc (found_pos + newline_len + 1);
796 res = g_input_stream_read (G_INPUT_STREAM (stream),
798 found_pos + newline_len,
801 *length = (gsize)found_pos;
802 g_warn_if_fail (res == found_pos + newline_len);
810 scan_for_chars (GDataInputStream *stream,
812 const char *stop_chars)
814 GBufferedInputStream *bstream;
815 GDataInputStreamPrivate *priv;
817 gsize start, end, peeked;
820 gsize available, checked;
821 const char *stop_char;
825 bstream = G_BUFFERED_INPUT_STREAM (stream);
827 checked = *checked_out;
831 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
833 peeked = end - start;
835 for (i = 0; checked < available && i < peeked; i++)
837 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
839 if (buffer[i] == *stop_char)
846 *checked_out = checked;
851 * g_data_input_stream_read_until:
852 * @stream: a given #GDataInputStream.
853 * @stop_chars: characters to terminate the read.
854 * @length: a #gsize to get the length of the data read in.
855 * @cancellable: optional #GCancellable object, %NULL to ignore.
856 * @error: #GError for error reporting.
858 * Reads a string from the data input stream, up to the first
859 * occurrance of any of the stop characters.
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;
928 #define __G_DATA_INPUT_STREAM_C__
929 #include "gioaliasdef.c"