IOChannel

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.

Members

Functions

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

flush
glib.types.IOStatus flush()

Flushes the write buffer for the GIOChannel.

getBufferCondition
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 glib.types.IOCondition.Out may be set.

getBufferSize
size_t getBufferSize()

Gets the buffer size.

getBuffered
bool getBuffered()

Returns whether channel is buffered.

getCloseOnUnref
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 g_io_channel_new_file (), and false for all other channels.

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

getFlags
glib.types.IOFlags getFlags()

Gets the current flags for a #GIOChannel, including read-only flags such as G_IO_FLAG_IS_READABLE.

getLineTerm
string getLineTerm(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.

init_
void init_()

Initializes a #GIOChannel struct.

read
glib.types.IOError read(ubyte[] buf, size_t bytesRead)

Reads data from a #GIOChannel.

readChars
glib.types.IOStatus readChars(ubyte[] buf, size_t bytesRead)

Replacement for glib.iochannel.IOChannel.read with the new API.

readLine
glib.types.IOStatus readLine(string strReturn, size_t length, size_t terminatorPos)

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

readLineString
glib.types.IOStatus readLineString(glib.string_.String buffer, size_t terminatorPos)

Reads a line from a #GIOChannel, using a #GString as a buffer.

readToEnd
glib.types.IOStatus readToEnd(ubyte[] strReturn)

Reads all the remaining data from the file.

readUnichar
glib.types.IOStatus readUnichar(dchar thechar)

Reads a Unicode character from channel. This function cannot be called on a channel with null encoding.

seek
glib.types.IOError seek(long offset, glib.types.SeekType type)

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

seekPosition
glib.types.IOStatus seekPosition(long offset, glib.types.SeekType type)

Replacement for glib.iochannel.IOChannel.seek with the new API.

setBufferSize
void setBufferSize(size_t size)

Sets the buffer size.

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

setCloseOnUnref
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 g_io_channel_new_file (), and false for all other channels.

setEncoding
glib.types.IOStatus setEncoding(string 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.

setFlags
glib.types.IOStatus setFlags(glib.types.IOFlags flags)

Sets the (writeable) flags in channel to (flags & G_IO_FLAG_SET_MASK).

setLineTerm
void setLineTerm(string lineTerm)

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

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

unixGetFd
int unixGetFd()

Returns the file descriptor of the #GIOChannel.

write
glib.types.IOError write(string buf, size_t count, size_t bytesWritten)

Writes data to a #GIOChannel.

writeChars
glib.types.IOStatus writeChars(ubyte[] buf, size_t bytesWritten)

Replacement for glib.iochannel.IOChannel.write with the new API.

writeUnichar
glib.types.IOStatus writeUnichar(dchar thechar)

Writes a Unicode character to channel. This function cannot be called on a channel with null encoding.

Static functions

errorFromErrno
glib.types.IOChannelError errorFromErrno(int en)

Converts an errno error number to a #GIOChannelError.

errorQuark
glib.types.Quark errorQuark()
newFile
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 doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

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

Inherited Members

From Boxed

cInstancePtr
void* cInstancePtr;

Pointer to the C boxed value

getType
GType getType()

Get the GType of this boxed type.

gType
GType gType [@property getter]

Boxed GType property.

self
Boxed self()

Convenience method to return this cast to a type. For use in D with statements.

copy_
void* copy_()

Make a copy of the wrapped C boxed data.

boxedCopy
void* boxedCopy(void* cBoxed)

Copy a C boxed value using g_boxed_copy.

boxedFree
void boxedFree(void* cBoxed)

Free a C boxed value using g_boxed_free.