Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
QIODevice Class Referenceabstract

The QIODevice class is the base class of I/O devices. More...

#include <qiodevice.h>

Inheritance diagram for QIODevice:
QBuffer QFile QGStringBuffer QStringBuffer

Public Member Functions

 QIODevice ()
 
virtual ~QIODevice ()
 
int flags () const
 
int mode () const
 
int state () const
 
bool isDirectAccess () const
 
bool isSequentialAccess () const
 
bool isCombinedAccess () const
 
bool isBuffered () const
 
bool isRaw () const
 
bool isSynchronous () const
 
bool isAsynchronous () const
 
bool isTranslated () const
 
bool isReadable () const
 
bool isWritable () const
 
bool isReadWrite () const
 
bool isInactive () const
 
bool isOpen () const
 
int status () const
 
void resetStatus ()
 
virtual bool open (int mode)=0
 
virtual void close ()=0
 
virtual void flush ()=0
 
virtual uint size () const =0
 
virtual int at () const
 
virtual bool at (int)
 
virtual bool atEnd () const
 
bool reset ()
 
virtual int readBlock (char *data, uint maxlen)=0
 
virtual int writeBlock (const char *data, uint len)=0
 
virtual int readLine (char *data, uint maxlen)
 
int writeBlock (const QByteArray &data)
 
QByteArray readAll ()
 
virtual int getch ()=0
 
virtual int putch (int)=0
 
virtual int ungetch (int)=0
 

Protected Member Functions

void setFlags (int f)
 
void setType (int)
 
void setMode (int)
 
void setState (int)
 
void setStatus (int)
 

Protected Attributes

int ioIndex
 

Private Member Functions

 QIODevice (const QIODevice &)
 
QIODeviceoperator= (const QIODevice &)
 

Private Attributes

int ioMode
 
int ioSt
 

Detailed Description

The QIODevice class is the base class of I/O devices.

An I/O device represents a medium that one can read bytes from and/or write bytes to. The QIODevice class is the abstract superclass of all such devices; classes like QFile, QBuffer and QSocket inherit QIODevice and implement virtual functions like write() appropriately.

While applications sometimes use QIODevice directly, mostly it is better to go through QTextStream and QDataStream, which provide stream operations on any QIODevice subclass. QTextStream provides text-oriented stream functionality (for human-readable ASCII files, for example), while QDataStream deals with binary data in a totally platform-independent manner.

The public member functions in QIODevice roughly fall into two groups: The action functions and the state access functions. The most important action functions are:

There are also some other, less used, action functions:

The state access are all "get" functions. The QIODevice subclass calls setState() to update the state, and simple access functions tell the user of the device what the device's state is. Here are the settings, and their associated access functions:

QIODevice provides numerous pure virtual functions you need to implement when subclassing it. Here is a skeleton subclass with all the members you are certain to need, and some it's likely that you will need:

class YourDevice : public QIODevice
{
public:
YourDevice();
~YourDevice();
bool open( int mode );
void close();
void flush();
uint size() const;
int at() const; // not a pure virtual function
bool at( int ); // not a pure virtual function
bool atEnd() const; // not a pure virtual function
int readBlock( char *data, uint maxlen );
int writeBlock( const char *data, uint len );
int readLine( char *data, uint maxlen );
int getch();
int putch( int );
int ungetch( int );
};

The three non-pure virtual functions can be ignored if your device is sequential (e.g. an RS-232 port).

See also
QDataStream, QTextStream

Definition at line 88 of file qiodevice.h.

Constructor & Destructor Documentation

QIODevice::QIODevice ( )

Constructs an I/O device.

Definition at line 179 of file qiodevice.cpp.

180 {
181  ioMode = 0; // initial mode
182  ioSt = IO_Ok;
183  ioIndex = 0;
184 }
int ioSt
Definition: qiodevice.h:145
#define IO_Ok
Definition: qiodevice.h:77
int ioMode
Definition: qiodevice.h:144
int ioIndex
Definition: qiodevice.h:141
QIODevice::~QIODevice ( )
virtual

Destructs the I/O device.

Definition at line 190 of file qiodevice.cpp.

