Project:DUI

Class List Class Hierarchy
Summary: Ctors Methods Mixins

Module dglib.IOChannel

Class IOChannel

Implemented interfaces:


public class

IOChannel



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.
Currently full support is available on UNIX platforms, support for Windows is only partially complete. To create a new GIOChannel 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 GIOChannel 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_close(). To add a GIOChannel 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 GIOChannel, and provide a function to be called whenever these events occur. GIOChannel 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 GIOChannel 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.

Constructor Summary
public (GIOChannel* gIOChannel)
          Creates a IOChannel from a GtkIOChannel
(int fd)
          Creates a new GIOChannel given a file descriptor.
(String filename, String mode, ErrorG error)
          Open a file filename as a GIOChannel using mode mode.

Methods Summary
void init()
          Initializes a GIOChannel struct.
IOStatus readChars(String buf, gsize count, gsize* bytes_read, ErrorG error)
          Replacement for gIoChannelRead() with the new API.
IOStatus readUnichar(gunichar* thechar, ErrorG error)
          This function cannot be called on a channel with NULL encoding.
IOStatus readLine(String str, gsize length, gsize terminator_pos, ErrorG error)
          Reads a line, including the terminating character(s), from a GIOChannel into a newly-allocated string.
IOStatus readLineString(StringG buffer, gsize terminator_pos, ErrorG error)
          Reads a line from a GIOChannel, using a GString as a buffer.
IOStatus readToEnd(String str, gsize length, ErrorG error)
          Reads all the remaining data from the file.
IOStatus writeChars(String buf, gssize count, gsize* bytes_written, ErrorG error)
          Replacement for gIoChannelWrite() with the new API.
IOStatus writeUnichar(gunichar thechar, ErrorG error)
          This function cannot be called on a channel with NULL encoding.
IOStatus flush(ErrorG error)
          Flushes the write buffer for the GIOChannel.
IOStatus seekPosition(gint64 offset, SeekType type, ErrorG error)
          Replacement for gIoChannelSeek() with the new API.
IOStatus shutdown(bit flush, ErrorG err)
          Close an IO channel.
void ref()
          Converts an errno error number to a GIOChannelError.
void unref()
          Decrements the reference count of a GIOChannel.
GSource* createWatch(IOCondition condition)
          Creates a GSource that's dispatched when condition is met for the given channel.
guint addWatch(IOCondition condition, void* func, gpointer user_data)
          Adds the GIOChannel into the main event loop with the default priority.
guint watchFull(gint priority, IOCondition condition, void* func, gpointer user_data, void* notify)
          Adds the GIOChannel into the main event loop with the given priority.
gsize getBufferSize()
          Specifies the type of function passed to gIoAddWatch() or gIoAddWatchFull(), which is called when the requested condition on a GIOChannel is satisfied.
void setBufferSize(gsize size)
          Sets the buffer size.
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.
IOFlags getFlags()
          Gets the current flags for a GIOChannel, including read-only flags such as GIOFLAGISREADABLE.
IOStatus setFlags(IOFlags flags, ErrorG error)
          Sets the (writeable) flags in channel to (flags & GIOCHANNELSETMASK).
String getLineTerm(gint* length)
          This returns the string that GIOChannel uses to determine where in the file a line break occurs.
void setLineTerm(String line_term, gint length)
          This sets the string that GIOChannel uses to determine where in the file a line break occurs.
bit getBuffered()
          Returns whether channel is buffered.
void setBuffered(bit buffered)
          The buffering state can only be set if the channel's encoding is NULL.
String getEncoding()
          Gets the encoding for the input/output of the channel.
IOStatus setEncoding(String encoding, ErrorG error)
          Sets the encoding for the input/output of the channel.
bit getCloseOnUnref()
          Returns whether the file/socket/whatever associated with channel will be closed when channel receives its final unref and is destroyed.
void setCloseOnUnref(bit do_close)
          Setting this flag to TRUE for a channel you have already closed can cause problems.
IOError read(String buf, gsize count, gsize* bytes_read)
          Warning
gIoChannelRead is deprecated and should not be used in newly-written code.
IOError write(String buf, gsize count, gsize* bytes_written)
          Warning
gIoChannelWrite is deprecated and should not be used in newly-written code.
IOError seek(gint64 offset, SeekType type)
          Warning
