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
34 * @see_also: #GInputStream
36 * Data input stream implements #GInputStream and includes functions for
37 * reading data directly from an input stream.
41 struct _GDataInputStreamPrivate {
42 GDataStreamByteOrder byte_order;
43 GDataStreamNewlineType newline_type;
52 static void g_data_input_stream_set_property (GObject *object,
56 static void g_data_input_stream_get_property (GObject *object,
61 G_DEFINE_TYPE (GDataInputStream,
63 G_TYPE_BUFFERED_INPUT_STREAM)
67 g_data_input_stream_class_init (GDataInputStreamClass *klass)
69 GObjectClass *object_class;
71 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
73 object_class = G_OBJECT_CLASS (klass);
74 object_class->get_property = g_data_input_stream_get_property;
75 object_class->set_property = g_data_input_stream_set_property;
78 * GDataStream:byte-order:
80 * The ::byte-order property determines the byte ordering that
81 * is used when reading multi-byte entities (such as integers)
84 g_object_class_install_property (object_class,
86 g_param_spec_enum ("byte-order",
89 G_TYPE_DATA_STREAM_BYTE_ORDER,
90 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
91 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
94 * GDataStream:newline-type:
96 * The :newline-type property determines what is considered
97 * as a line ending when reading complete lines from the stream.
99 g_object_class_install_property (object_class,
101 g_param_spec_enum ("newline-type",
103 P_("The accepted types of line ending"),
104 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
105 G_DATA_STREAM_NEWLINE_TYPE_LF,
106 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
110 g_data_input_stream_set_property (GObject *object,
115 GDataInputStreamPrivate *priv;
116 GDataInputStream *dstream;
118 dstream = G_DATA_INPUT_STREAM (object);
119 priv = dstream->priv;
123 case PROP_BYTE_ORDER:
124 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
127 case PROP_NEWLINE_TYPE:
128 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 g_data_input_stream_get_property (GObject *object,
144 GDataInputStreamPrivate *priv;
145 GDataInputStream *dstream;
147 dstream = G_DATA_INPUT_STREAM (object);
148 priv = dstream->priv;
152 case PROP_BYTE_ORDER:
153 g_value_set_enum (value, priv->byte_order);
156 case PROP_NEWLINE_TYPE:
157 g_value_set_enum (value, priv->newline_type);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 g_data_input_stream_init (GDataInputStream *stream)
169 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
170 G_TYPE_DATA_INPUT_STREAM,
171 GDataInputStreamPrivate);
173 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
174 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
178 * g_data_input_stream_new:
179 * @base_stream: a #GInputStream.
181 * Creates a new data input stream for the @base_stream.
183 * Returns: a new #GDataInputStream.
186 g_data_input_stream_new (GInputStream *base_stream)
188 GDataInputStream *stream;
190 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
192 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
193 "base-stream", base_stream,
200 * g_data_input_stream_set_byte_order:
201 * @stream: a given #GDataInputStream.
202 * @order: a #GDataStreamByteOrder to set.
204 * This function sets the byte order for the given @stream. All subsequent
205 * reads from the @stream will be read in the given @order.
209 g_data_input_stream_set_byte_order (GDataInputStream *stream,
210 GDataStreamByteOrder order)
212 GDataInputStreamPrivate *priv;
214 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
218 if (priv->byte_order != order)
220 priv->byte_order = order;
222 g_object_notify (G_OBJECT (stream), "byte-order");
227 * g_data_input_stream_get_byte_order:
228 * @stream: a given #GDataInputStream.
230 * Gets the byte order for the data input stream.
232 * Returns: the @stream's current #GDataStreamByteOrder.
235 g_data_input_stream_get_byte_order (GDataInputStream *stream)
237 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
239 return stream->priv->byte_order;
243 * g_data_input_stream_set_newline_type:
244 * @stream: a #GDataInputStream.
245 * @type: the type of new line return as #GDataStreamNewlineType.
247 * Sets the newline type for the @stream.
249 * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
250 * should it always be set to {_LF, _CR, _CR_LF}
254 g_data_input_stream_set_newline_type (GDataInputStream *stream,
255 GDataStreamNewlineType type)
257 GDataInputStreamPrivate *priv;
259 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
263 if (priv->newline_type != type)
265 priv->newline_type = type;
267 g_object_notify (G_OBJECT (stream), "newline-type");
272 * g_data_input_stream_get_newline_type:
273 * @stream: a given #GDataInputStream.
275 * Gets the current newline type for the @stream.
277 * Returns: #GDataStreamNewlineType for the given @stream.
279 GDataStreamNewlineType
280 g_data_input_stream_get_newline_type (GDataInputStream *stream)
282 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
284 return stream->priv->newline_type;
288 read_data (GDataInputStream *stream,
291 GCancellable *cancellable,
297 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
299 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
306 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
307 _("Unexpected early end-of-stream"));
312 /* This should always succeed, since its in the buffer */
313 res = g_input_stream_read (G_INPUT_STREAM (stream),
316 g_assert (res == size);
322 * g_data_input_stream_read_byte:
323 * @stream: a given #GDataInputStream.
324 * @cancellable: optional #GCancellable object, %NULL to ignore.
325 * @error: #GError for error reporting.
327 * Reads an unsigned 8-bit/1-byte value from @stream.
329 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
330 * if an error occured.
333 g_data_input_stream_read_byte (GDataInputStream *stream,
334 GCancellable *cancellable,
339 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
341 if (read_data (stream, &c, 1, cancellable, error))
349 * g_data_input_stream_read_int16:
350 * @stream: a given #GDataInputStream.
351 * @cancellable: optional #GCancellable object, %NULL to ignore.
352 * @error: #GError for error reporting.
354 * Reads a 16-bit/2-byte value from @stream.
356 * In order to get the correct byte order for this read operation,
357 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
359 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
363 g_data_input_stream_read_int16 (GDataInputStream *stream,
364 GCancellable *cancellable,
369 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
371 if (read_data (stream, &v, 2, cancellable, error))
373 switch (stream->priv->byte_order)
375 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
376 v = GINT16_FROM_BE (v);
378 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
379 v = GINT16_FROM_LE (v);
381 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
393 * g_data_input_stream_read_uint16:
394 * @stream: a given #GDataInputStream.
395 * @cancellable: optional #GCancellable object, %NULL to ignore.
396 * @error: #GError for error reporting.
398 * Reads an unsigned 16-bit/2-byte value from @stream.
400 * In order to get the correct byte order for this read operation,
401 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
403 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
407 g_data_input_stream_read_uint16 (GDataInputStream *stream,
408 GCancellable *cancellable,
413 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
415 if (read_data (stream, &v, 2, cancellable, error))
417 switch (stream->priv->byte_order)
419 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
420 v = GUINT16_FROM_BE (v);
422 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
423 v = GUINT16_FROM_LE (v);
425 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
437 * g_data_input_stream_read_int32:
438 * @stream: a given #GDataInputStream.
439 * @cancellable: optional #GCancellable object, %NULL to ignore.
440 * @error: #GError for error reporting.
442 * Reads a signed 32-bit/4-byte value from @stream.
444 * In order to get the correct byte order for this read operation,
445 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
447 * If @cancellable is not %NULL, then the operation can be cancelled by
448 * triggering the cancellable object from another thread. If the operation
449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
451 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
455 g_data_input_stream_read_int32 (GDataInputStream *stream,
456 GCancellable *cancellable,
461 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
463 if (read_data (stream, &v, 4, cancellable, error))
465 switch (stream->priv->byte_order)
467 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
468 v = GINT32_FROM_BE (v);
470 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
471 v = GINT32_FROM_LE (v);
473 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
485 * g_data_input_stream_read_uint32:
486 * @stream: a given #GDataInputStream.
487 * @cancellable: optional #GCancellable object, %NULL to ignore.
488 * @error: #GError for error reporting.
490 * Reads an unsigned 32-bit/4-byte value from @stream.
492 * In order to get the correct byte order for this read operation,
493 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
495 * If @cancellable is not %NULL, then the operation can be cancelled by
496 * triggering the cancellable object from another thread. If the operation
497 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
499 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
503 g_data_input_stream_read_uint32 (GDataInputStream *stream,
504 GCancellable *cancellable,
509 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
511 if (read_data (stream, &v, 4, cancellable, error))
513 switch (stream->priv->byte_order)
515 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
516 v = GUINT32_FROM_BE (v);
518 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
519 v = GUINT32_FROM_LE (v);
521 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
533 * g_data_input_stream_read_int64:
534 * @stream: a given #GDataInputStream.
535 * @cancellable: optional #GCancellable object, %NULL to ignore.
536 * @error: #GError for error reporting.
538 * Reads a 64-bit/8-byte value from @stream.
540 * In order to get the correct byte order for this read operation,
541 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
543 * If @cancellable is not %NULL, then the operation can be cancelled by
544 * triggering the cancellable object from another thread. If the operation
545 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
547 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
551 g_data_input_stream_read_int64 (GDataInputStream *stream,
552 GCancellable *cancellable,
557 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
559 if (read_data (stream, &v, 8, cancellable, error))
561 switch (stream->priv->byte_order)
563 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
564 v = GINT64_FROM_BE (v);
566 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
567 v = GINT64_FROM_LE (v);
569 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
581 * g_data_input_stream_read_uint64:
582 * @stream: a given #GDataInputStream.
583 * @cancellable: optional #GCancellable object, %NULL to ignore.
584 * @error: #GError for error reporting.
586 * Reads an unsigned 64-bit/8-byte value from @stream.
588 * In order to get the correct byte order for this read operation,
589 * see g_data_stream_get_byte_order().
591 * If @cancellable is not %NULL, then the operation can be cancelled by
592 * triggering the cancellable object from another thread. If the operation
593 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
595 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
599 g_data_input_stream_read_uint64 (GDataInputStream *stream,
600 GCancellable *cancellable,
605 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
607 if (read_data (stream, &v, 8, cancellable, error))
609 switch (stream->priv->byte_order)
611 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
612 v = GUINT64_FROM_BE (v);
614 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
615 v = GUINT64_FROM_LE (v);
617 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
628 scan_for_newline (GDataInputStream *stream,
630 gboolean *last_saw_cr_out,
631 int *newline_len_out)
633 GBufferedInputStream *bstream;
634 GDataInputStreamPrivate *priv;
636 gsize start, end, peeked;
640 gsize available, checked;
641 gboolean last_saw_cr;
645 bstream = G_BUFFERED_INPUT_STREAM (stream);
647 checked = *checked_out;
648 last_saw_cr = *last_saw_cr_out;
653 buffer = (guint8*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
655 peeked = end - start;
657 for (i = 0; checked < available && i < peeked; i++)
659 switch (priv->newline_type)
661 case G_DATA_STREAM_NEWLINE_TYPE_LF:
664 found_pos = start + i;
668 case G_DATA_STREAM_NEWLINE_TYPE_CR:
671 found_pos = start + i;
675 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
676 if (last_saw_cr && buffer[i] == 10)
678 found_pos = start + i - 1;
683 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
684 if (buffer[i] == 10) /* LF */
689 found_pos = start + i - 1;
695 found_pos = start + i;
699 else if (last_saw_cr)
701 /* Last was cr, this is not LF, end is CR */
702 found_pos = start + i - 1;
705 /* Don't check for CR here, instead look at last_saw_cr on next byte */
709 last_saw_cr = (buffer[i] == 13);
713 *newline_len_out = newline_len;
720 *checked_out = checked;
721 *last_saw_cr_out = last_saw_cr;
727 * g_data_input_stream_read_line:
728 * @stream: a given #GDataInputStream.
729 * @length: a #gsize to get the length of the data read in.
730 * @cancellable: optional #GCancellable object, %NULL to ignore.
731 * @error: #GError for error reporting.
733 * Reads a line from the data input stream.
735 * If @cancellable is not %NULL, then the operation can be cancelled by
736 * triggering the cancellable object from another thread. If the operation
737 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
739 * Returns: a string with the line that was read in. Set @length to
740 * a #gsize to get the length of the read line. Returns %NULL on an error.
743 g_data_input_stream_read_line (GDataInputStream *stream,
745 GCancellable *cancellable,
748 GBufferedInputStream *bstream;
750 gboolean last_saw_cr;
756 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
758 bstream = G_BUFFERED_INPUT_STREAM (stream);
764 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
766 if (g_buffered_input_stream_get_available (bstream) ==
767 g_buffered_input_stream_get_buffer_size (bstream))
768 g_buffered_input_stream_set_buffer_size (bstream,
769 2 * g_buffered_input_stream_get_buffer_size (bstream));
771 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
777 if (g_buffered_input_stream_get_available (bstream) == 0)
792 line = g_malloc (found_pos + newline_len + 1);
794 res = g_input_stream_read (G_INPUT_STREAM (stream),
796 found_pos + newline_len,
799 *length = (gsize)found_pos;
800 g_assert (res == found_pos + newline_len);
808 scan_for_chars (GDataInputStream *stream,
810 const char *stop_chars)
812 GBufferedInputStream *bstream;
813 GDataInputStreamPrivate *priv;
815 gsize start, end, peeked;
818 gsize available, checked;
819 const char *stop_char;
823 bstream = G_BUFFERED_INPUT_STREAM (stream);
825 checked = *checked_out;
829 buffer = (guint8*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
831 peeked = end - start;
833 for (i = 0; checked < available && i < peeked; i++)
835 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
837 if (buffer[i] == *stop_char)
844 *checked_out = checked;
849 * g_data_input_stream_read_until:
850 * @stream: a given #GDataInputStream.
851 * @stop_chars: characters to terminate the read.
852 * @length: a #gsize to get the length of the data read in.
853 * @cancellable: optional #GCancellable object, %NULL to ignore.
854 * @error: #GError for error reporting.
856 * Reads a string from the data input stream, up to the first
857 * occurrance of any of the stop characters.
859 * Returns: a string with the data that was read before encountering
860 * any of the stop characters. Set @length to a #gsize to get the length
861 * of the string. This function will return %NULL on an error.
864 g_data_input_stream_read_until (GDataInputStream *stream,
865 const gchar *stop_chars,
867 GCancellable *cancellable,
870 GBufferedInputStream *bstream;
877 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
879 bstream = G_BUFFERED_INPUT_STREAM (stream);
884 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
886 if (g_buffered_input_stream_get_available (bstream) ==
887 g_buffered_input_stream_get_buffer_size (bstream))
888 g_buffered_input_stream_set_buffer_size (bstream,
889 2 * g_buffered_input_stream_get_buffer_size (bstream));
891 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
897 if (g_buffered_input_stream_get_available (bstream) == 0)
912 data_until = g_malloc (found_pos + stop_char_len + 1);
914 res = g_input_stream_read (G_INPUT_STREAM (stream),
916 found_pos + stop_char_len,
919 *length = (gsize)found_pos;
920 g_assert (res == found_pos + stop_char_len);
921 data_until[found_pos] = 0;
926 #define __G_DATA_INPUT_STREAM_C__
927 #include "gioaliasdef.c"