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_close().
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.
GTK+ contains the convenience function gtk_input_add_full()
which creates a GIOChannel from a file descriptor and adds it to the
main event loop.
The event source can later be removed with gtk_input_remove().
Similar functions can also be found in GDK.
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
struct GIOChannel
struct GIOChannel
{
/*< private >*/
guint ref_count;
GIOFuncs *funcs;
gchar *encoding;
GIConv read_cd;
GIConv write_cd;
gchar *line_term; /* String which indicates the end of a line of text */
guint line_term_len; /* So we can have null in the line term */
gsize buf_size;
GString *read_buf; /* Raw data from the channel */
GString *encoded_read_buf; /* Channel data converted to UTF-8 */
GString *write_buf; /* Data ready to be written to the file */
gchar partial_write_buf[6]; /* UTF-8 partial characters, null terminated */
/* Group the flags together, immediately after partial_write_buf, to save memory */
guint use_buffer : 1; /* The encoding uses the buffers */
guint do_encode : 1; /* The encoding uses the GIConv coverters */
guint close_on_unref : 1; /* Close the channel on final unref */
guint is_readable : 1; /* Cached GIOFlag */
guint is_writeable : 1; /* ditto */
guint is_seekable : 1; /* ditto */
}; |
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 ()
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.
g_io_channel_unix_get_fd ()
Returns the file descriptor of the UNIX GIOChannel.
g_io_channel_init ()
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).
g_io_channel_new_file ()
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).
g_io_channel_read_unichar ()
This function cannot be called on a channel with NULL encoding.
g_io_channel_read_line ()
Reads a line, including the terminating character(s),
from a GIOChannel into a newly-allocated string.
length will contain allocated memory if the return
is G_IO_STATUS_NORMAL.
g_io_channel_read_line_string ()
Reads a line from a GIOChannel, using a GString as a buffer.
g_io_channel_read_to_end ()
Reads all the remaining data from the file.
Return value: G_IO_STATUS_NORMAL on success. This function never
g_io_channel_write_chars ()
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().
g_io_channel_write_unichar ()
This function cannot be called on a channel with NULL encoding.
g_io_channel_flush ()
Flushes the write buffer for the GIOChannel.
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_io_channel_shutdown ()
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().
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.
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
#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 ()
Converts an errno error number to a GIOChannelError.
g_io_channel_ref ()
Increments the reference count of a GIOChannel.
g_io_channel_unref ()
Decrements the reference count of a GIOChannel.
g_io_create_watch ()
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 at the default priority.
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.
struct GIOFuncs
struct GIOFuncs
{
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);
}; |
A table of functions used to handle different types of GIOChannel in a
generic way.
g_io_channel_get_buffer_size ()
Gets the buffer size.
g_io_channel_set_buffer_size ()
Sets the buffer size.
g_io_channel_get_buffer_condition ()
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.
g_io_channel_get_flags ()
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.
g_io_channel_set_flags ()
Sets the (writeable) flags in channel to (flags & G_IO_CHANNEL_SET_MASK).
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_channel_get_line_term ()
This returns the string that GIOChannel uses to determine
where in the file a line break occurs. A value of NULL
indicates auto detection.
g_io_channel_set_line_term ()
This sets the string that GIOChannel uses to determine
where in the file a line break occurs.
g_io_channel_get_buffered ()
Returns whether channel is buffered.
g_io_channel_set_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.
g_io_channel_get_encoding ()
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.
g_io_channel_set_encoding ()
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 under the following conditions:
1. The channel was just created, and has not been written to
or read from yet.
2. The channel is write-only.
3. 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.)
4. The current encoding is NULL or UTF-8.
5. 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).
6. 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 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.
g_io_channel_get_close_on_unref ()
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.
g_io_channel_set_close_on_unref ()
Setting this flag to TRUE for a channel you have already closed
can cause problems.
g_io_channel_read ()
Warning |
g_io_channel_read is deprecated and should not be used in newly-written code. |
Reads data from a GIOChannel. This function is deprecated. New code should
use g_io_channel_read_chars() instead.
g_io_channel_write ()
Warning |
g_io_channel_write is deprecated and should not be used in newly-written code. |
Writes data to a GIOChannel. This function is deprecated. New code should
use g_io_channel_write_chars() instead.
g_io_channel_seek ()
Warning |
g_io_channel_seek is deprecated and should not be used in newly-written code. |
Sets the current position in the GIOChannel, similar to the standard library
function fseek(). This function is deprecated. New
code should use g_io_channel_seek_position() instead.
g_io_channel_close ()
Warning |
g_io_channel_close is deprecated and should not be used in newly-written code. |
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(). This
function is deprecated: you should use g_io_channel_shutdown()
instead.