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

Buffer Class Reference

A character buffer readable and writable with standard I/O methods. More...

#include <miodevice.h>

Inheritance diagram for Buffer:

IODevice List of all members.

Public Methods

 Buffer (int mode=IO_ReadWrite)
 Buffer (String &buffer, int mode=IO_ReadWrite)
virtual bool open (int mode)
virtual uint size () const
virtual int at () const
bool reset ()
virtual bool atEnd () const
virtual int readBlock (char *data, uint maxlen) 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 readLine (char *data, uint maxlen) 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

A character buffer readable and writable with standard I/O methods.

@example

      String myString;
        Buffer myBuffer (myString);       // Attach buffer to a string (buffer)
      TextOStream myStream (myBuffer);  // Attach stream to the buffer
      myStream << "Hello, world!";      // Put something in the stream
      cout << myString << newl;         // Prints out "Hello, world!\n"

Definition at line 244 of file miodevice.h.


Constructor & Destructor Documentation

Buffer int    mode = IO_ReadWrite
 

Default constructor for Buffer.

Definition at line 779 of file miodevice.cc.

References MagiC::i18n(), and open().

Buffer String   buffer,
int    mode = IO_ReadWrite
 

Constructs a buffer to use a given string as the buffer.

Notice that the string is _not_ destructed along with the Buffer.

Definition at line 796 of file miodevice.cc.

References MagiC::i18n(), and open().


Member Function Documentation

virtual int at   const [inline, virtual]
 

Returns the current position in device.

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

Reimplemented from IODevice.

Definition at line 252 of file miodevice.h.

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 from IODevice.

Definition at line 841 of file miodevice.cc.

References size().

int getch   throw (device_not_open) [virtual]
 

Reads and returns one character from device.

Returns:
The character read.

Reimplemented from IODevice.

Definition at line 907 of file miodevice.cc.

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.

Reimplemented from IODevice.

Definition at line 815 of file miodevice.cc.

References String::empty(), IO_Append, IO_Truncate, String::length(), and reset().

Referenced by Buffer().

void putch char    c throw (device_not_open) [virtual]
 

Writes one character to device.

Reimplemented from IODevice.

Definition at line 917 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.

Reimplemented from IODevice.

Definition at line 849 of file miodevice.cc.

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.

Reimplemented from IODevice.

Definition at line 872 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 from IODevice.

Definition at line 832 of file miodevice.cc.

References IODevice::reset().

Referenced by open().

virtual uint size   const [inline, virtual]
 

Returns the size of the file in bytes.

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

Reimplemented from IODevice.

Definition at line 251 of file miodevice.h.

References String::length().

Referenced by atEnd().

void ungetch char    c throw (device_not_open) [virtual]
 

Unreads one character from device.

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

Reimplemented from IODevice.

Definition at line 930 of file miodevice.cc.

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.

Reimplemented from IODevice.

Definition at line 900 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.

Reimplemented from IODevice.

Definition at line 864 of file miodevice.cc.


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