Next: , Previous: File Utilities, Up: Top


4 IO Channels

portable support for using files, pipes and sockets.

4.1 Overview

The <gio-channel> 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 <gio-channel> 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 <gio-channel> 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 <gio-channel> 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 <gio-channel>, and provide a function to be called whenever these events occur.

<gio-channel> 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 <gio-channel> 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.

4.2 Usage

— Class: <gio-channel>

Opaque pointer.

This class defines no direct slots.

— Function: g-io-channel-unix-new (fd int) ⇒  (ret <gio-channel>)

Creates a new <gio-channel> given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.

The returned <gio-channel> has a reference count of 1.

The default encoding for <gio-channel> 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 ‘#f’ 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.
ret
a new <gio-channel>.
— Function: g-io-channel-unix-get-fd (channel <gio-channel>) ⇒  (ret int)

Returns the file descriptor of the <gio-channel>.

On Windows this function returns the file descriptor or socket of the <gio-channel>.

channel
a <gio-channel>, created with g-io-channel-unix-new.
ret
the file descriptor of the <gio-channel>.
— Function: g-io-channel-new-file (filename mchars) (mode mchars) ⇒  (ret <gio-channel>)

Open a file filename as a <gio-channel> 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’.
ret
A <gio-channel> on success, ‘#f’ on failure.
— Function: g-io-channel-read-line (self <gio-channel>) ⇒  (ret <gio-status>) (string_return mchars)

Reads a line, including the terminating character(s), from a <gio-channel> into a newly-allocated string. str-return will contain allocated memory if the return is ‘G_IO_STATUS_NORMAL’.

channel
a <gio-channel>
str-return
The line read from the <gio-channel>, 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 ‘#f’ instead.
length
location to store length of the read data, or ‘#f
terminator-pos
location to store position of line terminator, or ‘#f
error
A location to return an error of type <g-convert-error> or <gio-channel-error>
ret
the status of the operation.
— Function: g-io-channel-flush (channel <gio-channel>) ⇒  (ret <gio-status>)

Flushes the write buffer for the GIOChannel.

channel
a <gio-channel>
error
location to store an error of type <gio-channel-error>
ret
the status of the operation: One of <g-io-channel-normal>, <g-io-channel-again>, or <g-io-channel-error>.
— Function: g-io-channel-seek-position (self <gio-channel>) (offset int64) (type <g-seek-type>) ⇒  (ret <gio-status>)

Replacement for g-io-channel-seek with the new API.

channel
a <gio-channel>
offset
The offset in bytes from the position specified by type
type
a <g-seek-type>. 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 <gio-channel-error>
ret
the status of the operation.
— Function: g-io-channel-shutdown (channel <gio-channel>) (flush bool) ⇒  (ret <gio-status>)

Close an IO channel. Any pending data to be written will be flushed if flush is ‘#t’. The channel will not be freed until the last reference is dropped using g-io-channel-unref.

channel
a <gio-channel>
flush
if ‘#t’, flush pending
err
location to store a <gio-channel-error>
ret
the status of the operation.
— Function: g-io-channel-error-from-errno (en int) ⇒  (ret <gio-channel-error>)

Converts an ‘errno’ error number to a <gio-channel-error>.

en
an ‘errno’ error number, e.g. ‘EINVAL’.
ret
a <gio-channel-error> error number, e.g. ‘G_IO_CHANNEL_ERROR_INVAL’.
— Function: g-io-create-watch (channel <gio-channel>) (condition <gio-condition>) ⇒  (ret <g-source>)

Creates a <g-source> 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.

On Windows, polling a <g-source> 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 <gio-channel> to watch
condition
conditions to watch for
ret
a new <g-source>
— Function: g-io-add-watch (channel <gio-channel>) (condition <gio-condition>) (func scm) ⇒  (ret bool)

Adds the <gio-channel> into the main event loop with the default priority.

channel
a <gio-channel>.
condition
the condition to watch for.
func
the function to call when the condition is satisfied.
user-data
user data to pass to func.
ret
the event source id.
— Function: g-io-channel-get-buffer-size (channel <gio-channel>) ⇒  (ret size_t)

Gets the buffer size.

channel
a <gio-channel>
ret
the size of the buffer.
— Function: g-io-channel-set-buffer-size (channel <gio-channel>) (size size_t)

Sets the buffer size.

channel
a <gio-channel>
size
the size of the buffer. 0 == pick a good size
— Function: g-io-channel-get-buffer-condition (channel <gio-channel>) ⇒  (ret <gio-condition>)

This function returns a <gio-condition> depending on whether there is data to be read/space to write data in the internal buffers in the <gio-channel>. Only the flags ‘G_IO_IN’ and ‘G_IO_OUT’ may be set.

channel
A <gio-channel>
ret
A <gio-condition>
— Function: g-io-channel-get-flags (channel <gio-channel>) ⇒  (ret <gio-flags>)

Gets the current flags for a <gio-channel>, 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 <gio-channel>
ret
the flags which are set on the channel
— Function: g-io-channel-set-flags (channel <gio-channel>) (flags <gio-flags>) ⇒  (ret <gio-status>)

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

channel
a <gio-channel>.
flags
the flags to set on the IO channel.
error
A location to return an error of type <gio-channel-error>.
ret
the status of the operation.
— Function: g-io-channel-get-line-term (channel <gio-channel>) ⇒  (ret mchars) (length int)

This returns the string that <gio-channel> uses to determine where in the file a line break occurs. A value of ‘#f’ indicates auto detection.

channel
a <gio-channel>
length
a location to return the length of the line terminator
ret
The line termination string. This value is owned by GLib and must not be freed.
— Function: g-io-channel-set-line-term (channel <gio-channel>) (line_term mchars) (length int)

This sets the string that <gio-channel> uses to determine where in the file a line break occurs.

channel
a <gio-channel>
line-term
The line termination string. Use ‘#f’ for auto detect. Auto detection breaks on "\n", "\r\n", "\r", "\0", and the Unicode paragraph separator. Auto detection 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 embeded nuls.
— Function: g-io-channel-get-buffered (channel <gio-channel>) ⇒  (ret bool)

Returns whether channel is buffered.

channel
a <gio-channel>.
ret
#t’ if the channel is buffered.
— Function: g-io-channel-set-buffered (channel <gio-channel>) (buffered bool)

The buffering state can only be set if the channel's encoding is ‘#f’. 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 <gio-channel>
buffered
whether to set the channel buffered or unbuffered
— Function: g-io-channel-get-encoding (channel <gio-channel>) ⇒  (ret mchars)

Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The encoding ‘#f’ makes the channel safe for binary data.

channel
a <gio-channel>
ret
A string containing the encoding, this string is owned by GLib and must not be freed.
— Function: g-io-channel-set-encoding (channel <gio-channel>) (encoding mchars) ⇒  (ret <gio-status>)

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 ‘#f’ is safe to use with binary data.

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

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 ‘#f’ 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 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 <gio-channel>
encoding
the encoding type
error
location to store an error of type <g-convert-error>.
ret
G_IO_STATUS_NORMAL’ if the encoding was successfully set.
— Function: g-io-channel-close (channel <gio-channel>)

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 <gio-channel>