1.12 <GIOChannel>

Structure: <GIOChannel>

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.

Procedures

Method: iochannel:close self
Method: close self

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.

self

Passed: a <GIOChannel>.

A <GIOChannel>

Procedure: iochannel:error-from-errno en ⇒ %return

Converts an errno error number to a <%GIOChannelError>.

en

Passed: a 32-bit integer.

an errno error number, e.g. EINVAL

%return

Returned: a <%GIOChannelError>.

a <%GIOChannelError> error number, e.g. inval.

Procedure: iochannel:error-quark ⇒ %return
%return

Returned: an unsigned 32-bit integer.

Method: iochannel:flush self ⇒ %return
Method: flush self ⇒ %return

Flushes the write buffer for the GIOChannel.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <%GIOStatus>.

the status of the operation: One of normal, again, or error.

Method: iochannel:get-buffer-condition self ⇒ %return
Method: get-buffer-condition self ⇒ %return

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.

self

Passed: a <GIOChannel>.

A <GIOChannel>

%return

Returned: a <GIOCondition>.

A <GIOCondition>

Method: iochannel:get-buffer-size self ⇒ %return
Method: get-buffer-size self ⇒ %return

Gets the buffer size.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: an unsigned 64-bit integer.

the size of the buffer.

Method: iochannel:get-buffered? self ⇒ %return
Method: get-buffered? self ⇒ %return

Returns whether self is buffered.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a boolean.

#t if the self is buffered.

Method: iochannel:get-close-on-unref? self ⇒ %return
Method: get-close-on-unref? self ⇒ %return

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>.

%return

Returned: a boolean.

#t if the channel will be closed, #f otherwise.

Method: iochannel:get-encoding self ⇒ %return
Method: get-encoding self ⇒ %return

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a string.

A string containing the encoding, this string is owned by GLib and must not be freed.

Method: iochannel:get-flags self ⇒ %return
Method: get-flags self ⇒ %return

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <%GIOFlags>.

the flags which are set on the channel

Method: iochannel:get-line-term self ⇒ %return length
Method: get-line-term self ⇒ %return length

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a string.

The line termination string. This value is owned by GLib and must not be freed.

length

Returned: a 32-bit integer.

a location to return the length of the line terminator

Method: iochannel:init self
Method: init self

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>).

self

Passed: a <GIOChannel>.

a <GIOChannel>

Procedure: iochannel:new-file filename mode ⇒ %return

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).

filename

Passed: a file name, as a string.

A string containing the name of a file

mode

Passed: a string.

One of "r", "w", "a", "r+", "w+", "a+". These have the same meaning as in fopen()

%return

Returned: a <GIOChannel>.

A <GIOChannel> on success, #f on failure.

Method: iochannel:read self buf count bytes-read ⇒ %return
Method: read self buf count bytes-read ⇒ %return

Reads data from a <GIOChannel>.

self

Passed: a <GIOChannel>.

a <GIOChannel>

buf

Passed: a string.

a buffer to read the data into (which should be at least count bytes long)

count

Passed: an unsigned 64-bit integer.

the number of bytes to read from the <GIOChannel>

bytes-read

Passed: an unsigned 64-bit integer.

returns the number of bytes actually read

%return

Returned: a <%GIOError>.

none if the operation was successful.

Method: iochannel:read-chars! self buf ⇒ %return buf bytes-read
Method: read-chars! self buf ⇒ %return buf bytes-read

Replacement for iochannel:read with the new API.

self

Passed: a <GIOChannel>.

a <GIOChannel>

buf

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

%return

Returned: a <%GIOStatus>.

the status of the operation.

bytes-read

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.

count

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.

Method: iochannel:read-line self ⇒ %return str-return length terminator-pos
Method: read-line self ⇒ %return str-return length terminator-pos

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <%GIOStatus>.

the status of the operation.

str-return

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.

length

Returned: an unsigned 64-bit integer.

location to store length of the read data, or #f

terminator-pos

Returned: an unsigned 64-bit integer.

location to store position of line terminator, or #f

Method: iochannel:read-line-string self buffer [terminator-pos] ⇒ %return
Method: read-line-string self buffer [terminator-pos] ⇒ %return

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

self

Passed: a <GIOChannel>.

a <GIOChannel>

buffer

Passed: a <GString>.

a <GString> into which the line will be written. If buffer already contains data, the old data will be overwritten.

terminator-pos

Passed, or left out: an unsigned 64-bit integer.

location to store position of line terminator, or #f

%return

Returned: a <%GIOStatus>.

the status of the operation.

Method: iochannel:read-to-end self ⇒ %return str-return
Method: read-to-end self ⇒ %return str-return

Reads all the remaining data from the file.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <%GIOStatus>.

normal on success. This function never returns eof.

str-return

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.

length

Not returned: it is the length of str-return.

location to store length of the data

Method: iochannel:read-unichar self ⇒ %return thechar
Method: read-unichar self ⇒ %return thechar

Reads a Unicode character from self. This function cannot be called on a channel with #f encoding.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <%GIOStatus>.

