Blame gio/gdatainputstream.c

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