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"
29 * SECTION:gdatainputstream
30 * @short_description: Data Input Stream.
31 * @see_also: #GInputStream.
33 * Data input stream implements #GInputStream and includes functions for
34 * reading data directly from an input stream.
38 struct _GDataInputStreamPrivate {
39 GDataStreamByteOrder byte_order;
40 GDataStreamNewlineType newline_type;
47 static void g_data_input_stream_set_property (GObject *object,
51 static void g_data_input_stream_get_property (GObject *object,
56 G_DEFINE_TYPE (GDataInputStream,
58 G_TYPE_BUFFERED_INPUT_STREAM)
62 g_data_input_stream_class_init (GDataInputStreamClass *klass)
64 GObjectClass *object_class;
66 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
68 object_class = G_OBJECT_CLASS (klass);
69 object_class->get_property = g_data_input_stream_get_property;
70 object_class->set_property = g_data_input_stream_set_property;
74 g_data_input_stream_set_property (GObject *object,
79 GDataInputStreamPrivate *priv;
80 GDataInputStream *dstream;
82 dstream = G_DATA_INPUT_STREAM (object);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 g_data_input_stream_get_property (GObject *object,
101 GDataInputStreamPrivate *priv;
102 GDataInputStream *dstream;
104 dstream = G_DATA_INPUT_STREAM (object);
105 priv = dstream->priv;
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 g_data_input_stream_init (GDataInputStream *stream)
119 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
120 G_TYPE_DATA_INPUT_STREAM,
121 GDataInputStreamPrivate);
123 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
124 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
128 * g_data_input_stream_new:
129 * @base_stream: a #GInputStream.
131 * Creates a new data input stream for the @base_stream.
133 * Returns: a new #GDataInputStream.
136 g_data_input_stream_new (GInputStream *base_stream)
138 GDataInputStream *stream;
140 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
142 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
143 "base-stream", base_stream,
150 * g_data_input_stream_set_byte_order:
151 * @stream: a given #GDataInputStream.
152 * @order: a #GDataStreamByteOrder to set.
154 * This function sets the byte order for the given @stream. All subsequent
155 * reads from the @stream will be read in the given @order.
159 g_data_input_stream_set_byte_order (GDataInputStream *stream,
160 GDataStreamByteOrder order)
162 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
164 stream->priv->byte_order = order;
168 * g_data_input_stream_get_byte_order:
169 * @stream: a given #GDataInputStream.
171 * Gets the byte order for the data input stream.
173 * Returns: the @stream's current #GDataStreamByteOrder.
176 g_data_input_stream_get_byte_order (GDataInputStream *stream)
178 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
180 return stream->priv->byte_order;
184 * g_data_input_stream_set_newline_type:
185 * @stream: a #GDataInputStream.
186 * @type: the type of new line return as #GDataStreamNewlineType.
188 * Sets the newline type for the @stream.
190 * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
191 * should it always be set to {_LF, _CR, _CR_LF}
195 g_data_input_stream_set_newline_type (GDataInputStream *stream,
196 GDataStreamNewlineType type)
198 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
200 stream->priv->newline_type = type;
204 * g_data_input_stream_get_newline_type:
205 * @stream: a given #GDataInputStream.
207 * Gets the current newline type for the @stream.
209 * Returns: #GDataStreamNewlineType for the given @stream.
211 GDataStreamNewlineType
212 g_data_input_stream_get_newline_type (GDataInputStream *stream)
214 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
216 return stream->priv->newline_type;
220 read_data (GDataInputStream *stream,
223 GCancellable *cancellable,
229 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
231 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
238 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
239 _("Unexpected early end-of-stream"));
244 /* This should always succeed, since its in the buffer */
245 res = g_input_stream_read (G_INPUT_STREAM (stream),
248 g_assert (res == size);
254 * g_data_input_stream_read_byte:
255 * @stream: a given #GDataInputStream.
256 * @cancellable: optional #GCancellable object, %NULL to ignore.
257 * @error: #GError for error reporting.
259 * Reads an unsigned 8-bit/1-byte value from @stream.
261 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
262 * if an error occured.
265 g_data_input_stream_read_byte (GDataInputStream *stream,
266 GCancellable *cancellable,
271 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
273 if (read_data (stream, &c, 1, cancellable, error))
281 * g_data_input_stream_read_int16:
282 * @stream: a given #GDataInputStream.
283 * @cancellable: optional #GCancellable object, %NULL to ignore.
284 * @error: #GError for error reporting.
286 * Reads a 16-bit/2-byte value from @stream.
288 * In order to get the correct byte order for this read operation,
289 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
291 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
295 g_data_input_stream_read_int16 (GDataInputStream *stream,
296 GCancellable *cancellable,
301 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
303 if (read_data (stream, &v, 2, cancellable, error))
305 switch (stream->priv->byte_order)
307 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
308 v = GINT16_FROM_BE (v);
310 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
311 v = GINT16_FROM_LE (v);
313 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
325 * g_data_input_stream_read_uint16:
326 * @stream: a given #GDataInputStream.
327 * @cancellable: optional #GCancellable object, %NULL to ignore.
328 * @error: #GError for error reporting.
330 * Reads an unsigned 16-bit/2-byte value from @stream.
332 * In order to get the correct byte order for this read operation,
333 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
335 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
339 g_data_input_stream_read_uint16 (GDataInputStream *stream,
340 GCancellable *cancellable,
345 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
347 if (read_data (stream, &v, 2, cancellable, error))
349 switch (stream->priv->byte_order)
351 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
352 v = GUINT16_FROM_BE (v);
354 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
355 v = GUINT16_FROM_LE (v);
357 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
369 * g_data_input_stream_read_int32:
370 * @stream: a given #GDataInputStream.
371 * @cancellable: optional #GCancellable object, %NULL to ignore.
372 * @error: #GError for error reporting.
374 * Reads a signed 32-bit/4-byte value from @stream.
376 * In order to get the correct byte order for this read operation,
377 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
379 * If @cancellable is not %NULL, then the operation can be cancelled by
380 * triggering the cancellable object from another thread. If the operation
381 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
383 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
387 g_data_input_stream_read_int32 (GDataInputStream *stream,
388 GCancellable *cancellable,
393 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
395 if (read_data (stream, &v, 4, cancellable, error))
397 switch (stream->priv->byte_order)
399 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
400 v = GINT32_FROM_BE (v);
402 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
403 v = GINT32_FROM_LE (v);
405 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
417 * g_data_input_stream_read_uint32:
418 * @stream: a given #GDataInputStream.
419 * @cancellable: optional #GCancellable object, %NULL to ignore.
420 * @error: #GError for error reporting.
422 * Reads an unsigned 32-bit/4-byte value from @stream.
424 * In order to get the correct byte order for this read operation,
425 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
427 * If @cancellable is not %NULL, then the operation can be cancelled by
428 * triggering the cancellable object from another thread. If the operation
429 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
431 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
435 g_data_input_stream_read_uint32 (GDataInputStream *stream,
436 GCancellable *cancellable,
441 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
443 if (read_data (stream, &v, 4, cancellable, error))
445 switch (stream->priv->byte_order)
447 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
448 v = GUINT32_FROM_BE (v);
450 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
451 v = GUINT32_FROM_LE (v);
453 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
465 * g_data_input_stream_read_int64:
466 * @stream: a given #GDataInputStream.
467 * @cancellable: optional #GCancellable object, %NULL to ignore.
468 * @error: #GError for error reporting.
470 * Reads a 64-bit/8-byte value from @stream.
472 * In order to get the correct byte order for this read operation,
473 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
475 * If @cancellable is not %NULL, then the operation can be cancelled by
476 * triggering the cancellable object from another thread. If the operation
477 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
479 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
483 g_data_input_stream_read_int64 (GDataInputStream *stream,
484 GCancellable *cancellable,
489 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
491 if (read_data (stream, &v, 8, cancellable, error))
493 switch (stream->priv->byte_order)
495 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
496 v = GINT64_FROM_BE (v);
498 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
499 v = GINT64_FROM_LE (v);
501 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
513 * g_data_input_stream_read_uint64:
514 * @stream: a given #GDataInputStream.
515 * @cancellable: optional #GCancellable object, %NULL to ignore.
516 * @error: #GError for error reporting.
518 * Reads an unsigned 64-bit/8-byte value from @stream.
520 * In order to get the correct byte order for this read operation,
521 * see g_data_stream_get_byte_order().
523 * If @cancellable is not %NULL, then the operation can be cancelled by
524 * triggering the cancellable object from another thread. If the operation
525 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
527 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
531 g_data_input_stream_read_uint64 (GDataInputStream *stream,
532 GCancellable *cancellable,
537 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
539 if (read_data (stream, &v, 8, cancellable, error))
541 switch (stream->priv->byte_order)
543 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
544 v = GUINT64_FROM_BE (v);
546 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
547 v = GUINT64_FROM_LE (v);
549 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
560 scan_for_newline (GDataInputStream *stream,
562 gboolean *last_saw_cr_out,
563 int *newline_len_out)
565 GBufferedInputStream *bstream;
566 GDataInputStreamPrivate *priv;
568 gsize start, end, peeked;
572 gsize available, checked;
573 gboolean last_saw_cr;
577 bstream = G_BUFFERED_INPUT_STREAM (stream);
579 checked = *checked_out;
580 last_saw_cr = *last_saw_cr_out;
585 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
587 peeked = end - start;
589 for (i = 0; checked < available && i < peeked; i++)
591 switch (priv->newline_type)
593 case G_DATA_STREAM_NEWLINE_TYPE_LF:
596 found_pos = start + i;
600 case G_DATA_STREAM_NEWLINE_TYPE_CR:
603 found_pos = start + i;
607 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
608 if (last_saw_cr && buffer[i] == 10)
610 found_pos = start + i - 1;
615 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
616 if (buffer[i] == 10) /* LF */
621 found_pos = start + i - 1;
627 found_pos = start + i;
631 else if (last_saw_cr)
633 /* Last was cr, this is not LF, end is CR */
634 found_pos = start + i - 1;
637 /* Don't check for CR here, instead look at last_saw_cr on next byte */
641 last_saw_cr = (buffer[i] == 13);
645 *newline_len_out = newline_len;
652 *checked_out = checked;
653 *last_saw_cr_out = last_saw_cr;
659 * g_data_input_stream_read_line:
660 * @stream: a given #GDataInputStream.
661 * @length: a #gsize to get the length of the data read in.
662 * @cancellable: optional #GCancellable object, %NULL to ignore.
663 * @error: #GError for error reporting.
665 * Reads a line from the data input stream.
667 * If @cancellable is not %NULL, then the operation can be cancelled by
668 * triggering the cancellable object from another thread. If the operation
669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
671 * Returns: a string with the line that was read in. Set @length to
672 * a #gsize to get the length of the read line. Returns %NULL on an error.
675 g_data_input_stream_read_line (GDataInputStream *stream,
677 GCancellable *cancellable,
680 GBufferedInputStream *bstream;
682 gboolean last_saw_cr;
688 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
690 bstream = G_BUFFERED_INPUT_STREAM (stream);
696 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
698 if (g_buffered_input_stream_get_available (bstream) ==
699 g_buffered_input_stream_get_buffer_size (bstream))
700 g_buffered_input_stream_set_buffer_size (bstream,
701 2 * g_buffered_input_stream_get_buffer_size (bstream));
703 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
709 if (g_buffered_input_stream_get_available (bstream) == 0)
724 line = g_malloc (found_pos + newline_len + 1);
726 res = g_input_stream_read (G_INPUT_STREAM (stream),
728 found_pos + newline_len,
731 *length = (gsize)found_pos;
732 g_assert (res == found_pos + newline_len);
740 scan_for_chars (GDataInputStream *stream,
742 const char *stop_chars)
744 GBufferedInputStream *bstream;
745 GDataInputStreamPrivate *priv;
747 gsize start, end, peeked;
750 gsize available, checked;
751 const char *stop_char;
755 bstream = G_BUFFERED_INPUT_STREAM (stream);
757 checked = *checked_out;
761 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
763 peeked = end - start;
765 for (i = 0; checked < available && i < peeked; i++)
767 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
769 if (buffer[i] == *stop_char)
776 *checked_out = checked;
781 * g_data_input_stream_read_until:
782 * @stream: a given #GDataInputStream.
783 * @stop_chars: characters to terminate the read.
784 * @length: a #gsize to get the length of the data read in.
785 * @cancellable: optional #GCancellable object, %NULL to ignore.
786 * @error: #GError for error reporting.
788 * Reads a string from the data input stream, up to the first
789 * occurrance of any of the stop characters.
791 * Returns: a string with the data that was read before encountering
792 * any of the stop characters. Set @length to a #gsize to get the length
793 * of the string. This function will return %NULL on an error.
796 g_data_input_stream_read_until (GDataInputStream *stream,
797 const gchar *stop_chars,
799 GCancellable *cancellable,
802 GBufferedInputStream *bstream;
809 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
811 bstream = G_BUFFERED_INPUT_STREAM (stream);
816 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
818 if (g_buffered_input_stream_get_available (bstream) ==
819 g_buffered_input_stream_get_buffer_size (bstream))
820 g_buffered_input_stream_set_buffer_size (bstream,
821 2 * g_buffered_input_stream_get_buffer_size (bstream));
823 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
829 if (g_buffered_input_stream_get_available (bstream) == 0)
844 data_until = g_malloc (found_pos + stop_char_len + 1);
846 res = g_input_stream_read (G_INPUT_STREAM (stream),
848 found_pos + stop_char_len,
851 *length = (gsize)found_pos;
852 g_assert (res == found_pos + stop_char_len);
853 data_until[found_pos] = 0;