2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
2007-11-27 14:44:48 +01:00
|
|
|
* Copyright (C) 2007 Jürg Billeter
|
2009-01-28 17:39:39 +01:00
|
|
|
* Copyright © 2009 Codethink Limited
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gdatainputstream.h"
|
2012-08-02 21:49:59 +02:00
|
|
|
#include "gtask.h"
|
2009-01-28 17:39:39 +01:00
|
|
|
#include "gcancellable.h"
|
2007-12-01 03:53:34 +01:00
|
|
|
#include "gioenumtypes.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gioerror.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "glibintl.h"
|
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
#include <string.h>
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gdatainputstream
|
2007-11-28 07:43:33 +01:00
|
|
|
* @short_description: Data Input Stream
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-28 07:43:33 +01:00
|
|
|
* @see_also: #GInputStream
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* Data input stream implements #GInputStream and includes functions for
|
2007-12-12 13:19:02 +01:00
|
|
|
* reading structured data directly from a binary input stream.
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
struct _GDataInputStreamPrivate {
|
|
|
|
GDataStreamByteOrder byte_order;
|
|
|
|
GDataStreamNewlineType newline_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2007-12-01 03:53:34 +01:00
|
|
|
PROP_0,
|
|
|
|
PROP_BYTE_ORDER,
|
|
|
|
PROP_NEWLINE_TYPE
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void g_data_input_stream_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void g_data_input_stream_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GDataInputStream,
|
|
|
|
g_data_input_stream,
|
|
|
|
G_TYPE_BUFFERED_INPUT_STREAM)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_data_input_stream_class_init (GDataInputStreamClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
|
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
object_class->get_property = g_data_input_stream_get_property;
|
|
|
|
object_class->set_property = g_data_input_stream_set_property;
|
2007-12-01 03:53:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GDataStream:byte-order:
|
|
|
|
*
|
|
|
|
* The ::byte-order property determines the byte ordering that
|
|
|
|
* is used when reading multi-byte entities (such as integers)
|
|
|
|
* from the stream.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_BYTE_ORDER,
|
|
|
|
g_param_spec_enum ("byte-order",
|
|
|
|
P_("Byte order"),
|
|
|
|
P_("The byte order"),
|
|
|
|
G_TYPE_DATA_STREAM_BYTE_ORDER,
|
|
|
|
G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
|
|
|
|
G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDataStream:newline-type:
|
|
|
|
*
|
|
|
|
* The :newline-type property determines what is considered
|
|
|
|
* as a line ending when reading complete lines from the stream.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_NEWLINE_TYPE,
|
|
|
|
g_param_spec_enum ("newline-type",
|
|
|
|
P_("Newline type"),
|
|
|
|
P_("The accepted types of line ending"),
|
|
|
|
G_TYPE_DATA_STREAM_NEWLINE_TYPE,
|
|
|
|
G_DATA_STREAM_NEWLINE_TYPE_LF,
|
|
|
|
G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GDataInputStream *dstream;
|
|
|
|
|
|
|
|
dstream = G_DATA_INPUT_STREAM (object);
|
|
|
|
|
2009-09-07 09:10:01 +02:00
|
|
|
switch (prop_id)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-12-01 03:53:34 +01:00
|
|
|
case PROP_BYTE_ORDER:
|
|
|
|
g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_NEWLINE_TYPE:
|
|
|
|
g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
|
|
|
|
break;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_data_input_stream_get_property (GObject *object,
|
2007-11-29 08:17:59 +01:00
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GDataInputStreamPrivate *priv;
|
|
|
|
GDataInputStream *dstream;
|
|
|
|
|
|
|
|
dstream = G_DATA_INPUT_STREAM (object);
|
|
|
|
priv = dstream->priv;
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2007-12-01 03:53:34 +01:00
|
|
|
case PROP_BYTE_ORDER:
|
|
|
|
g_value_set_enum (value, priv->byte_order);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_NEWLINE_TYPE:
|
|
|
|
g_value_set_enum (value, priv->newline_type);
|
|
|
|
break;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
g_data_input_stream_init (GDataInputStream *stream)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
stream->priv = g_data_input_stream_get_instance_private (stream);
|
2007-11-26 17:13:05 +01:00
|
|
|
stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
|
|
|
|
stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_new:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @base_stream: a #GInputStream.
|
|
|
|
*
|
|
|
|
* Creates a new data input stream for the @base_stream.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Returns: a new #GDataInputStream.
|
|
|
|
**/
|
|
|
|
GDataInputStream *
|
|
|
|
g_data_input_stream_new (GInputStream *base_stream)
|
|
|
|
{
|
|
|
|
GDataInputStream *stream;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
|
|
|
|
|
|
|
|
stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
|
|
|
|
"base-stream", base_stream,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_set_byte_order:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @order: a #GDataStreamByteOrder to set.
|
|
|
|
*
|
|
|
|
* This function sets the byte order for the given @stream. All subsequent
|
|
|
|
* reads from the @stream will be read in the given @order.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
void
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_set_byte_order (GDataInputStream *stream,
|
|
|
|
GDataStreamByteOrder order)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-12-01 03:53:34 +01:00
|
|
|
GDataInputStreamPrivate *priv;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
|
|
|
|
|
2007-12-01 03:53:34 +01:00
|
|
|
priv = stream->priv;
|
|
|
|
|
|
|
|
if (priv->byte_order != order)
|
|
|
|
{
|
|
|
|
priv->byte_order = order;
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (stream), "byte-order");
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_get_byte_order:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Gets the byte order for the data input stream.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: the @stream's current #GDataStreamByteOrder.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
GDataStreamByteOrder
|
|
|
|
g_data_input_stream_get_byte_order (GDataInputStream *stream)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
|
|
|
|
|
|
|
|
return stream->priv->byte_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_set_newline_type:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @stream: a #GDataInputStream.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @type: the type of new line return as #GDataStreamNewlineType.
|
|
|
|
*
|
|
|
|
* Sets the newline type for the @stream.
|
|
|
|
*
|
2007-12-12 13:19:02 +01:00
|
|
|
* Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
|
|
|
|
* chunk ends in "CR" we must read an additional byte to know if this is "CR" or
|
2011-08-29 20:49:32 +02:00
|
|
|
* "CR LF", and this might block if there is no more data available.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
void
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_set_newline_type (GDataInputStream *stream,
|
|
|
|
GDataStreamNewlineType type)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-12-01 03:53:34 +01:00
|
|
|
GDataInputStreamPrivate *priv;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
|
|
|
|
|
2007-12-01 03:53:34 +01:00
|
|
|
priv = stream->priv;
|
|
|
|
|
|
|
|
if (priv->newline_type != type)
|
|
|
|
{
|
|
|
|
priv->newline_type = type;
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (stream), "newline-type");
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_get_newline_type:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
*
|
|
|
|
* Gets the current newline type for the @stream.
|
|
|
|
*
|
|
|
|
* Returns: #GDataStreamNewlineType for the given @stream.
|
|
|
|
**/
|
|
|
|
GDataStreamNewlineType
|
|
|
|
g_data_input_stream_get_newline_type (GDataInputStream *stream)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
|
|
|
|
|
|
|
|
return stream->priv->newline_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-11-29 08:17:59 +01:00
|
|
|
read_data (GDataInputStream *stream,
|
|
|
|
void *buffer,
|
|
|
|
gsize size,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
gsize available;
|
|
|
|
gssize res;
|
|
|
|
|
|
|
|
while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
|
|
|
|
{
|
|
|
|
res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
|
|
|
|
size - available,
|
|
|
|
cancellable, error);
|
|
|
|
if (res < 0)
|
|
|
|
return FALSE;
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
_("Unexpected early end-of-stream"));
|
2007-11-26 17:13:05 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-23 05:30:06 +01:00
|
|
|
/* This should always succeed, since it's in the buffer */
|
2007-11-26 17:13:05 +01:00
|
|
|
res = g_input_stream_read (G_INPUT_STREAM (stream),
|
|
|
|
buffer, size,
|
|
|
|
NULL, NULL);
|
2007-12-10 15:07:42 +01:00
|
|
|
g_warn_if_fail (res == size);
|
2007-11-26 17:13:05 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_byte:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads an unsigned 8-bit/1-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
|
2008-01-16 21:00:23 +01:00
|
|
|
* if an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
guchar
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_byte (GDataInputStream *stream,
|
2007-12-12 13:19:02 +01:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
guchar c;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
|
|
|
|
|
|
|
|
if (read_data (stream, &c, 1, cancellable, error))
|
|
|
|
return c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_int16:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads a 16-bit/2-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: a signed 16-bit/2-byte value read from @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
gint16
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_int16 (GDataInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
gint16 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 2, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GINT16_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GINT16_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_uint16:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads an unsigned 16-bit/2-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
guint16
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_uint16 (GDataInputStream *stream,
|
2007-12-12 13:19:02 +01:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
guint16 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 2, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GUINT16_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GUINT16_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_int32:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads a signed 32-bit/4-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
gint32
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_int32 (GDataInputStream *stream,
|
2007-12-12 13:19:02 +01:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
gint32 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 4, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GINT32_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GINT32_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_uint32:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads an unsigned 32-bit/4-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
guint32
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_uint32 (GDataInputStream *stream,
|
2007-12-12 13:19:02 +01:00
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
guint32 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 4, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GUINT32_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GUINT32_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_int64:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads a 64-bit/8-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: a signed 64-bit/8-byte value read from @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
gint64
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_int64 (GDataInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
gint64 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 8, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GINT64_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GINT64_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_uint64:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Reads an unsigned 64-bit/8-byte value from @stream.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* In order to get the correct byte order for this read operation,
|
2010-06-20 02:32:35 +02:00
|
|
|
* see g_data_input_stream_get_byte_order().
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 05:29:02 +01:00
|
|
|
* Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
|
2008-01-16 21:00:23 +01:00
|
|
|
* an error occurred.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
guint64
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_uint64 (GDataInputStream *stream,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
guint64 v;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
|
|
|
|
|
|
|
|
if (read_data (stream, &v, 8, cancellable, error))
|
|
|
|
{
|
|
|
|
switch (stream->priv->byte_order)
|
|
|
|
{
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
|
|
|
|
v = GUINT64_FROM_BE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
|
|
|
|
v = GUINT64_FROM_LE (v);
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
|
|
|
scan_for_newline (GDataInputStream *stream,
|
2007-11-29 08:17:59 +01:00
|
|
|
gsize *checked_out,
|
|
|
|
gboolean *last_saw_cr_out,
|
|
|
|
int *newline_len_out)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GBufferedInputStream *bstream;
|
|
|
|
GDataInputStreamPrivate *priv;
|
2007-11-27 14:44:48 +01:00
|
|
|
const char *buffer;
|
2007-11-26 17:13:05 +01:00
|
|
|
gsize start, end, peeked;
|
|
|
|
int i;
|
|
|
|
gssize found_pos;
|
|
|
|
int newline_len;
|
|
|
|
gsize available, checked;
|
|
|
|
gboolean last_saw_cr;
|
|
|
|
|
|
|
|
priv = stream->priv;
|
|
|
|
|
|
|
|
bstream = G_BUFFERED_INPUT_STREAM (stream);
|
|
|
|
|
|
|
|
checked = *checked_out;
|
|
|
|
last_saw_cr = *last_saw_cr_out;
|
|
|
|
found_pos = -1;
|
|
|
|
newline_len = 0;
|
|
|
|
|
2007-11-27 14:44:48 +01:00
|
|
|
start = checked;
|
2007-12-05 12:05:49 +01:00
|
|
|
buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
|
2007-11-27 14:44:48 +01:00
|
|
|
end = available;
|
|
|
|
peeked = end - start;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2007-11-27 14:44:48 +01:00
|
|
|
for (i = 0; checked < available && i < peeked; i++)
|
|
|
|
{
|
|
|
|
switch (priv->newline_type)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
case G_DATA_STREAM_NEWLINE_TYPE_LF:
|
|
|
|
if (buffer[i] == 10)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
found_pos = start + i;
|
|
|
|
newline_len = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_NEWLINE_TYPE_CR:
|
|
|
|
if (buffer[i] == 13)
|
|
|
|
{
|
|
|
|
found_pos = start + i;
|
|
|
|
newline_len = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
|
|
|
|
if (last_saw_cr && buffer[i] == 10)
|
|
|
|
{
|
|
|
|
found_pos = start + i - 1;
|
|
|
|
newline_len = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case G_DATA_STREAM_NEWLINE_TYPE_ANY:
|
|
|
|
if (buffer[i] == 10) /* LF */
|
|
|
|
{
|
|
|
|
if (last_saw_cr)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
/* CR LF */
|
2007-11-26 17:13:05 +01:00
|
|
|
found_pos = start + i - 1;
|
|
|
|
newline_len = 2;
|
|
|
|
}
|
2007-11-27 14:44:48 +01:00
|
|
|
else
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
/* LF */
|
|
|
|
found_pos = start + i;
|
2007-11-26 17:13:05 +01:00
|
|
|
newline_len = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 14:44:48 +01:00
|
|
|
else if (last_saw_cr)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
/* Last was cr, this is not LF, end is CR */
|
|
|
|
found_pos = start + i - 1;
|
|
|
|
newline_len = 1;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
2007-11-27 14:44:48 +01:00
|
|
|
/* Don't check for CR here, instead look at last_saw_cr on next byte */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_saw_cr = (buffer[i] == 13);
|
|
|
|
|
|
|
|
if (found_pos != -1)
|
|
|
|
{
|
|
|
|
*newline_len_out = newline_len;
|
|
|
|
return found_pos;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 14:44:48 +01:00
|
|
|
checked = end;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
*checked_out = checked;
|
|
|
|
*last_saw_cr_out = last_saw_cr;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_line:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2011-06-16 19:14:44 +02:00
|
|
|
* Reads a line from the data input stream. Note that no encoding
|
|
|
|
* checks or conversion is performed; the input is not guaranteed to
|
|
|
|
* be UTF-8, and may in fact have embedded NUL characters.
|
2009-03-01 18:12:58 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
2009-03-01 18:12:58 +01:00
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
|
|
|
*
|
2011-06-16 19:14:44 +02:00
|
|
|
* Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
|
|
|
|
* NUL terminated byte array with the line that was read in (without
|
|
|
|
* the newlines). Set @length to a #gsize to get the length of the
|
|
|
|
* read line. On an error, it will return %NULL and @error will be
|
|
|
|
* set. If there's no content to read, it will still return %NULL,
|
|
|
|
* but @error won't be set.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
char *
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_line (GDataInputStream *stream,
|
|
|
|
gsize *length,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GBufferedInputStream *bstream;
|
|
|
|
gsize checked;
|
|
|
|
gboolean last_saw_cr;
|
|
|
|
gssize found_pos;
|
|
|
|
gssize res;
|
|
|
|
int newline_len;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
|
|
|
|
|
|
|
|
bstream = G_BUFFERED_INPUT_STREAM (stream);
|
|
|
|
|
|
|
|
newline_len = 0;
|
|
|
|
checked = 0;
|
|
|
|
last_saw_cr = FALSE;
|
|
|
|
|
|
|
|
while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
|
|
|
|
{
|
|
|
|
if (g_buffered_input_stream_get_available (bstream) ==
|
|
|
|
g_buffered_input_stream_get_buffer_size (bstream))
|
|
|
|
g_buffered_input_stream_set_buffer_size (bstream,
|
|
|
|
2 * g_buffered_input_stream_get_buffer_size (bstream));
|
|
|
|
|
|
|
|
res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
|
|
|
|
if (res < 0)
|
|
|
|
return NULL;
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
/* End of stream */
|
|
|
|
if (g_buffered_input_stream_get_available (bstream) == 0)
|
|
|
|
{
|
|
|
|
if (length)
|
|
|
|
*length = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
found_pos = checked;
|
|
|
|
newline_len = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
line = g_malloc (found_pos + newline_len + 1);
|
|
|
|
|
|
|
|
res = g_input_stream_read (G_INPUT_STREAM (stream),
|
|
|
|
line,
|
|
|
|
found_pos + newline_len,
|
|
|
|
NULL, NULL);
|
|
|
|
if (length)
|
|
|
|
*length = (gsize)found_pos;
|
2007-12-10 15:07:42 +01:00
|
|
|
g_warn_if_fail (res == found_pos + newline_len);
|
2008-12-01 20:02:58 +01:00
|
|
|
line[found_pos] = 0;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2011-06-16 20:00:36 +02:00
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_line_utf8:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
|
|
|
* Reads a UTF-8 encoded line from the data input stream.
|
|
|
|
*
|
|
|
|
* If @cancellable is not %NULL, then the operation can be cancelled by
|
|
|
|
* triggering the cancellable object from another thread. If the operation
|
|
|
|
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): a NUL terminated UTF-8 string with the
|
|
|
|
* line that was read in (without the newlines). Set @length to a
|
|
|
|
* #gsize to get the length of the read line. On an error, it will
|
|
|
|
* return %NULL and @error will be set. For UTF-8 conversion errors,
|
|
|
|
* the set error domain is %G_CONVERT_ERROR. If there's no content to
|
|
|
|
* read, it will still return %NULL, but @error won't be set.
|
2011-06-17 14:52:05 +02:00
|
|
|
*
|
|
|
|
* Since: 2.30
|
2011-06-16 20:00:36 +02:00
|
|
|
**/
|
|
|
|
char *
|
|
|
|
g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
|
|
|
|
gsize *length,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
res = g_data_input_stream_read_line (stream, length, cancellable, error);
|
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!g_utf8_validate (res, -1, NULL))
|
|
|
|
{
|
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
|
|
|
g_free (res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-11-27 14:44:48 +01:00
|
|
|
static gssize
|
|
|
|
scan_for_chars (GDataInputStream *stream,
|
2007-11-29 08:17:59 +01:00
|
|
|
gsize *checked_out,
|
2010-03-23 07:12:01 +01:00
|
|
|
const char *stop_chars,
|
|
|
|
gssize stop_chars_len)
|
2007-11-27 14:44:48 +01:00
|
|
|
{
|
|
|
|
GBufferedInputStream *bstream;
|
|
|
|
const char *buffer;
|
|
|
|
gsize start, end, peeked;
|
|
|
|
int i;
|
|
|
|
gsize available, checked;
|
|
|
|
const char *stop_char;
|
2010-03-23 07:12:01 +01:00
|
|
|
const char *stop_end;
|
2007-11-27 14:44:48 +01:00
|
|
|
|
|
|
|
bstream = G_BUFFERED_INPUT_STREAM (stream);
|
2010-03-23 07:12:01 +01:00
|
|
|
stop_end = stop_chars + stop_chars_len;
|
2007-11-27 14:44:48 +01:00
|
|
|
|
|
|
|
checked = *checked_out;
|
2009-09-09 06:18:23 +02:00
|
|
|
|
2007-11-27 14:44:48 +01:00
|
|
|
start = checked;
|
2007-12-05 12:05:49 +01:00
|
|
|
buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
|
2007-11-27 14:44:48 +01:00
|
|
|
end = available;
|
|
|
|
peeked = end - start;
|
|
|
|
|
|
|
|
for (i = 0; checked < available && i < peeked; i++)
|
|
|
|
{
|
2010-03-23 07:12:01 +01:00
|
|
|
for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
|
2007-11-27 14:44:48 +01:00
|
|
|
{
|
|
|
|
if (buffer[i] == *stop_char)
|
|
|
|
return (start + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checked = end;
|
|
|
|
|
|
|
|
*checked_out = checked;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_until:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2007-11-27 14:44:48 +01:00
|
|
|
* @stop_chars: characters to terminate the read.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2007-11-26 17:13:05 +01:00
|
|
|
* @error: #GError for error reporting.
|
2009-03-01 18:12:58 +01:00
|
|
|
*
|
|
|
|
* Reads a string from the data input stream, up to the first
|
2009-02-06 15:08:19 +01:00
|
|
|
* occurrence of any of the stop characters.
|
2007-11-28 05:29:02 +01:00
|
|
|
*
|
2010-03-23 16:01:53 +01:00
|
|
|
* Note that, in contrast to g_data_input_stream_read_until_async(),
|
|
|
|
* this function consumes the stop character that it finds.
|
|
|
|
*
|
2010-03-23 07:12:01 +01:00
|
|
|
* Don't use this function in new code. Its functionality is
|
|
|
|
* inconsistent with g_data_input_stream_read_until_async(). Both
|
|
|
|
* functions will be marked as deprecated in a future release. Use
|
|
|
|
* g_data_input_stream_read_upto() instead, but note that that function
|
|
|
|
* does not consume the stop character.
|
|
|
|
*
|
2010-12-21 18:00:48 +01:00
|
|
|
* Returns: (transfer full): a string with the data that was read
|
|
|
|
* before encountering any of the stop characters. Set @length to
|
|
|
|
* a #gsize to get the length of the string. This function will
|
|
|
|
* return %NULL on an error.
|
2009-03-01 18:12:58 +01:00
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
char *
|
2007-11-29 08:17:59 +01:00
|
|
|
g_data_input_stream_read_until (GDataInputStream *stream,
|
|
|
|
const gchar *stop_chars,
|
|
|
|
gsize *length,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-11-27 14:44:48 +01:00
|
|
|
GBufferedInputStream *bstream;
|
2010-03-23 07:12:01 +01:00
|
|
|
gchar *result;
|
2007-11-27 14:44:48 +01:00
|
|
|
|
|
|
|
bstream = G_BUFFERED_INPUT_STREAM (stream);
|
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
result = g_data_input_stream_read_upto (stream, stop_chars, -1,
|
|
|
|
length, cancellable, error);
|
2007-11-27 14:44:48 +01:00
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
/* If we're not at end of stream then we have a stop_char to consume. */
|
|
|
|
if (result != NULL && g_buffered_input_stream_get_available (bstream) > 0)
|
2007-11-27 14:44:48 +01:00
|
|
|
{
|
2010-03-23 07:12:01 +01:00
|
|
|
gsize res;
|
|
|
|
gchar b;
|
2007-11-27 14:44:48 +01:00
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
|
|
|
|
g_assert (res == 1);
|
2007-11-27 14:44:48 +01:00
|
|
|
}
|
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
return result;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2009-01-28 17:39:39 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gboolean last_saw_cr;
|
|
|
|
gsize checked;
|
|
|
|
|
|
|
|
gchar *stop_chars;
|
2010-03-23 07:12:01 +01:00
|
|
|
gssize stop_chars_len;
|
2009-01-28 17:39:39 +01:00
|
|
|
gsize length;
|
|
|
|
} GDataInputStreamReadData;
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:49:59 +02:00
|
|
|
g_data_input_stream_read_complete (GTask *task,
|
|
|
|
gsize read_length,
|
|
|
|
gsize skip_length)
|
2009-01-28 17:39:39 +01:00
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
GDataInputStreamReadData *data = g_task_get_task_data (task);
|
|
|
|
GInputStream *stream = g_task_get_source_object (task);
|
|
|
|
char *line = NULL;
|
|
|
|
|
2009-01-28 17:39:39 +01:00
|
|
|
if (read_length || skip_length)
|
|
|
|
{
|
|
|
|
gssize bytes;
|
|
|
|
|
|
|
|
data->length = read_length;
|
2012-08-02 21:49:59 +02:00
|
|
|
line = g_malloc (read_length + 1);
|
|
|
|
line[read_length] = '\0';
|
2009-01-28 17:39:39 +01:00
|
|
|
|
|
|
|
/* we already checked the buffer. this shouldn't fail. */
|
2012-08-02 21:49:59 +02:00
|
|
|
bytes = g_input_stream_read (stream, line, read_length, NULL, NULL);
|
2009-01-28 17:39:39 +01:00
|
|
|
g_assert_cmpint (bytes, ==, read_length);
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
bytes = g_input_stream_skip (stream, skip_length, NULL, NULL);
|
2009-01-28 17:39:39 +01:00
|
|
|
g_assert_cmpint (bytes, ==, skip_length);
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
g_task_return_pointer (task, line, g_free);
|
|
|
|
g_object_unref (task);
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_data_input_stream_read_line_ready (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
GTask *task = user_data;
|
|
|
|
GDataInputStreamReadData *data = g_task_get_task_data (task);
|
|
|
|
GBufferedInputStream *buffer = g_task_get_source_object (task);
|
2009-01-28 17:39:39 +01:00
|
|
|
gssize found_pos;
|
|
|
|
gint newline_len;
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
/* this is a callback. finish the async call. */
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
gssize bytes;
|
|
|
|
|
|
|
|
bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
|
|
|
|
|
|
|
|
if (bytes <= 0)
|
|
|
|
{
|
|
|
|
if (bytes < 0)
|
|
|
|
/* stream error. */
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
g_task_return_error (task, error);
|
|
|
|
g_object_unref (task);
|
|
|
|
return;
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
g_data_input_stream_read_complete (task, data->checked, 0);
|
2009-01-28 17:39:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only proceed if we got more bytes... */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->stop_chars)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
found_pos = scan_for_chars (G_DATA_INPUT_STREAM (buffer),
|
2009-01-28 17:39:39 +01:00
|
|
|
&data->checked,
|
2010-03-23 07:12:01 +01:00
|
|
|
data->stop_chars,
|
|
|
|
data->stop_chars_len);
|
2009-01-28 17:39:39 +01:00
|
|
|
newline_len = 0;
|
|
|
|
}
|
|
|
|
else
|
2012-08-02 21:49:59 +02:00
|
|
|
found_pos = scan_for_newline (G_DATA_INPUT_STREAM (buffer), &data->checked,
|
2009-01-28 17:39:39 +01:00
|
|
|
&data->last_saw_cr, &newline_len);
|
|
|
|
|
|
|
|
if (found_pos == -1)
|
|
|
|
/* didn't find a full line; need to buffer some more bytes */
|
|
|
|
{
|
|
|
|
gsize size;
|
|
|
|
|
|
|
|
size = g_buffered_input_stream_get_buffer_size (buffer);
|
|
|
|
|
|
|
|
if (g_buffered_input_stream_get_available (buffer) == size)
|
|
|
|
/* need to grow the buffer */
|
|
|
|
g_buffered_input_stream_set_buffer_size (buffer, size * 2);
|
|
|
|
|
|
|
|
/* try again */
|
2012-08-02 21:49:59 +02:00
|
|
|
g_buffered_input_stream_fill_async (buffer, -1,
|
|
|
|
g_task_get_priority (task),
|
|
|
|
g_task_get_cancellable (task),
|
2009-01-28 17:39:39 +01:00
|
|
|
g_data_input_stream_read_line_ready,
|
|
|
|
user_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* read the line and the EOL. no error is possible. */
|
2012-08-02 21:49:59 +02:00
|
|
|
g_data_input_stream_read_complete (task, found_pos, newline_len);
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_data_input_stream_read_data_free (gpointer user_data)
|
|
|
|
{
|
|
|
|
GDataInputStreamReadData *data = user_data;
|
|
|
|
|
|
|
|
g_free (data->stop_chars);
|
|
|
|
g_slice_free (GDataInputStreamReadData, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_data_input_stream_read_async (GDataInputStream *stream,
|
|
|
|
const gchar *stop_chars,
|
2010-03-23 07:12:01 +01:00
|
|
|
gssize stop_chars_len,
|
2009-01-28 17:39:39 +01:00
|
|
|
gint io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
2012-08-02 21:49:59 +02:00
|
|
|
gpointer user_data)
|
2009-01-28 17:39:39 +01:00
|
|
|
{
|
|
|
|
GDataInputStreamReadData *data;
|
2012-08-02 21:49:59 +02:00
|
|
|
GTask *task;
|
2009-01-28 17:39:39 +01:00
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
data = g_slice_new0 (GDataInputStreamReadData);
|
2010-03-23 07:12:01 +01:00
|
|
|
if (stop_chars_len == -1)
|
|
|
|
stop_chars_len = strlen (stop_chars);
|
|
|
|
data->stop_chars = g_memdup (stop_chars, stop_chars_len);
|
|
|
|
data->stop_chars_len = stop_chars_len;
|
2009-01-28 17:39:39 +01:00
|
|
|
data->last_saw_cr = FALSE;
|
2012-08-02 21:49:59 +02:00
|
|
|
|
|
|
|
task = g_task_new (stream, cancellable, callback, user_data);
|
|
|
|
g_task_set_task_data (task, data, g_data_input_stream_read_data_free);
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
|
|
|
|
g_data_input_stream_read_line_ready (NULL, NULL, task);
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
g_data_input_stream_read_finish (GDataInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gsize *length,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
GTask *task = G_TASK (result);
|
2009-01-28 17:39:39 +01:00
|
|
|
gchar *line;
|
|
|
|
|
2012-08-02 21:49:59 +02:00
|
|
|
line = g_task_propagate_pointer (task, error);
|
2009-01-28 17:39:39 +01:00
|
|
|
|
|
|
|
if (length && line)
|
2012-08-02 21:49:59 +02:00
|
|
|
{
|
|
|
|
GDataInputStreamReadData *data = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
*length = data->length;
|
|
|
|
}
|
2009-01-28 17:39:39 +01:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_line_async:
|
|
|
|
* @stream: a given #GDataInputStream.
|
2009-03-01 18:12:58 +01:00
|
|
|
* @io_priority: the <link linkend="io-priority">I/O priority</link>
|
|
|
|
* of the request.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
|
|
|
* @callback: (scope async): callback to call when the request is satisfied.
|
|
|
|
* @user_data: (closure): the data to pass to callback function.
|
2009-01-28 17:39:39 +01:00
|
|
|
*
|
|
|
|
* The asynchronous version of g_data_input_stream_read_line(). It is
|
|
|
|
* an error to have two outstanding calls to this function.
|
2009-02-17 07:32:44 +01:00
|
|
|
*
|
2009-03-01 18:12:58 +01:00
|
|
|
* When the operation is finished, @callback will be called. You
|
|
|
|
* can then call g_data_input_stream_read_line_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
2009-02-17 07:32:44 +01:00
|
|
|
* Since: 2.20
|
|
|
|
*/
|
2009-01-28 17:39:39 +01:00
|
|
|
void
|
|
|
|
g_data_input_stream_read_line_async (GDataInputStream *stream,
|
|
|
|
gint io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
g_data_input_stream_read_async (stream, NULL, 0, io_priority,
|
2012-08-02 21:49:59 +02:00
|
|
|
cancellable, callback, user_data);
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_until_async:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @stop_chars: characters to terminate the read.
|
2009-03-01 18:12:58 +01:00
|
|
|
* @io_priority: the <link linkend="io-priority">I/O priority</link>
|
|
|
|
* of the request.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
|
|
|
* @callback: (scope async): callback to call when the request is satisfied.
|
|
|
|
* @user_data: (closure): the data to pass to callback function.
|
2009-01-28 17:39:39 +01:00
|
|
|
*
|
2009-02-17 07:32:44 +01:00
|
|
|
* The asynchronous version of g_data_input_stream_read_until().
|
|
|
|
* It is an error to have two outstanding calls to this function.
|
|
|
|
*
|
2010-03-23 16:01:53 +01:00
|
|
|
* Note that, in contrast to g_data_input_stream_read_until(),
|
|
|
|
* this function does not consume the stop character that it finds. You
|
|
|
|
* must read it for yourself.
|
|
|
|
*
|
2009-03-01 18:12:58 +01:00
|
|
|
* When the operation is finished, @callback will be called. You
|
|
|
|
* can then call g_data_input_stream_read_until_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
2010-03-23 07:12:01 +01:00
|
|
|
* Don't use this function in new code. Its functionality is
|
|
|
|
* inconsistent with g_data_input_stream_read_until(). Both functions
|
|
|
|
* will be marked as deprecated in a future release. Use
|
|
|
|
* g_data_input_stream_read_upto_async() instead.
|
|
|
|
*
|
2009-02-17 07:32:44 +01:00
|
|
|
* Since: 2.20
|
|
|
|
*/
|
2009-01-28 17:39:39 +01:00
|
|
|
void
|
|
|
|
g_data_input_stream_read_until_async (GDataInputStream *stream,
|
|
|
|
const gchar *stop_chars,
|
|
|
|
gint io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
g_return_if_fail (stop_chars != NULL);
|
|
|
|
|
2010-03-23 07:12:01 +01:00
|
|
|
g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
|
2012-08-02 21:49:59 +02:00
|
|
|
cancellable, callback, user_data);
|
2009-01-28 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_line_finish:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
2009-01-28 17:39:39 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
|
|
|
* Finish an asynchronous call started by
|
2011-06-16 19:14:44 +02:00
|
|
|
* g_data_input_stream_read_line_async(). Note the warning about
|
|
|
|
* string encoding in g_data_input_stream_read_line() applies here as
|
|
|
|
* well.
|
2009-02-17 07:32:44 +01:00
|
|
|
*
|
2011-06-16 19:14:44 +02:00
|
|
|
* Returns: (transfer full) (array zero-terminated=1) (element-type guint8): a
|
|
|
|
* NUL-terminated byte array with the line that was read in
|
|
|
|
* (without the newlines). Set @length to a #gsize to get the
|
|
|
|
* length of the read line. On an error, it will return %NULL and
|
|
|
|
* @error will be set. If there's no content to read, it will
|
|
|
|
* still return %NULL, but @error won't be set.
|
2009-03-01 18:12:58 +01:00
|
|
|
*
|
2009-09-12 18:49:01 +02:00
|
|
|
* Since: 2.20
|
2009-02-17 07:32:44 +01:00
|
|
|
*/
|
2009-01-28 17:39:39 +01:00
|
|
|
gchar *
|
|
|
|
g_data_input_stream_read_line_finish (GDataInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gsize *length,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
|
2009-01-28 17:39:39 +01:00
|
|
|
|
|
|
|
return g_data_input_stream_read_finish (stream, result, length, error);
|
|
|
|
}
|
|
|
|
|
2011-06-16 20:00:36 +02:00
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_line_finish_utf8:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback.
|
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
|
|
|
* Finish an asynchronous call started by
|
|
|
|
* g_data_input_stream_read_line_async().
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): a string with the line that was read in
|
|
|
|
* (without the newlines). Set @length to a #gsize to get the length
|
|
|
|
* of the read line. On an error, it will return %NULL and @error
|
|
|
|
* will be set. For UTF-8 conversion errors, the set error domain is
|
|
|
|
* %G_CONVERT_ERROR. If there's no content to read, it will still
|
|
|
|
* return %NULL, but @error won't be set.
|
|
|
|
*
|
2011-06-17 14:52:05 +02:00
|
|
|
* Since: 2.30
|
2011-06-16 20:00:36 +02:00
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gsize *length,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar *res;
|
|
|
|
|
|
|
|
res = g_data_input_stream_read_line_finish (stream, result, length, error);
|
2012-02-13 16:41:20 +01:00
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-16 20:00:36 +02:00
|
|
|
if (!g_utf8_validate (res, -1, NULL))
|
|
|
|
{
|
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR,
|
|
|
|
G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
|
|
|
g_free (res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-01-28 17:39:39 +01:00
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_until_finish:
|
|
|
|
* @stream: a given #GDataInputStream.
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in.
|
2009-01-28 17:39:39 +01:00
|
|
|
* @error: #GError for error reporting.
|
|
|
|
*
|
|
|
|
* Finish an asynchronous call started by
|
|
|
|
* g_data_input_stream_read_until_async().
|
2009-02-17 07:32:44 +01:00
|
|
|
*
|
|
|
|
* Since: 2.20
|
2009-03-01 18:12:58 +01:00
|
|
|
*
|
2010-12-21 18:00:48 +01:00
|
|
|
* Returns: (transfer full): a string with the data that was read
|
|
|
|
* before encountering any of the stop characters. Set @length to
|
|
|
|
* a #gsize to get the length of the string. This function will
|
|
|
|
* return %NULL on an error.
|
2009-02-17 07:32:44 +01:00
|
|
|
*/
|
2009-01-28 17:39:39 +01:00
|
|
|
gchar *
|
|
|
|
g_data_input_stream_read_until_finish (GDataInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gsize *length,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
|
2009-01-28 17:39:39 +01:00
|
|
|
|
|
|
|
return g_data_input_stream_read_finish (stream, result, length, error);
|
|
|
|
}
|
2010-03-23 07:12:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_upto:
|
|
|
|
* @stream: a #GDataInputStream
|
|
|
|
* @stop_chars: characters to terminate the read
|
|
|
|
* @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
|
|
|
|
* nul-terminated
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in
|
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
|
2010-03-23 07:12:01 +01:00
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Reads a string from the data input stream, up to the first
|
|
|
|
* occurrence of any of the stop characters.
|
|
|
|
*
|
|
|
|
* In contrast to g_data_input_stream_read_until(), this function
|
|
|
|
* does <emphasis>not</emphasis> consume the stop character. You have
|
|
|
|
* to use g_data_input_stream_read_byte() to get it before calling
|
|
|
|
* g_data_input_stream_read_upto() again.
|
|
|
|
*
|
|
|
|
* Note that @stop_chars may contain '\0' if @stop_chars_len is
|
|
|
|
* specified.
|
|
|
|
*
|
2010-12-21 18:00:48 +01:00
|
|
|
* Returns: (transfer full): a string with the data that was read
|
|
|
|
* before encountering any of the stop characters. Set @length to
|
|
|
|
* a #gsize to get the length of the string. This function will
|
|
|
|
* return %NULL on an error
|
2010-03-23 07:12:01 +01:00
|
|
|
*
|
2011-06-07 23:19:24 +02:00
|
|
|
* Since: 2.26
|
2010-03-23 07:12:01 +01:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
g_data_input_stream_read_upto (GDataInputStream *stream,
|
|
|
|
const gchar *stop_chars,
|
|
|
|
gssize stop_chars_len,
|
|
|
|
gsize *length,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GBufferedInputStream *bstream;
|
|
|
|
gsize checked;
|
|
|
|
gssize found_pos;
|
|
|
|
gssize res;
|
|
|
|
char *data_until;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
|
|
|
|
|
|
|
|
if (stop_chars_len < 0)
|
|
|
|
stop_chars_len = strlen (stop_chars);
|
|
|
|
|
|
|
|
bstream = G_BUFFERED_INPUT_STREAM (stream);
|
|
|
|
|
|
|
|
checked = 0;
|
|
|
|
|
|
|
|
while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
|
|
|
|
{
|
|
|
|
if (g_buffered_input_stream_get_available (bstream) ==
|
|
|
|
g_buffered_input_stream_get_buffer_size (bstream))
|
|
|
|
g_buffered_input_stream_set_buffer_size (bstream,
|
|
|
|
2 * g_buffered_input_stream_get_buffer_size (bstream));
|
|
|
|
|
|
|
|
res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
|
|
|
|
if (res < 0)
|
|
|
|
return NULL;
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
/* End of stream */
|
|
|
|
if (g_buffered_input_stream_get_available (bstream) == 0)
|
|
|
|
{
|
|
|
|
if (length)
|
|
|
|
*length = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
found_pos = checked;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data_until = g_malloc (found_pos + 1);
|
|
|
|
|
|
|
|
res = g_input_stream_read (G_INPUT_STREAM (stream),
|
|
|
|
data_until,
|
|
|
|
found_pos,
|
|
|
|
NULL, NULL);
|
|
|
|
if (length)
|
|
|
|
*length = (gsize)found_pos;
|
|
|
|
g_warn_if_fail (res == found_pos);
|
|
|
|
data_until[found_pos] = 0;
|
|
|
|
|
|
|
|
return data_until;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_upto_async:
|
|
|
|
* @stream: a #GDataInputStream
|
|
|
|
* @stop_chars: characters to terminate the read
|
|
|
|
* @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
|
|
|
|
* nul-terminated
|
2010-10-25 14:42:36 +02:00
|
|
|
* @io_priority: the <link linkend="io-priority">I/O priority</link>
|
|
|
|
* of the request.
|
2010-12-21 18:00:48 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
|
|
|
|
* @callback: (scope async): callback to call when the request is satisfied
|
|
|
|
* @user_data: (closure): the data to pass to callback function
|
2010-03-23 07:12:01 +01:00
|
|
|
*
|
|
|
|
* The asynchronous version of g_data_input_stream_read_upto().
|
|
|
|
* It is an error to have two outstanding calls to this function.
|
|
|
|
*
|
|
|
|
* In contrast to g_data_input_stream_read_until(), this function
|
|
|
|
* does <emphasis>not</emphasis> consume the stop character. You have
|
|
|
|
* to use g_data_input_stream_read_byte() to get it before calling
|
|
|
|
* g_data_input_stream_read_upto() again.
|
|
|
|
*
|
|
|
|
* Note that @stop_chars may contain '\0' if @stop_chars_len is
|
|
|
|
* specified.
|
|
|
|
*
|
|
|
|
* When the operation is finished, @callback will be called. You
|
|
|
|
* can then call g_data_input_stream_read_upto_finish() to get
|
|
|
|
* the result of the operation.
|
|
|
|
*
|
2011-06-07 23:19:24 +02:00
|
|
|
* Since: 2.26
|
2010-03-23 07:12:01 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_data_input_stream_read_upto_async (GDataInputStream *stream,
|
|
|
|
const gchar *stop_chars,
|
|
|
|
gssize stop_chars_len,
|
|
|
|
gint io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
g_return_if_fail (stop_chars != NULL);
|
|
|
|
|
|
|
|
g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
|
2012-08-02 21:49:59 +02:00
|
|
|
cancellable, callback, user_data);
|
2010-03-23 07:12:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_data_input_stream_read_upto_finish:
|
|
|
|
* @stream: a #GDataInputStream
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
2010-12-21 18:00:48 +01:00
|
|
|
* @length: (out): a #gsize to get the length of the data read in
|
2010-03-23 07:12:01 +01:00
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish an asynchronous call started by
|
|
|
|
* g_data_input_stream_read_upto_async().
|
|
|
|
*
|
|
|
|
* Note that this function does <emphasis>not</emphasis> consume the
|
|
|
|
* stop character. You have to use g_data_input_stream_read_byte() to
|
|
|
|
* get it before calling g_data_input_stream_read_upto_async() again.
|
|
|
|
*
|
2010-12-21 18:00:48 +01:00
|
|
|
* Returns: (transfer full): a string with the data that was read
|
|
|
|
* before encountering any of the stop characters. Set @length to
|
|
|
|
* a #gsize to get the length of the string. This function will
|
|
|
|
* return %NULL on an error.
|
2010-03-23 07:12:01 +01:00
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
g_data_input_stream_read_upto_finish (GDataInputStream *stream,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gsize *length,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:49:59 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
|
2010-03-23 07:12:01 +01:00
|
|
|
|
|
|
|
return g_data_input_stream_read_finish (stream, result, length, error);
|
|
|
|
}
|