Blame glib/giochannel.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * giochannel.c: IO Channel abstraction
Packit ae235b
 * Copyright 1998 Owen Taylor
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 Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* 
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include "giochannel.h"
Packit ae235b
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:iochannels
Packit ae235b
 * @title: IO Channels
Packit ae235b
 * @short_description: portable support for using files, pipes and sockets
Packit ae235b
 * @see_also: g_io_add_watch(), g_io_add_watch_full(), g_source_remove(),
Packit ae235b
 *     #GMainLoop
Packit ae235b
 *
Packit ae235b
 * The #GIOChannel data type aims to provide a portable method for
Packit ae235b
 * using file descriptors, pipes, and sockets, and integrating them
Packit ae235b
 * into the [main event loop][glib-The-Main-Event-Loop]. Currently,
Packit ae235b
 * full support is available on UNIX platforms, support for Windows
Packit ae235b
 * is only partially complete.
Packit ae235b
 *
Packit ae235b
 * To create a new #GIOChannel on UNIX systems use
Packit ae235b
 * g_io_channel_unix_new(). This works for plain file descriptors,
Packit ae235b
 * pipes and sockets. Alternatively, a channel can be created for a
Packit ae235b
 * file in a system independent manner using g_io_channel_new_file().
Packit ae235b
 *
Packit ae235b
 * Once a #GIOChannel has been created, it can be used in a generic
Packit ae235b
 * manner with the functions g_io_channel_read_chars(),
Packit ae235b
 * g_io_channel_write_chars(), g_io_channel_seek_position(), and
Packit ae235b
 * g_io_channel_shutdown().
Packit ae235b
 *
Packit ae235b
 * To add a #GIOChannel to the [main event loop][glib-The-Main-Event-Loop],
Packit ae235b
 * use g_io_add_watch() or g_io_add_watch_full(). Here you specify which
Packit ae235b
 * events you are interested in on the #GIOChannel, and provide a
Packit ae235b
 * function to be called whenever these events occur.
Packit ae235b
 *
Packit ae235b
 * #GIOChannel instances are created with an initial reference count of 1.
Packit ae235b
 * g_io_channel_ref() and g_io_channel_unref() can be used to
Packit ae235b
 * increment or decrement the reference count respectively. When the
Packit ae235b
 * reference count falls to 0, the #GIOChannel is freed. (Though it
Packit ae235b
 * isn't closed automatically, unless it was created using
Packit ae235b
 * g_io_channel_new_file().) Using g_io_add_watch() or
Packit ae235b
 * g_io_add_watch_full() increments a channel's reference count.
Packit ae235b
 *
Packit ae235b
 * The new functions g_io_channel_read_chars(),
Packit ae235b
 * g_io_channel_read_line(), g_io_channel_read_line_string(),
Packit ae235b
 * g_io_channel_read_to_end(), g_io_channel_write_chars(),
Packit ae235b
 * g_io_channel_seek_position(), and g_io_channel_flush() should not be
Packit ae235b
 * mixed with the deprecated functions g_io_channel_read(),
Packit ae235b
 * g_io_channel_write(), and g_io_channel_seek() on the same channel.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GIOChannel:
Packit ae235b
 *
Packit ae235b
 * A data structure representing an IO Channel. The fields should be
Packit ae235b
 * considered private and should only be accessed with the following
Packit ae235b
 * functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GIOFuncs:
Packit ae235b
 * @io_read: reads raw bytes from the channel.  This is called from
Packit ae235b
 *           various functions such as g_io_channel_read_chars() to
Packit ae235b
 *           read raw bytes from the channel.  Encoding and buffering
Packit ae235b
 *           issues are dealt with at a higher level.
Packit ae235b
 * @io_write: writes raw bytes to the channel.  This is called from
Packit ae235b
 *            various functions such as g_io_channel_write_chars() to
Packit ae235b
 *            write raw bytes to the channel.  Encoding and buffering
Packit ae235b
 *            issues are dealt with at a higher level.
Packit ae235b
 * @io_seek: (optional) seeks the channel.  This is called from
Packit ae235b
 *           g_io_channel_seek() on channels that support it.
Packit ae235b
 * @io_close: closes the channel.  This is called from
Packit ae235b
 *            g_io_channel_close() after flushing the buffers.
Packit ae235b
 * @io_create_watch: creates a watch on the channel.  This call
Packit ae235b
 *                   corresponds directly to g_io_create_watch().
Packit ae235b
 * @io_free: called from g_io_channel_unref() when the channel needs to
Packit ae235b
 *           be freed.  This function must free the memory associated
Packit ae235b
 *           with the channel, including freeing the #GIOChannel
Packit ae235b
 *           structure itself.  The channel buffers have been flushed
Packit ae235b
 *           and possibly @io_close has been called by the time this
Packit ae235b
 *           function is called.
Packit ae235b
 * @io_set_flags: sets the #GIOFlags on the channel.  This is called
Packit ae235b
 *                from g_io_channel_set_flags() with all flags except
Packit ae235b
 *                for %G_IO_FLAG_APPEND and %G_IO_FLAG_NONBLOCK masked
Packit ae235b
 *                out.
Packit ae235b
 * @io_get_flags: gets the #GIOFlags for the channel.  This function
Packit ae235b
 *                need only return the %G_IO_FLAG_APPEND and
Packit ae235b
 *                %G_IO_FLAG_NONBLOCK flags; g_io_channel_get_flags()
Packit ae235b
 *                automatically adds the others as appropriate.
Packit ae235b
 *
Packit ae235b
 * A table of functions used to handle different types of #GIOChannel
Packit ae235b
 * in a generic way.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GIOStatus:
Packit ae235b
 * @G_IO_STATUS_ERROR: An error occurred.
Packit ae235b
 * @G_IO_STATUS_NORMAL: Success.
Packit ae235b
 * @G_IO_STATUS_EOF: End of file.
Packit ae235b
 * @G_IO_STATUS_AGAIN: Resource temporarily unavailable.
Packit ae235b
 *
Packit ae235b
 * Stati returned by most of the #GIOFuncs functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GIOError:
Packit ae235b
 * @G_IO_ERROR_NONE: no error
Packit ae235b
 * @G_IO_ERROR_AGAIN: an EAGAIN error occurred
Packit ae235b
 * @G_IO_ERROR_INVAL: an EINVAL error occurred
Packit ae235b
 * @G_IO_ERROR_UNKNOWN: another error occurred
Packit ae235b
 *
Packit ae235b
 * #GIOError is only used by the deprecated functions
Packit ae235b
 * g_io_channel_read(), g_io_channel_write(), and g_io_channel_seek().
Packit ae235b
 **/
Packit ae235b
Packit ae235b
#define G_IO_NICE_BUF_SIZE	1024
Packit ae235b
Packit ae235b
/* This needs to be as wide as the largest character in any possible encoding */
Packit ae235b
#define MAX_CHAR_SIZE		10
Packit ae235b
Packit ae235b
/* Some simplifying macros, which reduce the need to worry whether the
Packit ae235b
 * buffers have been allocated. These also make USE_BUF () an lvalue,
Packit ae235b
 * which is used in g_io_channel_read_to_end ().
Packit ae235b
 */
Packit ae235b
#define USE_BUF(channel)	((channel)->encoding ? (channel)->encoded_read_buf \
Packit ae235b
				 : (channel)->read_buf)
Packit ae235b
#define BUF_LEN(string)		((string) ? (string)->len : 0)
Packit ae235b
Packit ae235b
static GIOError		g_io_error_get_from_g_error	(GIOStatus    status,
Packit ae235b
							 GError      *err);
Packit ae235b
static void		g_io_channel_purge		(GIOChannel  *channel);
Packit ae235b
static GIOStatus	g_io_channel_fill_buffer	(GIOChannel  *channel,
Packit ae235b
							 GError     **err);
Packit ae235b
static GIOStatus	g_io_channel_read_line_backend	(GIOChannel  *channel,
Packit ae235b
							 gsize       *length,
Packit ae235b
							 gsize       *terminator_pos,
Packit ae235b
							 GError     **error);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_init:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Initializes a #GIOChannel struct. 
Packit ae235b
 *
Packit ae235b
 * This is called by each of the above functions when creating a 
Packit ae235b
 * #GIOChannel, and so is not often needed by the application 
Packit ae235b
 * programmer (unless you are creating a new type of #GIOChannel).
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_io_channel_init (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  channel->ref_count = 1;
Packit ae235b
  channel->encoding = g_strdup ("UTF-8");
Packit ae235b
  channel->line_term = NULL;
Packit ae235b
  channel->line_term_len = 0;
Packit ae235b
  channel->buf_size = G_IO_NICE_BUF_SIZE;
Packit ae235b
  channel->read_cd = (GIConv) -1;
Packit ae235b
  channel->write_cd = (GIConv) -1;
Packit ae235b
  channel->read_buf = NULL; /* Lazy allocate buffers */
Packit ae235b
  channel->encoded_read_buf = NULL;
Packit ae235b
  channel->write_buf = NULL;
Packit ae235b
  channel->partial_write_buf[0] = '\0';
Packit ae235b
  channel->use_buffer = TRUE;
Packit ae235b
  channel->do_encode = FALSE;
Packit ae235b
  channel->close_on_unref = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_ref:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Increments the reference count of a #GIOChannel.
Packit ae235b
 *
Packit ae235b
 * Returns: the @channel that was passed in (since 2.6)
Packit ae235b
 */
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_ref (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&channel->ref_count);
Packit ae235b
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_unref:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Decrements the reference count of a #GIOChannel.
Packit ae235b
 */
Packit ae235b
void 
Packit ae235b
g_io_channel_unref (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  gboolean is_zero;
Packit ae235b
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  is_zero = g_atomic_int_dec_and_test (&channel->ref_count);
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (is_zero))
Packit ae235b
    {
Packit ae235b
      if (channel->close_on_unref)
Packit ae235b
        g_io_channel_shutdown (channel, TRUE, NULL);
Packit ae235b
      else
Packit ae235b
        g_io_channel_purge (channel);
Packit ae235b
      g_free (channel->encoding);
Packit ae235b
      if (channel->read_cd != (GIConv) -1)
Packit ae235b
        g_iconv_close (channel->read_cd);
Packit ae235b
      if (channel->write_cd != (GIConv) -1)
Packit ae235b
        g_iconv_close (channel->write_cd);
Packit ae235b
      g_free (channel->line_term);
Packit ae235b
      if (channel->read_buf)
Packit ae235b
        g_string_free (channel->read_buf, TRUE);
Packit ae235b
      if (channel->write_buf)
Packit ae235b
        g_string_free (channel->write_buf, TRUE);
Packit ae235b
      if (channel->encoded_read_buf)
Packit ae235b
        g_string_free (channel->encoded_read_buf, TRUE);
Packit ae235b
      channel->funcs->io_free (channel);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOError
Packit ae235b
g_io_error_get_from_g_error (GIOStatus  status,
Packit ae235b
			     GError    *err)
Packit ae235b
{
Packit ae235b
  switch (status)
Packit ae235b
    {
Packit ae235b
      case G_IO_STATUS_NORMAL:
Packit ae235b
      case G_IO_STATUS_EOF:
Packit ae235b
        return G_IO_ERROR_NONE;
Packit ae235b
      case G_IO_STATUS_AGAIN:
Packit ae235b
        return G_IO_ERROR_AGAIN;
Packit ae235b
      case G_IO_STATUS_ERROR:
Packit ae235b
	g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
	
Packit ae235b
        if (err->domain != G_IO_CHANNEL_ERROR)
Packit ae235b
          return G_IO_ERROR_UNKNOWN;
Packit ae235b
        switch (err->code)
Packit ae235b
          {
Packit ae235b
            case G_IO_CHANNEL_ERROR_INVAL:
Packit ae235b
              return G_IO_ERROR_INVAL;
Packit ae235b
            default:
Packit ae235b
              return G_IO_ERROR_UNKNOWN;
Packit ae235b
          }
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @buf: a buffer to read the data into (which should be at least 
Packit ae235b
 *       count bytes long)
Packit ae235b
 * @count: the number of bytes to read from the #GIOChannel
Packit ae235b
 * @bytes_read: returns the number of bytes actually read
Packit ae235b
 * 
Packit ae235b
 * Reads data from a #GIOChannel. 
Packit ae235b
 * 
Packit ae235b
 * Returns: %G_IO_ERROR_NONE if the operation was successful. 
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.2: Use g_io_channel_read_chars() instead.
Packit ae235b
 **/
Packit ae235b
GIOError 
Packit ae235b
g_io_channel_read (GIOChannel *channel, 
Packit ae235b
		   gchar      *buf, 
Packit ae235b
		   gsize       count,
Packit ae235b
		   gsize      *bytes_read)
Packit ae235b
{
Packit ae235b
  GError *err = NULL;
Packit ae235b
  GIOError error;
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
  g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    {
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = 0;
Packit ae235b
      return G_IO_ERROR_NONE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
Packit ae235b
  status = channel->funcs->io_read (channel, buf, count, bytes_read, &err;;
Packit ae235b
Packit ae235b
  error = g_io_error_get_from_g_error (status, err);
Packit ae235b
Packit ae235b
  if (err)
Packit ae235b
    g_error_free (err);
Packit ae235b
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_write:
Packit ae235b
 * @channel:  a #GIOChannel
Packit ae235b
 * @buf: the buffer containing the data to write
Packit ae235b
 * @count: the number of bytes to write
Packit ae235b
 * @bytes_written: the number of bytes actually written
Packit ae235b
 * 
Packit ae235b
 * Writes data to a #GIOChannel. 
Packit ae235b
 * 
Packit ae235b
 * Returns:  %G_IO_ERROR_NONE if the operation was successful.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.2: Use g_io_channel_write_chars() instead.
Packit ae235b
 **/
Packit ae235b
GIOError 
Packit ae235b
g_io_channel_write (GIOChannel  *channel, 
Packit ae235b
		    const gchar *buf, 
Packit ae235b
		    gsize        count,
Packit ae235b
		    gsize       *bytes_written)
Packit ae235b
{
Packit ae235b
  GError *err = NULL;
Packit ae235b
  GIOError error;
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
  g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
Packit ae235b
  status = channel->funcs->io_write (channel, buf, count, bytes_written, &err;;
Packit ae235b
Packit ae235b
  error = g_io_error_get_from_g_error (status, err);
Packit ae235b
Packit ae235b
  if (err)
Packit ae235b
    g_error_free (err);
Packit ae235b
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_seek:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @offset: an offset, in bytes, which is added to the position specified 
Packit ae235b
 *          by @type
Packit ae235b
 * @type: the position in the file, which can be %G_SEEK_CUR (the current
Packit ae235b
 *        position), %G_SEEK_SET (the start of the file), or %G_SEEK_END 
Packit ae235b
 *        (the end of the file)
Packit ae235b
 * 
Packit ae235b
 * Sets the current position in the #GIOChannel, similar to the standard 
Packit ae235b
 * library function fseek(). 
Packit ae235b
 * 
Packit ae235b
 * Returns: %G_IO_ERROR_NONE if the operation was successful.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.2: Use g_io_channel_seek_position() instead.
Packit ae235b
 **/
Packit ae235b
GIOError 
Packit ae235b
g_io_channel_seek (GIOChannel *channel,
Packit ae235b
		   gint64      offset, 
Packit ae235b
		   GSeekType   type)
Packit ae235b
{
Packit ae235b
  GError *err = NULL;
Packit ae235b
  GIOError error;
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
Packit ae235b
  g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
Packit ae235b
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
      case G_SEEK_CUR:
Packit ae235b
      case G_SEEK_SET:
Packit ae235b
      case G_SEEK_END:
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_warning ("g_io_channel_seek: unknown seek type");
Packit ae235b
        return G_IO_ERROR_UNKNOWN;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = channel->funcs->io_seek (channel, offset, type, &err;;
Packit ae235b
Packit ae235b
  error = g_io_error_get_from_g_error (status, err);
Packit ae235b
Packit ae235b
  if (err)
Packit ae235b
    g_error_free (err);
Packit ae235b
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* The function g_io_channel_new_file() is prototyped in both
Packit ae235b
 * giounix.c and giowin32.c, so we stick its documentation here.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_new_file:
Packit ae235b
 * @filename: (type filename): A string containing the name of a file
Packit ae235b
 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
Packit ae235b
 *        the same meaning as in fopen()
Packit ae235b
 * @error: A location to return an error of type %G_FILE_ERROR
Packit ae235b
 *
Packit ae235b
 * Open a file @filename as a #GIOChannel using mode @mode. This
Packit ae235b
 * channel will be closed when the last reference to it is dropped,
Packit ae235b
 * so there is no need to call g_io_channel_close() (though doing
Packit ae235b
 * so will not cause problems, as long as no attempt is made to
Packit ae235b
 * access the channel after it is closed).
Packit ae235b
 *
Packit ae235b
 * Returns: A #GIOChannel on success, %NULL on failure.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_close:
Packit ae235b
 * @channel: A #GIOChannel
Packit ae235b
 * 
Packit ae235b
 * Close an IO channel. Any pending data to be written will be
Packit ae235b
 * flushed, ignoring errors. The channel will not be freed until the
Packit ae235b
 * last reference is dropped using g_io_channel_unref(). 
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.2: Use g_io_channel_shutdown() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_channel_close (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GError *err = NULL;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  g_io_channel_purge (channel);
Packit ae235b
Packit ae235b
  channel->funcs->io_close (channel, &err;;
Packit ae235b
Packit ae235b
  if (err)
Packit ae235b
    { /* No way to return the error */
Packit ae235b
      g_warning ("Error closing channel: %s", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  channel->close_on_unref = FALSE; /* Because we already did */
Packit ae235b
  channel->is_readable = FALSE;
Packit ae235b
  channel->is_writeable = FALSE;
Packit ae235b
  channel->is_seekable = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_shutdown:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @flush: if %TRUE, flush pending
Packit ae235b
 * @err: location to store a #GIOChannelError
Packit ae235b
 * 
Packit ae235b
 * Close an IO channel. Any pending data to be written will be
Packit ae235b
 * flushed if @flush is %TRUE. The channel will not be freed until the
Packit ae235b
 * last reference is dropped using g_io_channel_unref().
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_shutdown (GIOChannel  *channel,
Packit ae235b
		       gboolean     flush,
Packit ae235b
		       GError     **err)
Packit ae235b
{
Packit ae235b
  GIOStatus status, result;
Packit ae235b
  GError *tmperr = NULL;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (channel->write_buf && channel->write_buf->len > 0)
Packit ae235b
    {
Packit ae235b
      if (flush)
Packit ae235b
        {
Packit ae235b
          GIOFlags flags;
Packit ae235b
      
Packit ae235b
          /* Set the channel to blocking, to avoid a busy loop
Packit ae235b
           */
Packit ae235b
          flags = g_io_channel_get_flags (channel);
Packit ae235b
          /* Ignore any errors here, they're irrelevant */
Packit ae235b
          g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
Packit ae235b
Packit ae235b
          result = g_io_channel_flush (channel, &tmperr);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        result = G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
      g_string_truncate(channel->write_buf, 0);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    result = G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
  if (channel->partial_write_buf[0] != '\0')
Packit ae235b
    {
Packit ae235b
      if (flush)
Packit ae235b
        g_warning ("Partial character at end of write buffer not flushed.\n");
Packit ae235b
      channel->partial_write_buf[0] = '\0';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = channel->funcs->io_close (channel, err);
Packit ae235b
Packit ae235b
  channel->close_on_unref = FALSE; /* Because we already did */
Packit ae235b
  channel->is_readable = FALSE;
Packit ae235b
  channel->is_writeable = FALSE;
Packit ae235b
  channel->is_seekable = FALSE;
Packit ae235b
Packit ae235b
  if (status != G_IO_STATUS_NORMAL)
Packit ae235b
    {
Packit ae235b
      g_clear_error (&tmperr);
Packit ae235b
      return status;
Packit ae235b
    }
Packit ae235b
  else if (result != G_IO_STATUS_NORMAL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (err, tmperr);
Packit ae235b
      return result;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* This function is used for the final flush on close or unref */
Packit ae235b
static void
Packit ae235b
g_io_channel_purge (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GError *err = NULL;
Packit ae235b
  GIOStatus status G_GNUC_UNUSED;
Packit ae235b
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  if (channel->write_buf && channel->write_buf->len > 0)
Packit ae235b
    {
Packit ae235b
      GIOFlags flags;
Packit ae235b
Packit ae235b
      /* Set the channel to blocking, to avoid a busy loop
Packit ae235b
       */
Packit ae235b
      flags = g_io_channel_get_flags (channel);
Packit ae235b
      g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
Packit ae235b
Packit ae235b
      status = g_io_channel_flush (channel, &err;;
Packit ae235b
Packit ae235b
      if (err)
Packit ae235b
        { /* No way to return the error */
Packit ae235b
          g_warning ("Error flushing string: %s", err->message);
Packit ae235b
          g_error_free (err);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Flush these in case anyone tries to close without unrefing */
Packit ae235b
Packit ae235b
  if (channel->read_buf)
Packit ae235b
    g_string_truncate (channel->read_buf, 0);
Packit ae235b
  if (channel->write_buf)
Packit ae235b
    g_string_truncate (channel->write_buf, 0);
Packit ae235b
  if (channel->encoding)
Packit ae235b
    {
Packit ae235b
      if (channel->encoded_read_buf)
Packit ae235b
        g_string_truncate (channel->encoded_read_buf, 0);
Packit ae235b
Packit ae235b
      if (channel->partial_write_buf[0] != '\0')
Packit ae235b
        {
Packit ae235b
          g_warning ("Partial character at end of write buffer not flushed.\n");
Packit ae235b
          channel->partial_write_buf[0] = '\0';
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_create_watch:
Packit ae235b
 * @channel: a #GIOChannel to watch
Packit ae235b
 * @condition: conditions to watch for
Packit ae235b
 *
Packit ae235b
 * Creates a #GSource that's dispatched when @condition is met for the 
Packit ae235b
 * given @channel. For example, if condition is #G_IO_IN, the source will 
Packit ae235b
 * be dispatched when there's data available for reading.
Packit ae235b
 *
Packit ae235b
 * g_io_add_watch() is a simpler interface to this same functionality, for 
Packit ae235b
 * the case where you want to add the source to the default main loop context 
Packit ae235b
 * at the default priority.
Packit ae235b
 *
Packit ae235b
 * On Windows, polling a #GSource created to watch a channel for a socket
Packit ae235b
 * puts the socket in non-blocking mode. This is a side-effect of the
Packit ae235b
 * implementation and unavoidable.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GSource
Packit ae235b
 */
Packit ae235b
GSource *
Packit ae235b
g_io_create_watch (GIOChannel   *channel,
Packit ae235b
		   GIOCondition  condition)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, NULL);
Packit ae235b
Packit ae235b
  return channel->funcs->io_create_watch (channel, condition);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_add_watch_full: (rename-to g_io_add_watch)
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @priority: the priority of the #GIOChannel source
Packit ae235b
 * @condition: the condition to watch for
Packit ae235b
 * @func: the function to call when the condition is satisfied
Packit ae235b
 * @user_data: user data to pass to @func
Packit ae235b
 * @notify: the function to call when the source is removed
Packit ae235b
 *
Packit ae235b
 * Adds the #GIOChannel into the default main loop context
Packit ae235b
 * with the given priority.
Packit ae235b
 *
Packit ae235b
 * This internally creates a main loop source using g_io_create_watch()
Packit ae235b
 * and attaches it to the main loop context with g_source_attach().
Packit ae235b
 * You can do these steps manually if you need greater control.
Packit ae235b
 *
Packit ae235b
 * Returns: the event source id
Packit ae235b
 */
Packit ae235b
guint 
Packit ae235b
g_io_add_watch_full (GIOChannel    *channel,
Packit ae235b
		     gint           priority,
Packit ae235b
		     GIOCondition   condition,
Packit ae235b
		     GIOFunc        func,
Packit ae235b
		     gpointer       user_data,
Packit ae235b
		     GDestroyNotify notify)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
  guint id;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (channel != NULL, 0);
Packit ae235b
Packit ae235b
  source = g_io_create_watch (channel, condition);
Packit ae235b
Packit ae235b
  if (priority != G_PRIORITY_DEFAULT)
Packit ae235b
    g_source_set_priority (source, priority);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
Packit ae235b
Packit ae235b
  id = g_source_attach (source, NULL);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  return id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_add_watch:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @condition: the condition to watch for
Packit ae235b
 * @func: the function to call when the condition is satisfied
Packit ae235b
 * @user_data: user data to pass to @func
Packit ae235b
 *
Packit ae235b
 * Adds the #GIOChannel into the default main loop context
Packit ae235b
 * with the default priority.
Packit ae235b
 *
Packit ae235b
 * Returns: the event source id
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * GIOFunc:
Packit ae235b
 * @source: the #GIOChannel event source
Packit ae235b
 * @condition: the condition which has been satisfied
Packit ae235b
 * @data: user data set in g_io_add_watch() or g_io_add_watch_full()
Packit ae235b
 *
Packit ae235b
 * Specifies the type of function passed to g_io_add_watch() or
Packit ae235b
 * g_io_add_watch_full(), which is called when the requested condition
Packit ae235b
 * on a #GIOChannel is satisfied.
Packit ae235b
 *
Packit ae235b
 * Returns: the function should return %FALSE if the event source
Packit ae235b
 *          should be removed
Packit ae235b
 **/
Packit ae235b
/**
Packit ae235b
 * GIOCondition:
Packit ae235b
 * @G_IO_IN: There is data to read.
Packit ae235b
 * @G_IO_OUT: Data can be written (without blocking).
Packit ae235b
 * @G_IO_PRI: There is urgent data to read.
Packit ae235b
 * @G_IO_ERR: Error condition.
Packit ae235b
 * @G_IO_HUP: Hung up (the connection has been broken, usually for
Packit ae235b
 *            pipes and sockets).
Packit ae235b
 * @G_IO_NVAL: Invalid request. The file descriptor is not open.
Packit ae235b
 *
Packit ae235b
 * A bitwise combination representing a condition to watch for on an
Packit ae235b
 * event source.
Packit ae235b
 **/
Packit ae235b
guint 
Packit ae235b
g_io_add_watch (GIOChannel   *channel,
Packit ae235b
		GIOCondition  condition,
Packit ae235b
		GIOFunc       func,
Packit ae235b
		gpointer      user_data)
Packit ae235b
{
Packit ae235b
  return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_buffer_condition:
Packit ae235b
 * @channel: A #GIOChannel
Packit ae235b
 *
Packit ae235b
 * This function returns a #GIOCondition depending on whether there
Packit ae235b
 * is data to be read/space to write data in the internal buffers in 
Packit ae235b
 * the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
Packit ae235b
 *
Packit ae235b
 * Returns: A #GIOCondition
Packit ae235b
 **/
Packit ae235b
GIOCondition
Packit ae235b
g_io_channel_get_buffer_condition (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOCondition condition = 0;
Packit ae235b
Packit ae235b
  if (channel->encoding)
Packit ae235b
    {
Packit ae235b
      if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
Packit ae235b
        condition |= G_IO_IN; /* Only return if we have full characters */
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (channel->read_buf && (channel->read_buf->len > 0))
Packit ae235b
        condition |= G_IO_IN;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
Packit ae235b
    condition |= G_IO_OUT;
Packit ae235b
Packit ae235b
  return condition;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_error_from_errno:
Packit ae235b
 * @en: an `errno` error number, e.g. `EINVAL`
Packit ae235b
 *
Packit ae235b
 * Converts an `errno` error number to a #GIOChannelError.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GIOChannelError error number, e.g. 
Packit ae235b
 *      %G_IO_CHANNEL_ERROR_INVAL.
Packit ae235b
 **/
Packit ae235b
GIOChannelError
Packit ae235b
g_io_channel_error_from_errno (gint en)
Packit ae235b
{
Packit ae235b
#ifdef EAGAIN
Packit ae235b
  g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  switch (en)
Packit ae235b
    {
Packit ae235b
#ifdef EBADF
Packit ae235b
    case EBADF:
Packit ae235b
      g_warning("Invalid file descriptor.\n");
Packit ae235b
      return G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EFAULT
Packit ae235b
    case EFAULT:
Packit ae235b
      g_warning("Buffer outside valid address space.\n");
Packit ae235b
      return G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EFBIG
Packit ae235b
    case EFBIG:
Packit ae235b
      return G_IO_CHANNEL_ERROR_FBIG;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EINTR
Packit ae235b
    /* In general, we should catch EINTR before we get here,
Packit ae235b
     * but close() is allowed to return EINTR by POSIX, so
Packit ae235b
     * we need to catch it here; EINTR from close() is
Packit ae235b
     * unrecoverable, because it's undefined whether
Packit ae235b
     * the fd was actually closed or not, so we just return
Packit ae235b
     * a generic error code.
Packit ae235b
     */
Packit ae235b
    case EINTR:
Packit ae235b
      return G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EINVAL
Packit ae235b
    case EINVAL:
Packit ae235b
      return G_IO_CHANNEL_ERROR_INVAL;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EIO
Packit ae235b
    case EIO:
Packit ae235b
      return G_IO_CHANNEL_ERROR_IO;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EISDIR
Packit ae235b
    case EISDIR:
Packit ae235b
      return G_IO_CHANNEL_ERROR_ISDIR;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOSPC
Packit ae235b
    case ENOSPC:
Packit ae235b
      return G_IO_CHANNEL_ERROR_NOSPC;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENXIO
Packit ae235b
    case ENXIO:
Packit ae235b
      return G_IO_CHANNEL_ERROR_NXIO;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EOVERFLOW
Packit ae235b
#if EOVERFLOW != EFBIG
Packit ae235b
    case EOVERFLOW:
Packit ae235b
      return G_IO_CHANNEL_ERROR_OVERFLOW;
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPIPE
Packit ae235b
    case EPIPE:
Packit ae235b
      return G_IO_CHANNEL_ERROR_PIPE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      return G_IO_CHANNEL_ERROR_FAILED;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_buffer_size:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @size: the size of the buffer, or 0 to let GLib pick a good size
Packit ae235b
 *
Packit ae235b
 * Sets the buffer size.
Packit ae235b
 **/  
Packit ae235b
void
Packit ae235b
g_io_channel_set_buffer_size (GIOChannel *channel,
Packit ae235b
                              gsize       size)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  if (size == 0)
Packit ae235b
    size = G_IO_NICE_BUF_SIZE;
Packit ae235b
Packit ae235b
  if (size < MAX_CHAR_SIZE)
Packit ae235b
    size = MAX_CHAR_SIZE;
Packit ae235b
Packit ae235b
  channel->buf_size = size;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_buffer_size:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Gets the buffer size.
Packit ae235b
 *
Packit ae235b
 * Returns: the size of the buffer.
Packit ae235b
 **/  
Packit ae235b
gsize
Packit ae235b
g_io_channel_get_buffer_size (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, 0);
Packit ae235b
Packit ae235b
  return channel->buf_size;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_line_term:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @line_term: (nullable): The line termination string. Use %NULL for
Packit ae235b
 *             autodetect.  Autodetection breaks on "\n", "\r\n", "\r", "\0",
Packit ae235b
 *             and the Unicode paragraph separator. Autodetection should not be
Packit ae235b
 *             used for anything other than file-based channels.
Packit ae235b
 * @length: The length of the termination string. If -1 is passed, the
Packit ae235b
 *          string is assumed to be nul-terminated. This option allows
Packit ae235b
 *          termination strings with embedded nuls.
Packit ae235b
 *
Packit ae235b
 * This sets the string that #GIOChannel uses to determine
Packit ae235b
 * where in the file a line break occurs.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_channel_set_line_term (GIOChannel	*channel,
Packit ae235b
                            const gchar	*line_term,
Packit ae235b
			    gint         length)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
  g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
Packit ae235b
Packit ae235b
  if (line_term == NULL)
Packit ae235b
    length = 0;
Packit ae235b
  else if (length < 0)
Packit ae235b
    length = strlen (line_term);
Packit ae235b
Packit ae235b
  g_free (channel->line_term);
Packit ae235b
  channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
Packit ae235b
  channel->line_term_len = length;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_line_term:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @length: a location to return the length of the line terminator
Packit ae235b
 *
Packit ae235b
 * This returns the string that #GIOChannel uses to determine
Packit ae235b
 * where in the file a line break occurs. A value of %NULL
Packit ae235b
 * indicates autodetection.
Packit ae235b
 *
Packit ae235b
 * Returns: The line termination string. This value
Packit ae235b
 *   is owned by GLib and must not be freed.
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_io_channel_get_line_term (GIOChannel *channel,
Packit ae235b
			    gint       *length)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, NULL);
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = channel->line_term_len;
Packit ae235b
Packit ae235b
  return channel->line_term;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_flags:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @flags: the flags to set on the IO channel
Packit ae235b
 * @error: A location to return an error of type #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Sets the (writeable) flags in @channel to (@flags & %G_IO_FLAG_SET_MASK).
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation. 
Packit ae235b
 **/
Packit ae235b
/**
Packit ae235b
 * GIOFlags:
Packit ae235b
 * @G_IO_FLAG_APPEND: turns on append mode, corresponds to %O_APPEND
Packit ae235b
 *     (see the documentation of the UNIX open() syscall)
Packit ae235b
 * @G_IO_FLAG_NONBLOCK: turns on nonblocking mode, corresponds to
Packit ae235b
 *     %O_NONBLOCK/%O_NDELAY (see the documentation of the UNIX open()
Packit ae235b
 *     syscall)
Packit ae235b
 * @G_IO_FLAG_IS_READABLE: indicates that the io channel is readable.
Packit ae235b
 *     This flag cannot be changed.
Packit ae235b
 * @G_IO_FLAG_IS_WRITABLE: indicates that the io channel is writable.
Packit ae235b
 *     This flag cannot be changed.
Packit ae235b
 * @G_IO_FLAG_IS_WRITEABLE: a misspelled version of @G_IO_FLAG_IS_WRITABLE
Packit ae235b
 *     that existed before the spelling was fixed in GLib 2.30. It is kept
Packit ae235b
 *     here for compatibility reasons. Deprecated since 2.30
Packit ae235b
 * @G_IO_FLAG_IS_SEEKABLE: indicates that the io channel is seekable,
Packit ae235b
 *     i.e. that g_io_channel_seek_position() can be used on it.
Packit ae235b
 *     This flag cannot be changed.
Packit ae235b
 * @G_IO_FLAG_MASK: the mask that specifies all the valid flags.
Packit ae235b
 * @G_IO_FLAG_GET_MASK: the mask of the flags that are returned from
Packit ae235b
 *     g_io_channel_get_flags()
Packit ae235b
 * @G_IO_FLAG_SET_MASK: the mask of the flags that the user can modify
Packit ae235b
 *     with g_io_channel_set_flags()
Packit ae235b
 *
Packit ae235b
 * Specifies properties of a #GIOChannel. Some of the flags can only be
Packit ae235b
 * read with g_io_channel_get_flags(), but not changed with
Packit ae235b
 * g_io_channel_set_flags().
Packit ae235b
 */
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_set_flags (GIOChannel  *channel,
Packit ae235b
                        GIOFlags     flags,
Packit ae235b
                        GError     **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  return (*channel->funcs->io_set_flags) (channel,
Packit ae235b
					  flags & G_IO_FLAG_SET_MASK,
Packit ae235b
					  error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_flags:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Gets the current flags for a #GIOChannel, including read-only
Packit ae235b
 * flags such as %G_IO_FLAG_IS_READABLE.
Packit ae235b
 *
Packit ae235b
 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITABLE
Packit ae235b
 * are cached for internal use by the channel when it is created.
Packit ae235b
 * If they should change at some later point (e.g. partial shutdown
Packit ae235b
 * of a socket with the UNIX shutdown() function), the user
Packit ae235b
 * should immediately call g_io_channel_get_flags() to update
Packit ae235b
 * the internal values of these flags.
Packit ae235b
 *
Packit ae235b
 * Returns: the flags which are set on the channel
Packit ae235b
 **/
Packit ae235b
GIOFlags
Packit ae235b
g_io_channel_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOFlags flags;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, 0);
Packit ae235b
Packit ae235b
  flags = (* channel->funcs->io_get_flags) (channel);
Packit ae235b
Packit ae235b
  /* Cross implementation code */
Packit ae235b
Packit ae235b
  if (channel->is_seekable)
Packit ae235b
    flags |= G_IO_FLAG_IS_SEEKABLE;
Packit ae235b
  if (channel->is_readable)
Packit ae235b
    flags |= G_IO_FLAG_IS_READABLE;
Packit ae235b
  if (channel->is_writeable)
Packit ae235b
    flags |= G_IO_FLAG_IS_WRITABLE;
Packit ae235b
Packit ae235b
  return flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_close_on_unref:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @do_close: Whether to close the channel on the final unref of
Packit ae235b
 *            the GIOChannel data structure.
Packit ae235b
 *
Packit ae235b
 * Whether to close the channel on the final unref of the #GIOChannel
Packit ae235b
 * data structure. The default value of this is %TRUE for channels
Packit ae235b
 * created by g_io_channel_new_file (), and %FALSE for all other channels.
Packit ae235b
 *
Packit ae235b
 * Setting this flag to %TRUE for a channel you have already closed
Packit ae235b
 * can cause problems when the final reference to the #GIOChannel is dropped.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_channel_set_close_on_unref	(GIOChannel *channel,
Packit ae235b
				 gboolean    do_close)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  channel->close_on_unref = do_close;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_close_on_unref:
Packit ae235b
 * @channel: a #GIOChannel.
Packit ae235b
 *
Packit ae235b
 * Returns whether the file/socket/whatever associated with @channel
Packit ae235b
 * will be closed when @channel receives its final unref and is
Packit ae235b
 * destroyed. The default value of this is %TRUE for channels created
Packit ae235b
 * by g_io_channel_new_file (), and %FALSE for all other channels.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the channel will be closed, %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_io_channel_get_close_on_unref	(GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, FALSE);
Packit ae235b
Packit ae235b
  return channel->close_on_unref;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_seek_position:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @offset: The offset in bytes from the position specified by @type
Packit ae235b
 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
Packit ae235b
 *                      cases where a call to g_io_channel_set_encoding ()
Packit ae235b
 *                      is allowed. See the documentation for
Packit ae235b
 *                      g_io_channel_set_encoding () for details.
Packit ae235b
 * @error: A location to return an error of type #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Replacement for g_io_channel_seek() with the new API.
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 **/
Packit ae235b
/**
Packit ae235b
 * GSeekType:
Packit ae235b
 * @G_SEEK_CUR: the current position in the file.
Packit ae235b
 * @G_SEEK_SET: the start of the file.
Packit ae235b
 * @G_SEEK_END: the end of the file.
Packit ae235b
 *
Packit ae235b
 * An enumeration specifying the base position for a
Packit ae235b
 * g_io_channel_seek_position() operation.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_seek_position (GIOChannel  *channel,
Packit ae235b
                            gint64       offset,
Packit ae235b
                            GSeekType    type,
Packit ae235b
                            GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  /* For files, only one of the read and write buffers can contain data.
Packit ae235b
   * For sockets, both can contain data.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
      case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
Packit ae235b
        if (channel->use_buffer)
Packit ae235b
          {
Packit ae235b
            if (channel->do_encode && channel->encoded_read_buf
Packit ae235b
                && channel->encoded_read_buf->len > 0)
Packit ae235b
              {
Packit ae235b
                g_warning ("Seek type G_SEEK_CUR not allowed for this"
Packit ae235b
                  " channel's encoding.\n");
Packit ae235b
                return G_IO_STATUS_ERROR;
Packit ae235b
              }
Packit ae235b
          if (channel->read_buf)
Packit ae235b
            offset -= channel->read_buf->len;
Packit ae235b
          if (channel->encoded_read_buf)
Packit ae235b
            {
Packit ae235b
              g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
Packit ae235b
Packit ae235b
              /* If there's anything here, it's because the encoding is UTF-8,
Packit ae235b
               * so we can just subtract the buffer length, the same as for
Packit ae235b
               * the unencoded data.
Packit ae235b
               */
Packit ae235b
Packit ae235b
              offset -= channel->encoded_read_buf->len;
Packit ae235b
            }
Packit ae235b
          }
Packit ae235b
        break;
Packit ae235b
      case G_SEEK_SET:
Packit ae235b
      case G_SEEK_END:
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_warning ("g_io_channel_seek_position: unknown seek type");
Packit ae235b
        return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      status = g_io_channel_flush (channel, error);
Packit ae235b
      if (status != G_IO_STATUS_NORMAL)
Packit ae235b
        return status;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = channel->funcs->io_seek (channel, offset, type, error);
Packit ae235b
Packit ae235b
  if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
Packit ae235b
    {
Packit ae235b
      if (channel->read_buf)
Packit ae235b
        g_string_truncate (channel->read_buf, 0);
Packit ae235b
Packit ae235b
      /* Conversion state no longer matches position in file */
Packit ae235b
      if (channel->read_cd != (GIConv) -1)
Packit ae235b
        g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
Packit ae235b
      if (channel->write_cd != (GIConv) -1)
Packit ae235b
        g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
      if (channel->encoded_read_buf)
Packit ae235b
        {
Packit ae235b
          g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
Packit ae235b
          g_string_truncate (channel->encoded_read_buf, 0);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (channel->partial_write_buf[0] != '\0')
Packit ae235b
        {
Packit ae235b
          g_warning ("Partial character at end of write buffer not flushed.\n");
Packit ae235b
          channel->partial_write_buf[0] = '\0';
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_flush:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @error: location to store an error of type #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Flushes the write buffer for the GIOChannel.
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation: One of
Packit ae235b
 *   #G_IO_STATUS_NORMAL, #G_IO_STATUS_AGAIN, or
Packit ae235b
 *   #G_IO_STATUS_ERROR.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_flush (GIOChannel	*channel,
Packit ae235b
		    GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gsize this_time = 1, bytes_written = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (channel->write_buf == NULL || channel->write_buf->len == 0)
Packit ae235b
    return G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      g_assert (this_time > 0);
Packit ae235b
Packit ae235b
      status = channel->funcs->io_write (channel,
Packit ae235b
					 channel->write_buf->str + bytes_written,
Packit ae235b
					 channel->write_buf->len - bytes_written,
Packit ae235b
					 &this_time, error);
Packit ae235b
      bytes_written += this_time;
Packit ae235b
    }
Packit ae235b
  while ((bytes_written < channel->write_buf->len)
Packit ae235b
         && (status == G_IO_STATUS_NORMAL));
Packit ae235b
Packit ae235b
  g_string_erase (channel->write_buf, 0, bytes_written);
Packit ae235b
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_buffered:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @buffered: whether to set the channel buffered or unbuffered
Packit ae235b
 *
Packit ae235b
 * The buffering state can only be set if the channel's encoding
Packit ae235b
 * is %NULL. For any other encoding, the channel must be buffered.
Packit ae235b
 *
Packit ae235b
 * A buffered channel can only be set unbuffered if the channel's
Packit ae235b
 * internal buffers have been flushed. Newly created channels or
Packit ae235b
 * channels which have returned %G_IO_STATUS_EOF
Packit ae235b
 * not require such a flush. For write-only channels, a call to
Packit ae235b
 * g_io_channel_flush () is sufficient. For all other channels,
Packit ae235b
 * the buffers may be flushed by a call to g_io_channel_seek_position ().
Packit ae235b
 * This includes the possibility of seeking with seek type %G_SEEK_CUR
Packit ae235b
 * and an offset of zero. Note that this means that socket-based
Packit ae235b
 * channels cannot be set unbuffered once they have had data
Packit ae235b
 * read from them.
Packit ae235b
 *
Packit ae235b
 * On unbuffered channels, it is safe to mix read and write
Packit ae235b
 * calls from the new and old APIs, if this is necessary for
Packit ae235b
 * maintaining old code.
Packit ae235b
 *
Packit ae235b
 * The default state of the channel is buffered.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_io_channel_set_buffered (GIOChannel *channel,
Packit ae235b
                           gboolean    buffered)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (channel != NULL);
Packit ae235b
Packit ae235b
  if (channel->encoding != NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("Need to have NULL encoding to set the buffering state of the "
Packit ae235b
                 "channel.\n");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
Packit ae235b
  g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
Packit ae235b
Packit ae235b
  channel->use_buffer = buffered;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_buffered:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Returns whether @channel is buffered.
Packit ae235b
 *
Packit ae235b
 * Return Value: %TRUE if the @channel is buffered. 
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_io_channel_get_buffered (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, FALSE);
Packit ae235b
Packit ae235b
  return channel->use_buffer;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_set_encoding:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @encoding: (nullable): the encoding type
Packit ae235b
 * @error: location to store an error of type #GConvertError
Packit ae235b
 *
Packit ae235b
 * Sets the encoding for the input/output of the channel. 
Packit ae235b
 * The internal encoding is always UTF-8. The default encoding 
Packit ae235b
 * for the external file is UTF-8.
Packit ae235b
 *
Packit ae235b
 * The encoding %NULL is safe to use with binary data.
Packit ae235b
 *
Packit ae235b
 * The encoding can only be set if one of the following conditions
Packit ae235b
 * is true:
Packit ae235b
 * 
Packit ae235b
 * - The channel was just created, and has not been written to or read from yet.
Packit ae235b
 *
Packit ae235b
 * - The channel is write-only.
Packit ae235b
 *
Packit ae235b
 * - The channel is a file, and the file pointer was just repositioned
Packit ae235b
 *   by a call to g_io_channel_seek_position(). (This flushes all the
Packit ae235b
 *   internal buffers.)
Packit ae235b
 *
Packit ae235b
 * - The current encoding is %NULL or UTF-8.
Packit ae235b
 *
Packit ae235b
 * - One of the (new API) read functions has just returned %G_IO_STATUS_EOF
Packit ae235b
 *   (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
Packit ae235b
 * 
Packit ae235b
 * -  One of the functions g_io_channel_read_chars() or 
Packit ae235b
 *    g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or 
Packit ae235b
 *    %G_IO_STATUS_ERROR. This may be useful in the case of 
Packit ae235b
 *    %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
Packit ae235b
 *    Returning one of these statuses from g_io_channel_read_line(),
Packit ae235b
 *    g_io_channel_read_line_string(), or g_io_channel_read_to_end()
Packit ae235b
 *    does not guarantee that the encoding can be changed.
Packit ae235b
 *
Packit ae235b
 * Channels which do not meet one of the above conditions cannot call
Packit ae235b
 * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if 
Packit ae235b
 * they are "seekable", cannot call g_io_channel_write_chars() after 
Packit ae235b
 * calling one of the API "read" functions.
Packit ae235b
 *
Packit ae235b
 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set
Packit ae235b
 */
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_set_encoding (GIOChannel	*channel,
Packit ae235b
                           const gchar	*encoding,
Packit ae235b
			   GError      **error)
Packit ae235b
{
Packit ae235b
  GIConv read_cd, write_cd;
Packit ae235b
  gboolean did_encode;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  /* Make sure the encoded buffers are empty */
Packit ae235b
Packit ae235b
  g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
Packit ae235b
			channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (!channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      g_warning ("Need to set the channel buffered before setting the encoding.\n");
Packit ae235b
      g_warning ("Assuming this is what you meant and acting accordingly.\n");
Packit ae235b
Packit ae235b
      channel->use_buffer = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (channel->partial_write_buf[0] != '\0')
Packit ae235b
    {
Packit ae235b
      g_warning ("Partial character at end of write buffer not flushed.\n");
Packit ae235b
      channel->partial_write_buf[0] = '\0';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  did_encode = channel->do_encode;
Packit ae235b
Packit ae235b
  if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
Packit ae235b
    {
Packit ae235b
      channel->do_encode = FALSE;
Packit ae235b
      read_cd = write_cd = (GIConv) -1;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gint err = 0;
Packit ae235b
      const gchar *from_enc = NULL, *to_enc = NULL;
Packit ae235b
Packit ae235b
      if (channel->is_readable)
Packit ae235b
        {
Packit ae235b
          read_cd = g_iconv_open ("UTF-8", encoding);
Packit ae235b
Packit ae235b
          if (read_cd == (GIConv) -1)
Packit ae235b
            {
Packit ae235b
              err = errno;
Packit ae235b
              from_enc = encoding;
Packit ae235b
              to_enc = "UTF-8";
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        read_cd = (GIConv) -1;
Packit ae235b
Packit ae235b
      if (channel->is_writeable && err == 0)
Packit ae235b
        {
Packit ae235b
          write_cd = g_iconv_open (encoding, "UTF-8");
Packit ae235b
Packit ae235b
          if (write_cd == (GIConv) -1)
Packit ae235b
            {
Packit ae235b
              err = errno;
Packit ae235b
              from_enc = "UTF-8";
Packit ae235b
              to_enc = encoding;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        write_cd = (GIConv) -1;
Packit ae235b
Packit ae235b
      if (err != 0)
Packit ae235b
        {
Packit ae235b
          g_assert (from_enc);
Packit ae235b
          g_assert (to_enc);
Packit ae235b
Packit ae235b
          if (err == EINVAL)
Packit ae235b
            g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
Packit ae235b
                         _("Conversion from character set “%s” to “%s” is not supported"),
Packit ae235b
                         from_enc, to_enc);
Packit ae235b
          else
Packit ae235b
            g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                         _("Could not open converter from “%s” to “%s”: %s"),
Packit ae235b
                         from_enc, to_enc, g_strerror (err));
Packit ae235b
Packit ae235b
          if (read_cd != (GIConv) -1)
Packit ae235b
            g_iconv_close (read_cd);
Packit ae235b
          if (write_cd != (GIConv) -1)
Packit ae235b
            g_iconv_close (write_cd);
Packit ae235b
Packit ae235b
          return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      channel->do_encode = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* The encoding is ok, so set the fields in channel */
Packit ae235b
Packit ae235b
  if (channel->read_cd != (GIConv) -1)
Packit ae235b
    g_iconv_close (channel->read_cd);
Packit ae235b
  if (channel->write_cd != (GIConv) -1)
Packit ae235b
    g_iconv_close (channel->write_cd);
Packit ae235b
Packit ae235b
  if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
Packit ae235b
    {
Packit ae235b
      g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
Packit ae235b
Packit ae235b
      /* This is just validated UTF-8, so we can copy it back into read_buf
Packit ae235b
       * so it can be encoded in whatever the new encoding is.
Packit ae235b
       */
Packit ae235b
Packit ae235b
      g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
Packit ae235b
                            channel->encoded_read_buf->len);
Packit ae235b
      g_string_truncate (channel->encoded_read_buf, 0);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  channel->read_cd = read_cd;
Packit ae235b
  channel->write_cd = write_cd;
Packit ae235b
Packit ae235b
  g_free (channel->encoding);
Packit ae235b
  channel->encoding = g_strdup (encoding);
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_get_encoding:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 *
Packit ae235b
 * Gets the encoding for the input/output of the channel. 
Packit ae235b
 * The internal encoding is always UTF-8. The encoding %NULL 
Packit ae235b
 * makes the channel safe for binary data.
Packit ae235b
 *
Packit ae235b
 * Returns: A string containing the encoding, this string is
Packit ae235b
 *   owned by GLib and must not be freed.
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_io_channel_get_encoding (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (channel != NULL, NULL);
Packit ae235b
Packit ae235b
  return channel->encoding;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_channel_fill_buffer (GIOChannel  *channel,
Packit ae235b
                          GError     **err)
Packit ae235b
{
Packit ae235b
  gsize read_size, cur_len, oldlen;
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
Packit ae235b
    {
Packit ae235b
      status = g_io_channel_flush (channel, err);
Packit ae235b
      if (status != G_IO_STATUS_NORMAL)
Packit ae235b
        return status;
Packit ae235b
    }
Packit ae235b
  if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
Packit ae235b
    {
Packit ae235b
      g_warning ("Partial character at end of write buffer not flushed.\n");
Packit ae235b
      channel->partial_write_buf[0] = '\0';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!channel->read_buf)
Packit ae235b
    channel->read_buf = g_string_sized_new (channel->buf_size);
Packit ae235b
Packit ae235b
  cur_len = channel->read_buf->len;
Packit ae235b
Packit ae235b
  g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
Packit ae235b
Packit ae235b
  status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
Packit ae235b
                                    channel->buf_size, &read_size, err);
Packit ae235b
Packit ae235b
  g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
Packit ae235b
Packit ae235b
  g_string_truncate (channel->read_buf, read_size + cur_len);
Packit ae235b
Packit ae235b
  if ((status != G_IO_STATUS_NORMAL) &&
Packit ae235b
      ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
Packit ae235b
    return status;
Packit ae235b
Packit ae235b
  g_assert (channel->read_buf->len > 0);
Packit ae235b
Packit ae235b
  if (channel->encoded_read_buf)
Packit ae235b
    oldlen = channel->encoded_read_buf->len;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      oldlen = 0;
Packit ae235b
      if (channel->encoding)
Packit ae235b
        channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (channel->do_encode)
Packit ae235b
    {
Packit ae235b
      gsize errnum, inbytes_left, outbytes_left;
Packit ae235b
      gchar *inbuf, *outbuf;
Packit ae235b
      int errval;
Packit ae235b
Packit ae235b
      g_assert (channel->encoded_read_buf);
Packit ae235b
Packit ae235b
reencode:
Packit ae235b
Packit ae235b
      inbytes_left = channel->read_buf->len;
Packit ae235b
      outbytes_left = MAX (channel->read_buf->len,
Packit ae235b
                           channel->encoded_read_buf->allocated_len
Packit ae235b
                           - channel->encoded_read_buf->len - 1); /* 1 for NULL */
Packit ae235b
      outbytes_left = MAX (outbytes_left, 6);
Packit ae235b
Packit ae235b
      inbuf = channel->read_buf->str;
Packit ae235b
      g_string_set_size (channel->encoded_read_buf,
Packit ae235b
                         channel->encoded_read_buf->len + outbytes_left);
Packit ae235b
      outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
Packit ae235b
               - outbytes_left;
Packit ae235b
Packit ae235b
      errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
Packit ae235b
			&outbuf, &outbytes_left);
Packit ae235b
      errval = errno;
Packit ae235b
Packit ae235b
      g_assert (inbuf + inbytes_left == channel->read_buf->str
Packit ae235b
                + channel->read_buf->len);
Packit ae235b
      g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
Packit ae235b
                + channel->encoded_read_buf->len);
Packit ae235b
Packit ae235b
      g_string_erase (channel->read_buf, 0,
Packit ae235b
		      channel->read_buf->len - inbytes_left);
Packit ae235b
      g_string_truncate (channel->encoded_read_buf,
Packit ae235b
			 channel->encoded_read_buf->len - outbytes_left);
Packit ae235b
Packit ae235b
      if (errnum == (gsize) -1)
Packit ae235b
        {
Packit ae235b
          switch (errval)
Packit ae235b
            {
Packit ae235b
              case EINVAL:
Packit ae235b
                if ((oldlen == channel->encoded_read_buf->len)
Packit ae235b
                  && (status == G_IO_STATUS_EOF))
Packit ae235b
                  status = G_IO_STATUS_EOF;
Packit ae235b
                else
Packit ae235b
                  status = G_IO_STATUS_NORMAL;
Packit ae235b
                break;
Packit ae235b
              case E2BIG:
Packit ae235b
                /* Buffer size at least 6, wrote at least on character */
Packit ae235b
                g_assert (inbuf != channel->read_buf->str);
Packit ae235b
                goto reencode;
Packit ae235b
              case EILSEQ:
Packit ae235b
                if (oldlen < channel->encoded_read_buf->len)
Packit ae235b
                  status = G_IO_STATUS_NORMAL;
Packit ae235b
                else
Packit ae235b
                  {
Packit ae235b
                    g_set_error_literal (err, G_CONVERT_ERROR,
Packit ae235b
                      G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                      _("Invalid byte sequence in conversion input"));
Packit ae235b
                    return G_IO_STATUS_ERROR;
Packit ae235b
                  }
Packit ae235b
                break;
Packit ae235b
              default:
Packit ae235b
                g_assert (errval != EBADF); /* The converter should be open */
Packit ae235b
                g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                  _("Error during conversion: %s"), g_strerror (errval));
Packit ae235b
                return G_IO_STATUS_ERROR;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      g_assert ((status != G_IO_STATUS_NORMAL)
Packit ae235b
               || (channel->encoded_read_buf->len > 0));
Packit ae235b
    }
Packit ae235b
  else if (channel->encoding) /* UTF-8 */
Packit ae235b
    {
Packit ae235b
      gchar *nextchar, *lastchar;
Packit ae235b
Packit ae235b
      g_assert (channel->encoded_read_buf);
Packit ae235b
Packit ae235b
      nextchar = channel->read_buf->str;
Packit ae235b
      lastchar = channel->read_buf->str + channel->read_buf->len;
Packit ae235b
Packit ae235b
      while (nextchar < lastchar)
Packit ae235b
        {
Packit ae235b
          gunichar val_char;
Packit ae235b
Packit ae235b
          val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
Packit ae235b
Packit ae235b
          switch (val_char)
Packit ae235b
            {
Packit ae235b
              case -2:
Packit ae235b
                /* stop, leave partial character in buffer */
Packit ae235b
                lastchar = nextchar;
Packit ae235b
                break;
Packit ae235b
              case -1:
Packit ae235b
                if (oldlen < channel->encoded_read_buf->len)
Packit ae235b
                  status = G_IO_STATUS_NORMAL;
Packit ae235b
                else
Packit ae235b
                  {
Packit ae235b
                    g_set_error_literal (err, G_CONVERT_ERROR,
Packit ae235b
                      G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                      _("Invalid byte sequence in conversion input"));
Packit ae235b
                    status = G_IO_STATUS_ERROR;
Packit ae235b
                  }
Packit ae235b
                lastchar = nextchar;
Packit ae235b
                break;
Packit ae235b
              default:
Packit ae235b
                nextchar = g_utf8_next_char (nextchar);
Packit ae235b
                break;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (lastchar > channel->read_buf->str)
Packit ae235b
        {
Packit ae235b
          gint copy_len = lastchar - channel->read_buf->str;
Packit ae235b
Packit ae235b
          g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
Packit ae235b
                               copy_len);
Packit ae235b
          g_string_erase (channel->read_buf, 0, copy_len);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read_line:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @str_return: (out): The line read from the #GIOChannel, including the
Packit ae235b
 *              line terminator. This data should be freed with g_free()
Packit ae235b
 *              when no longer needed. This is a nul-terminated string. 
Packit ae235b
 *              If a @length of zero is returned, this will be %NULL instead.
Packit ae235b
 * @length: (out) (optional): location to store length of the read data, or %NULL
Packit ae235b
 * @terminator_pos: (out) (optional): location to store position of line terminator, or %NULL
Packit ae235b
 * @error: A location to return an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Reads a line, including the terminating character(s),
Packit ae235b
 * from a #GIOChannel into a newly-allocated string.
Packit ae235b
 * @str_return will contain allocated memory if the return
Packit ae235b
 * is %G_IO_STATUS_NORMAL.
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_read_line (GIOChannel  *channel,
Packit ae235b
                        gchar      **str_return,
Packit ae235b
                        gsize       *length,
Packit ae235b
			gsize       *terminator_pos,
Packit ae235b
		        GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gsize got_length;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
Packit ae235b
Packit ae235b
  if (length && status != G_IO_STATUS_ERROR)
Packit ae235b
    *length = got_length;
Packit ae235b
Packit ae235b
  if (status == G_IO_STATUS_NORMAL)
Packit ae235b
    {
Packit ae235b
      g_assert (USE_BUF (channel));
Packit ae235b
      *str_return = g_strndup (USE_BUF (channel)->str, got_length);
Packit ae235b
      g_string_erase (USE_BUF (channel), 0, got_length);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    *str_return = NULL;
Packit ae235b
  
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read_line_string:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @buffer: a #GString into which the line will be written.
Packit ae235b
 *          If @buffer already contains data, the old data will
Packit ae235b
 *          be overwritten.
Packit ae235b
 * @terminator_pos: (nullable): location to store position of line terminator, or %NULL
Packit ae235b
 * @error: a location to store an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Reads a line from a #GIOChannel, using a #GString as a buffer.
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_read_line_string (GIOChannel  *channel,
Packit ae235b
                               GString	   *buffer,
Packit ae235b
			       gsize       *terminator_pos,
Packit ae235b
                               GError	  **error)
Packit ae235b
{
Packit ae235b
  gsize length;
Packit ae235b
  GIOStatus status;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (buffer->len > 0)
Packit ae235b
    g_string_truncate (buffer, 0); /* clear out the buffer */
Packit ae235b
Packit ae235b
  status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
Packit ae235b
Packit ae235b
  if (status == G_IO_STATUS_NORMAL)
Packit ae235b
    {
Packit ae235b
      g_assert (USE_BUF (channel));
Packit ae235b
      g_string_append_len (buffer, USE_BUF (channel)->str, length);
Packit ae235b
      g_string_erase (USE_BUF (channel), 0, length);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_channel_read_line_backend (GIOChannel  *channel,
Packit ae235b
                                gsize       *length,
Packit ae235b
                                gsize       *terminator_pos,
Packit ae235b
                                GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gsize checked_to, line_term_len, line_length, got_term_len;
Packit ae235b
  gboolean first_time = TRUE;
Packit ae235b
Packit ae235b
  if (!channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      /* Can't do a raw read in read_line */
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                           _("Can’t do a raw read in g_io_channel_read_line_string"));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
  if (channel->line_term)
Packit ae235b
    line_term_len = channel->line_term_len;
Packit ae235b
  else
Packit ae235b
    line_term_len = 3;
Packit ae235b
    /* This value used for setting checked_to, it's the longest of the four
Packit ae235b
     * we autodetect for.
Packit ae235b
     */
Packit ae235b
Packit ae235b
  checked_to = 0;
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      gchar *nextchar, *lastchar;
Packit ae235b
      GString *use_buf;
Packit ae235b
Packit ae235b
      if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
Packit ae235b
        {
Packit ae235b
read_again:
Packit ae235b
          status = g_io_channel_fill_buffer (channel, error);
Packit ae235b
          switch (status)
Packit ae235b
            {
Packit ae235b
              case G_IO_STATUS_NORMAL:
Packit ae235b
                if (BUF_LEN (USE_BUF (channel)) == 0)
Packit ae235b
                  /* Can happen when using conversion and only read
Packit ae235b
                   * part of a character
Packit ae235b
                   */
Packit ae235b
                  {
Packit ae235b
                    first_time = FALSE;
Packit ae235b
                    continue;
Packit ae235b
                  }
Packit ae235b
                break;
Packit ae235b
              case G_IO_STATUS_EOF:
Packit ae235b
                if (BUF_LEN (USE_BUF (channel)) == 0)
Packit ae235b
                  {
Packit ae235b
                    if (length)
Packit ae235b
                      *length = 0;
Packit ae235b
Packit ae235b
                    if (channel->encoding && channel->read_buf->len != 0)
Packit ae235b
                      {
Packit ae235b
                        g_set_error_literal (error, G_CONVERT_ERROR,
Packit ae235b
                                             G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                                             _("Leftover unconverted data in "
Packit ae235b
                                               "read buffer"));
Packit ae235b
                        return G_IO_STATUS_ERROR;
Packit ae235b
                      }
Packit ae235b
                    else
Packit ae235b
                      return G_IO_STATUS_EOF;
Packit ae235b
                  }
Packit ae235b
                break;
Packit ae235b
              default:
Packit ae235b
                if (length)
Packit ae235b
                  *length = 0;
Packit ae235b
                return status;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_assert (BUF_LEN (USE_BUF (channel)) != 0);
Packit ae235b
Packit ae235b
      use_buf = USE_BUF (channel); /* The buffer has been created by this point */
Packit ae235b
Packit ae235b
      first_time = FALSE;
Packit ae235b
Packit ae235b
      lastchar = use_buf->str + use_buf->len;
Packit ae235b
Packit ae235b
      for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
Packit ae235b
           channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
Packit ae235b
        {
Packit ae235b
          if (channel->line_term)
Packit ae235b
            {
Packit ae235b
              if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
Packit ae235b
                {
Packit ae235b
                  line_length = nextchar - use_buf->str;
Packit ae235b
                  got_term_len = line_term_len;
Packit ae235b
                  goto done;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          else /* auto detect */
Packit ae235b
            {
Packit ae235b
              switch (*nextchar)
Packit ae235b
                {
Packit ae235b
                  case '\n': /* unix */
Packit ae235b
                    line_length = nextchar - use_buf->str;
Packit ae235b
                    got_term_len = 1;
Packit ae235b
                    goto done;
Packit ae235b
                  case '\r': /* Warning: do not use with sockets */
Packit ae235b
                    line_length = nextchar - use_buf->str;
Packit ae235b
                    if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
Packit ae235b
                       && (lastchar == use_buf->str + use_buf->len))
Packit ae235b
                      goto read_again; /* Try to read more data */
Packit ae235b
                    if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
Packit ae235b
                      got_term_len = 2;
Packit ae235b
                    else /* mac */
Packit ae235b
                      got_term_len = 1;
Packit ae235b
                    goto done;
Packit ae235b
                  case '\xe2': /* Unicode paragraph separator */
Packit ae235b
                    if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
Packit ae235b
                      {
Packit ae235b
                        line_length = nextchar - use_buf->str;
Packit ae235b
                        got_term_len = 3;
Packit ae235b
                        goto done;
Packit ae235b
                      }
Packit ae235b
                    break;
Packit ae235b
                  case '\0': /* Embeded null in input */
Packit ae235b
                    line_length = nextchar - use_buf->str;
Packit ae235b
                    got_term_len = 1;
Packit ae235b
                    goto done;
Packit ae235b
                  default: /* no match */
Packit ae235b
                    break;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* If encoding != NULL, valid UTF-8, didn't overshoot */
Packit ae235b
      g_assert (nextchar == lastchar);
Packit ae235b
Packit ae235b
      /* Check for EOF */
Packit ae235b
Packit ae235b
      if (status == G_IO_STATUS_EOF)
Packit ae235b
        {
Packit ae235b
          if (channel->encoding && channel->read_buf->len > 0)
Packit ae235b
            {
Packit ae235b
              g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                                   _("Channel terminates in a partial character"));
Packit ae235b
              return G_IO_STATUS_ERROR;
Packit ae235b
            }
Packit ae235b
          line_length = use_buf->len;
Packit ae235b
          got_term_len = 0;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (use_buf->len > line_term_len - 1)
Packit ae235b
	checked_to = use_buf->len - (line_term_len - 1);
Packit ae235b
      else
Packit ae235b
	checked_to = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
done:
Packit ae235b
Packit ae235b
  if (terminator_pos)
Packit ae235b
    *terminator_pos = line_length;
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = line_length + got_term_len;
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read_to_end:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @str_return:  (out) (array length=length) (element-type guint8): Location to
Packit ae235b
 *              store a pointer to a string holding the remaining data in the
Packit ae235b
 *              #GIOChannel. This data should be freed with g_free() when no
Packit ae235b
 *              longer needed. This data is terminated by an extra nul
Packit ae235b
 *              character, but there may be other nuls in the intervening data.
Packit ae235b
 * @length: (out): location to store length of the data
Packit ae235b
 * @error: location to return an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Reads all the remaining data from the file.
Packit ae235b
 *
Packit ae235b
 * Returns: %G_IO_STATUS_NORMAL on success. 
Packit ae235b
 *     This function never returns %G_IO_STATUS_EOF.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_read_to_end (GIOChannel  *channel,
Packit ae235b
                          gchar      **str_return,
Packit ae235b
                          gsize	      *length,
Packit ae235b
                          GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
    
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
    G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (str_return)
Packit ae235b
    *str_return = NULL;
Packit ae235b
  if (length)
Packit ae235b
    *length = 0;
Packit ae235b
Packit ae235b
  if (!channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                           _("Can’t do a raw read in g_io_channel_read_to_end"));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  do
Packit ae235b
    status = g_io_channel_fill_buffer (channel, error);
Packit ae235b
  while (status == G_IO_STATUS_NORMAL);
Packit ae235b
Packit ae235b
  if (status != G_IO_STATUS_EOF)
Packit ae235b
    return status;
Packit ae235b
Packit ae235b
  if (channel->encoding && channel->read_buf->len > 0)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                           _("Channel terminates in a partial character"));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (USE_BUF (channel) == NULL)
Packit ae235b
    {
Packit ae235b
      /* length is already set to zero */
Packit ae235b
      if (str_return)
Packit ae235b
        *str_return = g_strdup ("");
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (length)
Packit ae235b
        *length = USE_BUF (channel)->len;
Packit ae235b
Packit ae235b
      if (str_return)
Packit ae235b
        *str_return = g_string_free (USE_BUF (channel), FALSE);
Packit ae235b
      else
Packit ae235b
        g_string_free (USE_BUF (channel), TRUE);
Packit ae235b
Packit ae235b
      if (channel->encoding)
Packit ae235b
	channel->encoded_read_buf = NULL;
Packit ae235b
      else
Packit ae235b
	channel->read_buf = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read_chars:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @buf: (out caller-allocates) (array length=count) (element-type guint8):
Packit ae235b
 *     a buffer to read data into
Packit ae235b
 * @count: (in): the size of the buffer. Note that the buffer may not be
Packit ae235b
 *     complelely filled even if there is data in the buffer if the
Packit ae235b
 *     remaining data is not a complete character.
Packit ae235b
 * @bytes_read: (out) (optional): The number of bytes read. This may be
Packit ae235b
 *     zero even on success if count < 6 and the channel's encoding
Packit ae235b
 *     is non-%NULL. This indicates that the next UTF-8 character is
Packit ae235b
 *     too wide for the buffer.
Packit ae235b
 * @error: a location to return an error of type #GConvertError
Packit ae235b
 *     or #GIOChannelError.
Packit ae235b
 *
Packit ae235b
 * Replacement for g_io_channel_read() with the new API.
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 */
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_read_chars (GIOChannel  *channel,
Packit ae235b
                         gchar       *buf,
Packit ae235b
                         gsize        count,
Packit ae235b
                         gsize       *bytes_read,
Packit ae235b
                         GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gsize got_bytes;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    {
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = 0;
Packit ae235b
      return G_IO_STATUS_NORMAL;
Packit ae235b
    }
Packit ae235b
  g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if (!channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      gsize tmp_bytes;
Packit ae235b
Packit ae235b
      g_assert (!channel->read_buf || channel->read_buf->len == 0);
Packit ae235b
Packit ae235b
      status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
Packit ae235b
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = tmp_bytes;
Packit ae235b
Packit ae235b
      return status;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
  while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
Packit ae235b
    status = g_io_channel_fill_buffer (channel, error);
Packit ae235b
Packit ae235b
  /* Only return an error if we have no data */
Packit ae235b
Packit ae235b
  if (BUF_LEN (USE_BUF (channel)) == 0)
Packit ae235b
    {
Packit ae235b
      g_assert (status != G_IO_STATUS_NORMAL);
Packit ae235b
Packit ae235b
      if (status == G_IO_STATUS_EOF && channel->encoding
Packit ae235b
          && BUF_LEN (channel->read_buf) > 0)
Packit ae235b
        {
Packit ae235b
          g_set_error_literal (error, G_CONVERT_ERROR,
Packit ae235b
                               G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                               _("Leftover unconverted data in read buffer"));
Packit ae235b
          status = G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = 0;
Packit ae235b
Packit ae235b
      return status;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (status == G_IO_STATUS_ERROR)
Packit ae235b
    g_clear_error (error);
Packit ae235b
Packit ae235b
  got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
Packit ae235b
Packit ae235b
  g_assert (got_bytes > 0);
Packit ae235b
Packit ae235b
  if (channel->encoding)
Packit ae235b
    /* Don't validate for NULL encoding, binary safe */
Packit ae235b
    {
Packit ae235b
      gchar *nextchar, *prevchar;
Packit ae235b
Packit ae235b
      g_assert (USE_BUF (channel) == channel->encoded_read_buf);
Packit ae235b
Packit ae235b
      nextchar = channel->encoded_read_buf->str;
Packit ae235b
Packit ae235b
      do
Packit ae235b
        {
Packit ae235b
          prevchar = nextchar;
Packit ae235b
          nextchar = g_utf8_next_char (nextchar);
Packit ae235b
          g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
Packit ae235b
        }
Packit ae235b
      while (nextchar < channel->encoded_read_buf->str + got_bytes);
Packit ae235b
Packit ae235b
      if (nextchar > channel->encoded_read_buf->str + got_bytes)
Packit ae235b
        got_bytes = prevchar - channel->encoded_read_buf->str;
Packit ae235b
Packit ae235b
      g_assert (got_bytes > 0 || count < 6);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  memcpy (buf, USE_BUF (channel)->str, got_bytes);
Packit ae235b
  g_string_erase (USE_BUF (channel), 0, got_bytes);
Packit ae235b
Packit ae235b
  if (bytes_read)
Packit ae235b
    *bytes_read = got_bytes;
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_read_unichar:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @thechar: (out): a location to return a character
Packit ae235b
 * @error: a location to return an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Reads a Unicode character from @channel.
Packit ae235b
 * This function cannot be called on a channel with %NULL encoding.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GIOStatus
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_read_unichar (GIOChannel  *channel,
Packit ae235b
			   gunichar    *thechar,
Packit ae235b
			   GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status = G_IO_STATUS_NORMAL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
Packit ae235b
    status = g_io_channel_fill_buffer (channel, error);
Packit ae235b
Packit ae235b
  /* Only return an error if we have no data */
Packit ae235b
Packit ae235b
  if (BUF_LEN (USE_BUF (channel)) == 0)
Packit ae235b
    {
Packit ae235b
      g_assert (status != G_IO_STATUS_NORMAL);
Packit ae235b
Packit ae235b
      if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
Packit ae235b
        {
Packit ae235b
          g_set_error_literal (error, G_CONVERT_ERROR,
Packit ae235b
                               G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                               _("Leftover unconverted data in read buffer"));
Packit ae235b
          status = G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (thechar)
Packit ae235b
        *thechar = (gunichar) -1;
Packit ae235b
Packit ae235b
      return status;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (status == G_IO_STATUS_ERROR)
Packit ae235b
    g_clear_error (error);
Packit ae235b
Packit ae235b
  if (thechar)
Packit ae235b
    *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
Packit ae235b
Packit ae235b
  g_string_erase (channel->encoded_read_buf, 0,
Packit ae235b
                  g_utf8_next_char (channel->encoded_read_buf->str)
Packit ae235b
                  - channel->encoded_read_buf->str);
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_write_chars:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @buf: (array) (element-type guint8): a buffer to write data from
Packit ae235b
 * @count: the size of the buffer. If -1, the buffer
Packit ae235b
 *         is taken to be a nul-terminated string.
Packit ae235b
 * @bytes_written: (out): The number of bytes written. This can be nonzero
Packit ae235b
 *                 even if the return value is not %G_IO_STATUS_NORMAL.
Packit ae235b
 *                 If the return value is %G_IO_STATUS_NORMAL and the
Packit ae235b
 *                 channel is blocking, this will always be equal
Packit ae235b
 *                 to @count if @count >= 0.
Packit ae235b
 * @error: a location to return an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Replacement for g_io_channel_write() with the new API.
Packit ae235b
 *
Packit ae235b
 * On seekable channels with encodings other than %NULL or UTF-8, generic
Packit ae235b
 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
Packit ae235b
 * may only be made on a channel from which data has been read in the
Packit ae235b
 * cases described in the documentation for g_io_channel_set_encoding ().
Packit ae235b
 *
Packit ae235b
 * Returns: the status of the operation.
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_write_chars (GIOChannel   *channel,
Packit ae235b
                          const gchar  *buf,
Packit ae235b
                          gssize        count,
Packit ae235b
			  gsize        *bytes_written,
Packit ae235b
                          GError      **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gssize wrote_bytes = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  if ((count < 0) && buf)
Packit ae235b
    count = strlen (buf);
Packit ae235b
  
Packit ae235b
  if (count == 0)
Packit ae235b
    {
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      return G_IO_STATUS_NORMAL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  /* Raw write case */
Packit ae235b
Packit ae235b
  if (!channel->use_buffer)
Packit ae235b
    {
Packit ae235b
      gsize tmp_bytes;
Packit ae235b
      
Packit ae235b
      g_assert (!channel->write_buf || channel->write_buf->len == 0);
Packit ae235b
      g_assert (channel->partial_write_buf[0] == '\0');
Packit ae235b
      
Packit ae235b
      status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
Packit ae235b
Packit ae235b
      if (bytes_written)
Packit ae235b
	*bytes_written = tmp_bytes;
Packit ae235b
Packit ae235b
      return status;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* General case */
Packit ae235b
Packit ae235b
  if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
Packit ae235b
    || (BUF_LEN (channel->encoded_read_buf) > 0)))
Packit ae235b
    {
Packit ae235b
      if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
Packit ae235b
        {
Packit ae235b
          g_warning("Mixed reading and writing not allowed on encoded files");
Packit ae235b
          return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
      status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
Packit ae235b
      if (status != G_IO_STATUS_NORMAL)
Packit ae235b
        {
Packit ae235b
          if (bytes_written)
Packit ae235b
            *bytes_written = 0;
Packit ae235b
          return status;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!channel->write_buf)
Packit ae235b
    channel->write_buf = g_string_sized_new (channel->buf_size);
Packit ae235b
Packit ae235b
  while (wrote_bytes < count)
Packit ae235b
    {
Packit ae235b
      gsize space_in_buf;
Packit ae235b
Packit ae235b
      /* If the buffer is full, try a write immediately. In
Packit ae235b
       * the nonblocking case, this prevents the user from
Packit ae235b
       * writing just a little bit to the buffer every time
Packit ae235b
       * and never receiving an EAGAIN.
Packit ae235b
       */
Packit ae235b
Packit ae235b
      if (channel->write_buf->len >= channel->buf_size - MAX_CHAR_SIZE)
Packit ae235b
        {
Packit ae235b
          gsize did_write = 0, this_time;
Packit ae235b
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              status = channel->funcs->io_write (channel, channel->write_buf->str
Packit ae235b
                                                 + did_write, channel->write_buf->len
Packit ae235b
                                                 - did_write, &this_time, error);
Packit ae235b
              did_write += this_time;
Packit ae235b
            }
Packit ae235b
          while (status == G_IO_STATUS_NORMAL &&
Packit ae235b
                 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
Packit ae235b
Packit ae235b
          g_string_erase (channel->write_buf, 0, did_write);
Packit ae235b
Packit ae235b
          if (status != G_IO_STATUS_NORMAL)
Packit ae235b
            {
Packit ae235b
              if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
Packit ae235b
                status = G_IO_STATUS_NORMAL;
Packit ae235b
              if (bytes_written)
Packit ae235b
                *bytes_written = wrote_bytes;
Packit ae235b
              return status;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
Packit ae235b
                     - channel->write_buf->len; /* 1 for NULL */
Packit ae235b
Packit ae235b
      /* This is only true because g_io_channel_set_buffer_size ()
Packit ae235b
       * ensures that channel->buf_size >= MAX_CHAR_SIZE.
Packit ae235b
       */
Packit ae235b
      g_assert (space_in_buf >= MAX_CHAR_SIZE);
Packit ae235b
Packit ae235b
      if (!channel->encoding)
Packit ae235b
        {
Packit ae235b
          gssize write_this = MIN (space_in_buf, count - wrote_bytes);
Packit ae235b
Packit ae235b
          g_string_append_len (channel->write_buf, buf, write_this);
Packit ae235b
          buf += write_this;
Packit ae235b
          wrote_bytes += write_this;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          const gchar *from_buf;
Packit ae235b
          gsize from_buf_len, from_buf_old_len, left_len;
Packit ae235b
          gsize err;
Packit ae235b
          gint errnum;
Packit ae235b
Packit ae235b
          if (channel->partial_write_buf[0] != '\0')
Packit ae235b
            {
Packit ae235b
              g_assert (wrote_bytes == 0);
Packit ae235b
Packit ae235b
              from_buf = channel->partial_write_buf;
Packit ae235b
              from_buf_old_len = strlen (channel->partial_write_buf);
Packit ae235b
              g_assert (from_buf_old_len > 0);
Packit ae235b
              from_buf_len = MIN (6, from_buf_old_len + count);
Packit ae235b
Packit ae235b
              memcpy (channel->partial_write_buf + from_buf_old_len, buf,
Packit ae235b
                      from_buf_len - from_buf_old_len);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              from_buf = buf;
Packit ae235b
              from_buf_len = count - wrote_bytes;
Packit ae235b
              from_buf_old_len = 0;
Packit ae235b
            }
Packit ae235b
Packit ae235b
reconvert:
Packit ae235b
Packit ae235b
          if (!channel->do_encode) /* UTF-8 encoding */
Packit ae235b
            {
Packit ae235b
              const gchar *badchar;
Packit ae235b
              gsize try_len = MIN (from_buf_len, space_in_buf);
Packit ae235b
Packit ae235b
              /* UTF-8, just validate, emulate g_iconv */
Packit ae235b
Packit ae235b
              if (!g_utf8_validate (from_buf, try_len, &badchar))
Packit ae235b
                {
Packit ae235b
                  gunichar try_char;
Packit ae235b
                  gsize incomplete_len = from_buf + try_len - badchar;
Packit ae235b
Packit ae235b
                  left_len = from_buf + from_buf_len - badchar;
Packit ae235b
Packit ae235b
                  try_char = g_utf8_get_char_validated (badchar, incomplete_len);
Packit ae235b
Packit ae235b
                  switch (try_char)
Packit ae235b
                    {
Packit ae235b
                      case -2:
Packit ae235b
                        g_assert (incomplete_len < 6);
Packit ae235b
                        if (try_len == from_buf_len)
Packit ae235b
                          {
Packit ae235b
                            errnum = EINVAL;
Packit ae235b
                            err = (gsize) -1;
Packit ae235b
                          }
Packit ae235b
                        else
Packit ae235b
                          {
Packit ae235b
                            errnum = 0;
Packit ae235b
                            err = (gsize) 0;
Packit ae235b
                          }
Packit ae235b
                        break;
Packit ae235b
                      case -1:
Packit ae235b
                        g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
Packit ae235b
                        /* FIXME bail here? */
Packit ae235b
                        errnum = EILSEQ;
Packit ae235b
                        err = (gsize) -1;
Packit ae235b
                        break;
Packit ae235b
                      default:
Packit ae235b
                        g_assert_not_reached ();
Packit ae235b
                        err = (gsize) -1;
Packit ae235b
                        errnum = 0; /* Don't confunse the compiler */
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  err = (gsize) 0;
Packit ae235b
                  errnum = 0;
Packit ae235b
                  left_len = from_buf_len - try_len;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              g_string_append_len (channel->write_buf, from_buf,
Packit ae235b
                                   from_buf_len - left_len);
Packit ae235b
              from_buf += from_buf_len - left_len;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
               gchar *outbuf;
Packit ae235b
Packit ae235b
               left_len = from_buf_len;
Packit ae235b
               g_string_set_size (channel->write_buf, channel->write_buf->len
Packit ae235b
                                  + space_in_buf);
Packit ae235b
               outbuf = channel->write_buf->str + channel->write_buf->len
Packit ae235b
                        - space_in_buf;
Packit ae235b
               err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
Packit ae235b
                              &outbuf, &space_in_buf);
Packit ae235b
               errnum = errno;
Packit ae235b
               g_string_truncate (channel->write_buf, channel->write_buf->len
Packit ae235b
                                  - space_in_buf);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (err == (gsize) -1)
Packit ae235b
            {
Packit ae235b
              switch (errnum)
Packit ae235b
        	{
Packit ae235b
                  case EINVAL:
Packit ae235b
                    g_assert (left_len < 6);
Packit ae235b
Packit ae235b
                    if (from_buf_old_len == 0)
Packit ae235b
                      {
Packit ae235b
                        /* Not from partial_write_buf */
Packit ae235b
Packit ae235b
                        memcpy (channel->partial_write_buf, from_buf, left_len);
Packit ae235b
                        channel->partial_write_buf[left_len] = '\0';
Packit ae235b
                        if (bytes_written)
Packit ae235b
                          *bytes_written = count;
Packit ae235b
                        return G_IO_STATUS_NORMAL;
Packit ae235b
                      }
Packit ae235b
Packit ae235b
                    /* Working in partial_write_buf */
Packit ae235b
Packit ae235b
                    if (left_len == from_buf_len)
Packit ae235b
                      {
Packit ae235b
                        /* Didn't convert anything, must still have
Packit ae235b
                         * less than a full character
Packit ae235b
                         */
Packit ae235b
Packit ae235b
                        g_assert (count == from_buf_len - from_buf_old_len);
Packit ae235b
Packit ae235b
                        channel->partial_write_buf[from_buf_len] = '\0';
Packit ae235b
Packit ae235b
                        if (bytes_written)
Packit ae235b
                          *bytes_written = count;
Packit ae235b
Packit ae235b
                        return G_IO_STATUS_NORMAL;
Packit ae235b
                      }
Packit ae235b
Packit ae235b
                    g_assert (from_buf_len - left_len >= from_buf_old_len);
Packit ae235b
Packit ae235b
                    /* We converted all the old data. This is fine */
Packit ae235b
Packit ae235b
                    break;
Packit ae235b
                  case E2BIG:
Packit ae235b
                    if (from_buf_len == left_len)
Packit ae235b
                      {
Packit ae235b
                        /* Nothing was written, add enough space for
Packit ae235b
                         * at least one character.
Packit ae235b
                         */
Packit ae235b
                        space_in_buf += MAX_CHAR_SIZE;
Packit ae235b
                        goto reconvert;
Packit ae235b
                      }
Packit ae235b
                    break;
Packit ae235b
                  case EILSEQ:
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
                    if (from_buf_old_len > 0 && from_buf_len == left_len)
Packit ae235b
                      g_warning ("Illegal sequence due to partial character "
Packit ae235b
                                 "at the end of a previous write.\n");
Packit ae235b
                    else
Packit ae235b
                      wrote_bytes += from_buf_len - left_len - from_buf_old_len;
Packit ae235b
                    if (bytes_written)
Packit ae235b
                      *bytes_written = wrote_bytes;
Packit ae235b
                    channel->partial_write_buf[0] = '\0';
Packit ae235b
                    return G_IO_STATUS_ERROR;
Packit ae235b
                  default:
Packit ae235b
                    g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                      _("Error during conversion: %s"), g_strerror (errnum));
Packit ae235b
                    if (from_buf_len >= left_len + from_buf_old_len)
Packit ae235b
                      wrote_bytes += from_buf_len - left_len - from_buf_old_len;
Packit ae235b
                    if (bytes_written)
Packit ae235b
                      *bytes_written = wrote_bytes;
Packit ae235b
                    channel->partial_write_buf[0] = '\0';
Packit ae235b
                    return G_IO_STATUS_ERROR;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_assert (from_buf_len - left_len >= from_buf_old_len);
Packit ae235b
Packit ae235b
          wrote_bytes += from_buf_len - left_len - from_buf_old_len;
Packit ae235b
Packit ae235b
          if (from_buf_old_len > 0)
Packit ae235b
            {
Packit ae235b
              /* We were working in partial_write_buf */
Packit ae235b
Packit ae235b
              buf += from_buf_len - left_len - from_buf_old_len;
Packit ae235b
              channel->partial_write_buf[0] = '\0';
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            buf = from_buf;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = count;
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_write_unichar:
Packit ae235b
 * @channel: a #GIOChannel
Packit ae235b
 * @thechar: a character
Packit ae235b
 * @error: location to return an error of type #GConvertError
Packit ae235b
 *         or #GIOChannelError
Packit ae235b
 *
Packit ae235b
 * Writes a Unicode character to @channel.
Packit ae235b
 * This function cannot be called on a channel with %NULL encoding.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GIOStatus
Packit ae235b
 **/
Packit ae235b
GIOStatus
Packit ae235b
g_io_channel_write_unichar (GIOChannel  *channel,
Packit ae235b
			    gunichar     thechar,
Packit ae235b
			    GError     **error)
Packit ae235b
{
Packit ae235b
  GIOStatus status;
Packit ae235b
  gchar static_buf[6];
Packit ae235b
  gsize char_len, wrote_len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL),
Packit ae235b
			G_IO_STATUS_ERROR);
Packit ae235b
  g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
Packit ae235b
Packit ae235b
  char_len = g_unichar_to_utf8 (thechar, static_buf);
Packit ae235b
Packit ae235b
  if (channel->partial_write_buf[0] != '\0')
Packit ae235b
    {
Packit ae235b
      g_warning ("Partial character written before writing unichar.\n");
Packit ae235b
      channel->partial_write_buf[0] = '\0';
Packit ae235b
    }
Packit ae235b
Packit ae235b
  status = g_io_channel_write_chars (channel, static_buf,
Packit ae235b
                                     char_len, &wrote_len, error);
Packit ae235b
Packit ae235b
  /* We validate UTF-8, so we can't get a partial write */
Packit ae235b
Packit ae235b
  g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
Packit ae235b
Packit ae235b
  return status;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_IO_CHANNEL_ERROR:
Packit ae235b
 *
Packit ae235b
 * Error domain for #GIOChannel operations. Errors in this domain will
Packit ae235b
 * be from the #GIOChannelError enumeration. See #GError for
Packit ae235b
 * information on error domains.
Packit ae235b
 **/
Packit ae235b
/**
Packit ae235b
 * GIOChannelError:
Packit ae235b
 * @G_IO_CHANNEL_ERROR_FBIG: File too large.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_INVAL: Invalid argument.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_IO: IO error.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_ISDIR: File is a directory.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_NOSPC: No space left on device.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_NXIO: No such device or address.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_OVERFLOW: Value too large for defined datatype.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_PIPE: Broken pipe.
Packit ae235b
 * @G_IO_CHANNEL_ERROR_FAILED: Some other error.
Packit ae235b
 *
Packit ae235b
 * Error codes returned by #GIOChannel operations.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-io-channel-error-quark, g_io_channel_error)