glib.iochannel
Module for [IOChannel] class
Types 2
The [glib.iochannel.IOChannel] data type aims to provide a portable method for using file descriptors, pipes, and sockets, and integrating them into the main event loop (see [glib.main_context.MainContext]). Currently, full support is available on UNIX platforms; support for Windows is only partially complete.
To create a new [glib.iochannel.IOChannel] on UNIX systems use [glib.iochannel.IOChannel.unixNew]. This works for plain file descriptors, pipes and sockets. Alternatively, a channel can be created for a file in a system independent manner using [glib.iochannel.IOChannel.newFile].
Once a [glib.iochannel.IOChannel] has been created, it can be used in a generic manner with the functions [glib.iochannel.IOChannel.readChars], [glib.iochannel.IOChannel.writeChars], [glib.iochannel.IOChannel.seekPosition], and [glib.iochannel.IOChannel.shutdown].
To add a [glib.iochannel.IOChannel] to the main event loop, use func@GLib.io_add_watch or func@GLib.io_add_watch_full. Here you specify which events you are interested in on the [glib.iochannel.IOChannel], and provide a function to be called whenever these events occur.
[glib.iochannel.IOChannel] instances are created with an initial reference count of 1. [glib.iochannel.IOChannel.ref_] and [glib.iochannel.IOChannel.unref] can be used to increment or decrement the reference count respectively. When the reference count falls to 0, the [glib.iochannel.IOChannel] is freed. (Though it isn’t closed automatically, unless it was created using [glib.iochannel.IOChannel.newFile].) Using func@GLib.io_add_watch or func@GLib.io_add_watch_full increments a channel’s reference count.
The new functions [glib.iochannel.IOChannel.readChars], [glib.iochannel.IOChannel.readLine], [glib.iochannel.IOChannel.readLineString], [glib.iochannel.IOChannel.readToEnd], [glib.iochannel.IOChannel.writeChars], [glib.iochannel.IOChannel.seekPosition], and [glib.iochannel.IOChannel.flush] should not be mixed with the deprecated functions [glib.iochannel.IOChannel.read], [glib.iochannel.IOChannel.write], and [glib.iochannel.IOChannel.seek] on the same channel.
glib.iochannel.IOChannel newFile(string filename, string mode)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 [glib.iochannel.IOChannel.close] (though doi...glib.iochannel.IOChannel unixNew(int fd)Creates a new #GIOChannel given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.void close()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 [glib.iochannel.IOChannel.unref].glib.types.IOStatus flush()Flushes the write buffer for the GIOChannel. Returns: the status of the operation: One of `GIOSTATUSNORMAL`, `GIOSTATUSAGAIN`, or `GIOSTATUS_ERROR`. Throws: [IOChannelException]glib.types.IOCondition getBufferCondition()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 [glib.types.IOCondition.In] and [g...size_t getBufferSize()Gets the buffer size. Returns: the size of the buffer.bool getBuffered()Returns whether channel is buffered. Returns: true if the channel is buffered.bool getCloseOnUnref()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 gio...string getEncoding()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. Returns: A string containing the encoding, th...glib.types.IOFlags getFlags()Gets the current flags for a #GIOChannel, including read-only flags such as `GIOFLAGISREADABLE`.string getLineTerm(out int length)This returns the string that #GIOChannel uses to determine where in the file a line break occurs. A value of null indicates autodetection.void init_()Initializes a #GIOChannel struct.glib.types.IOError read(ref ubyte[] buf, out size_t bytesRead)Reads data from a #GIOChannel.glib.types.IOStatus readChars(ref ubyte[] buf, out size_t bytesRead)Replacement for [glib.iochannel.IOChannel.read] with the new API.glib.types.IOStatus readLine(out string strReturn, out size_t length, out size_t terminatorPos)Reads a line, including the terminating character(s), from a #GIOChannel into a newly-allocated string. strreturn will contain allocated memory if the return is `GIOSTATUSNORMAL`.glib.types.IOStatus readLineString(glib.string_.String buffer, out size_t terminatorPos)Reads a line from a #GIOChannel, using a #GString as a buffer.glib.types.IOStatus readToEnd(out ubyte[] strReturn)Reads all the remaining data from the file.glib.types.IOStatus readUnichar(out dchar thechar)Reads a Unicode character from channel. This function cannot be called on a channel with null encoding.glib.types.IOError seek(long offset, glib.types.SeekType type)Sets the current position in the #GIOChannel, similar to the standard library function fseek().glib.types.IOStatus seekPosition(long offset, glib.types.SeekType type)Replacement for [glib.iochannel.IOChannel.seek] with the new API.void setBufferSize(size_t size)Sets the buffer size.void setBuffered(bool buffered)The buffering state can only be set if the channel's encoding is null. For any other encoding, the channel must be buffered.void setCloseOnUnref(bool doClose)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 giochannelnewfile (), and false for all other channels.glib.types.IOStatus setEncoding(string encoding = null)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.glib.types.IOStatus setFlags(glib.types.IOFlags flags)Sets the (writeable) flags in channel to (flags & `GIOFLAGSETMASK`).void setLineTerm(string lineTerm = null)This sets the string that #GIOChannel uses to determine where in the file a line break occurs.glib.types.IOStatus shutdown(bool flush)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 [glib.iochannel.IOChannel.unref].int unixGetFd()Returns the file descriptor of the #GIOChannel.glib.types.IOError write(string buf, out size_t bytesWritten)Writes data to a #GIOChannel.glib.types.IOStatus writeChars(ubyte[] buf, out size_t bytesWritten)Replacement for [glib.iochannel.IOChannel.write] with the new API.glib.types.IOStatus writeUnichar(dchar thechar)Writes a Unicode character to channel. This function cannot be called on a channel with null encoding.glib.types.IOChannelError errorFromErrno(int en)Converts an `errno` error number to a #GIOChannelError.