gIoChannelSeek is deprecated and should not be used in newly-written code.
void close()
          Warning
gIoChannelClose is deprecated and should not be used in newly-written code.




public ctor(IOChannel)(GIOChannel* gIOChannel)
Creates a IOChannel from a GtkIOChannel

Parameters:
*gtkWidget - the gtk struct address pointer

Returns:



ctor(IOChannel)(int fd)
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 gGetCharset()) with the gIoChannelSetEncoding() function.
If you want to read raw binary data without interpretation, then call the gIoCharsetSetEncoding() function with NULL for the encoding argument.

Parameters:
fd - a file descriptor.

Returns:
a new GIOChannel.

init

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

Parameters:
channel - a GIOChannel.



ctor(IOChannel)(String filename, String mode, ErrorG error)
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 gIoChannelClose() (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

Parameters:
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 GFILEERROR.

Returns:
A GIOChannel on success, NULL on failure.

readChars

IOStatus readChars(String buf, gsize count, gsize* bytes_read, ErrorG error)
Replacement for gIoChannelRead() with the new API.

Parameters:
channel - a GIOChannel
buf - a buffer to read data into
count - the size of the buffer. Note that the buffer may not be complelely filled even if there is data in the buffer if the remaining data is not a complete character.
bytesRead - The number of bytes read. This may be zero even on success if count < 6 and the channel's encoding is non-NULL. This indicates that the next UTF-8 character is too wide for the buffer.
error - A location to return an error of type GConvertError or GIOChannelError.

Returns:
the status of the operation.

readUnichar

IOStatus readUnichar(gunichar* thechar, ErrorG error)
This function cannot be called on a channel with NULL encoding.

Parameters:
channel - a GIOChannel
thechar - a location to return a character
error - A location to return an error of type GConvertError or GIOChannelError

Returns:
a GIOStatus

readLine

IOStatus readLine(String str, gsize length, gsize terminator_pos, ErrorG error)
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.

Parameters:
channel - a GIOChannel
strReturn - The line read from the GIOChannel, including the line terminator. This data should be freed with gFree() when no longer needed. This is a nul-terminated string. If a length of zero is returned, this will be NULL instead.
length - location to store length of the read data, or NULL
terminatorPos - location to store position of line terminator, or NULL
error - A location to return an error of type GConvertError or GIOChannelError

Returns:
the status of the operation.

readLineString

IOStatus readLineString(StringG buffer, gsize terminator_pos, ErrorG error)
Reads a line from a GIOChannel, using a GString as a buffer.

Parameters:
channel - a GIOChannel
buffer - a GString into which the line will be written. If buffer already contains data, the old data will be overwritten.
terminatorPos - location to store position of line terminator, or NULL
error - a location to store an error of type GConvertError or GIOChannelError

Returns:
the status of the operation.

readToEnd

IOStatus readToEnd(String str, gsize length, ErrorG error)
Reads all the remaining data from the file.

Parameters:
channel - a GIOChannel
strReturn - Location to store a pointer to a string holding the remaining data in the GIOChannel. This data should be freed with gFree() when no longer needed. This data is terminated by an extra nul character, but there may be other nuls in the intervening data.
length - Location to store length of the data
error - A location to return an error of type GConvertError or GIOChannelError

Returns:
GIOSTATUSNORMAL on success. This function never returns GIOSTATUSEOF.

writeChars

IOStatus writeChars(String buf, gssize count, gsize* bytes_written, ErrorG error)
Replacement for gIoChannelWrite() with the new API.
br> On seekable channels with encodings other than NULL or UTF-8, generic mixing of reading and writing is not allowed. A call to gIoChannelWriteChars() may only be made on a channel from which data has been read in the cases described in the documentation for gIoChannelSetEncoding().

Parameters:
channel - a GIOChannel
buf - a buffer to write data from
count - the size of the buffer. If -1, the buffer is taken to be a nul-terminated string.
bytesWritten - The number of bytes written. This can be nonzero even if the return value is not GIOSTATUSNORMAL. If the return value is GIOSTATUSNORMAL and the channel is blocking, this will always be equal to count if count >= 0.
error - A location to return an error of type GConvertError or GIOChannelError

Returns:
the status of the operation.

writeUnichar

IOStatus writeUnichar(gunichar thechar, ErrorG error)
This function cannot be called on a channel with NULL encoding.

Parameters:
channel - a GIOChannel
thechar - a character
error - A location to return an error of type GConvertError or GIOChannelError

Returns:
a GIOStatus

flush

IOStatus flush(ErrorG error)
Flushes the write buffer for the GIOChannel.

Parameters:
channel - a GIOChannel
error - location to store an error of type GIOChannelError

Returns:
the status of the operation: One of GIOCHANNELNORMAL, GIOCHANNELAGAIN, or GIOCHANNELERROR.

seekPosition

IOStatus seekPosition(gint64 offset, SeekType type, ErrorG error)
Replacement for gIoChannelSeek() with the new API.

Parameters:
channel - a GIOChannel
offset - The offset in bytes from the position specified by type
type - a GSeekType. The type GSEEKCUR is only allowed in those cases where a call to gIoChannelSetEncoding() is allowed. See the documentation for gIoChannelSetEncoding() for details.
error - A location to return an error of type GIOChannelError

Returns:
the status of the operation.

shutdown

IOStatus shutdown(bit flush, ErrorG err)
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 gIoChannelUnref().

Parameters:
channel - a GIOChannel
flush - if TRUE, flush pending
err - location to store a GIOChannelError

Returns:
the status of the operation.

ref

void ref()
Converts an errno error number to a GIOChannelError.

Parameters:
en - an errno error number, e.g. EINVAL.
channel - a GIOChannel.

Returns:
a GIOChannelError error number, e.g. GIOCHANNELERRORINVAL. Increments the reference count of a GIOChannel.

unref

void unref()
Decrements the reference count of a GIOChannel.

Parameters:
channel - a GIOChannel.

createWatch

GSource* createWatch(IOCondition condition)
Creates a GSource that's dispatched when condition is met for the given channel.
For example, if condition is GIOIN, the source will be dispatched when there's data available for reading. gIoAddWatch() 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.

Parameters:
channel - a GIOChannel to watch
condition - conditions to watch for

Returns:
a new GSource

addWatch

guint addWatch(IOCondition condition, void* func, gpointer user_data)
Adds the GIOChannel into the main event loop with the default priority.

Parameters:
channel - a GIOChannel.
condition - the condition to watch for.
func - the function to call when the condition is satisfied.
userData - user data to pass to func.

Returns:
the event source id.

watchFull

guint watchFull(gint priority, IOCondition condition, void* func, gpointer user_data, void* notify)
Adds the GIOChannel into the main event loop with the given priority.

Parameters:
channel - a GIOChannel.
priority - the priority of the GIOChannel source.
condition - the condition to watch for.
func - the function to call when the condition is satisfied.
userData - user data to pass to func.
notify - the function to call when the source is removed.

Returns:
the event source id.

getBufferSize

gsize getBufferSize()
Specifies the type of function passed to gIoAddWatch() or gIoAddWatchFull(), which is called when the requested condition on a GIOChannel is satisfied.

Parameters:
source - the GIOChannel event source.
condition - the condition which has been satisfied.
data - user data set in gIoAddWatch() or gIoAddWatchFull().
channel - a GIOChannel

Returns:
the function should return FALSE if the event source should be removed. Gets the buffer size.
the size of the buffer.

setBufferSize

void setBufferSize(gsize size)
Sets the buffer size.

Parameters:
channel - a GIOChannel
size - the size of the buffer. 0 == pick a good size

getBufferCondition

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 GIOIN and GIOOUT may be set.

Parameters:
channel - A GIOChannel

Returns:
A GIOCondition

getFlags

IOFlags getFlags()
Gets the current flags for a GIOChannel, including read-only flags such as GIOFLAGISREADABLE.
br> The values of the flags GIOFLAGISREADABLE and GIOFLAGISWRITEABLE 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 gIoChannelGetFlags() to update the internal values of these flags.

Parameters:
channel - a GIOChannel

Returns:
the flags which are set on the channel

setFlags

IOStatus setFlags(IOFlags flags, ErrorG error)
Sets the (writeable) flags in channel to (flags & GIOCHANNELSETMASK).

Parameters:
channel - a GIOChannel.
flags - the flags to set on the IO channel.
error - A location to return an error of type GIOChannelError.

Returns:
the status of the operation.

getLineTerm

String getLineTerm(gint* length)
This returns the string that GIOChannel uses to determine where in the file a line break occurs.
A value of NULL indicates auto detection.

Parameters:
channel - a GIOChannel
length - a location to return the length of the line terminator

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

setLineTerm

void setLineTerm(String line_term, gint length)
This sets the string that GIOChannel uses to determine where in the file a line break occurs.

Parameters:
channel - a GIOChannel
lineTerm - The line termination string. Use NULL 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.

getBuffered

bit getBuffered()
Returns whether channel is buffered.

Parameters:
channel - a GIOChannel.

Returns:
TRUE if the channel is buffered.

setBuffered

void setBuffered(bit buffered)
The buffering state can only be set if the channel's encoding is NULL.
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 GIOSTATUSEOF not require such a flush. For write-only channels, a call to gIoChannelFlush() is sufficient. For all other channels, the buffers may be flushed by a call to gIoChannelSeekPosition(). This includes the possibility of seeking with seek type GSEEKCUR 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.

Parameters:
channel - a GIOChannel
buffered - whether to set the channel buffered or unbuffered

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.

Parameters:
channel - a GIOChannel

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

setEncoding

IOStatus setEncoding(String encoding, ErrorG error)
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 NULL 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 gIoChannelSeekPosition(). (This flushes all the internal buffers.)
4. The current encoding is NULL or UTF-8.
5. One of the (new API) read functions has just returned GIOSTATUSEOF (or, in the case of gIoChannelReadToEnd(), GIOSTATUSNORMAL).
6. One of the functions gIoChannelReadChars() or gIoChannelReadUnichar() has returned GIOSTATUSAGAIN or GIOSTATUSERROR. This may be useful in the case of GCONVERTERRORILLEGALSEQUENCE. Returning one of these statuses from gIoChannelReadLine(), gIoChannelReadLineString(), or gIoChannelReadToEnd() does not guarantee that the encoding can be changed.
Channels which do not meet one of the above conditions cannot call gIoChannelSeekPosition() with an offset of GSEEKCUR, and, if they are "seekable", cannot call gIoChannelWriteChars() after calling one of the API "read" functions.

Parameters:
channel - a GIOChannel
encoding - the encoding type
error - location to store an error of type GConvertError.

Returns:
GIOSTATUSNORMAL if the encoding was successfully set.

getCloseOnUnref

bit 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 gIoChannelNewFile(), and FALSE for all other channels.

Parameters:
channel - a GIOChannel.

Returns:
Whether the channel will be closed on the final unref of the GIOChannel data structure.

setCloseOnUnref

void setCloseOnUnref(bit do_close)
Setting this flag to TRUE for a channel you have already closed can cause problems.

Parameters:
channel - a GIOChannel
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.

read

IOError read(String buf, gsize count, gsize* bytes_read)
Warning
gIoChannelRead is deprecated and should not be used in newly-written code.
Use gIoChannelReadChars() instead.
Reads data from a GIOChannel.

Parameters:
channel - a GIOChannel.
buf - a buffer to read the data into (which should be at least count bytes long).
count - the number of bytes to read from the GIOChannel.
bytesRead - returns the number of bytes actually read.

Returns:
GIOERRORNONE if the operation was successful.

write

IOError write(String buf, gsize count, gsize* bytes_written)
Warning
gIoChannelWrite is deprecated and should not be used in newly-written code.
Use gIoChannelWriteChars() instead.
Writes data to a GIOChannel.

Parameters:
channel - a GIOChannel.
buf - the buffer containing the data to write.
count - the number of bytes to write.
bytesWritten - the number of bytes actually written.

Returns:
GIOERRORNONE if the operation was successful.

seek

IOError seek(gint64 offset, SeekType type)
Warning
gIoChannelSeek is deprecated and should not be used in newly-written code.
Use gIoChannelSeekPosition() instead.
Sets the current position in the GIOChannel, similar to the standard library function fseek().

Parameters:
channel - a GIOChannel.
offset - an offset, in bytes, which is added to the position specified by type
type - the position in the file, which can be GSEEKCUR (the current position), GSEEKSET (the start of the file), or GSEEKEND (the end of the file).

Returns:
GIOERRORNONE if the operation was successful.

close

void close()
Warning
gIoChannelClose is deprecated and should not be used in newly-written code.
Use gIoChannelShutdown() 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 gIoChannelUnref().

Parameters:
channel - A GIOChanne