glibmm 2.52.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Examples

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Glib::IOChannel Class Reference

IOChannel aims to provide portable I/O support for files, pipes and sockets, and to integrate them with the GLib main event loop. More...

`#include <glibmm/iochannel.h>`

Inheritance diagram for Glib::IOChannel:
[legend]

## Public Member Functions
virtual | ~IOChannel ()
IOStatus | read (gunichar& thechar)
| Read a single UCS-4 character. More...
IOStatus | read (char* buf, gsize count, gsize& bytes_read)
| Read a character sequence into memory. More...
IOStatus | read (Glib::ustring& str, gsize count)
| Read a maximum of count bytes into str. More...
IOStatus | read_line (Glib::ustring& line)
| Read a whole line. More...
IOStatus | read_to_end (Glib::ustring& str)
| Reads all the remaining data from the file. More...
IOStatus | write (const Glib::ustring& str)
| Write a string to the I/O channel. More...
IOStatus | write (const char* buf, gssize count, gsize& bytes_written)
| Write a memory area of count bytes to the I/O channel. More...
IOStatus | write (gunichar unichar)
| Write a single UCS-4 character to the I/O channel. More...
IOStatus | seek (gint64 offset, SeekType type=SEEK_TYPE_SET)
| Seek the I/O channel to a specific position. More...
IOStatus | flush ()
| Flush the buffers of the I/O channel. More...
IOStatus | close (bool flush_pending=true)
| Close the I/O channel. More...
gsize | get_buffer_size () const
| Get the IOChannel internal buffer size. More...
void | set_buffer_size (gsize size)
| Set the internal IOChannel buffer size. More...
IOFlags | get_flags () const
| Get the current flags for a IOChannel, including read-only flags such as Glib::IO_FLAG_IS_READABLE. More...
IOStatus | set_flags (IOFlags flags)
| Set flags on the IOChannel. More...
void | set_buffered (bool buffered)
| Set the buffering status of the I/O channel. More...
bool | get_buffered () const
| Get the buffering status of the I/O channel. More...
IOCondition | get_buffer_condition () const
| Returns an IOCondition depending on whether there is data to be read/space to write data in the internal buffers in the I/O channel. More...
bool | get_close_on_unref () const
| Returns whether the file/socket/whatever associated with the I/O channel will be closed when the channel receives its final unref and is destroyed. More...
void | set_close_on_unref (bool do_close)
| Setting this flag to `true` for a channel you have already closed can cause problems. More...
IOStatus | set_encoding (const std::string& encoding=std::string())
| Sets the encoding for the input/output of the channel. More...
std::string | get_encoding () const
| Get the encoding of the I/O channel. More...
void | set_line_term (const std::string& term=std::string())
std::string | get_line_term () const
Glib::RefPtr< IOSource > | create_watch (IOCondition condition)
| Creates an IOSource object. More...
virtual void | reference () const
virtual void | unreference () const
GIOChannel* | gobj ()
const GIOChannel* | gobj () const
Public Member Functions inherited from sigc::trackable
| trackable () noexcept
| trackable (const trackable &src) noexcept
| trackable (trackable &&src)
| ~trackable ()
void | add_destroy_notify_callback (void *data, func_destroy_notify func) const
void | notify_callbacks ()
trackable & | operator= (const trackable &src)
trackable & | operator= (trackable &&src)
void | remove_destroy_notify_callback (void *data) const

## Static Public Member Functions
static Glib::RefPtr< IOChannel > | create_from_file (const std::string& filename, const std::string& mode)
| Open a file filename as an I/O channel using mode mode. More...
static Glib::RefPtr< IOChannel > | create_from_fd (int fd)
| Creates an I/O channel from a file descriptor. More...
static Glib::RefPtr< IOChannel > | create_from_win32_fd (int fd)
| Create an I/O channel for C runtime (emulated Unix-like) file descriptors. More...
static Glib::RefPtr< IOChannel > | create_from_win32_socket (int socket)
| Create an I/O channel for a winsock socket. More...

