1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gdatainputstream.h"
27 struct _GDataInputStreamPrivate {
28 GDataStreamByteOrder byte_order;
29 GDataStreamNewlineType newline_type;
36 static void g_data_input_stream_set_property (GObject *object,
40 static void g_data_input_stream_get_property (GObject *object,
45 G_DEFINE_TYPE (GDataInputStream,
47 G_TYPE_BUFFERED_INPUT_STREAM)
51 g_data_input_stream_class_init (GDataInputStreamClass *klass)
53 GObjectClass *object_class;
55 g_type_class_add_private (klass, sizeof (GDataInputStreamPrivate));
57 object_class = G_OBJECT_CLASS (klass);
58 object_class->get_property = g_data_input_stream_get_property;
59 object_class->set_property = g_data_input_stream_set_property;
63 g_data_input_stream_set_property (GObject *object,
68 GDataInputStreamPrivate *priv;
69 GDataInputStream *dstream;
71 dstream = G_DATA_INPUT_STREAM (object);
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 g_data_input_stream_get_property (GObject *object,
90 GDataInputStreamPrivate *priv;
91 GDataInputStream *dstream;
93 dstream = G_DATA_INPUT_STREAM (object);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 g_data_input_stream_init (GDataInputStream *stream)
108 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
109 G_TYPE_DATA_INPUT_STREAM,
110 GDataInputStreamPrivate);
112 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
113 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
117 * g_data_input_stream_new:
118 * @base_stream: a given #GInputStream.
120 * Returns: a new #GDataInputStream.
123 g_data_input_stream_new (GInputStream *base_stream)
125 GDataInputStream *stream;
127 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
129 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
130 "base-stream", base_stream,
137 * g_data_input_stream_set_byte_order:
138 * @stream: a given #GDataInputStream.
139 * @order: a #GDataStreamByteOrder to set.
141 * This function sets the byte order for the given @stream. All subsequent
142 * reads from the @stream will be read in the given @order.
146 g_data_input_stream_set_byte_order (GDataInputStream *stream,
147 GDataStreamByteOrder order)
149 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
151 stream->priv->byte_order = order;
155 * g_data_input_stream_get_byte_order:
156 * @stream: a given #GDataInputStream.
158 * Returns the @stream's current #GDataStreamByteOrder.
161 g_data_input_stream_get_byte_order (GDataInputStream *stream)
163 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
165 return stream->priv->byte_order;
169 * g_data_input_stream_set_newline_type:
170 * @stream: a given #GDataInputStream.
171 * @type: the type of new line return as #GDataStreamNewlineType.
173 * Sets the newline type for the @stream.
175 * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
176 * should it always be set to {_LF, _CR, _CR_LF}
180 g_data_input_stream_set_newline_type (GDataInputStream *stream,
181 GDataStreamNewlineType type)
183 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
185 stream->priv->newline_type = type;
189 * g_data_input_stream_get_newline_type:
190 * @stream: a given #GDataInputStream.
192 * Gets the current newline type for the @stream.
194 * Returns: #GDataStreamNewlineType for the given @stream.
196 GDataStreamNewlineType
197 g_data_input_stream_get_newline_type (GDataInputStream *stream)
199 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
201 return stream->priv->newline_type;
205 read_data (GDataInputStream *stream,
208 GCancellable *cancellable,
214 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
216 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
223 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
224 _("Unexpected early end-of-stream"));
229 /* This should always succeed, since its in the buffer */
230 res = g_input_stream_read (G_INPUT_STREAM (stream),
233 g_assert (res == size);
239 * g_data_input_stream_read_byte:
240 * @stream: a given #GDataInputStream.
241 * @cancellable: optional #GCancellable object, %NULL to ignore.
242 * @error: #GError for error reporting.
244 * In order to get the correct byte order for this read operation,
245 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
247 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
248 * if an error occured.
252 g_data_input_stream_read_byte (GDataInputStream *stream,
253 GCancellable *cancellable,
258 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
260 if (read_data (stream, &c, 1, cancellable, error))
268 * g_data_input_stream_read_int16:
269 * @stream: a given #GDataInputStream.
270 * @cancellable: optional #GCancellable object, %NULL to ignore.
271 * @error: #GError for error reporting.
273 * In order to get the correct byte order for this read operation,
274 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
276 * Returns a signed 16-bit/2-byte value read from @stream or %0 if
280 g_data_input_stream_read_int16 (GDataInputStream *stream,
281 GCancellable *cancellable,
286 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
288 if (read_data (stream, &v, 2, cancellable, error))
290 switch (stream->priv->byte_order)
292 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
293 v = GINT16_FROM_BE (v);
295 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
296 v = GINT16_FROM_LE (v);
298 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
310 * g_data_input_stream_read_uint16:
311 * @stream: a given #GDataInputStream.
312 * @cancellable: optional #GCancellable object, %NULL to ignore.
313 * @error: #GError for error reporting.
315 * In order to get the correct byte order for this read operation,
316 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
318 * Returns an unsigned 16-bit/2-byte value read from the @stream or %0 if
322 g_data_input_stream_read_uint16 (GDataInputStream *stream,
323 GCancellable *cancellable,
328 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
330 if (read_data (stream, &v, 2, cancellable, error))
332 switch (stream->priv->byte_order)
334 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
335 v = GUINT16_FROM_BE (v);
337 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
338 v = GUINT16_FROM_LE (v);
340 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
352 * g_data_input_stream_read_int32:
353 * @stream: a given #GDataInputStream.
354 * @cancellable: optional #GCancellable object, %NULL to ignore.
355 * @error: #GError for error reporting.
357 * In order to get the correct byte order for this read operation,
358 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
360 * Returns a signed 32-bit/4-byte value read from the @stream or %0 if
364 g_data_input_stream_read_int32 (GDataInputStream *stream,
365 GCancellable *cancellable,
370 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
372 if (read_data (stream, &v, 4, cancellable, error))
374 switch (stream->priv->byte_order)
376 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
377 v = GINT32_FROM_BE (v);
379 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
380 v = GINT32_FROM_LE (v);
382 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
394 * g_data_input_stream_read_uint32:
395 * @stream: a given #GDataInputStream.
396 * @cancellable: optional #GCancellable object, %NULL to ignore.
397 * @error: #GError for error reporting.
399 * In order to get the correct byte order for this read operation,
400 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
402 * Returns an unsigned 32-bit/4-byte value read from the @stream or %0 if
406 g_data_input_stream_read_uint32 (GDataInputStream *stream,
407 GCancellable *cancellable,
412 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
414 if (read_data (stream, &v, 4, cancellable, error))
416 switch (stream->priv->byte_order)
418 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
419 v = GUINT32_FROM_BE (v);
421 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
422 v = GUINT32_FROM_LE (v);
424 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
436 * g_data_input_stream_read_int64:
437 * @stream: a given #GDataInputStream.
438 * @cancellable: optional #GCancellable object, %NULL to ignore.
439 * @error: #GError for error reporting.
441 * In order to get the correct byte order for this read operation,
442 * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
444 * Returns a signed 64-bit/8-byte value read from @stream or %0 if
448 g_data_input_stream_read_int64 (GDataInputStream *stream,
449 GCancellable *cancellable,
454 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
456 if (read_data (stream, &v, 8, cancellable, error))
458 switch (stream->priv->byte_order)
460 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
461 v = GINT64_FROM_BE (v);
463 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
464 v = GINT64_FROM_LE (v);
466 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
478 * g_data_input_stream_read_uint64:
479 * @stream: a given #GDataInputStream.
480 * @cancellable: optional #GCancellable object, %NULL to ignore.
481 * @error: #GError for error reporting.
483 * In order to get the correct byte order for this read operation,
484 * see g_data_stream_get_byte_order().
486 * Returns an unsigned 64-bit/8-byte read from @stream or %0 if
490 g_data_input_stream_read_uint64 (GDataInputStream *stream,
491 GCancellable *cancellable,
496 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
498 if (read_data (stream, &v, 8, cancellable, error))
500 switch (stream->priv->byte_order)
502 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
503 v = GUINT64_FROM_BE (v);
505 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
506 v = GUINT64_FROM_LE (v);
508 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
519 scan_for_newline (GDataInputStream *stream,
521 gboolean *last_saw_cr_out,
522 int *newline_len_out)
524 GBufferedInputStream *bstream;
525 GDataInputStreamPrivate *priv;
527 gsize start, end, peeked;
531 gsize available, checked;
532 gboolean last_saw_cr;
536 bstream = G_BUFFERED_INPUT_STREAM (stream);
538 available = g_buffered_input_stream_get_available (bstream);
540 checked = *checked_out;
541 last_saw_cr = *last_saw_cr_out;
545 while (checked < available)
548 end = MIN (start + sizeof(buffer), available);
549 peeked = g_buffered_input_stream_peek (bstream, buffer, start, end - start);
550 end = start + peeked;
552 for (i = 0; i < peeked; i++)
554 switch (priv->newline_type)
556 case G_DATA_STREAM_NEWLINE_TYPE_LF:
559 found_pos = start + i;
563 case G_DATA_STREAM_NEWLINE_TYPE_CR:
566 found_pos = start + i;
570 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
571 if (last_saw_cr && buffer[i] == 10)
573 found_pos = start + i - 1;
578 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
579 if (buffer[i] == 10) /* LF */
584 found_pos = start + i - 1;
590 found_pos = start + i;
594 else if (last_saw_cr)
596 /* Last was cr, this is not LF, end is CR */
597 found_pos = start + i - 1;
600 /* Don't check for CR here, instead look at last_saw_cr on next byte */
604 last_saw_cr = (buffer[i] == 13);
608 *newline_len_out = newline_len;
615 *checked_out = checked;
616 *last_saw_cr_out = last_saw_cr;
622 * g_data_input_stream_read_line:
623 * @stream: a given #GDataInputStream.
624 * @length: a #gsize to get the length of the data read in.
625 * @cancellable: optional #GCancellable object, %NULL to ignore.
626 * @error: #GError for error reporting.
628 * Returns a string with the line that was read in. Set @length to
629 * a #gsize to get the length of the read line. This function will
630 * return %NULL on an error.
633 g_data_input_stream_read_line (GDataInputStream *stream,
635 GCancellable *cancellable,
638 GBufferedInputStream *bstream;
640 gboolean last_saw_cr;
646 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
648 bstream = G_BUFFERED_INPUT_STREAM (stream);
654 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
656 if (g_buffered_input_stream_get_available (bstream) ==
657 g_buffered_input_stream_get_buffer_size (bstream))
658 g_buffered_input_stream_set_buffer_size (bstream,
659 2 * g_buffered_input_stream_get_buffer_size (bstream));
661 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
667 if (g_buffered_input_stream_get_available (bstream) == 0)
682 line = g_malloc (found_pos + newline_len + 1);
684 res = g_input_stream_read (G_INPUT_STREAM (stream),
686 found_pos + newline_len,
689 *length = (gsize)found_pos;
690 g_assert (res == found_pos + newline_len);
698 * g_data_input_stream_read_until:
699 * @stream: a given #GDataInputStream.
700 * @stop_char: character to terminate the read.
701 * @length: a #gsize to get the length of the data read in.
702 * @cancellable: optional #GCancellable object, %NULL to ignore.
703 * @error: #GError for error reporting.
705 * NOTE: not supported for #GDataInputStream.
709 g_data_input_stream_read_until (GDataInputStream *stream,
712 GCancellable *cancellable,
715 /* TODO: should be implemented */
716 g_assert_not_reached ();