191 {
192 }
QIODevice::QIODevice ( const QIODevice )
private

Member Function Documentation

int QIODevice::at ( ) const
virtual

Virtual function that returns the current I/O device index.

This index is the data read/write head of the I/O device.

See also
size()

Reimplemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

Definition at line 471 of file qiodevice.cpp.

472 {
473  return ioIndex;
474 }
int ioIndex
Definition: qiodevice.h:141
bool QIODevice::at ( int  pos)
virtual

Virtual function that sets the I/O device index to pos.

See also
size()

Reimplemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

Definition at line 481 of file qiodevice.cpp.

482 {
483 #if defined(CHECK_RANGE)
484  if ( (uint)pos > size() ) {
485  qWarning( "QIODevice::at: Index %d out of range", pos );
486  return FALSE;
487  }
488 #endif
489  ioIndex = pos;
490  return TRUE;
491 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
unsigned uint
Definition: qglobal.h:351
const bool TRUE
Definition: qglobal.h:371
virtual uint size() const =0
int ioIndex
Definition: qiodevice.h:141
bool QIODevice::atEnd ( ) const
virtual

Virtual function that returns TRUE if the I/O device index is at the end of the input.

Reimplemented in QFile.

Definition at line 498 of file qiodevice.cpp.

499 {
500  if ( isSequentialAccess() || isTranslated() ) {
501  QIODevice* that = (QIODevice*)this;
502  int c = that->getch();
503  bool result = c < 0;
504  that->ungetch(c);
505  return result;
506  } else {
507  return at() == (int)size();
508  }
509 }
virtual int ungetch(int)=0
static QCString result
virtual int getch()=0
bool isSequentialAccess() const
Definition: qiodevice.h:99
bool isTranslated() const
Definition: qiodevice.h:105
The QIODevice class is the base class of I/O devices.
Definition: qiodevice.h:88
virtual uint size() const =0
virtual int at() const
Definition: qiodevice.cpp:471
void QIODevice::close ( )
pure virtual

Closes the I/O device.

This virtual function must be reimplemented by all subclasses.

See also
open()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::flags ( ) const
inline

Returns the current I/O device flags setting.

Flags consists of mode flags and state flags.

See also
mode(), state()

Definition at line 94 of file qiodevice.h.

94 { return ioMode; }
int ioMode
Definition: qiodevice.h:144
void QIODevice::flush ( )
pure virtual

Flushes an open I/O device.

This virtual function must be reimplemented by all subclasses.

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::getch ( )
pure virtual

Reads a single byte/character from the I/O device.

Returns the byte/character read, or -1 if the end of the I/O device has been reached.

This virtual function must be reimplemented by all subclasses.

See also
putch(), ungetch()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

bool QIODevice::isAsynchronous ( ) const
inline

Returns TRUE if the I/O device is a asynchronous device, otherwise FALSE.

This mode is currently not in use.

See also
isSynchronous()

Definition at line 104 of file qiodevice.h.

104 { return ((ioMode & IO_Async) == IO_Async); }
int ioMode
Definition: qiodevice.h:144
#define IO_Async
Definition: qiodevice.h:57
bool QIODevice::isBuffered ( ) const
inline

Returns TRUE if the I/O device is a buffered (not raw) device, otherwise FALSE.

See also
isRaw()

Definition at line 101 of file qiodevice.h.

101 { return ((ioMode & IO_Raw) != IO_Raw); }
#define IO_Raw
Definition: qiodevice.h:56
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isCombinedAccess ( ) const
inline

Returns TRUE if the I/O device is a combined access (both direct and sequential) device, otherwise FALSE.

This access method is currently not in use.

Definition at line 100 of file qiodevice.h.

100 { return ((ioMode & IO_Combined) == IO_Combined); }
#define IO_Combined
Definition: qiodevice.h:51
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isDirectAccess ( ) const
inline

Returns TRUE if the I/O device is a direct access (not sequential) device, otherwise FALSE.

See also
isSequentialAccess()

Definition at line 98 of file qiodevice.h.

98 { return ((ioMode & IO_Direct) == IO_Direct); }
#define IO_Direct
Definition: qiodevice.h:49
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isInactive ( ) const
inline

Returns TRUE if the I/O device state is 0, i.e. the device is not open.

See also
isOpen()

Definition at line 109 of file qiodevice.h.

109 { return state() == 0; }
int state() const
Definition: qiodevice.h:96
bool QIODevice::isOpen ( ) const
inline

Returns TRUE if the I/O device state has been opened, otherwise FALSE.

See also
isInactive()

Definition at line 110 of file qiodevice.h.

110 { return state() == IO_Open; }
int state() const
Definition: qiodevice.h:96
#define IO_Open
Definition: qiodevice.h:71
bool QIODevice::isRaw ( ) const
inline

Returns TRUE if the I/O device is a raw (not buffered) device, otherwise FALSE.

See also
isBuffered()

Definition at line 102 of file qiodevice.h.

102 { return ((ioMode & IO_Raw) == IO_Raw); }
#define IO_Raw
Definition: qiodevice.h:56
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isReadable ( ) const
inline

Returns TRUE if the I/O device was opened using IO_ReadOnly or IO_ReadWrite mode.

See also
isWritable(), isReadWrite()

Definition at line 106 of file qiodevice.h.

106 { return ((ioMode & IO_ReadOnly) == IO_ReadOnly); }
#define IO_ReadOnly
Definition: qiodevice.h:61
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isReadWrite ( ) const
inline

Returns TRUE if the I/O device was opened using IO_ReadWrite mode.

See also
isReadable(), isWritable()

Definition at line 108 of file qiodevice.h.

108 { return ((ioMode & IO_ReadWrite) == IO_ReadWrite); }
#define IO_ReadWrite
Definition: qiodevice.h:63
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isSequentialAccess ( ) const
inline

Returns TRUE if the I/O device is a sequential access (not direct) device, otherwise FALSE. Operations involving size() and at(int) are not valid on sequential devices.

See also
isDirectAccess()

Definition at line 99 of file qiodevice.h.

99 { return ((ioMode & IO_Sequential) == IO_Sequential); }
#define IO_Sequential
Definition: qiodevice.h:50
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isSynchronous ( ) const
inline

Returns TRUE if the I/O device is a synchronous device, otherwise FALSE.

See also
isAsynchronous()

Definition at line 103 of file qiodevice.h.

103 { return ((ioMode & IO_Async) != IO_Async); }
int ioMode
Definition: qiodevice.h:144
#define IO_Async
Definition: qiodevice.h:57
bool QIODevice::isTranslated ( ) const
inline

Returns TRUE if the I/O device translates carriage-return and linefeed characters.

A QFile is translated if it is opened with the IO_Translate mode flag.

Definition at line 105 of file qiodevice.h.

105 { return ((ioMode & IO_Translate) == IO_Translate); }
#define IO_Translate
Definition: qiodevice.h:66
int ioMode
Definition: qiodevice.h:144
bool QIODevice::isWritable ( ) const
inline

Returns TRUE if the I/O device was opened using IO_WriteOnly or IO_ReadWrite mode.

See also
isReadable(), isReadWrite()

Definition at line 107 of file qiodevice.h.

107 { return ((ioMode & IO_WriteOnly) == IO_WriteOnly); }
#define IO_WriteOnly
Definition: qiodevice.h:62
int ioMode
Definition: qiodevice.h:144
int QIODevice::mode ( ) const
inline

Returns bits OR'ed together that specify the current operation mode.

These are the flags that were given to the open() function.

The flags are: IO_ReadOnly, IO_WriteOnly, IO_ReadWrite, IO_Append, IO_Truncate and IO_Translate.

Definition at line 95 of file qiodevice.h.

95 { return ioMode & IO_ModeMask; }
#define IO_ModeMask
Definition: qiodevice.h:67
int ioMode
Definition: qiodevice.h:144
bool QIODevice::open ( int  mode)
pure virtual

Opens the I/O device using the specified mode. Returns TRUE if successful, or FALSE if the device could not be opened.

The mode parameter m must be a combination of the following flags.

  • IO_Raw specified raw (unbuffered) file access.
  • IO_ReadOnly opens a file in read-only mode.
  • IO_WriteOnly opens a file in write-only mode.
  • IO_ReadWrite opens a file in read/write mode.
  • IO_Append sets the file index to the end of the file.
  • IO_Truncate truncates the file.
  • IO_Translate enables carriage returns and linefeed translation for text files under MS-DOS, Window, OS/2 and Macintosh. On Unix systems this flag has no effect. Use with caution as it will also transform every linefeed written to the file into a CRLF pair. This is likely to corrupt your file when writing binary data to it. Cannot be combined with IO_Raw.

This virtual function must be reimplemented by all subclasses.

See also
close()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

QIODevice& QIODevice::operator= ( const QIODevice )
private
int QIODevice::putch ( int  ch)
pure virtual

Writes the character ch to the I/O device.

Returns ch, or -1 if some error occurred.

This virtual function must be reimplemented by all subclasses.

See also
getch(), ungetch()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

QByteArray QIODevice::readAll ( )

This convenience function returns all of the remaining data in the device. Note that this only works for direct access devices, such as QFile.

See also
isDirectAccess()

Definition at line 535 of file qiodevice.cpp.

536 {
537  int n = size()-at();
538  QByteArray ba(size()-at());
539  char* c = ba.data();
540  while ( n ) {
541  int r = readBlock( c, n );
542  if ( r < 0 )
543  return QByteArray();
544  n -= r;
545  c += r;
546  }
547  return ba;
548 }
virtual int readBlock(char *data, uint maxlen)=0
std::void_t< T > n
QArray< char > QByteArray
Definition: qcstring.h:116
virtual uint size() const =0
virtual int at() const
Definition: qiodevice.cpp:471
int QIODevice::readBlock ( char *  data,
uint  maxlen 
)
pure virtual

Reads at most maxlen bytes from the I/O device into data and returns the number of bytes actually read.

This virtual function must be reimplemented by all subclasses.

See also
writeBlock()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::readLine ( char *  data,
uint  maxlen 
)
virtual

Reads a line of text, up to maxlen bytes including a terminating \0. If there is a newline at the end if the line, it is not stripped.

Returns the number of bytes read, or -1 in case of error.

This virtual function can be reimplemented much more efficiently by the most subclasses.

See also
readBlock(), QTextStream::readLine()

Reimplemented in QFile, and QBuffer.

Definition at line 581 of file qiodevice.cpp.

582 {
583  if ( maxlen == 0 ) // application bug?
584  return 0;
585  int pos = at(); // get current position
586  int s = (int)size(); // size of I/O device
587  char *p = data;
588  if ( pos >= s )
589  return 0;
590  while ( pos++ < s && --maxlen ) { // read one byte at a time
591  readBlock( p, 1 );
592  if ( *p++ == '\n' ) // end of line
593  break;
594  }
595  *p++ = '\0';
596  return (int)((intptr_t)p - (intptr_t)data);
597 }
static const int maxlen
Definition: qregexp.cpp:904
virtual int readBlock(char *data, uint maxlen)=0
p
Definition: test.py:223
static QCString * s
Definition: config.cpp:1042
virtual uint size() const =0
virtual int at() const
Definition: qiodevice.cpp:471
bool QIODevice::reset ( )
inline

Sets the device index to 0.

See also
at()

Definition at line 123 of file qiodevice.h.

123 { return at(0); }
virtual int at() const
Definition: qiodevice.cpp:471
void QIODevice::resetStatus ( )
inline

Sets the I/O device status to IO_Ok.

See also
status()

Definition at line 113 of file qiodevice.h.

113 { ioSt = IO_Ok; }
int ioSt
Definition: qiodevice.h:145
#define IO_Ok
Definition: qiodevice.h:77
void QIODevice::setFlags ( int  f)
inlineprotected

Definition at line 136 of file qiodevice.h.

void QIODevice::setMode ( int  m)
protected

Definition at line 378 of file qiodevice.cpp.

379 {
380 #if defined(CHECK_RANGE)
381  if ( (m & IO_ModeMask) != m )
382  qWarning( "QIODevice::setMode: Specified mode out of range" );
383 #endif
384  ioMode &= ~IO_ModeMask; // reset mode bits
385  ioMode |= m;
386 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
#define IO_ModeMask
Definition: qiodevice.h:67
int ioMode
Definition: qiodevice.h:144
void QIODevice::setState ( int  s)
protected

Definition at line 393 of file qiodevice.cpp.

394 {
395 #if defined(CHECK_RANGE)
396  if ( ((uint)s & IO_StateMask) != (uint)s )
397  qWarning( "QIODevice::setState: Specified state out of range" );
398 #endif
399  ioMode &= ~IO_StateMask; // reset state bits
400  ioMode |= (uint)s;
401 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
#define IO_StateMask
Definition: qiodevice.h:72
int ioMode
Definition: qiodevice.h:144
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
void QIODevice::setStatus ( int  s)
protected

Definition at line 408 of file qiodevice.cpp.

409 {
410  ioSt = s;
411 }
int ioSt
Definition: qiodevice.h:145
static QCString * s
Definition: config.cpp:1042
void QIODevice::setType ( int  t)
protected

Definition at line 363 of file qiodevice.cpp.

364 {
365 #if defined(CHECK_RANGE)
366  if ( (t & IO_TypeMask) != t )
367  qWarning( "QIODevice::setType: Specified type out of range" );
368 #endif
369  ioMode &= ~IO_TypeMask; // reset type bits
370  ioMode |= t;
371 }
#define IO_TypeMask
Definition: qiodevice.h:52
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int ioMode
Definition: qiodevice.h:144
uint QIODevice::size ( ) const
pure virtual

Virtual function that returns the size of the I/O device.

See also
at()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::state ( ) const
inline

Returns bits OR'ed together that specify the current state.

The flags are: IO_Open.

Subclasses may define more flags.

Definition at line 96 of file qiodevice.h.

96 { return ioMode & IO_StateMask; }
#define IO_StateMask
Definition: qiodevice.h:72
int ioMode
Definition: qiodevice.h:144
int QIODevice::status ( ) const
inline

Returns the I/O device status.

The I/O device status returns an error code. If open() returns FALSE or readBlock() or writeBlock() return -1, this function can be called to get the reason why the operation did not succeed.

The status codes are:

  • IO_Ok The operation was successful.
  • IO_ReadError Could not read from the device.
  • IO_WriteError Could not write to the device.
  • IO_FatalError A fatal unrecoverable error occurred.
  • IO_OpenError Could not open the device.
  • IO_ConnectError Could not connect to the device.
  • IO_AbortError The operation was unexpectedly aborted.
  • IO_TimeOutError The operation timed out.
  • IO_OnCloseError An unspecified error happened on close.
See also
resetStatus()

Definition at line 112 of file qiodevice.h.

112 { return ioSt; }
int ioSt
Definition: qiodevice.h:145
int QIODevice::ungetch ( int  ch)
pure virtual

Puts the character ch back into the I/O device and decrements the index if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns ch, or -1 if some error occurred.

This virtual function must be reimplemented by all subclasses.

See also
getch(), putch()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::writeBlock ( const char *  data,
uint  len 
)
pure virtual

Writes len bytes from p to the I/O device and returns the number of bytes actually written.

This virtual function must be reimplemented by all subclasses.

See also
readBlock()

Implemented in QStringBuffer, QFile, QBuffer, and QGStringBuffer.

int QIODevice::writeBlock ( const QByteArray data)

This convenience function is the same as calling writeBlock( data.data(), data.size() ).

Definition at line 564 of file qiodevice.cpp.

565 {
566  return writeBlock( data.data(), data.size() );
567 }
virtual int writeBlock(const char *data, uint len)=0
type * data() const
Definition: qarray.h:63
uint size() const
Definition: qarray.h:65

Member Data Documentation

int QIODevice::ioIndex
protected

Definition at line 141 of file qiodevice.h.

int QIODevice::ioMode
private

Definition at line 144 of file qiodevice.h.

int QIODevice::ioSt
private

Definition at line 145 of file qiodevice.h.


The documentation for this class was generated from the following files: