## IO Channels IO Channels — portable support for using files, pipes and sockets |

## Synopsis

    #include <glib.h>

                        GIOChannel;

    GIOChannel*         g_io_channel_unix_new               (int fd);
    gint                g_io_channel_unix_get_fd            (GIOChannel *channel);
    GIOChannel*         g_io_channel_win32_new_fd           (gint fd);
    GIOChannel *        g_io_channel_win32_new_socket       (gint socket);
    GIOChannel *        g_io_channel_win32_new_messages     (gsize hwnd);

    void                g_io_channel_init                   (GIOChannel *channel);

    GIOChannel*         g_io_channel_new_file               (const gchar *filename,
                                                             const gchar *mode,
                                                             GError **error);
    GIOStatus           g_io_channel_read_chars             (GIOChannel *channel,
                                                             gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_read,
                                                             GError **error);
    GIOStatus           g_io_channel_read_unichar           (GIOChannel *channel,
                                                             gunichar *thechar,
                                                             GError **error);
    GIOStatus           g_io_channel_read_line              (GIOChannel *channel,
                                                             gchar **str_return,
                                                             gsize *length,
                                                             gsize *terminator_pos,
                                                             GError **error);
    GIOStatus           g_io_channel_read_line_string       (GIOChannel *channel,
                                                             GString *buffer,
                                                             gsize *terminator_pos,
                                                             GError **error);
    GIOStatus           g_io_channel_read_to_end            (GIOChannel *channel,
                                                             gchar **str_return,
                                                             gsize *length,
                                                             GError **error);
    GIOStatus           g_io_channel_write_chars            (GIOChannel *channel,
                                                             const gchar *buf,
                                                             gssize count,
                                                             gsize *bytes_written,
                                                             GError **error);
    GIOStatus           g_io_channel_write_unichar          (GIOChannel *channel,
                                                             gunichar thechar,
                                                             GError **error);
    GIOStatus           g_io_channel_flush                  (GIOChannel *channel,
                                                             GError **error);
    GIOStatus           g_io_channel_seek_position          (GIOChannel *channel,
                                                             gint64 offset,
                                                             GSeekType type,
                                                             GError **error);
    enum                GSeekType;
    GIOStatus           g_io_channel_shutdown               (GIOChannel *channel,
                                                             gboolean flush,
                                                             GError **err);

    enum                GIOStatus;
    enum                GIOChannelError;
    #define             G_IO_CHANNEL_ERROR
    GIOChannelError     g_io_channel_error_from_errno       (gint en);

    GIOChannel *        g_io_channel_ref                    (GIOChannel *channel);
    void                g_io_channel_unref                  (GIOChannel *channel);

    GSource *           g_io_create_watch                   (GIOChannel *channel,
                                                             GIOCondition condition);
    guint               g_io_add_watch                      (GIOChannel *channel,
                                                             GIOCondition condition,
                                                             GIOFunc func,
                                                             gpointer user_data);
    guint               g_io_add_watch_full                 (GIOChannel *channel,
                                                             gint priority,
                                                             GIOCondition condition,
                                                             GIOFunc func,
                                                             gpointer user_data,
                                                             GDestroyNotify notify);
    enum                GIOCondition;
    gboolean            (*GIOFunc)                          (GIOChannel *source,
                                                             GIOCondition condition,
                                                             gpointer data);

                        GIOFuncs;

    gsize               g_io_channel_get_buffer_size        (GIOChannel *channel);
    void                g_io_channel_set_buffer_size        (GIOChannel *channel,
                                                             gsize size);
    GIOCondition        g_io_channel_get_buffer_condition   (GIOChannel *channel);
    GIOFlags            g_io_channel_get_flags              (GIOChannel *channel);
    GIOStatus           g_io_channel_set_flags              (GIOChannel *channel,
                                                             GIOFlags flags,
                                                             GError **error);
    enum                GIOFlags;
    const gchar*        g_io_channel_get_line_term          (GIOChannel *channel,
                                                             gint *length);
    void                g_io_channel_set_line_term          (GIOChannel *channel,
                                                             const gchar *line_term,
                                                             gint length);
    gboolean            g_io_channel_get_buffered           (GIOChannel *channel);
    void                g_io_channel_set_buffered           (GIOChannel *channel,
                                                             gboolean buffered);
    const gchar*        g_io_channel_get_encoding           (GIOChannel *channel);
    GIOStatus           g_io_channel_set_encoding           (GIOChannel *channel,
                                                             const gchar *encoding,
                                                             GError **error);
    gboolean            g_io_channel_get_close_on_unref     (GIOChannel *channel);
    void                g_io_channel_set_close_on_unref     (GIOChannel *channel,
                                                             gboolean do_close);

    GIOError            g_io_channel_read                   (GIOChannel *channel,
                                                             gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_read);
    enum                GIOError;
    GIOError            g_io_channel_write                  (GIOChannel *channel,
                                                             const gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_written);
    GIOError            g_io_channel_seek                   (GIOChannel *channel,
                                                             gint64 offset,
                                                             GSeekType type);
    void                g_io_channel_close                  (GIOChannel *channel);

## Description

The GIOChannel data type aims to provide a portable method for using file descriptors, pipes, and sockets, and integrating them into the main event loop. Currently full support is available on UNIX platforms, support for Windows is only partially complete.

To create a new GIOChannel on UNIX systems use `g_io_channel_unix_new()`. This works for plain file descriptors, pipes and sockets. Alternatively, a channel can be created for a file in a system independent manner using `g_io_channel_new_file()`.

Once a GIOChannel has been created, it can be used in a generic manner with the functions `g_io_channel_read_chars()`, `g_io_channel_write_chars()`, `g_io_channel_seek_position()`, and `g_io_channel_shutdown()`.

To add a GIOChannel to the main event loop use `g_io_add_watch()` or `g_io_add_watch_full()`. Here you specify which events you are interested in on the GIOChannel, and provide a function to be called whenever these events occur.

GIOChannel instances are created with an initial reference count of 1. `g_io_channel_ref()` and `g_io_channel_unref()` can be used to increment or decrement the reference count respectively. When the reference count falls to 0, the GIOChannel is freed. (Though it isn't closed automatically, unless it was created using `g_io_channel_new_from_file()`.) Using `g_io_add_watch()` or `g_io_add_watch_full()` increments a channel's reference count.

The new functions `g_io_channel_read_chars()`, `g_io_channel_read_line()`, `g_io_channel_read_line_string()`, `g_io_channel_read_to_end()`, `g_io_channel_write_chars()`, `g_io_channel_seek_position()`, and `g_io_channel_flush()` should not be mixed with the deprecated functions `g_io_channel_read()`, `g_io_channel_write()`, and `g_io_channel_seek()` on the same channel.

## Details

### GIOChannel

    typedef struct {
    } GIOChannel;

A data structure representing an IO Channel. The fields should be considered private and should only be accessed with the following functions.

### g_io_channel_unix_new ()

    GIOChannel*         g_io_channel_unix_new               (int fd);

Creates a new GIOChannel given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.

The returned GIOChannel has a reference count of 1.

The default encoding for GIOChannel is UTF-8. If your application is reading output from a command using via pipe, you may need to set the encoding to the encoding of the current locale (see `g_get_charset()`) with the `g_io_channel_set_encoding()` function.

If you want to read raw binary data without interpretation, then call the `g_io_channel_set_encoding()` function with `NULL` for the encoding argument.

This function is available in GLib on Windows, too, but you should avoid using it on Windows. The domain of file descriptors and sockets overlap. There is no way for GLib to know which one you mean in case the argument you pass to this function happens to be both a valid file descriptor and socket. If that happens a warning is issued, and GLib assumes that it is the file descriptor you mean.

`fd` : | a file descriptor.
Returns : | a new GIOChannel.

### g_io_channel_unix_get_fd ()

    gint                g_io_channel_unix_get_fd            (GIOChannel *channel);

Returns the file descriptor of the GIOChannel.

On Windows this function returns the file descriptor or socket of the GIOChannel.

`channel` : | a GIOChannel, created with `g_io_channel_unix_new()`.
Returns : | the file descriptor of the GIOChannel.

### g_io_channel_win32_new_fd ()

    GIOChannel*         g_io_channel_win32_new_fd           (gint fd);

Creates a new GIOChannel given a file descriptor on Windows. This works for file descriptors from the C runtime.

This function works for file descriptors as returned by the `open()`, `creat()`, `pipe()` and `fileno()` calls in the Microsoft C runtime. In order to meaningfully use this function your code should use the same C runtime as GLib uses, which is msvcrt.dll. Note that in current Microsoft compilers it is near impossible to convince it to build code that would use msvcrt.dll. The last Microsoft compiler version that supported using msvcrt.dll as the C runtime was version 6. The GNU compiler and toolchain for Windows, also known as Mingw, fully supports msvcrt.dll.

If you have created a GIOChannel for a file descriptor and started watching (polling) it, you shouldn't call `read()` on the file descriptor. This is because adding polling for a file descriptor is implemented in GLib on Windows by starting a thread that sits blocked in a `read()` from the file descriptor most of the time. All reads from the file descriptor should be done by this internal GLib thread. Your code should call only `g_io_channel_read()`.

This function is available only in GLib on Windows.

`fd` : | a C library file descriptor.
Returns : | a new GIOChannel.

### g_io_channel_win32_new_socket ()

    GIOChannel *        g_io_channel_win32_new_socket       (gint socket);

Creates a new GIOChannel given a socket on Windows.

This function works for sockets created by Winsock. It's available only in GLib on Windows.

Polling a GSource created to watch a channel for a socket puts the socket in non-blocking mode. This is a side-effect of the implementation and unavoidable.

`socket` : | a Winsock socket
Returns : | a new GIOChannel

### g_io_channel_win32_new_messages ()

    GIOChannel *        g_io_channel_win32_new_messages     (gsize hwnd);

Creates a new GIOChannel given a window handle on Windows.

This function creates a GIOChannel that can be used to poll for Windows messages for the window in question.

`hwnd` : | a window handle.
Returns : | a new GIOChannel.

### g_io_channel_init ()

    void                g_io_channel_init                   (GIOChannel *channel);

Initializes a GIOChannel struct.

This is called by each of the above functions when creating a GIOChannel, and so is not often needed by the application programmer (unless you are creating a new type of GIOChannel).

`channel` : | a GIOChannel

### g_io_channel_new_file ()

    GIOChannel*         g_io_channel_new_file               (const gchar *filename,
                                                             const gchar *mode,
                                                             GError **error);

Open a file `filename` as a GIOChannel using mode `mode`. This channel will be closed when the last reference to it is dropped, so there is no need to call `g_io_channel_close()` (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

`filename` : | A string containing the name of a file
`mode` : | One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as in `fopen()`
`error` : | A location to return an error of type `G_FILE_ERROR`
Returns : | A GIOChannel on success, `NULL` on failure.

### g_io_channel_read_chars ()

    GIOStatus           g_io_channel_read_chars             (GIOChannel *channel,
                                                             gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_read,
                                                             GError **error);

Replacement for `g_io_channel_read()` with the new API.

`channel` : | a GIOChannel
`buf` : | a buffer to read data into
`count` : | the size of the buffer. Note that the buffer may not be complelely filled even if there is data in the buffer if the remaining data is not a complete character.
`bytes_read` : | The number of bytes read. This may be zero even on success if count < 6 and the channel's encoding is non-`NULL`. This indicates that the next UTF-8 character is too wide for the buffer.
`error` : | a location to return an error of type GConvertError or GIOChannelError.
Returns : | the status of the operation.

### g_io_channel_read_unichar ()

    GIOStatus           g_io_channel_read_unichar           (GIOChannel *channel,
                                                             gunichar *thechar,
                                                             GError **error);

Reads a Unicode character from `channel`. This function cannot be called on a channel with `NULL` encoding.

`channel` : | a GIOChannel
`thechar` : | a location to return a character
`error` : | a location to return an error of type GConvertError or GIOChannelError
Returns : | a GIOStatus

### g_io_channel_read_line ()

    GIOStatus           g_io_channel_read_line              (GIOChannel *channel,
                                                             gchar **str_return,
                                                             gsize *length,
                                                             gsize *terminator_pos,
                                                             GError **error);

Reads a line, including the terminating character(s), from a GIOChannel into a newly-allocated string. `str_return` will contain allocated memory if the return is `G_IO_STATUS_NORMAL`.

`channel` : | a GIOChannel
`str_return` : | The line read from the GIOChannel, including the line terminator. This data should be freed with `g_free()` when no longer needed. This is a nul-terminated string. If a `length` of zero is returned, this will be `NULL` instead.
`length` : | location to store length of the read data, or `NULL`
`terminator_pos` : | location to store position of line terminator, or `NULL`
`error` : | A location to return an error of type GConvertError or GIOChannelError
Returns : | the status of the operation.

### g_io_channel_read_line_string ()

    GIOStatus           g_io_channel_read_line_string       (GIOChannel *channel,
                                                             GString *buffer,
                                                             gsize *terminator_pos,
                                                             GError **error);

Reads a line from a GIOChannel, using a GString as a buffer.

`channel` : | a GIOChannel
`buffer` : | a GString into which the line will be written. If `buffer` already contains data, the old data will be overwritten.
`terminator_pos` : | location to store position of line terminator, or `NULL`
`error` : | a location to store an error of type GConvertError or GIOChannelError
Returns : | the status of the operation.

### g_io_channel_read_to_end ()

    GIOStatus           g_io_channel_read_to_end            (GIOChannel *channel,
                                                             gchar **str_return,
                                                             gsize *length,
                                                             GError **error);

Reads all the remaining data from the file.

`channel` : | a GIOChannel
`str_return` : | Location to store a pointer to a string holding the remaining data in the GIOChannel. This data should be freed with `g_free()` when no longer needed. This data is terminated by an extra nul character, but there may be other nuls in the intervening data.
`length` : | location to store length of the data
`error` : | location to return an error of type GConvertError or GIOChannelError
Returns : | `G_IO_STATUS_NORMAL` on success. This function never returns `G_IO_STATUS_EOF`.

### g_io_channel_write_chars ()

    GIOStatus           g_io_channel_write_chars            (GIOChannel *channel,
                                                             const gchar *buf,
                                                             gssize count,
                                                             gsize *bytes_written,
                                                             GError **error);

Replacement for `g_io_channel_write()` with the new API.

On seekable channels with encodings other than `NULL` or UTF-8, generic mixing of reading and writing is not allowed. A call to `g_io_channel_write_chars()` may only be made on a channel from which data has been read in the cases described in the documentation for `g_io_channel_set_encoding()`.

`channel` : | a GIOChannel
`buf` : | a buffer to write data from
`count` : | the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.
`bytes_written` : | The number of bytes written. This can be nonzero even if the return value is not `G_IO_STATUS_NORMAL`. If the return value is `G_IO_STATUS_NORMAL` and the channel is blocking, this will always be equal to `count` if `count` >= 0.
`error` : | a location to return an error of type GConvertError or GIOChannelError
Returns : | the status of the operation.

### g_io_channel_write_unichar ()

    GIOStatus           g_io_channel_write_unichar          (GIOChannel *channel,
                                                             gunichar thechar,
                                                             GError **error);

Writes a Unicode character to `channel`. This function cannot be called on a channel with `NULL` encoding.

`channel` : | a GIOChannel
`thechar` : | a character
`error` : | location to return an error of type GConvertError or GIOChannelError
Returns : | a GIOStatus

### g_io_channel_flush ()

    GIOStatus           g_io_channel_flush                  (GIOChannel *channel,
                                                             GError **error);

Flushes the write buffer for the GIOChannel.

`channel` : | a GIOChannel
`error` : | location to store an error of type GIOChannelError
Returns : | the status of the operation: One of G_IO_STATUS_NORMAL, G_IO_STATUS_AGAIN, or G_IO_STATUS_ERROR.

### g_io_channel_seek_position ()

    GIOStatus           g_io_channel_seek_position          (GIOChannel *channel,
                                                             gint64 offset,
                                                             GSeekType type,
                                                             GError **error);

Replacement for `g_io_channel_seek()` with the new API.

`channel` : | a GIOChannel
`offset` : | The offset in bytes from the position specified by `type`
`type` : | a GSeekType. The type `G_SEEK_CUR` is only allowed in those cases where a call to `g_io_channel_set_encoding()` is allowed. See the documentation for `g_io_channel_set_encoding()` for details.
`error` : | A location to return an error of type GIOChannelError
Returns : | the status of the operation.

### enum GSeekType

    typedef enum
    {
      G_SEEK_CUR,
      G_SEEK_SET,
      G_SEEK_END
    } GSeekType;

An enumeration specifying the base position for a `g_io_channel_seek_position()` operation.

`G_SEEK_CUR` | the current position in the file.
`G_SEEK_SET` | the start of the file.
`G_SEEK_END` | the end of the file.

### g_io_channel_shutdown ()

    GIOStatus           g_io_channel_shutdown               (GIOChannel *channel,
                                                             gboolean flush,
                                                             GError **err);

Close an IO channel. Any pending data to be written will be flushed if `flush` is `TRUE`. The channel will not be freed until the last reference is dropped using `g_io_channel_unref()`.

`channel` : | a GIOChannel
`flush` : | if `TRUE`, flush pending
`err` : | location to store a GIOChannelError
Returns : | the status of the operation.

### enum GIOStatus

    typedef enum
    {
      G_IO_STATUS_ERROR,
      G_IO_STATUS_NORMAL,
      G_IO_STATUS_EOF,
      G_IO_STATUS_AGAIN
    } GIOStatus;

Stati returned by most of the GIOFuncs functions.

`G_IO_STATUS_ERROR` | An error occurred.
`G_IO_STATUS_NORMAL` | Success.
`G_IO_STATUS_EOF` | End of file.
`G_IO_STATUS_AGAIN` | Resource temporarily unavailable.

### enum GIOChannelError

    typedef enum
    {
      /* Derived from errno */
      G_IO_CHANNEL_ERROR_FBIG,
      G_IO_CHANNEL_ERROR_INVAL,
      G_IO_CHANNEL_ERROR_IO,
      G_IO_CHANNEL_ERROR_ISDIR,
      G_IO_CHANNEL_ERROR_NOSPC,
      G_IO_CHANNEL_ERROR_NXIO,
      G_IO_CHANNEL_ERROR_OVERFLOW,
      G_IO_CHANNEL_ERROR_PIPE,
      /* Other */
      G_IO_CHANNEL_ERROR_FAILED
    } GIOChannelError;

Error codes returned by GIOChannel operations.

`G_IO_CHANNEL_ERROR_FBIG` | File too large.
`G_IO_CHANNEL_ERROR_INVAL` | Invalid argument.
`G_IO_CHANNEL_ERROR_IO` | IO error.
`G_IO_CHANNEL_ERROR_ISDIR` | File is a directory.
`G_IO_CHANNEL_ERROR_NOSPC` | No space left on device.
`G_IO_CHANNEL_ERROR_NXIO` | No such device or address.
`G_IO_CHANNEL_ERROR_OVERFLOW` | Value too large for defined datatype.
`G_IO_CHANNEL_ERROR_PIPE` | Broken pipe.
`G_IO_CHANNEL_ERROR_FAILED` | Some other error.

### G_IO_CHANNEL_ERROR

    #define G_IO_CHANNEL_ERROR g_io_channel_error_quark()

Error domain for GIOChannel operations. Errors in this domain will be from the GIOChannelError enumeration. See GError for information on error domains.

### g_io_channel_error_from_errno ()

    GIOChannelError     g_io_channel_error_from_errno       (gint en);

Converts an `errno` error number to a GIOChannelError.

`en` : | an `errno` error number, e.g. `EINVAL`
Returns : | a GIOChannelError error number, e.g. `G_IO_CHANNEL_ERROR_INVAL`.

### g_io_channel_ref ()

    GIOChannel *        g_io_channel_ref                    (GIOChannel *channel);

Increments the reference count of a GIOChannel.

`channel` : | a GIOChannel
Returns : | the `channel` that was passed in (since 2.6)

### g_io_channel_unref ()

    void                g_io_channel_unref                  (GIOChannel *channel);

Decrements the reference count of a GIOChannel.

`channel` : | a GIOChannel

### g_io_create_watch ()

    GSource *           g_io_create_watch                   (GIOChannel *channel,
                                                             GIOCondition condition);

Creates a GSource that's dispatched when `condition` is met for the given `channel`. For example, if condition is G_IO_IN, the source will be dispatched when there's data available for reading.

`g_io_add_watch()` is a simpler interface to this same functionality, for the case where you want to add the source to the default main loop context at the default priority.

On Windows, polling a GSource created to watch a channel for a socket puts the socket in non-blocking mode. This is a side-effect of the implementation and unavoidable.

`channel` : | a GIOChannel to watch
`condition` : | conditions to watch for
Returns : | a new GSource

### g_io_add_watch ()

    guint               g_io_add_watch                      (GIOChannel *channel,
                                                             GIOCondition condition,
                                                             GIOFunc func,
                                                             gpointer user_data);

Adds the GIOChannel into the default main loop context with the default priority.

`channel` : | a GIOChannel
`condition` : | the condition to watch for
`func` : | the function to call when the condition is satisfied
`user_data` : | user data to pass to `func`
Returns : | the event source id

### g_io_add_watch_full ()

    guint               g_io_add_watch_full                 (GIOChannel *channel,
                                                             gint priority,
                                                             GIOCondition condition,
                                                             GIOFunc func,
                                                             gpointer user_data,
                                                             GDestroyNotify notify);

Adds the GIOChannel into the default main loop context with the given priority.

This internally creates a main loop source using `g_io_create_watch()` and attaches it to the main loop context with `g_source_attach()`. You can do these steps manuallt if you need greater control.

`channel` : | a GIOChannel
`priority` : | the priority of the GIOChannel source
`condition` : | the condition to watch for
`func` : | the function to call when the condition is satisfied
`user_data` : | user data to pass to `func`
`notify` : | the function to call when the source is removed
Returns : | the event source id

### enum GIOCondition

    typedef enum
    {
      G_IO_IN	GLIB_SYSDEF_POLLIN,
      G_IO_OUT	GLIB_SYSDEF_POLLOUT,
      G_IO_PRI	GLIB_SYSDEF_POLLPRI,
      G_IO_ERR	GLIB_SYSDEF_POLLERR,
      G_IO_HUP	GLIB_SYSDEF_POLLHUP,
      G_IO_NVAL	GLIB_SYSDEF_POLLNVAL
    } GIOCondition;

A bitwise combination representing a condition to watch for on an event source.

`G_IO_IN` | There is data to read.
`G_IO_OUT` | Data can be written (without blocking).
`G_IO_PRI` | There is urgent data to read.
`G_IO_ERR` | Error condition.
`G_IO_HUP` | Hung up (the connection has been broken, usually for pipes and sockets).
`G_IO_NVAL` | Invalid request. The file descriptor is not open.

### GIOFunc ()

    gboolean            (*GIOFunc)                          (GIOChannel *source,
                                                             GIOCondition condition,
                                                             gpointer data);

Specifies the type of function passed to `g_io_add_watch()` or `g_io_add_watch_full()`, which is called when the requested condition on a GIOChannel is satisfied.

`source` : | the GIOChannel event source
`condition` : | the condition which has been satisfied
`data` : | user data set in `g_io_add_watch()` or `g_io_add_watch_full()`
Returns : | the function should return `FALSE` if the event source should be removed

### GIOFuncs

    typedef struct {
      GIOStatus (*io_read)           (GIOChannel   *channel,
    			          gchar        *buf,
    				  gsize         count,
    				  gsize        *bytes_read,
    				  GError      **err);
      GIOStatus (*io_write)          (GIOChannel   *channel,
    				  const gchar  *buf,
    				  gsize         count,
    				  gsize        *bytes_written,
    				  GError      **err);
      GIOStatus (*io_seek)           (GIOChannel   *channel,
    				  gint64        offset,
    				  GSeekType     type,
    				  GError      **err);
      GIOStatus  (*io_close)         (GIOChannel   *channel,
    				  GError      **err);
      GSource*   (*io_create_watch)  (GIOChannel   *channel,
    				  GIOCondition  condition);
      void       (*io_free)          (GIOChannel   *channel);
      GIOStatus  (*io_set_flags)     (GIOChannel   *channel,
                                      GIOFlags      flags,
    				  GError      **err);
      GIOFlags   (*io_get_flags)     (GIOChannel   *channel);
    } GIOFuncs;

A table of functions used to handle different types of GIOChannel in a generic way.

### g_io_channel_get_buffer_size ()

    gsize               g_io_channel_get_buffer_size        (GIOChannel *channel);

Gets the buffer size.

`channel` : | a GIOChannel
Returns : | the size of the buffer.

### g_io_channel_set_buffer_size ()

    void                g_io_channel_set_buffer_size        (GIOChannel *channel,
                                                             gsize size);

Sets the buffer size.

`channel` : | a GIOChannel
`size` : | the size of the buffer, or 0 to let GLib pick a good size

### g_io_channel_get_buffer_condition ()

    GIOCondition        g_io_channel_get_buffer_condition   (GIOChannel *channel);

This function returns a GIOCondition depending on whether there is data to be read/space to write data in the internal buffers in the GIOChannel. Only the flags `G_IO_IN` and `G_IO_OUT` may be set.

`channel` : | A GIOChannel
Returns : | A GIOCondition

### g_io_channel_get_flags ()

    GIOFlags            g_io_channel_get_flags              (GIOChannel *channel);

Gets the current flags for a GIOChannel, including read-only flags such as `G_IO_FLAG_IS_READABLE`.

The values of the flags `G_IO_FLAG_IS_READABLE` and `G_IO_FLAG_IS_WRITEABLE` are cached for internal use by the channel when it is created. If they should change at some later point (e.g. partial shutdown of a socket with the UNIX `shutdown()` function), the user should immediately call `g_io_channel_get_flags()` to update the internal values of these flags.

`channel` : | a GIOChannel
Returns : | the flags which are set on the channel

### g_io_channel_set_flags ()

    GIOStatus           g_io_channel_set_flags              (GIOChannel *channel,
                                                             GIOFlags flags,
                                                             GError **error);

Sets the (writeable) flags in `channel` to (`flags` & `G_IO_CHANNEL_SET_MASK`).

`channel` : | a GIOChannel
`flags` : | the flags to set on the IO channel
`error` : | A location to return an error of type GIOChannelError
Returns : | the status of the operation.

### enum GIOFlags

    typedef enum
    {
      G_IO_FLAG_APPEND = 1 << 0,
      G_IO_FLAG_NONBLOCK = 1 << 1,
      G_IO_FLAG_IS_READABLE = 1 << 2,	/* Read only flag */
      G_IO_FLAG_IS_WRITEABLE = 1 << 3,	/* Read only flag */
      G_IO_FLAG_IS_SEEKABLE = 1 << 4,	/* Read only flag */
      G_IO_FLAG_MASK = (1 << 5) - 1,
      G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK,
      G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
    } GIOFlags;

Specifies properties of a GIOChannel. Some of the flags can only be read with `g_io_channel_get_flags()`, but not changed with `g_io_channel_set_flags()`.

`G_IO_FLAG_APPEND` | turns on append mode, corresponds to `O_APPEND` (see the documentation of the UNIX `open()` syscall).
`G_IO_FLAG_NONBLOCK` | turns on nonblocking mode, corresponds to `O_NONBLOCK`/`O_NDELAY` (see the documentation of the UNIX `open()` syscall).
`G_IO_FLAG_IS_READABLE` | indicates that the io channel is readable. This flag can not be changed.
`G_IO_FLAG_IS_WRITEABLE` | indicates that the io channel is writable. This flag can not be changed.
`G_IO_FLAG_IS_SEEKABLE` | indicates that the io channel is seekable, i.e. that `g_io_channel_seek_position()` can be used on it. This flag can not be changed.
`G_IO_FLAG_MASK` |
`G_IO_FLAG_GET_MASK` |
`G_IO_FLAG_SET_MASK` |

### g_io_channel_get_line_term ()

    const gchar*        g_io_channel_get_line_term          (GIOChannel *channel,
                                                             gint *length);

This returns the string that GIOChannel uses to determine where in the file a line break occurs. A value of `NULL` indicates autodetection.

`channel` : | a GIOChannel
`length` : | a location to return the length of the line terminator
Returns : | The line termination string. This value is owned by GLib and must not be freed.

### g_io_channel_set_line_term ()

    void                g_io_channel_set_line_term          (GIOChannel *channel,
                                                             const gchar *line_term,
                                                             gint length);

This sets the string that GIOChannel uses to determine where in the file a line break occurs.

`channel` : | a GIOChannel
`line_term` : | The line termination string. Use `NULL` for autodetect. Autodetection breaks on "\n", "\r\n", "\r", "\0", and the Unicode paragraph separator. Autodetection should not be used for anything other than file-based channels.
`length` : | The length of the termination string. If -1 is passed, the string is assumed to be nul-terminated. This option allows termination strings with embedded nuls.

### g_io_channel_get_buffered ()

    gboolean            g_io_channel_get_buffered           (GIOChannel *channel);

Returns whether `channel` is buffered.

`channel` : | a GIOChannel
Returns : | `TRUE` if the `channel` is buffered.

### g_io_channel_set_buffered ()

    void                g_io_channel_set_buffered           (GIOChannel *channel,
                                                             gboolean buffered);

The buffering state can only be set if the channel's encoding is `NULL`. For any other encoding, the channel must be buffered.

A buffered channel can only be set unbuffered if the channel's internal buffers have been flushed. Newly created channels or channels which have returned `G_IO_STATUS_EOF` not require such a flush. For write-only channels, a call to `g_io_channel_flush()` is sufficient. For all other channels, the buffers may be flushed by a call to `g_io_channel_seek_position()`. This includes the possibility of seeking with seek type `G_SEEK_CUR` and an offset of zero. Note that this means that socket-based channels cannot be set unbuffered once they have had data read from them.

On unbuffered channels, it is safe to mix read and write calls from the new and old APIs, if this is necessary for maintaining old code.

The default state of the channel is buffered.

`channel` : | a GIOChannel
`buffered` : | whether to set the channel buffered or unbuffered

### g_io_channel_get_encoding ()

    const gchar*        g_io_channel_get_encoding           (GIOChannel *channel);

Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The encoding `NULL` makes the channel safe for binary data.

`channel` : | a GIOChannel
Returns : | A string containing the encoding, this string is owned by GLib and must not be freed.

### g_io_channel_set_encoding ()

    GIOStatus           g_io_channel_set_encoding           (GIOChannel *channel,
                                                             const gchar *encoding,
                                                             GError **error);

Sets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The default encoding for the external file is UTF-8.

The encoding `NULL` is safe to use with binary data.

The encoding can only be set if one of the following conditions is true:

- The channel was just created, and has not been written to or read from yet.

- The channel is write-only.

- The channel is a file, and the file pointer was just repositioned by a call to `g_io_channel_seek_position()`. (This flushes all the internal buffers.)

- The current encoding is `NULL` or UTF-8.

- One of the (new API) read functions has just returned `G_IO_STATUS_EOF` (or, in the case of `g_io_channel_read_to_end()`, `G_IO_STATUS_NORMAL`).

- One of the functions `g_io_channel_read_chars()` or `g_io_channel_read_unichar()` has returned `G_IO_STATUS_AGAIN` or `G_IO_STATUS_ERROR`. This may be useful in the case of `G_CONVERT_ERROR_ILLEGAL_SEQUENCE`. Returning one of these statuses from `g_io_channel_read_line()`, `g_io_channel_read_line_string()`, or `g_io_channel_read_to_end()` does not guarantee that the encoding can be changed.

Channels which do not meet one of the above conditions cannot call `g_io_channel_seek_position()` with an offset of `G_SEEK_CUR`, and, if they are "seekable", cannot call `g_io_channel_write_chars()` after calling one of the API "read" functions.

`channel` : | a GIOChannel
`encoding` : | the encoding type
`error` : | location to store an error of type GConvertError
Returns : | `G_IO_STATUS_NORMAL` if the encoding was successfully set.

### g_io_channel_get_close_on_unref ()

    gboolean            g_io_channel_get_close_on_unref     (GIOChannel *channel);

Returns whether the file/socket/whatever associated with `channel` will be closed when `channel` receives its final unref and is destroyed. The default value of this is `TRUE` for channels created by `g_io_channel_new_file()`, and `FALSE` for all other channels.

`channel` : | a GIOChannel.
Returns : | Whether the channel will be closed on the final unref of the GIOChannel data structure.

### g_io_channel_set_close_on_unref ()

    void                g_io_channel_set_close_on_unref     (GIOChannel *channel,
                                                             gboolean do_close);

Setting this flag to `TRUE` for a channel you have already closed can cause problems.

`channel` : | a GIOChannel
`do_close` : | Whether to close the channel on the final unref of the GIOChannel data structure. The default value of this is `TRUE` for channels created by `g_io_channel_new_file()`, and `FALSE` for all other channels.

### g_io_channel_read ()

    GIOError            g_io_channel_read                   (GIOChannel *channel,
                                                             gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_read);

### Warning

`g_io_channel_read` has been deprecated since version 2.2 and should not be used in newly-written code. Use `g_io_channel_read_chars()` instead.

Reads data from a GIOChannel.

`channel` : | a GIOChannel
`buf` : | a buffer to read the data into (which should be at least count bytes long)
`count` : | the number of bytes to read from the GIOChannel
`bytes_read` : | returns the number of bytes actually read
Returns : | `G_IO_ERROR_NONE` if the operation was successful.

### enum GIOError

    typedef enum
    {
      G_IO_ERROR_NONE,
      G_IO_ERROR_AGAIN,
      G_IO_ERROR_INVAL,
      G_IO_ERROR_UNKNOWN
    } GIOError;

GIOError is only used by the deprecated functions `g_io_channel_read()`, `g_io_channel_write()`, and `g_io_channel_seek()`.

`G_IO_ERROR_NONE` | no error
`G_IO_ERROR_AGAIN` | an EAGAIN error occurred
`G_IO_ERROR_INVAL` | an EINVAL error occurred
`G_IO_ERROR_UNKNOWN` | another error occurred

### g_io_channel_write ()

    GIOError            g_io_channel_write                  (GIOChannel *channel,
                                                             const gchar *buf,
                                                             gsize count,
                                                             gsize *bytes_written);

### Warning

`g_io_channel_write` has been deprecated since version 2.2 and should not be used in newly-written code. Use `g_io_channel_write_chars()` instead.

Writes data to a GIOChannel.

`channel` : | a GIOChannel
`buf` : | the buffer containing the data to write
`count` : | the number of bytes to write
`bytes_written` : | the number of bytes actually written
Returns : | `G_IO_ERROR_NONE` if the operation was successful.

### g_io_channel_seek ()

    GIOError            g_io_channel_seek                   (GIOChannel *channel,
                                                             gint64 offset,
                                                             GSeekType type);

### Warning

`g_io_channel_seek` has been deprecated since version 2.2 and should not be used in newly-written code. Use `g_io_channel_seek_position()` instead.

Sets the current position in the GIOChannel, similar to the standard library function `fseek()`.

`channel` : | a GIOChannel
`offset` : | an offset, in bytes, which is added to the position specified by `type`
`type` : | the position in the file, which can be `G_SEEK_CUR` (the current position), `G_SEEK_SET` (the start of the file), or `G_SEEK_END` (the end of the file)
Returns : | `G_IO_ERROR_NONE` if the operation was successful.

### g_io_channel_close ()

    void                g_io_channel_close                  (GIOChannel *channel);

### Warning

`g_io_channel_close` has been deprecated since version 2.2 and should not be used in newly-written code. Use `g_io_channel_shutdown()` instead.

Close an IO channel. Any pending data to be written will be flushed, ignoring errors. The channel will not be freed until the last reference is dropped using `g_io_channel_unref()`.

`channel` : | A GIOChannel

## See Also

`gtk_input_add_full()`, `gtk_input_remove()`, `gdk_input_add()`, `gdk_input_add_full()`, `gdk_input_remove()` | Convenience functions for creating GIOChannel instances and adding them to the main event loop.