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"
33 * SECTION:gdatainputstream
34 * @short_description: Data Input Stream
36 * @see_also: #GInputStream
38 * Data input stream implements #GInputStream and includes functions for
39 * reading structured data directly from a binary input stream.
43 struct _GDataInputStreamPrivate
{
44 GDataStreamByteOrder byte_order
;
45 GDataStreamNewlineType newline_type
;
54 static void g_data_input_stream_set_property (GObject
*object
,
58 static void g_data_input_stream_get_property (GObject
*object
,
63 G_DEFINE_TYPE (GDataInputStream
,
65 G_TYPE_BUFFERED_INPUT_STREAM
)
69 g_data_input_stream_class_init (GDataInputStreamClass
*klass
)
71 GObjectClass
*object_class
;
73 g_type_class_add_private (klass
, sizeof (GDataInputStreamPrivate
));
75 object_class
= G_OBJECT_CLASS (klass
);
76 object_class
->get_property
= g_data_input_stream_get_property
;
77 object_class
->set_property
= g_data_input_stream_set_property
;
80 * GDataStream:byte-order:
82 * The ::byte-order property determines the byte ordering that
83 * is used when reading multi-byte entities (such as integers)
86 g_object_class_install_property (object_class
,
88 g_param_spec_enum ("byte-order",
91 G_TYPE_DATA_STREAM_BYTE_ORDER
,
92 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
,
93 G_PARAM_READWRITE
|G_PARAM_STATIC_NAME
|G_PARAM_STATIC_BLURB
));
96 * GDataStream:newline-type:
98 * The :newline-type property determines what is considered
99 * as a line ending when reading complete lines from the stream.
101 g_object_class_install_property (object_class
,
103 g_param_spec_enum ("newline-type",
105 P_("The accepted types of line ending"),
106 G_TYPE_DATA_STREAM_NEWLINE_TYPE
,
107 G_DATA_STREAM_NEWLINE_TYPE_LF
,
108 G_PARAM_READWRITE
|G_PARAM_STATIC_NAME
|G_PARAM_STATIC_BLURB
));
112 g_data_input_stream_set_property (GObject
*object
,
117 GDataInputStreamPrivate
*priv
;
118 GDataInputStream
*dstream
;
120 dstream
= G_DATA_INPUT_STREAM (object
);
121 priv
= dstream
->priv
;
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 its 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_stream_get_byte_order() and g_data_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_stream_get_byte_order() and g_data_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_stream_get_byte_order() and g_data_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_stream_get_byte_order() and g_data_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_stream_get_byte_order() and g_data_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_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 (including the newlines).
743 * Set @length to a #gsize to get the length of the read line. Returns %NULL on an error.
746 g_data_input_stream_read_line (GDataInputStream
*stream
,
748 GCancellable
*cancellable
,
751 GBufferedInputStream
*bstream
;
753 gboolean last_saw_cr
;
759 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream
), NULL
);
761 bstream
= G_BUFFERED_INPUT_STREAM (stream
);
767 while ((found_pos
= scan_for_newline (stream
, &checked
, &last_saw_cr
, &newline_len
)) == -1)
769 if (g_buffered_input_stream_get_available (bstream
) ==
770 g_buffered_input_stream_get_buffer_size (bstream
))
771 g_buffered_input_stream_set_buffer_size (bstream
,
772 2 * g_buffered_input_stream_get_buffer_size (bstream
));
774 res
= g_buffered_input_stream_fill (bstream
, -1, cancellable
, error
);
780 if (g_buffered_input_stream_get_available (bstream
) == 0)
795 line
= g_malloc (found_pos
+ newline_len
+ 1);
797 res
= g_input_stream_read (G_INPUT_STREAM (stream
),
799 found_pos
+ newline_len
,
802 *length
= (gsize
)found_pos
;
803 g_warn_if_fail (res
== found_pos
+ newline_len
);
811 scan_for_chars (GDataInputStream
*stream
,
813 const char *stop_chars
)
815 GBufferedInputStream
*bstream
;
816 GDataInputStreamPrivate
*priv
;
818 gsize start
, end
, peeked
;
821 gsize available
, checked
;
822 const char *stop_char
;
826 bstream
= G_BUFFERED_INPUT_STREAM (stream
);
828 checked
= *checked_out
;
832 buffer
= (const char *)g_buffered_input_stream_peek_buffer (bstream
, &available
) + start
;
834 peeked
= end
- start
;
836 for (i
= 0; checked
< available
&& i
< peeked
; i
++)
838 for (stop_char
= stop_chars
; *stop_char
!= '\0'; stop_char
++)
840 if (buffer
[i
] == *stop_char
)
847 *checked_out
= checked
;
852 * g_data_input_stream_read_until:
853 * @stream: a given #GDataInputStream.
854 * @stop_chars: characters to terminate the read.
855 * @length: a #gsize to get the length of the data read in.
856 * @cancellable: optional #GCancellable object, %NULL to ignore.
857 * @error: #GError for error reporting.
859 * Reads a string from the data input stream, up to the first
860 * occurrance of any of the stop characters.
862 * Returns: a string with the data that was read before encountering
863 * any of the stop characters. Set @length to a #gsize to get the length
864 * of the string. This function will return %NULL on an error.
867 g_data_input_stream_read_until (GDataInputStream
*stream
,
868 const gchar
*stop_chars
,
870 GCancellable
*cancellable
,
873 GBufferedInputStream
*bstream
;
880 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream
), NULL
);
882 bstream
= G_BUFFERED_INPUT_STREAM (stream
);
887 while ((found_pos
= scan_for_chars (stream
, &checked
, stop_chars
)) == -1)
889 if (g_buffered_input_stream_get_available (bstream
) ==
890 g_buffered_input_stream_get_buffer_size (bstream
))
891 g_buffered_input_stream_set_buffer_size (bstream
,
892 2 * g_buffered_input_stream_get_buffer_size (bstream
));
894 res
= g_buffered_input_stream_fill (bstream
, -1, cancellable
, error
);
900 if (g_buffered_input_stream_get_available (bstream
) == 0)
915 data_until
= g_malloc (found_pos
+ stop_char_len
+ 1);
917 res
= g_input_stream_read (G_INPUT_STREAM (stream
),
919 found_pos
+ stop_char_len
,
922 *length
= (gsize
)found_pos
;
923 g_warn_if_fail (res
== found_pos
+ stop_char_len
);
924 data_until
[found_pos
] = 0;
929 #define __G_DATA_INPUT_STREAM_C__
930 #include "gioaliasdef.c"