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"
28 struct _GDataInputStreamPrivate {
29 GDataStreamByteOrder byte_order;
30 GDataStreamNewlineType newline_type;
37 static void g_data_input_stream_set_property (GObject *object,
41 static void g_data_input_stream_get_property (GObject *object,
46 G_DEFINE_TYPE (GDataInputStream,
48 G_TYPE_BUFFERED_INPUT_STREAM)
52 g_data_input_stream_class_init (GDataInputStreamClass *klass)
54 GObjectClass *object_class;
56 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
58 object_class = G_OBJECT_CLASS (klass);
59 object_class->get_property = g_data_input_stream_get_property;
60 object_class->set_property = g_data_input_stream_set_property;
64 g_data_input_stream_set_property (GObject *object,
69 GDataInputStreamPrivate *priv;
70 GDataInputStream *dstream;
72 dstream = G_DATA_INPUT_STREAM (object);
79 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 g_data_input_stream_get_property (GObject *object,
91 GDataInputStreamPrivate *priv;
92 GDataInputStream *dstream;
94 dstream = G_DATA_INPUT_STREAM (object);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 g_data_input_stream_init (GDataInputStream *stream)
109 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
110 G_TYPE_DATA_INPUT_STREAM,
111 GDataInputStreamPrivate);
113 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
114 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
118 * g_data_input_stream_new:
119 * @base_stream: a given #GInputStream.
121 * Returns: a new #GDataInputStream.
124 g_data_input_stream_new (GInputStream *base_stream)
126 GDataInputStream *stream;
128 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
130 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
131 "base-stream", base_stream,
138 * g_data_input_stream_set_byte_order:
139 * @stream: a given #GDataInputStream.
140 * @order: a #GDataStreamByteOrder to set.
142 * This function sets the byte order for the given @stream. All subsequent
143 * reads from the @stream will be read in the given @order.
147 g_data_input_stream_set_byte_order (GDataInputStream *stream,
148 GDataStreamByteOrder order)
150 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
152 stream->priv->byte_order = order;
156 * g_data_input_stream_get_byte_order:
157 * @stream: a given #GDataInputStream.
159 * Returns the @stream's current #GDataStreamByteOrder.
162 g_data_input_stream_get_byte_order (GDataInputStream *stream)
164 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
166 return stream->priv->byte_order;
170 * g_data_input_stream_set_newline_type:
171 * @stream: a given #GDataInputStream.
172 * @type: the type of new line return as #GDataStreamNewlineType.
174 * Sets the newline type for the @stream.
176 * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
177 * should it always be set to {_LF, _CR, _CR_LF}
181 g_data_input_stream_set_newline_type (GDataInputStream *stream,
182 GDataStreamNewlineType type)
184 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
186 stream->priv->newline_type = type;
190 * g_data_input_stream_get_newline_type:
191 * @stream: a given #GDataInputStream.
193 * Gets the current newline type for the @stream.
195 * Returns: #GDataStreamNewlineType for the given @stream.
197 GDataStreamNewlineType
198 g_data_input_stream_get_newline_type (GDataInputStream *stream)
200 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
202 return stream->priv->newline_type;
206 read_data (GDataInputStream *stream,
209 GCancellable *cancellable,
215 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
217 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
224 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
225 _("Unexpected early end-of-stream"));
230 /* This should always succeed, since its in the buffer */
231 res = g_input_stream_read (G_INPUT_STREAM (stream),
234 g_assert (res == size);
240 * g_data_input_stream_read_byte:
241 * @stream: a given #GDataInputStream.
242 * @cancellable: optional #GCancellable object, %NULL to ignore.
243 * @error: #GError for error reporting.
245 * In order to get the correct byte order for this read operation,
246 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
248 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
249 * if an error occured.
253 g_data_input_stream_read_byte (GDataInputStream *stream,
254 GCancellable *cancellable,
259 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
261 if (read_data (stream, &c, 1, cancellable, error))
269 * g_data_input_stream_read_int16:
270 * @stream: a given #GDataInputStream.
271 * @cancellable: optional #GCancellable object, %NULL to ignore.
272 * @error: #GError for error reporting.
274 * In order to get the correct byte order for this read operation,
275 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
277 * Returns a signed 16-bit/2-byte value read from @stream or %0 if
281 g_data_input_stream_read_int16 (GDataInputStream *stream,
282 GCancellable *cancellable,
287 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
289 if (read_data (stream, &v, 2, cancellable, error))
291 switch (stream->priv->byte_order)
293 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
294 v = GINT16_FROM_BE (v);
296 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
297 v = GINT16_FROM_LE (v);
299 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
311 * g_data_input_stream_read_uint16:
312 * @stream: a given #GDataInputStream.
313 * @cancellable: optional #GCancellable object, %NULL to ignore.
314 * @error: #GError for error reporting.
316 * In order to get the correct byte order for this read operation,
317 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
319 * Returns an unsigned 16-bit/2-byte value read from the @stream or %0 if
323 g_data_input_stream_read_uint16 (GDataInputStream *stream,
324 GCancellable *cancellable,
329 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
331 if (read_data (stream, &v, 2, cancellable, error))
333 switch (stream->priv->byte_order)
335 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
336 v = GUINT16_FROM_BE (v);
338 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
339 v = GUINT16_FROM_LE (v);
341 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
353 * g_data_input_stream_read_int32:
354 * @stream: a given #GDataInputStream.
355 * @cancellable: optional #GCancellable object, %NULL to ignore.
356 * @error: #GError for error reporting.
358 * In order to get the correct byte order for this read operation,
359 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
361 * Returns a signed 32-bit/4-byte value read from the @stream or %0 if
365 g_data_input_stream_read_int32 (GDataInputStream *stream,
366 GCancellable *cancellable,
371 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
373 if (read_data (stream, &v, 4, cancellable, error))
375 switch (stream->priv->byte_order)
377 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
378 v = GINT32_FROM_BE (v);
380 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
381 v = GINT32_FROM_LE (v);
383 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
395 * g_data_input_stream_read_uint32:
396 * @stream: a given #GDataInputStream.
397 * @cancellable: optional #GCancellable object, %NULL to ignore.
398 * @error: #GError for error reporting.
400 * In order to get the correct byte order for this read operation,
401 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
403 * Returns an unsigned 32-bit/4-byte value read from the @stream or %0 if
407 g_data_input_stream_read_uint32 (GDataInputStream *stream,
408 GCancellable *cancellable,
413 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
415 if (read_data (stream, &v, 4, cancellable, error))
417 switch (stream->priv->byte_order)
419 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
420 v = GUINT32_FROM_BE (v);
422 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
423 v = GUINT32_FROM_LE (v);
425 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
437 * g_data_input_stream_read_int64:
438 * @stream: a given #GDataInputStream.
439 * @cancellable: optional #GCancellable object, %NULL to ignore.
440 * @error: #GError for error reporting.
442 * In order to get the correct byte order for this read operation,
443 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
445 * Returns a signed 64-bit/8-byte value read from @stream or %0 if
449 g_data_input_stream_read_int64 (GDataInputStream *stream,
450 GCancellable *cancellable,
455 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
457 if (read_data (stream, &v, 8, cancellable, error))
459 switch (stream->priv->byte_order)
461 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
462 v = GINT64_FROM_BE (v);
464 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
465 v = GINT64_FROM_LE (v);
467 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
479 * g_data_input_stream_read_uint64:
480 * @stream: a given #GDataInputStream.
481 * @cancellable: optional #GCancellable object, %NULL to ignore.
482 * @error: #GError for error reporting.
484 * In order to get the correct byte order for this read operation,
485 * see g_data_stream_get_byte_order().
487 * Returns an unsigned 64-bit/8-byte read from @stream or %0 if
491 g_data_input_stream_read_uint64 (GDataInputStream *stream,
492 GCancellable *cancellable,
497 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
499 if (read_data (stream, &v, 8, cancellable, error))
501 switch (stream->priv->byte_order)
503 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
504 v = GUINT64_FROM_BE (v);
506 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
507 v = GUINT64_FROM_LE (v);
509 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
520 scan_for_newline (GDataInputStream *stream,
522 gboolean *last_saw_cr_out,
523 int *newline_len_out)
525 GBufferedInputStream *bstream;
526 GDataInputStreamPrivate *priv;
528 gsize start, end, peeked;
532 gsize available, checked;
533 gboolean last_saw_cr;
537 bstream = G_BUFFERED_INPUT_STREAM (stream);
539 checked = *checked_out;
540 last_saw_cr = *last_saw_cr_out;
545 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
547 peeked = end - start;
549 for (i = 0; checked < available && i < peeked; i++)
551 switch (priv->newline_type)
553 case G_DATA_STREAM_NEWLINE_TYPE_LF:
556 found_pos = start + i;
560 case G_DATA_STREAM_NEWLINE_TYPE_CR:
563 found_pos = start + i;
567 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
568 if (last_saw_cr && buffer[i] == 10)
570 found_pos = start + i - 1;
575 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
576 if (buffer[i] == 10) /* LF */
581 found_pos = start + i - 1;
587 found_pos = start + i;
591 else if (last_saw_cr)
593 /* Last was cr, this is not LF, end is CR */
594 found_pos = start + i - 1;
597 /* Don't check for CR here, instead look at last_saw_cr on next byte */
601 last_saw_cr = (buffer[i] == 13);
605 *newline_len_out = newline_len;
612 *checked_out = checked;
613 *last_saw_cr_out = last_saw_cr;
619 * g_data_input_stream_read_line:
620 * @stream: a given #GDataInputStream.
621 * @length: a #gsize to get the length of the data read in.
622 * @cancellable: optional #GCancellable object, %NULL to ignore.
623 * @error: #GError for error reporting.
625 * Returns a string with the line that was read in. Set @length to
626 * a #gsize to get the length of the read line. This function will
627 * return %NULL on an error.
630 g_data_input_stream_read_line (GDataInputStream *stream,
632 GCancellable *cancellable,
635 GBufferedInputStream *bstream;
637 gboolean last_saw_cr;
643 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
645 bstream = G_BUFFERED_INPUT_STREAM (stream);
651 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
653 if (g_buffered_input_stream_get_available (bstream) ==
654 g_buffered_input_stream_get_buffer_size (bstream))
655 g_buffered_input_stream_set_buffer_size (bstream,
656 2 * g_buffered_input_stream_get_buffer_size (bstream));
658 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
664 if (g_buffered_input_stream_get_available (bstream) == 0)
679 line = g_malloc (found_pos + newline_len + 1);
681 res = g_input_stream_read (G_INPUT_STREAM (stream),
683 found_pos + newline_len,
686 *length = (gsize)found_pos;
687 g_assert (res == found_pos + newline_len);
695 scan_for_chars (GDataInputStream *stream,
697 const char *stop_chars)
699 GBufferedInputStream *bstream;
700 GDataInputStreamPrivate *priv;
702 gsize start, end, peeked;
705 gsize available, checked;
706 const char *stop_char;
710 bstream = G_BUFFERED_INPUT_STREAM (stream);
712 checked = *checked_out;
716 buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
718 peeked = end - start;
720 for (i = 0; checked < available && i < peeked; i++)
722 for (stop_char = stop_chars; *stop_char != '\0'; stop_char++)
724 if (buffer[i] == *stop_char)
731 *checked_out = checked;
736 * g_data_input_stream_read_until:
737 * @stream: a given #GDataInputStream.
738 * @stop_chars: characters to terminate the read.
739 * @length: a #gsize to get the length of the data read in.
740 * @cancellable: optional #GCancellable object, %NULL to ignore.
741 * @error: #GError for error reporting.
743 * Returns a string with the data that was read before encountering any of
744 * the stop characters. Set @length to a #gsize to get the length of the
745 * read line. This function will return %NULL on an error.
748 g_data_input_stream_read_until (GDataInputStream *stream,
749 const gchar *stop_chars,
751 GCancellable *cancellable,
754 GBufferedInputStream *bstream;
761 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
763 bstream = G_BUFFERED_INPUT_STREAM (stream);
768 while ((found_pos = scan_for_chars (stream, &checked, stop_chars)) == -1)
770 if (g_buffered_input_stream_get_available (bstream) ==
771 g_buffered_input_stream_get_buffer_size (bstream))
772 g_buffered_input_stream_set_buffer_size (bstream,
773 2 * g_buffered_input_stream_get_buffer_size (bstream));
775 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
781 if (g_buffered_input_stream_get_available (bstream) == 0)
796 data_until = g_malloc (found_pos + stop_char_len + 1);
798 res = g_input_stream_read (G_INPUT_STREAM (stream),
800 found_pos + stop_char_len,
803 *length = (gsize)found_pos;
804 g_assert (res == found_pos + stop_char_len);
805 data_until[found_pos] = 0;