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 structured data directly from a binary 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 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
250 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
251 * "CR LF", and this might block if there is no more data availible.
255 g_data_input_stream_set_newline_type (GDataInputStream *stream,
256 GDataStreamNewlineType type)
258 GDataInputStreamPrivate *priv;
260 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
264 if (priv->newline_type != type)
266 priv->newline_type = type;
268 g_object_notify (G_OBJECT (stream), "newline-type");
273 * g_data_input_stream_get_newline_type:
274 * @stream: a given #GDataInputStream.
276 * Gets the current newline type for the @stream.
278 * Returns: #GDataStreamNewlineType for the given @stream.
280 GDataStreamNewlineType
281 g_data_input_stream_get_newline_type (GDataInputStream *stream)
283 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
285 return stream->priv->newline_type;
289 read_data (GDataInputStream *stream,
292 GCancellable *cancellable,
298 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
300 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
307 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
308 _("Unexpected early end-of-stream"));
313 /* This should always succeed, since its in the buffer */
314 res = g_input_stream_read (G_INPUT_STREAM (stream),
317 g_warn_if_fail (res == size);
323 * g_data_input_stream_read_byte:
324 * @stream: a given #GDataInputStream.
325 * @cancellable: optional #GCancellable object, %NULL to ignore.
326 * @error: #GError for error reporting.
328 * Reads an unsigned 8-bit/1-byte value from @stream.
330 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
331 * if an error occured.
334 g_data_input_stream_read_byte (GDataInputStream *stream,
335 GCancellable *cancellable,
340 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
342 if (read_data (stream, &c, 1, cancellable, error))
350 * g_data_input_stream_read_int16:
351 * @stream: a given #GDataInputStream.
352 * @cancellable: optional #GCancellable object, %NULL to ignore.
353 * @error: #GError for error reporting.
355 * Reads a 16-bit/2-byte value from @stream.
357 * In order to get the correct byte order for this read operation,
358 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
360 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
364 g_data_input_stream_read_int16 (GDataInputStream *stream,
365 GCancellable *cancellable,
370 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
372 if (read_data (stream, &v, 2, cancellable, error))
374 switch (stream->priv->byte_order)
376 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
377 v = GINT16_FROM_BE (v);
379 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
380 v = GINT16_FROM_LE (v);
382 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
394 * g_data_input_stream_read_uint16:
395 * @stream: a given #GDataInputStream.
396 * @cancellable: optional #GCancellable object, %NULL to ignore.
397 * @error: #GError for error reporting.
399 * Reads an unsigned 16-bit/2-byte value from @stream.
401 * In order to get the correct byte order for this read operation,
402 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
404 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
408 g_data_input_stream_read_uint16 (GDataInputStream *stream,
409 GCancellable *cancellable,
414 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
416 if (read_data (stream, &v, 2, cancellable, error))
418 switch (stream->priv->byte_order)
420 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
421 v = GUINT16_FROM_BE (v);
423 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
424 v = GUINT16_FROM_LE (v);
426 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
438 * g_data_input_stream_read_int32:
439 * @stream: a given #GDataInputStream.
440 * @cancellable: optional #GCancellable object, %NULL to ignore.
441 * @error: #GError for error reporting.
443 * Reads a signed 32-bit/4-byte value from @stream.
445 * In order to get the correct byte order for this read operation,
446 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
448 * If @cancellable is not %NULL, then the operation can be cancelled by
449 * triggering the cancellable object from another thread. If the operation
450 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
452 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
456 g_data_input_stream_read_int32 (GDataInputStream *stream,
457 GCancellable *cancellable,
462 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
464 if (read_data (stream, &v, 4, cancellable, error))
466 switch (stream->priv->byte_order)
468 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
469 v = GINT32_FROM_BE (v);
471 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
472 v = GINT32_FROM_LE (v);
474 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
486 * g_data_input_stream_read_uint32:
487 * @stream: a given #GDataInputStream.
488 * @cancellable: optional #GCancellable object, %NULL to ignore.
489 * @error: #GError for error reporting.
491 * Reads an unsigned 32-bit/4-byte value from @stream.
493 * In order to get the correct byte order for this read operation,
494 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
496 * If @cancellable is not %NULL, then the operation can be cancelled by
497 * triggering the cancellable object from another thread. If the operation
498 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
500 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
504 g_data_input_stream_read_uint32 (GDataInputStream *stream,
505 GCancellable *cancellable,
510 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
512 if (read_data (stream, &v, 4, cancellable, error))
514 switch (stream->priv->byte_order)
516 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
517 v = GUINT32_FROM_BE (v);
519 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
520 v = GUINT32_FROM_LE (v);
522 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
534 * g_data_input_stream_read_int64:
535 * @stream: a given #GDataInputStream.
536 * @cancellable: optional #GCancellable object, %NULL to ignore.
537 * @error: #GError for error reporting.
539 * Reads a 64-bit/8-byte value from @stream.
541 * In order to get the correct byte order for this read operation,
542 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
544 * If @cancellable is not %NULL, then the operation can be cancelled by
545 * triggering the cancellable object from another thread. If the operation
546 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
548 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
552 g_data_input_stream_read_int64 (GDataInputStream *stream,
553 GCancellable *cancellable,
558 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
560 if (read_data (stream, &v, 8, cancellable, error))
562 switch (stream->priv->byte_order)
564 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
565 v = GINT64_FROM_BE (v);
567 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
568 v = GINT64_FROM_LE (v);
570 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
582 * g_data_input_stream_read_uint64:
583 * @stream: a given #GDataInputStream.
584 * @cancellable: optional #GCancellable object, %NULL to ignore.
585 * @error: #GError for error reporting.
587 * Reads an unsigned 64-bit/8-byte value from @stream.
589 * In order to get the correct byte order for this read operation,
590 * see g_data_stream_get_byte_order().
592 * If @cancellable is not %NULL, then the operation can be cancelled by
593 * triggering the cancellable object from another thread. If the operation
594 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
596 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
600 g_data_input_stream_read_uint64 (GDataInputStream *stream,
601 GCancellable *cancellable,
606 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
608 if (read_data (stream, &v, 8, cancellable, error))
610 switch (stream->priv->byte_order)
612 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
613 v = GUINT64_FROM_BE (v);
615 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
616 v = GUINT64_FROM_LE (v);
618 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
629 scan_for_newline (GDataInputStream *stream,
631 gboolean *last_saw_cr_out,
632 int *newline_len_out)
634 GBufferedInputStream *bstream;
635 GDataInputStreamPrivate *priv;
637 gsize start, end, peeked;
641 gsize available, checked;
642 gboolean last_saw_cr;
646 bstream = G_BUFFERED_INPUT_STREAM (stream);
648 checked = *checked_out;
649 last_saw_cr = *last_saw_cr_out;
654 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
656 peeked = end - start;
658 for (i = 0; checked < available && i < peeked; i++)
660 switch (priv->newline_type)
662 case G_DATA_STREAM_NEWLINE_TYPE_LF:
665 found_pos = start + i;
669 case G_DATA_STREAM_NEWLINE_TYPE_CR:
672 found_pos = start + i;
676 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
677 if (last_saw_cr && buffer[i] == 10)
679 found_pos = start + i - 1;
684 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
685 if (buffer[i] == 10) /* LF */
690 found_pos = start + i - 1;
696 found_pos = start + i;
700 else if (last_saw_cr)
702 /* Last was cr, this is not LF, end is CR */
703 found_pos = start + i - 1;
706 /* Don't check for CR here, instead look at last_saw_cr on next byte */
710 last_saw_cr = (buffer[i] == 13);
714 *newline_len_out = newline_len;
721 *checked_out = checked;
722 *last_saw_cr_out = last_saw_cr;
728 * g_data_input_stream_read_line:
729 * @stream: a given #GDataInputStream.
730 * @length: a #gsize to get the length of the data read in.
731 * @cancellable: optional #GCancellable object, %NULL to ignore.
732 * @error: #GError for error reporting.
734 * Reads a line from the data input stream.
736 * If @cancellable is not %NULL, then the operation can be cancelled by
737 * triggering the cancellable object from another thread. If the operation
738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
740 * Returns: a string with the line that was read in (including the newlines).
741 * Set @length to a #gsize to get the length of the read line. Returns %NULL on an error.
744 g_data_input_stream_read_line (GDataInputStream *stream,
746 GCancellable *cancellable,
749 GBufferedInputStream *bstream;
751 gboolean last_saw_cr;
757 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
759 bstream = G_BUFFERED_INPUT_STREAM (stream);
765 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
767 if (g_buffered_input_stream_get_available (bstream) ==
768 g_buffered_input_stream_get_buffer_size (bstream))
769 g_buffered_input_stream_set_buffer_size (bstream,
770 2 * g_buffered_input_stream_get_buffer_size (bstream));
772 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
778 if (g_buffered_input_stream_get_available (bstream) == 0)
793 line = g_malloc (found_pos + newline_len + 1);
795 res = g_input_stream_read (G_INPUT_STREAM (stream),
797 found_pos + newline_len,
800 *length = (gsize)found_pos;
801 g_warn_if_fail (res == found_pos + newline_len);
809 scan_for_chars (GDataInputStream *stream,
811 const char *stop_chars)
813 GBufferedInputStream *bstream;
814 GDataInputStreamPrivate *priv;
816 gsize start, end, peeked;
819 gsize available, checked;
820 const char *stop_char;
824 bstream = G_BUFFERED_INPUT_STREAM (stream);
826 checked = *checked_out;
830 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
832 peeked = end - start;
834 for (i = 0; checked < available && i < peeked; i++)
836 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
838 if (buffer[i] == *stop_char)
845 *checked_out = checked;
850 * g_data_input_stream_read_until:
851 * @stream: a given #GDataInputStream.
852 * @stop_chars: characters to terminate the read.
853 * @length: a #gsize to get the length of the data read in.
854 * @cancellable: optional #GCancellable object, %NULL to ignore.
855 * @error: #GError for error reporting.
857 * Reads a string from the data input stream, up to the first
858 * occurrance of any of the stop characters.
860 * Returns: a string with the data that was read before encountering
861 * any of the stop characters. Set @length to a #gsize to get the length
862 * of the string. This function will return %NULL on an error.
865 g_data_input_stream_read_until (GDataInputStream *stream,
866 const gchar *stop_chars,
868 GCancellable *cancellable,
871 GBufferedInputStream *bstream;
878 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
880 bstream = G_BUFFERED_INPUT_STREAM (stream);
885 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
887 if (g_buffered_input_stream_get_available (bstream) ==
888 g_buffered_input_stream_get_buffer_size (bstream))
889 g_buffered_input_stream_set_buffer_size (bstream,
890 2 * g_buffered_input_stream_get_buffer_size (bstream));
892 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
898 if (g_buffered_input_stream_get_available (bstream) == 0)
913 data_until = g_malloc (found_pos + stop_char_len + 1);
915 res = g_input_stream_read (G_INPUT_STREAM (stream),
917 found_pos + stop_char_len,
920 *length = (gsize)found_pos;
921 g_warn_if_fail (res == found_pos + stop_char_len);
922 data_until[found_pos] = 0;
927 #define __G_DATA_INPUT_STREAM_C__
928 #include "gioaliasdef.c"