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"
31 * SECTION:gdatainputstream
32 * @short_description: Data Input Stream
33 * @see_also: #GInputStream
35 * Data input stream implements #GInputStream and includes functions for
36 * reading data directly from an input stream.
40 struct _GDataInputStreamPrivate {
41 GDataStreamByteOrder byte_order;
42 GDataStreamNewlineType newline_type;
49 static void g_data_input_stream_set_property (GObject *object,
53 static void g_data_input_stream_get_property (GObject *object,
58 G_DEFINE_TYPE (GDataInputStream,
60 G_TYPE_BUFFERED_INPUT_STREAM)
64 g_data_input_stream_class_init (GDataInputStreamClass *klass)
66 GObjectClass *object_class;
68 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
70 object_class = G_OBJECT_CLASS (klass);
71 object_class->get_property = g_data_input_stream_get_property;
72 object_class->set_property = g_data_input_stream_set_property;
76 g_data_input_stream_set_property (GObject *object,
81 GDataInputStreamPrivate *priv;
82 GDataInputStream *dstream;
84 dstream = G_DATA_INPUT_STREAM (object);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 g_data_input_stream_get_property (GObject *object,
103 GDataInputStreamPrivate *priv;
104 GDataInputStream *dstream;
106 dstream = G_DATA_INPUT_STREAM (object);
107 priv = dstream->priv;
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 g_data_input_stream_init (GDataInputStream *stream)
121 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
122 G_TYPE_DATA_INPUT_STREAM,
123 GDataInputStreamPrivate);
125 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
126 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
130 * g_data_input_stream_new:
131 * @base_stream: a #GInputStream.
133 * Creates a new data input stream for the @base_stream.
135 * Returns: a new #GDataInputStream.
138 g_data_input_stream_new (GInputStream *base_stream)
140 GDataInputStream *stream;
142 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
144 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
145 "base-stream", base_stream,
152 * g_data_input_stream_set_byte_order:
153 * @stream: a given #GDataInputStream.
154 * @order: a #GDataStreamByteOrder to set.
156 * This function sets the byte order for the given @stream. All subsequent
157 * reads from the @stream will be read in the given @order.
161 g_data_input_stream_set_byte_order (GDataInputStream *stream,
162 GDataStreamByteOrder order)
164 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
166 stream->priv->byte_order = order;
170 * g_data_input_stream_get_byte_order:
171 * @stream: a given #GDataInputStream.
173 * Gets the byte order for the data input stream.
175 * Returns: the @stream's current #GDataStreamByteOrder.
178 g_data_input_stream_get_byte_order (GDataInputStream *stream)
180 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
182 return stream->priv->byte_order;
186 * g_data_input_stream_set_newline_type:
187 * @stream: a #GDataInputStream.
188 * @type: the type of new line return as #GDataStreamNewlineType.
190 * Sets the newline type for the @stream.
192 * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
193 * should it always be set to {_LF, _CR, _CR_LF}
197 g_data_input_stream_set_newline_type (GDataInputStream *stream,
198 GDataStreamNewlineType type)
200 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
202 stream->priv->newline_type = type;
206 * g_data_input_stream_get_newline_type:
207 * @stream: a given #GDataInputStream.
209 * Gets the current newline type for the @stream.
211 * Returns: #GDataStreamNewlineType for the given @stream.
213 GDataStreamNewlineType
214 g_data_input_stream_get_newline_type (GDataInputStream *stream)
216 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
218 return stream->priv->newline_type;
222 read_data (GDataInputStream *stream,
225 GCancellable *cancellable,
231 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
233 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
240 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
241 _("Unexpected early end-of-stream"));
246 /* This should always succeed, since its in the buffer */
247 res = g_input_stream_read (G_INPUT_STREAM (stream),
250 g_assert (res == size);
256 * g_data_input_stream_read_byte:
257 * @stream: a given #GDataInputStream.
258 * @cancellable: optional #GCancellable object, %NULL to ignore.
259 * @error: #GError for error reporting.
261 * Reads an unsigned 8-bit/1-byte value from @stream.
263 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
264 * if an error occured.
267 g_data_input_stream_read_byte (GDataInputStream *stream,
268 GCancellable *cancellable,
273 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
275 if (read_data (stream, &c, 1, cancellable, error))
283 * g_data_input_stream_read_int16:
284 * @stream: a given #GDataInputStream.
285 * @cancellable: optional #GCancellable object, %NULL to ignore.
286 * @error: #GError for error reporting.
288 * Reads a 16-bit/2-byte value from @stream.
290 * In order to get the correct byte order for this read operation,
291 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
293 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
297 g_data_input_stream_read_int16 (GDataInputStream *stream,
298 GCancellable *cancellable,
303 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
305 if (read_data (stream, &v, 2, cancellable, error))
307 switch (stream->priv->byte_order)
309 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
310 v = GINT16_FROM_BE (v);
312 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
313 v = GINT16_FROM_LE (v);
315 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
327 * g_data_input_stream_read_uint16:
328 * @stream: a given #GDataInputStream.
329 * @cancellable: optional #GCancellable object, %NULL to ignore.
330 * @error: #GError for error reporting.
332 * Reads an unsigned 16-bit/2-byte value from @stream.
334 * In order to get the correct byte order for this read operation,
335 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
337 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
341 g_data_input_stream_read_uint16 (GDataInputStream *stream,
342 GCancellable *cancellable,
347 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
349 if (read_data (stream, &v, 2, cancellable, error))
351 switch (stream->priv->byte_order)
353 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
354 v = GUINT16_FROM_BE (v);
356 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
357 v = GUINT16_FROM_LE (v);
359 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
371 * g_data_input_stream_read_int32:
372 * @stream: a given #GDataInputStream.
373 * @cancellable: optional #GCancellable object, %NULL to ignore.
374 * @error: #GError for error reporting.
376 * Reads a signed 32-bit/4-byte value from @stream.
378 * In order to get the correct byte order for this read operation,
379 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
381 * If @cancellable is not %NULL, then the operation can be cancelled by
382 * triggering the cancellable object from another thread. If the operation
383 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
385 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
389 g_data_input_stream_read_int32 (GDataInputStream *stream,
390 GCancellable *cancellable,
395 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
397 if (read_data (stream, &v, 4, cancellable, error))
399 switch (stream->priv->byte_order)
401 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
402 v = GINT32_FROM_BE (v);
404 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
405 v = GINT32_FROM_LE (v);
407 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
419 * g_data_input_stream_read_uint32:
420 * @stream: a given #GDataInputStream.
421 * @cancellable: optional #GCancellable object, %NULL to ignore.
422 * @error: #GError for error reporting.
424 * Reads an unsigned 32-bit/4-byte value from @stream.
426 * In order to get the correct byte order for this read operation,
427 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
429 * If @cancellable is not %NULL, then the operation can be cancelled by
430 * triggering the cancellable object from another thread. If the operation
431 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
433 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
437 g_data_input_stream_read_uint32 (GDataInputStream *stream,
438 GCancellable *cancellable,
443 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
445 if (read_data (stream, &v, 4, cancellable, error))
447 switch (stream->priv->byte_order)
449 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
450 v = GUINT32_FROM_BE (v);
452 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
453 v = GUINT32_FROM_LE (v);
455 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
467 * g_data_input_stream_read_int64:
468 * @stream: a given #GDataInputStream.
469 * @cancellable: optional #GCancellable object, %NULL to ignore.
470 * @error: #GError for error reporting.
472 * Reads a 64-bit/8-byte value from @stream.
474 * In order to get the correct byte order for this read operation,
475 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
477 * If @cancellable is not %NULL, then the operation can be cancelled by
478 * triggering the cancellable object from another thread. If the operation
479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
481 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
485 g_data_input_stream_read_int64 (GDataInputStream *stream,
486 GCancellable *cancellable,
491 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
493 if (read_data (stream, &v, 8, cancellable, error))
495 switch (stream->priv->byte_order)
497 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
498 v = GINT64_FROM_BE (v);
500 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
501 v = GINT64_FROM_LE (v);
503 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
515 * g_data_input_stream_read_uint64:
516 * @stream: a given #GDataInputStream.
517 * @cancellable: optional #GCancellable object, %NULL to ignore.
518 * @error: #GError for error reporting.
520 * Reads an unsigned 64-bit/8-byte value from @stream.
522 * In order to get the correct byte order for this read operation,
523 * see g_data_stream_get_byte_order().
525 * If @cancellable is not %NULL, then the operation can be cancelled by
526 * triggering the cancellable object from another thread. If the operation
527 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
529 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
533 g_data_input_stream_read_uint64 (GDataInputStream *stream,
534 GCancellable *cancellable,
539 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
541 if (read_data (stream, &v, 8, cancellable, error))
543 switch (stream->priv->byte_order)
545 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
546 v = GUINT64_FROM_BE (v);
548 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
549 v = GUINT64_FROM_LE (v);
551 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
562 scan_for_newline (GDataInputStream *stream,
564 gboolean *last_saw_cr_out,
565 int *newline_len_out)
567 GBufferedInputStream *bstream;
568 GDataInputStreamPrivate *priv;
570 gsize start, end, peeked;
574 gsize available, checked;
575 gboolean last_saw_cr;
579 bstream = G_BUFFERED_INPUT_STREAM (stream);
581 checked = *checked_out;
582 last_saw_cr = *last_saw_cr_out;
587 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
589 peeked = end - start;
591 for (i = 0; checked < available && i < peeked; i++)
593 switch (priv->newline_type)
595 case G_DATA_STREAM_NEWLINE_TYPE_LF:
598 found_pos = start + i;
602 case G_DATA_STREAM_NEWLINE_TYPE_CR:
605 found_pos = start + i;
609 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
610 if (last_saw_cr && buffer[i] == 10)
612 found_pos = start + i - 1;
617 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
618 if (buffer[i] == 10) /* LF */
623 found_pos = start + i - 1;
629 found_pos = start + i;
633 else if (last_saw_cr)
635 /* Last was cr, this is not LF, end is CR */
636 found_pos = start + i - 1;
639 /* Don't check for CR here, instead look at last_saw_cr on next byte */
643 last_saw_cr = (buffer[i] == 13);
647 *newline_len_out = newline_len;
654 *checked_out = checked;
655 *last_saw_cr_out = last_saw_cr;
661 * g_data_input_stream_read_line:
662 * @stream: a given #GDataInputStream.
663 * @length: a #gsize to get the length of the data read in.
664 * @cancellable: optional #GCancellable object, %NULL to ignore.
665 * @error: #GError for error reporting.
667 * Reads a line from the data input stream.
669 * If @cancellable is not %NULL, then the operation can be cancelled by
670 * triggering the cancellable object from another thread. If the operation
671 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
673 * Returns: a string with the line that was read in. Set @length to
674 * a #gsize to get the length of the read line. Returns %NULL on an error.
677 g_data_input_stream_read_line (GDataInputStream *stream,
679 GCancellable *cancellable,
682 GBufferedInputStream *bstream;
684 gboolean last_saw_cr;
690 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
692 bstream = G_BUFFERED_INPUT_STREAM (stream);
698 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
700 if (g_buffered_input_stream_get_available (bstream) ==
701 g_buffered_input_stream_get_buffer_size (bstream))
702 g_buffered_input_stream_set_buffer_size (bstream,
703 2 * g_buffered_input_stream_get_buffer_size (bstream));
705 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
711 if (g_buffered_input_stream_get_available (bstream) == 0)
726 line = g_malloc (found_pos + newline_len + 1);
728 res = g_input_stream_read (G_INPUT_STREAM (stream),
730 found_pos + newline_len,
733 *length = (gsize)found_pos;
734 g_assert (res == found_pos + newline_len);
742 scan_for_chars (GDataInputStream *stream,
744 const char *stop_chars)
746 GBufferedInputStream *bstream;
747 GDataInputStreamPrivate *priv;
749 gsize start, end, peeked;
752 gsize available, checked;
753 const char *stop_char;
757 bstream = G_BUFFERED_INPUT_STREAM (stream);
759 checked = *checked_out;
763 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
765 peeked = end - start;
767 for (i = 0; checked < available && i < peeked; i++)
769 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
771 if (buffer[i] == *stop_char)
778 *checked_out = checked;
783 * g_data_input_stream_read_until:
784 * @stream: a given #GDataInputStream.
785 * @stop_chars: characters to terminate the read.
786 * @length: a #gsize to get the length of the data read in.
787 * @cancellable: optional #GCancellable object, %NULL to ignore.
788 * @error: #GError for error reporting.
790 * Reads a string from the data input stream, up to the first
791 * occurrance of any of the stop characters.
793 * Returns: a string with the data that was read before encountering
794 * any of the stop characters. Set @length to a #gsize to get the length
795 * of the string. This function will return %NULL on an error.
798 g_data_input_stream_read_until (GDataInputStream *stream,
799 const gchar *stop_chars,
801 GCancellable *cancellable,
804 GBufferedInputStream *bstream;
811 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
813 bstream = G_BUFFERED_INPUT_STREAM (stream);
818 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
820 if (g_buffered_input_stream_get_available (bstream) ==
821 g_buffered_input_stream_get_buffer_size (bstream))
822 g_buffered_input_stream_set_buffer_size (bstream,
823 2 * g_buffered_input_stream_get_buffer_size (bstream));
825 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
831 if (g_buffered_input_stream_get_available (bstream) == 0)
846 data_until = g_malloc (found_pos + stop_char_len + 1);
848 res = g_input_stream_read (G_INPUT_STREAM (stream),
850 found_pos + stop_char_len,
853 *length = (gsize)found_pos;
854 g_assert (res == found_pos + stop_char_len);
855 data_until[found_pos] = 0;
860 #define __G_DATA_INPUT_STREAM_C__
861 #include "gioaliasdef.c"