## Protected Member Functions
| IOChannel ()
| Constructor that should be used by derived classes. More...
virtual IOStatus | read_vfunc (char* buf, gsize count, gsize& bytes_read)
virtual IOStatus | write_vfunc (const char* buf, gsize count, gsize& bytes_written)
virtual IOStatus | seek_vfunc (gint64 offset, SeekType type)
virtual IOStatus | close_vfunc ()
virtual IOStatus | set_flags_vfunc (IOFlags flags)
virtual IOFlags | get_flags_vfunc ()
virtual Glib::RefPtr< Glib::Source > | create_watch_vfunc (IOCondition cond)

## Protected Attributes
GIOChannel* | gobject_

## Additional Inherited Members
Public Types inherited from sigc::trackable
typedef internal::func_destroy_notify | func_destroy_notify

## Detailed Description

IOChannel aims to provide portable I/O support for files, pipes and sockets, and to integrate them with the GLib main event loop.

Note that IOChannels implement an automatic implicit character set conversion to the data stream, and usually will not pass by default binary data unchanged. To set the encoding of the channel, use e.g. set_encoding("ISO-8859-15"). To set the channel to no encoding, use set_encoding() without any arguments.

You can create an IOChannel with one of the static create methods.

## Constructor & Destructor Documentation

virtual Glib::IOChannel::~IOChannel | ( |  | ) |
| virtual

Glib::IOChannel::IOChannel | ( |  | ) |
| protected

Constructor that should be used by derived classes.

Use this constructor if you want to inherit from IOChannel. It will set up a GIOChannel that will call the vfuncs of your class even if it is being used from C code, and it will keep a reference to the C++ code while the GIOChannel exists.

## Member Function Documentation

IOStatus Glib::IOChannel::close | ( | bool | flush_pending = `true` | ) |

Close the I/O 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. Accessing the channel after closing it is considered an error.

Parameters

  flush_pending | Whether to flush() pending data before closing the channel.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |

virtual IOStatus Glib::IOChannel::close_vfunc | ( |  | ) |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

static Glib::RefPtr<IOChannel> Glib::IOChannel::create_from_fd | ( | int | fd | ) |
| static

Creates an I/O channel from a file descriptor.

On Unix, IOChannels created with this function work for any file descriptor or socket.

On Win32, this can be used either for files opened with the MSVCRT (the Microsoft run-time C library) `_open()` or `_pipe()`, including file descriptors 0, 1 and 2 (corresponding to `stdin`, `stdout` and `stderr`), or for Winsock `SOCKET`s. If the parameter is a legal file descriptor, it is assumed to be such, otherwise it should be a `SOCKET`. This relies on `SOCKET`s and file descriptors not overlapping. If you want to be certain, call either create_from_win32_fd() or create_from_win32_socket() instead as appropriate.

The term file descriptor as used in the context of Win32 refers to the emulated Unix-like file descriptors MSVCRT provides. The native corresponding concept is file `HANDLE`. There isn't as of yet a way to get IOChannels for Win32 file `HANDLE`s.

static Glib::RefPtr<IOChannel> Glib::IOChannel::create_from_file | ( | const std::string & | filename,
|  | const std::string & | mode
| ) |  |
| static

Open a file filename as an I/O channel using mode mode.