a <%GIOStatus>

thechar

Returned: a character.

a location to return a character

Method: iochannel:ref self ⇒ %return
Method: ref self ⇒ %return

Increments the reference count of a <GIOChannel>.

self

Passed: a <GIOChannel>.

a <GIOChannel>

%return

Returned: a <GIOChannel>.

the self that was passed in (since 2.6)

Method: iochannel:seek self offset type ⇒ %return
Method: seek self offset type ⇒ %return

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

self

Passed: a <GIOChannel>.

a <GIOChannel>

offset

Passed: a 64-bit integer.

an offset, in bytes, which is added to the position specified by type

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)

%return

Returned: a <%GIOError>.

none if the operation was successful.

Method: iochannel:seek-position self offset type ⇒ %return
Method: seek-position self offset type ⇒ %return

Replacement for iochannel:seek with the new API.

self

Passed: a <GIOChannel>.

a <GIOChannel>

offset

Passed: a 64-bit integer.

The offset in bytes from the position specified by type

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.

%return

Returned: a <%GIOStatus>.

the status of the operation.

Method: iochannel:set-buffer-size self size
Method: set-buffer-size self size

Sets the buffer size.

self

Passed: a <GIOChannel>.

a <GIOChannel>

size

Passed: an unsigned 64-bit integer.

the size of the buffer, or 0 to let GLib pick a good size

Method: iochannel:set-buffered self buffered
Method: set-buffered self buffered

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

buffered

Passed: a boolean.

whether to set the channel buffered or unbuffered

Method: iochannel:set-close-on-unref self do-close
Method: set-close-on-unref self do-close

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

do-close

Passed: a boolean.

Whether to close the channel on the final unref of the GIOChannel data structure.

Method: iochannel:set-encoding self [encoding] ⇒ %return
Method: set-encoding self [encoding] ⇒ %return

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:

  • 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 iochannel:seek-position. (This flushes all the internal buffers.)
  • The current encoding is #f or UTF-8.

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

encoding

Passed, or left out: a string, or #f.

the encoding type

%return

Returned: a <%GIOStatus>.

normal if the encoding was successfully set

Method: iochannel:set-flags self flags ⇒ %return
Method: set-flags self flags ⇒ %return

Sets the (writeable) flags in self to (flags & set-mask).

self

Passed: a <GIOChannel>.

a <GIOChannel>

flags

Passed: a <%GIOFlags>, or a list of its symbols, or one of them.

the flags to set on the IO channel

%return

Returned: a <%GIOStatus>.

the status of the operation.

Method: iochannel:set-line-term self line-term length
Method: set-line-term self line-term length

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

self

Passed: a <GIOChannel>.

a <GIOChannel>

line-term

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.

length

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.

Method: iochannel:shutdown self flush ⇒ %return
Method: shutdown self flush ⇒ %return

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.

self

Passed: a <GIOChannel>.

a <GIOChannel>

flush

Passed: a boolean.

if #t, flush pending

%return

Returned: a <%GIOStatus>.

the status of the operation.

Method: iochannel:unix-get-fd self ⇒ %return
Method: unix-get-fd self ⇒ %return

Returns the file descriptor of the <GIOChannel>.

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

self

Passed: a <GIOChannel>.

a <GIOChannel>, created with iochannel:unix-new.

%return

Returned: a 32-bit integer.

the file descriptor of the <GIOChannel>.

Procedure: iochannel:unix-new fd ⇒ %return

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.

fd

Passed: a 32-bit integer.

a file descriptor.

%return

Returned: a <GIOChannel>.

a new <GIOChannel>.

Method: iochannel:unref self
Method: unref self

Decrements the reference count of a <GIOChannel>.

self

Passed: a <GIOChannel>.

a <GIOChannel>

Method: iochannel:write self buf count bytes-written ⇒ %return
Method: write self buf count bytes-written ⇒ %return

Writes data to a <GIOChannel>.

self

Passed: a <GIOChannel>.

a <GIOChannel>

buf

Passed: a string.

the buffer containing the data to write

count

Passed: an unsigned 64-bit integer.

the number of bytes to write

bytes-written

Passed: an unsigned 64-bit integer.

the number of bytes actually written

%return

Returned: a <%GIOError>.

none if the operation was successful.

Method: iochannel:write-chars self buf count ⇒ %return bytes-written
Method: write-chars self buf count ⇒ %return bytes-written

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 ().

self

Passed: a <GIOChannel>.

a <GIOChannel>

buf

Passed: a foreign pointer, or a bytevector.

a buffer to write data from

count

Passed: a 64-bit integer.

the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.

%return

Returned: a <%GIOStatus>.

the status of the operation.

bytes-written

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.

Method: iochannel:write-unichar self thechar ⇒ %return
Method: write-unichar self thechar ⇒ %return

Writes a Unicode character to self. This function cannot be called on a channel with #f encoding.

self

Passed: a <GIOChannel>.

a <GIOChannel>

thechar

Passed: a character.

a character

%return

Returned: a <%GIOStatus>.

a <%GIOStatus>