Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

IODevice Class Reference

Baseclass for I/O devices. More...

#include <miodevice.h>

Inheritance diagram for IODevice:

Buffer File Socket List of all members.

Public Methods

 IODevice ()
virtual ~IODevice ()
bool isDirectAccess () const
bool isSequentialAccess () const
bool isSynchronous () const
bool isAsynchronous () const
virtual bool open (int mode)
virtual void close ()
virtual void flush ()
virtual uint size () const
virtual int at () const
virtual bool atEnd () const
virtual bool seek (int)
bool reset ()
virtual int readBlock (char *data, uint maxlen) throw (device_not_open)
virtual int readLine (char *data, uint maxlen) throw (device_not_open)
Ref< StringreadAll () throw (device_not_open)
List< String > * readLines () throw (device_not_open)
virtual int writeBlock (const char *data, uint len) throw (device_not_open)
int writeBlock (const String &) throw (device_not_open)
virtual int getch () throw (device_not_open)
virtual void putch (char ch) throw (device_not_open)
virtual void ungetch (char ch) throw (device_not_open)

Detailed Description

Baseclass for I/O devices.

Definition at line 96 of file miodevice.h.


Constructor & Destructor Documentation

IODevice  
 

Default constructor for IODevice.

Definition at line 48 of file miodevice.cc.

~IODevice   [virtual]
 

Virtual destructor for IODevice.

Definition at line 58 of file miodevice.cc.


Member Function Documentation

int at   const [virtual]
 

Returns the current position in device.

Only meaningful for "block devices", which have a defined size.

Reimplemented in File, Buffer, and Socket.

Definition at line 156 of file miodevice.cc.

bool atEnd   const [virtual]
 

Returns true if currently at end of file, and false if not.

This is useful for iterating over a device.

Reimplemented in File, and Buffer.

Definition at line 166 of file miodevice.cc.

Referenced by TextIStream::operator>>(), and TextIStream::readLine().

void close   [virtual]
 

Closes the device.

Flushes open buffers.

Reimplemented in File.

Definition at line 122 of file miodevice.cc.

void flush   [virtual]
 

Flushes (writes) the buffers.

Flushing ensures that all data that has been written to the device is really written, and does not wait in a buffer. This is important if an application crashes unexpectedly, or if we just want to be able to read it immediately.

This has only effect if the device has been opened for buffered writing.

Reimplemented in File.

Definition at line 137 of file miodevice.cc.

Referenced by OStream::flush(), and TextOStream::printf().

int getch   throw (device_not_open) [virtual]
 

Reads and returns one character from device.

Returns:
The character read.

Reimplemented in File, Buffer, and Socket.

Definition at line 309 of file miodevice.cc.

Referenced by TextIStream::operator>>().

bool isAsynchronous   const
 

Returns true if the device is asynchronous, i.e., buffered.

Definition at line 85 of file miodevice.cc.

References IO_Raw.

bool isDirectAccess   const
 

Returns true if the device is capable of direct access.

Definition at line 64 of file miodevice.cc.

bool isSequentialAccess   const
 

Returns true if the device is capable of sequential access.

Definition at line 71 of file miodevice.cc.

bool isSynchronous   const
 

Returns true if the device is synchronous, i.e., raw or unbuffered.

Definition at line 78 of file miodevice.cc.

References IO_Raw.

bool open int    mode [virtual]
 

Opens device with given I/O mode.

Typical usage:

        File myFile ("/path/file.ext");
        myFile.open (IO_Readable);
        String myFileContents = myFile.readAll ();
        myFile.close ();

  • IO_Raw Raw (unbuffered) I/O mode
  • IO_Readable The device is opened read-only
  • IO_Writable The device is opened write-only
  • IO_ReadWrite The device is opened read-write. This is equivalent to (IO_Readable | IO_Writable).
  • IO_Append The device is appended, if possible
  • IO_Truncate The device is truncated
Exceptions:
invalid_flags  if the combination of mode flags was somehow invalid.
open_failure  if there was some other error while opening.
Returns:
true if successful, false if opening failed for an obvious reason.
Parameters:
mode  The opening mode is a or-ed combination of I/O mode flags. For example: (IO_Writable | IO_Truncate).