This channel will be closed when the last reference to it is dropped, so there is no need to call close() (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

Parameters

  filename | The name of the file to open.
  mode | One of `"r"`, `"w"`, `"a"`, `"r+"`, `"w+"`, `"a+"`. These have the same meaning as in `fopen()`.

Returns
  An IOChannel for the opened file.

Exceptions

  Glib::FileError |

static Glib::RefPtr<IOChannel> Glib::IOChannel::create_from_win32_fd | ( | int | fd | ) |
| static

Create an I/O channel for C runtime (emulated Unix-like) file descriptors.

After calling add_watch() on a I/O channel returned by this function, you shouldn't call read() on the file descriptor. This is because adding polling for a file descriptor is implemented on Win32 by starting a thread that sits blocked in a `read()` from the file descriptor most of the time. All reads from the file descriptor should be done by this internal GLib thread. Your code should call only IOChannel::read().

static Glib::RefPtr<IOChannel> Glib::IOChannel::create_from_win32_socket | ( | int | socket | ) |
| static

Create an I/O channel for a winsock socket.

The parameter should be a `SOCKET`. Contrary to I/O channels for file descriptors (on Win32), you can use normal `recv()` or `recvfrom()` on sockets even if GLib is polling them.

Glib::RefPtr<IOSource> Glib::IOChannel::create_watch | ( | IOCondition | condition | ) |

Creates an IOSource object.

Create a slot from a function to be called when condition is met for the channel with sigc::ptr_fun() or sigc::mem_fun() and pass it into the connect() function of the returned IOSource object. Polling of the channel will start when you attach a MainContext object to the returned IOSource object using its attach() function.

Glib::signal_io().connect() is a simpler interface to the same functionality, for the case where you want to add the source to the default main context.

Parameters

  condition | The condition to watch for.

Returns
  An IOSource object that can be polled from a MainContext's event loop.

virtual Glib::RefPtr<Glib::Source> Glib::IOChannel::create_watch_vfunc | ( | IOCondition | cond | ) |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

IOStatus Glib::IOChannel::flush | ( |  | ) |

Flush the buffers of the I/O channel.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOCondition Glib::IOChannel::get_buffer_condition | ( |  | ) | const

Returns an IOCondition depending on whether there is data to be read/space to write data in the internal buffers in the I/O channel.

Only the flags Glib::IO_IN and Glib::IO_OUT may be set.

Returns
  Bitwise combination of Glib::IOCondition flags.

gsize Glib::IOChannel::get_buffer_size | ( |  | ) | const

Get the IOChannel internal buffer size.

Returns
  The buffer size.

bool Glib::IOChannel::get_buffered | ( |  | ) | const

Get the buffering status of the I/O channel.

Returns
  The buffering status of the channel.

bool Glib::IOChannel::get_close_on_unref | ( |  | ) | const

Returns whether the file/socket/whatever associated with the I/O channel will be closed when the channel receives its final unref and is destroyed.

The default value of this is `true` for channels created by create_from_file(), and `false` for all other channels.

Returns
  Whether the channel will be closed on the final unref of the IOChannel object.

std::string Glib::IOChannel::get_encoding | ( |  | ) | const

Get the encoding of the I/O channel.

Returns
  The current encoding of the channel.

IOFlags Glib::IOChannel::get_flags | ( |  | ) | const

Get the current flags for a IOChannel, including read-only flags such as Glib::IO_FLAG_IS_READABLE.

The values of the flags Glib::IO_FLAG_IS_READABLE and Glib::IO_FLAG_IS_WRITEABLE 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 get_flags() to update the internal values of these flags.

Returns
  Bitwise combination of the flags set on the channel.

virtual IOFlags Glib::IOChannel::get_flags_vfunc | ( |  | ) |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

std::string Glib::IOChannel::get_line_term | ( |  | ) | const

GIOChannel* Glib::IOChannel::gobj | ( |  | ) |
| inline

const GIOChannel* Glib::IOChannel::gobj | ( |  | ) | const
| inline

IOStatus Glib::IOChannel::read | ( | gunichar & | thechar | ) |

Read a single UCS-4 character.

Return values

  thechar | The Unicode character.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::read | ( | char * | buf,
|  | gsize | count,
|  | gsize & | bytes_read
| ) |  |

Read a character sequence into memory.

Parameters

  buf | A buffer to read data into.
  count | The size of the buffer in bytes. 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.

Return values

  bytes_read | The number of bytes read. This may be zero even on success if count < 6 and the channel's encoding is not `""`. This indicates that the next UTF-8 character is too wide for the buffer.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::read | ( | Glib::ustring& | str,
|  | gsize | count
| ) |  |

Read a maximum of count bytes into str.

Parameters

  count | The maximum number of bytes to read.

Return values

  str | The characters that have been read.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::read_line | ( | Glib::ustring& | line | ) |

Read a whole line.

Reads until the line separator is found, which is included in the result string.

Return values

  line | The line that was read.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::read_to_end | ( | Glib::ustring& | str | ) |

Reads all the remaining data from the file.

Return values

  str | The resulting string.

Returns
  Glib::IO_STATUS_NORMAL on success. This function never returns Glib::IO_STATUS_EOF.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

virtual IOStatus Glib::IOChannel::read_vfunc | ( | char * | buf,
|  | gsize | count,
|  | gsize & | bytes_read
| ) |  |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

virtual void Glib::IOChannel::reference | ( |  | ) | const
| virtual

IOStatus Glib::IOChannel::seek | ( | gint64 | offset,
|  | SeekType | type = `SEEK_TYPE_SET`
| ) |  |

Seek the I/O channel to a specific position.

Parameters

  offset | The offset in bytes from the position specified by type.
  type | A SeekType. The type Glib::SEEK_TYPE_CUR is only allowed in those cases where a call to set_encoding() is allowed. See the documentation for set_encoding() for details.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

virtual IOStatus Glib::IOChannel::seek_vfunc | ( | gint64 | offset,
|  | SeekType | type
| ) |  |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

void Glib::IOChannel::set_buffer_size | ( | gsize | size | ) |

Set the internal IOChannel buffer size.

Parameters

  size | The buffer size the IOChannel should use.

void Glib::IOChannel::set_buffered | ( | bool | buffered | ) |

Set the buffering status of the I/O channel.

The buffering state can only be set if the channel's encoding is `""`. 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 Glib::IO_STATUS_EOF not require such a flush. For write-only channels, a call to flush() is sufficient. For all other channels, the buffers may be flushed by a call to seek(). This includes the possibility of seeking with seek type Glib::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.

The default state of the channel is buffered.

Parameters

  buffered | Whether to set the channel buffered or unbuffered.

void Glib::IOChannel::set_close_on_unref | ( | bool | do_close | ) |

Setting this flag to `true` for a channel you have already closed can cause problems.

Parameters

  do_close | Whether to close the channel on the final unref of the IOChannel object. The default value of this is `true` for channels created by create_from_file(), and `false` for all other channels.

IOStatus Glib::IOChannel::set_encoding | ( | const std::string & | encoding = `std::string()` | ) |

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 `""` 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 seek_position(). (This flushes all the internal buffers.)
4. The current encoding is `""` or UTF-8.
5. One of the read methods has just returned Glib::IO_STATUS_EOF (or, in the case of read_to_end(), Glib::IO_STATUS_NORMAL).
6. The read() method has returned Glib::IO_STATUS_AGAIN or thrown a Glib::Error exception. This may be useful in the case of ConvertError::ILLEGAL_SEQUENCE. Returning one of these statuses from read_line() or read_to_end() does not guarantee that the encoding can be changed.

Channels which do not meet one of the above conditions cannot call seek_position() with a seek type of Glib::SEEK_TYPE_CUR and, if they are "seekable", cannot call write() after calling one of the API "read" methods.

Parameters

  encoding | The encoding name, or `""` for binary.

Returns
  Glib::IO_STATUS_NORMAL if the encoding was successfully set.

Exceptions

  Glib::IOChannelError |

IOStatus Glib::IOChannel::set_flags | ( | IOFlags | flags | ) |

Set flags on the IOChannel.

Parameters

  flags | Bitwise combination of the flags to set.

Returns
  The operation result code.

Exceptions

  Glib::IOChannelError |

virtual IOStatus Glib::IOChannel::set_flags_vfunc | ( | IOFlags | flags | ) |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

void Glib::IOChannel::set_line_term | ( | const std::string & | term = `std::string()` | ) |

virtual void Glib::IOChannel::unreference | ( |  | ) | const
| virtual

IOStatus Glib::IOChannel::write | ( | const Glib::ustring& | str | ) |

Write a string to the I/O channel.

Note that this method does not return the number of characters written. If the channel is blocking and the returned value is Glib::IO_STATUS_NORMAL, the whole string was written.

Parameters

  str | the string to write.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::write | ( | const char * | buf,
|  | gssize | count,
|  | gsize & | bytes_written
| ) |  |

Write a memory area of count bytes to the I/O channel.

Parameters

  buf | The start of the memory area.
  count | The number of bytes to write.

Return values

  bytes_written | The number of bytes written to the channel.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

IOStatus Glib::IOChannel::write | ( | gunichar | unichar | ) |

Write a single UCS-4 character to the I/O channel.

Parameters

  unichar | The character to write.

Returns
  The status of the operation.

Exceptions

  Glib::IOChannelError |
  Glib::ConvertError |

virtual IOStatus Glib::IOChannel::write_vfunc | ( | const char * | buf,
|  | gsize | count,
|  | gsize & | bytes_written
| ) |  |
| protected virtual

Deprecated:
  Custom Glib::IOChannel implementation was never really supported.

Reimplemented in Glib::StreamIOChannel.

## Member Data Documentation

GIOChannel* Glib::IOChannel::gobject_
| protected