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 * In order to get the correct byte order for this read operation,
260 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
262 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
263 * if an error occured.
266 g_data_input_stream_read_byte (GDataInputStream *stream,
267 GCancellable *cancellable,
272 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
274 if (read_data (stream, &c, 1, cancellable, error))
282 * g_data_input_stream_read_int16:
283 * @stream: a given #GDataInputStream.
284 * @cancellable: optional #GCancellable object, %NULL to ignore.
285 * @error: #GError for error reporting.
287 * In order to get the correct byte order for this read operation,
288 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
290 * Returns a signed 16-bit/2-byte value read from @stream or %0 if
294 g_data_input_stream_read_int16 (GDataInputStream *stream,
295 GCancellable *cancellable,
300 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
302 if (read_data (stream, &v, 2, cancellable, error))
304 switch (stream->priv->byte_order)
306 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
307 v = GINT16_FROM_BE (v);
309 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
310 v = GINT16_FROM_LE (v);
312 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
324 * g_data_input_stream_read_uint16:
325 * @stream: a given #GDataInputStream.
326 * @cancellable: optional #GCancellable object, %NULL to ignore.
327 * @error: #GError for error reporting.
329 * In order to get the correct byte order for this read operation,
330 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
332 * Returns an unsigned 16-bit/2-byte value read from the @stream or %0 if
336 g_data_input_stream_read_uint16 (GDataInputStream *stream,
337 GCancellable *cancellable,
342 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
344 if (read_data (stream, &v, 2, cancellable, error))
346 switch (stream->priv->byte_order)
348 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
349 v = GUINT16_FROM_BE (v);
351 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
352 v = GUINT16_FROM_LE (v);
354 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
366 * g_data_input_stream_read_int32:
367 * @stream: a given #GDataInputStream.
368 * @cancellable: optional #GCancellable object, %NULL to ignore.
369 * @error: #GError for error reporting.
371 * In order to get the correct byte order for this read operation,
372 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
374 * If @cancellable is not %NULL, then the operation can be cancelled by
375 * triggering the cancellable object from another thread. If the operation
376 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
378 * Returns a signed 32-bit/4-byte value read from the @stream or %0 if
382 g_data_input_stream_read_int32 (GDataInputStream *stream,
383 GCancellable *cancellable,
388 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
390 if (read_data (stream, &v, 4, cancellable, error))
392 switch (stream->priv->byte_order)
394 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
395 v = GINT32_FROM_BE (v);
397 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
398 v = GINT32_FROM_LE (v);
400 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
412 * g_data_input_stream_read_uint32:
413 * @stream: a given #GDataInputStream.
414 * @cancellable: optional #GCancellable object, %NULL to ignore.
415 * @error: #GError for error reporting.
417 * In order to get the correct byte order for this read operation,
418 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
420 * If @cancellable is not %NULL, then the operation can be cancelled by
421 * triggering the cancellable object from another thread. If the operation
422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
424 * Returns an unsigned 32-bit/4-byte value read from the @stream or %0 if
428 g_data_input_stream_read_uint32 (GDataInputStream *stream,
429 GCancellable *cancellable,
434 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
436 if (read_data (stream, &v, 4, cancellable, error))
438 switch (stream->priv->byte_order)
440 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
441 v = GUINT32_FROM_BE (v);
443 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
444 v = GUINT32_FROM_LE (v);
446 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
458 * g_data_input_stream_read_int64:
459 * @stream: a given #GDataInputStream.
460 * @cancellable: optional #GCancellable object, %NULL to ignore.
461 * @error: #GError for error reporting.
463 * In order to get the correct byte order for this read operation,
464 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
466 * If @cancellable is not %NULL, then the operation can be cancelled by
467 * triggering the cancellable object from another thread. If the operation
468 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
470 * Returns a signed 64-bit/8-byte value read from @stream or %0 if
474 g_data_input_stream_read_int64 (GDataInputStream *stream,
475 GCancellable *cancellable,
480 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
482 if (read_data (stream, &v, 8, cancellable, error))
484 switch (stream->priv->byte_order)
486 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
487 v = GINT64_FROM_BE (v);
489 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
490 v = GINT64_FROM_LE (v);
492 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
504 * g_data_input_stream_read_uint64:
505 * @stream: a given #GDataInputStream.
506 * @cancellable: optional #GCancellable object, %NULL to ignore.
507 * @error: #GError for error reporting.
509 * In order to get the correct byte order for this read operation,
510 * see g_data_stream_get_byte_order().
512 * If @cancellable is not %NULL, then the operation can be cancelled by
513 * triggering the cancellable object from another thread. If the operation
514 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
516 * Returns an unsigned 64-bit/8-byte read from @stream or %0 if
520 g_data_input_stream_read_uint64 (GDataInputStream *stream,
521 GCancellable *cancellable,
526 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
528 if (read_data (stream, &v, 8, cancellable, error))
530 switch (stream->priv->byte_order)
532 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
533 v = GUINT64_FROM_BE (v);
535 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
536 v = GUINT64_FROM_LE (v);
538 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
549 scan_for_newline (GDataInputStream *stream,
551 gboolean *last_saw_cr_out,
552 int *newline_len_out)
554 GBufferedInputStream *bstream;
555 GDataInputStreamPrivate *priv;
557 gsize start, end, peeked;
561 gsize available, checked;
562 gboolean last_saw_cr;
566 bstream = G_BUFFERED_INPUT_STREAM (stream);
568 checked = *checked_out;
569 last_saw_cr = *last_saw_cr_out;
574 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
576 peeked = end - start;
578 for (i = 0; checked < available && i < peeked; i++)
580 switch (priv->newline_type)
582 case G_DATA_STREAM_NEWLINE_TYPE_LF:
585 found_pos = start + i;
589 case G_DATA_STREAM_NEWLINE_TYPE_CR:
592 found_pos = start + i;
596 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
597 if (last_saw_cr && buffer[i] == 10)
599 found_pos = start + i - 1;
604 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
605 if (buffer[i] == 10) /* LF */
610 found_pos = start + i - 1;
616 found_pos = start + i;
620 else if (last_saw_cr)
622 /* Last was cr, this is not LF, end is CR */
623 found_pos = start + i - 1;
626 /* Don't check for CR here, instead look at last_saw_cr on next byte */
630 last_saw_cr = (buffer[i] == 13);
634 *newline_len_out = newline_len;
641 *checked_out = checked;
642 *last_saw_cr_out = last_saw_cr;
648 * g_data_input_stream_read_line:
649 * @stream: a given #GDataInputStream.
650 * @length: a #gsize to get the length of the data read in.
651 * @cancellable: optional #GCancellable object, %NULL to ignore.
652 * @error: #GError for error reporting.
654 * Reads a line from the data input stream.
656 * If @cancellable is not %NULL, then the operation can be cancelled by
657 * triggering the cancellable object from another thread. If the operation
658 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
660 * Returns a string with the line that was read in. Set @length to
661 * a #gsize to get the length of the read line. Returns %NULL on an error.
664 g_data_input_stream_read_line (GDataInputStream *stream,
666 GCancellable *cancellable,
669 GBufferedInputStream *bstream;
671 gboolean last_saw_cr;
677 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
679 bstream = G_BUFFERED_INPUT_STREAM (stream);
685 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
687 if (g_buffered_input_stream_get_available (bstream) ==
688 g_buffered_input_stream_get_buffer_size (bstream))
689 g_buffered_input_stream_set_buffer_size (bstream,
690 2 * g_buffered_input_stream_get_buffer_size (bstream));
692 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
698 if (g_buffered_input_stream_get_available (bstream) == 0)
713 line = g_malloc (found_pos + newline_len + 1);
715 res = g_input_stream_read (G_INPUT_STREAM (stream),
717 found_pos + newline_len,
720 *length = (gsize)found_pos;
721 g_assert (res == found_pos + newline_len);
729 scan_for_chars (GDataInputStream *stream,
731 const char *stop_chars)
733 GBufferedInputStream *bstream;
734 GDataInputStreamPrivate *priv;
736 gsize start, end, peeked;
739 gsize available, checked;
740 const char *stop_char;
744 bstream = G_BUFFERED_INPUT_STREAM (stream);
746 checked = *checked_out;
750 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
752 peeked = end - start;
754 for (i = 0; checked < available && i < peeked; i++)
756 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
758 if (buffer[i] == *stop_char)
765 *checked_out = checked;
770 * g_data_input_stream_read_until:
771 * @stream: a given #GDataInputStream.
772 * @stop_chars: characters to terminate the read.
773 * @length: a #gsize to get the length of the data read in.
774 * @cancellable: optional #GCancellable object, %NULL to ignore.
775 * @error: #GError for error reporting.
777 * Returns a string with the data that was read before encountering any of
778 * the stop characters. Set @length to a #gsize to get the length of the
779 * read line. This function will return %NULL on an error.
782 g_data_input_stream_read_until (GDataInputStream *stream,
783 const gchar *stop_chars,
785 GCancellable *cancellable,
788 GBufferedInputStream *bstream;
795 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
797 bstream = G_BUFFERED_INPUT_STREAM (stream);
802 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
804 if (g_buffered_input_stream_get_available (bstream) ==
805 g_buffered_input_stream_get_buffer_size (bstream))
806 g_buffered_input_stream_set_buffer_size (bstream,
807 2 * g_buffered_input_stream_get_buffer_size (bstream));
809 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
815 if (g_buffered_input_stream_get_available (bstream) == 0)
830 data_until = g_malloc (found_pos + stop_char_len + 1);
832 res = g_input_stream_read (G_INPUT_STREAM (stream),
834 found_pos + stop_char_len,
837 *length = (gsize)found_pos;
838 g_assert (res == found_pos + stop_char_len);
839 data_until[found_pos] = 0;