Reimplemented in File, Buffer, and Socket.

Definition at line 112 of file miodevice.cc.

Referenced by IStream::IStream().

void putch char    ch throw (device_not_open) [virtual]
 

Writes one character to device.

Reimplemented in File, Buffer, and Socket.

Definition at line 318 of file miodevice.cc.

Ref< String > readAll   throw (device_not_open)
 

Reads the entire device into a string buffer.

Returns:
String buffer.

Definition at line 276 of file miodevice.cc.

int readBlock char *    data,
uint    maxlen
throw (device_not_open) [virtual]
 

Reads a data block from the device.

Returns:
Returns the number of bytes actually read, or 0 if none.
Parameters:
data  Data buffer
maxlen  Length of the buffer.

Reimplemented in File, Buffer, and Socket.

Definition at line 196 of file miodevice.cc.

Referenced by DataIStream::operator>>(), TextIStream::readRawBytes(), and DataIStream::readRawBytes().

int readLine char *    data,
uint    maxlen
throw (device_not_open) [virtual]
 

Reads a newline (\n) terminated line from device.

The buffer must be allocated to contain the given length of bytes.

The function spares one byte for the terminating zero, so the actual maximum number of bytes read is one less than the maxlen parameter.

Example:

  // Read in chunks
  char buffer [1024];
  int bytesRead = 0;
  int totalRead = 0;
  while (bytesRead = mpDevice->readLine (buffer, 1024)) {
      linebuf.append (buffer);
      totalRead += bytesRead;
  }

Note:
The default implementation reads the line with getch(). If you are implementing a device, you may want to implement a faster version that uses lower-level functions.
Returns:
Returns the number of bytes actually read, or 0 if none.
Parameters:
data  (output) Data buffer
maxlen  (input) Length of the buffer.

Reimplemented in File, Buffer, and Socket.

Definition at line 242 of file miodevice.cc.

Referenced by TextIStream::readLine().

List< String > * readLines   throw (device_not_open)
 

Reads all newline (\n) terminated lines from device.

Returns:
Returns list of the lines, including the terminating newline characters.

Definition at line 297 of file miodevice.cc.

bool reset  
 

Resets the state of the device to a newly opened state.

Returns:
TRUE if successful, FALSE if an error occurred.

Reimplemented in Buffer, and Socket.

Definition at line 187 of file miodevice.cc.

Referenced by Buffer::reset().

bool seek int    position [virtual]
 

Changes the current position in device.

The function has meaning only if the device is capable of direct access.

Returns:
true if successfull, false on failure.
Parameters:
position  New position.

Reimplemented in File.

Definition at line 178 of file miodevice.cc.

uint size   const [virtual]
 

Returns the size of the file in bytes.

Only meaningful for "block devices", which have a defined size.

Reimplemented in File, Buffer, and Socket.

Definition at line 146 of file miodevice.cc.

void ungetch char    ch throw (device_not_open) [virtual]
 

Unreads one character from device.

The function has no meaning if the device is already at beginning.

Reimplemented in File, Buffer, and Socket.

Definition at line 329 of file miodevice.cc.

Referenced by TextIStream::operator>>().

int writeBlock const String   str throw (device_not_open)
 

Writes a data block to device.

Returns:
Returns the number of bytes actually written, or 0 if none.
Parameters:
str  Data buffer to write.

Reimplemented in Buffer, and Socket.

Definition at line 266 of file miodevice.cc.

int writeBlock const char *    data,
uint    len
throw (device_not_open) [virtual]
 

Writes a data block to device.

Returns:
Returns the number of bytes actually written, or 0 if none.
Parameters:
data  (output) Data buffer
len  (input) Length of the buffer.

Reimplemented in File, Buffer, and Socket.

Definition at line 208 of file miodevice.cc.

Referenced by TextOStream::operator<<(), and TextOStream::printf().


The documentation for this class was generated from the following files:
Generated on Thu Feb 10 20:06:43 2005 for LibMagiC by doxygen1.2.18