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 (see
<GMainContext>). Currently, full support is
available on UNIX platforms; support for Windows is only partially
complete.
To create a new <GIOChannel> on UNIX systems use
iochannel: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
iochannel:new-file.
Once a <GIOChannel> has been created, it can be
used in a generic manner with the functions iochannel:read-chars!, iochannel:write-chars, iochannel:seek-position, and iochannel:shutdown.
To add a <GIOChannel> to the main event loop,
use io-add-watch or
GLib.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. iochannel:ref
and iochannel: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 iochannel:new-file.) Using
io-add-watch or GLib.io_add_watch_full
increments a channel’s reference count.
The new functions iochannel:read-chars!, iochannel:read-line, iochannel:read-line-string, iochannel:read-to-end, iochannel:write-chars, iochannel:seek-position, and iochannel:flush should not be mixed with the deprecated
functions iochannel:read,
iochannel:write, and iochannel:seek on the same channel.
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 iochannel:unref.
Passed: a <GIOChannel>.
Converts an errno error number to a
<%GIOChannelError>.
Passed: a 32-bit integer.
an errno error number, e.g. EINVAL
Returned: a <%GIOChannelError>.
a <%GIOChannelError> error number, e.g.
inval.
Returned: an unsigned 32-bit integer.
Flushes the write buffer for the GIOChannel.
Passed: a <GIOChannel>.
Returned: a <%GIOStatus>.
the status of the operation: One of normal,
again, or error.
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 in and out may
be set.
Passed: a <GIOChannel>.
Returned: a <GIOCondition>.
Gets the buffer size.
Passed: a <GIOChannel>.
Returned: an unsigned 64-bit integer.
the size of the buffer.
Returns whether self is buffered.
Passed: a <GIOChannel>.
Returned: a boolean.
#t if the self is buffered.
Returns whether the file/socket/whatever associated with self will
be closed when self receives its final unref and is destroyed. The
default value of this is #t for channels created by
g_io_channel_new_file (), and #f for all other channels.
Passed: a <GIOChannel>.
a <GIOChannel>.
Returned: a boolean.
#t if the channel will be closed, #f otherwise.
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.
Passed: a <GIOChannel>.
Returned: a string.
A string containing the encoding, this string is owned by GLib and must not be freed.
Gets the current flags for a <GIOChannel>,
including read-only flags such as is-readable.
The values of the flags is-readable and
is-writable 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 iochannel:get-flags to update the internal values of these
flags.
Passed: a <GIOChannel>.
Returned: a <%GIOFlags>.
the flags which are set on the channel
This returns the string that <GIOChannel> uses
to determine where in the file a line break occurs. A value of
#f indicates autodetection. Since 2.84, the return value is
always nul-terminated.
Passed: a <GIOChannel>.
Returned: a string.
The line termination string. This value is owned by GLib and must not be freed.
Returned: a 32-bit integer.
a location to return the length of the line terminator
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>).
Passed: a <GIOChannel>.
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
iochannel:close (though doing so will not
cause problems, as long as no attempt is made to access the channel
after it is closed).
Passed: a file name, as a string.
A string containing the name of a file
Passed: a string.
One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as
in fopen()
Returned: a <GIOChannel>.
A <GIOChannel> on success, #f on failure.
Reads data from a <GIOChannel>.
Passed: a <GIOChannel>.
Passed: a string.
a buffer to read the data into (which should be at least count bytes long)
Passed: an unsigned 64-bit integer.
the number of bytes to read from the
<GIOChannel>
Passed: an unsigned 64-bit integer.
returns the number of bytes actually read
Returned: a <%GIOError>.
none if the operation was successful.
Replacement for iochannel:read with the new
API.
Passed: a <GIOChannel>.
Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer. It is filled in, and returned: a bytevector.
a buffer to read data into
Returned: a <%GIOStatus>.
the status of the operation.
Returned: an unsigned 64-bit integer.
The number of bytes read. This may be zero even on success if count < 6
and the channel’s encoding is non-#f. This indicates that the
next UTF-8 character is too wide for the buffer.
Not returned: it is the length of buf.
the size of the buffer. Note that the buffer may not be completely filled even if there is data in the buffer if the remaining data is not a complete character.
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
normal.
Passed: a <GIOChannel>.
Returned: a <%GIOStatus>.
the status of the operation.
Returned: a string.
The line read from the <GIOChannel>, including
the line terminator. This data should be freed with
byte-array:free when no longer needed. This is a
nul-terminated string. If a length of zero is returned, this will
be #f instead.
Returned: an unsigned 64-bit integer.
location to store length of the read data, or #f
Returned: an unsigned 64-bit integer.
location to store position of line terminator, or #f
Reads a line from a <GIOChannel>, using a
<GString> as a buffer.
Passed: a <GIOChannel>.
Passed: a <GString>.
a <GString> into which the line will be written. If
buffer already contains data, the old data will be overwritten.
Passed, or left out: an unsigned 64-bit integer.
location to store position of line terminator, or #f
Returned: a <%GIOStatus>.
the status of the operation.
Reads all the remaining data from the file.
Passed: a <GIOChannel>.
Returned: a <%GIOStatus>.
Returned: a bytevector.
Location to store a pointer to a string holding the remaining data in
the <GIOChannel>. This data should be freed
with byte-array:free when no longer needed. This
data is terminated by an extra nul character, but there may be other
nuls in the intervening data.
Not returned: it is the length of str-return.
location to store length of the data
Reads a Unicode character from self. This function cannot be
called on a channel with #f encoding.
Passed: a <GIOChannel>.
Returned: a <%GIOStatus>.
Returned: a character.
a location to return a character
Increments the reference count of a
<GIOChannel>.
Passed: a <GIOChannel>.
Returned: a <GIOChannel>.
the self that was passed in (since 2.6)
Sets the current position in the <GIOChannel>,
similar to the standard library function fseek().
Passed: a <GIOChannel>.
Passed: a 64-bit integer.
an offset, in bytes, which is added to the position specified by type
Passed: a <%GSeekType>, or one of its symbols.
the position in the file, which can be cur (the
current position), set (the start of the file),
or end (the end of the file)
Returned: a <%GIOError>.
none if the operation was successful.
Replacement for iochannel:seek with the new
API.
Passed: a <GIOChannel>.
Passed: a 64-bit integer.
The offset in bytes from the position specified by type
Passed: a <%GSeekType>, or one of its symbols.
a <%GSeekType>. The type
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.
Returned: a <%GIOStatus>.
the status of the operation.
Sets the buffer size.
Passed: a <GIOChannel>.
Passed: an unsigned 64-bit integer.
the size of the buffer, or 0 to let GLib pick a good size
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 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 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.
Passed: a <GIOChannel>.
Passed: a boolean.
whether to set the channel buffered or unbuffered
Whether to close the channel on the final unref of the
<GIOChannel> data structure. The default value
of this is #t for channels created by g_io_channel_new_file (),
and #f for all other channels.
Setting this flag to #t for a channel you have already closed can
cause problems when the final reference to the
<GIOChannel> is dropped.
Passed: a <GIOChannel>.
Passed: a boolean.
Whether to close the channel on the final unref of the GIOChannel data structure.
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:
iochannel:seek-position.
(This flushes all the internal buffers.)
#f or UTF-8.
eof (or, in the case of iochannel:read-to-end, normal).
iochannel:read-chars! or iochannel:read-unichar has returned
again or error. This may
be useful in the case of illegal-sequence.
Returning one of these statuses from iochannel:read-line, iochannel:read-line-string, or iochannel:read-to-end does not guarantee that the
encoding can be changed.
Channels which do not meet one of the above conditions cannot call
iochannel:seek-position with an
offset of cur, and, if they are "seekable",
cannot call iochannel:write-chars
after calling one of the API "read" functions.
Passed: a <GIOChannel>.
Passed, or left out: a string, or #f.
the encoding type
Returned: a <%GIOStatus>.
normal if the encoding was successfully set
Sets the (writeable) flags in self to (flags &
set-mask).
Passed: a <GIOChannel>.
Passed: a <%GIOFlags>, or a list of its symbols,
or one of them.
the flags to set on the IO channel
Returned: a <%GIOStatus>.
the status of the operation.
This sets the string that <GIOChannel> uses to
determine where in the file a line break occurs.
Passed: a <GIOChannel>.
Passed: a string, or #f.
The line termination string. Use #f 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.
Passed: a 32-bit integer.
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.
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 iochannel:unref.
Passed: a <GIOChannel>.
Passed: a boolean.
if #t, flush pending
Returned: a <%GIOStatus>.
the status of the operation.
Returns the file descriptor of the <GIOChannel>.
On Windows this function returns the file descriptor or socket of the
<GIOChannel>.
Passed: a <GIOChannel>.
a <GIOChannel>, created with
iochannel:unix-new.
Returned: a 32-bit integer.
the file descriptor of the <GIOChannel>.
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
get-charset) with the iochannel:set-encoding function. By default, the fd
passed will not be closed when the final reference to the
<GIOChannel> data structure is dropped.
If you want to read raw binary data without interpretation, then call
the iochannel: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.
Passed: a 32-bit integer.
a file descriptor.
Returned: a <GIOChannel>.
a new <GIOChannel>.
Decrements the reference count of a
<GIOChannel>.
Passed: a <GIOChannel>.
Writes data to a <GIOChannel>.
Passed: a <GIOChannel>.
Passed: a string.
the buffer containing the data to write
Passed: an unsigned 64-bit integer.
the number of bytes to write
Passed: an unsigned 64-bit integer.
the number of bytes actually written
Returned: a <%GIOError>.
none if the operation was successful.
Replacement for iochannel:write with the
new API.
On seekable channels with encodings other than #f 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 ().
Passed: a <GIOChannel>.
Passed: a foreign pointer, or a bytevector.
a buffer to write data from
Passed: a 64-bit integer.
the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.
Returned: a <%GIOStatus>.
the status of the operation.
Returned: an unsigned 64-bit integer.
The number of bytes written. This can be nonzero even if the return
value is not normal. If the return value is
normal and the channel is blocking, this will
always be equal to count if count >= 0.
Writes a Unicode character to self. This function cannot be
called on a channel with #f encoding.
Passed: a <GIOChannel>.
Passed: a character.
a character
Returned: a <%GIOStatus>.