|
Packit |
1c1d7e |
/****************************************************************************
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Implementation of QIODevice class
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Created : 940913
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file is part of the tools module of the Qt GUI Toolkit.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file may be distributed under the terms of the Q Public License
|
|
Packit |
1c1d7e |
** as defined by Trolltech AS of Norway and appearing in the file
|
|
Packit |
1c1d7e |
** LICENSE.QPL included in the packaging of this file.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file may be distributed and/or modified under the terms of the
|
|
Packit |
1c1d7e |
** GNU General Public License version 2 as published by the Free Software
|
|
Packit |
1c1d7e |
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
Packit |
1c1d7e |
** packaging of this file.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|
Packit |
1c1d7e |
** licenses may use this file in accordance with the Qt Commercial License
|
|
Packit |
1c1d7e |
** Agreement provided with the Software.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
Packit |
1c1d7e |
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|
Packit |
1c1d7e |
** information about Qt Commercial License Agreements.
|
|
Packit |
1c1d7e |
** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|
Packit |
1c1d7e |
** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Contact info@trolltech.com if any conditions of this licensing are
|
|
Packit |
1c1d7e |
** not clear to you.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
**********************************************************************/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
#include "qiodevice.h"
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// NOT REVISED
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\class QIODevice qiodevice.h
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\brief The QIODevice class is the base class of I/O devices.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\ingroup io
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
An I/O device represents a medium that one can read bytes from
|
|
Packit |
1c1d7e |
and/or write bytes to. The QIODevice class is the abstract
|
|
Packit |
1c1d7e |
superclass of all such devices; classes like QFile, QBuffer and
|
|
Packit |
1c1d7e |
QSocket inherit QIODevice and implement virtual functions like
|
|
Packit |
1c1d7e |
write() appropriately.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
While applications sometimes use QIODevice directly, mostly it is
|
|
Packit |
1c1d7e |
better to go through QTextStream and QDataStream, which provide
|
|
Packit |
1c1d7e |
stream operations on any QIODevice subclass. QTextStream provides
|
|
Packit |
1c1d7e |
text-oriented stream functionality (for human-readable ASCII files,
|
|
Packit |
1c1d7e |
for example), while QDataStream deals with binary data in a totally
|
|
Packit |
1c1d7e |
platform-independent manner.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The public member functions in QIODevice roughly fall into two
|
|
Packit |
1c1d7e |
groups: The action functions and the state access functions. The
|
|
Packit |
1c1d7e |
most important action functions are:
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
open() opens a device for reading and/or writing, depending on
|
|
Packit |
1c1d7e |
the argument to open().
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
close() closes the device and tidies up.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
readBlock() reads a block of data from the device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
writeBlock() writes a block of data to the device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
readLine() reads a line (of text, usually) from the device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
flush() ensures that all buffered data are written to the real device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
There are also some other, less used, action functions:
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
getch() reads a single character.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
ungetch() forgets the last call to getch(), if possible.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
putch() writes a single character.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
size() returns the size of the device, if there is one.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
at() returns the current read/write pointer, if there is one
|
|
Packit |
1c1d7e |
for this device, or it moves the pointer.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
atEnd() says whether there is more to read, if that is a
|
|
Packit |
1c1d7e |
meaningful question for this device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
reset() moves the read/write pointer to the start of the
|
|
Packit |
1c1d7e |
device, if that is possible for this device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The state access are all "get" functions. The QIODevice subclass
|
|
Packit |
1c1d7e |
calls setState() to update the state, and simple access functions
|
|
Packit |
1c1d7e |
tell the user of the device what the device's state is. Here are
|
|
Packit |
1c1d7e |
the settings, and their associated access functions:
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Access type. Some devices are direct access (it is possible to
|
|
Packit |
1c1d7e |
read/write anywhere) while others are sequential. QIODevice
|
|
Packit |
1c1d7e |
provides the access functions isDirectAccess(), isSequentialAccess()
|
|
Packit |
1c1d7e |
and isCombinedAccess() to tell users what a given I/O device
|
|
Packit |
1c1d7e |
supports.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Buffering. Some devices are accessed in raw mode while others
|
|
Packit |
1c1d7e |
are buffered. Buffering usually provides greater efficiency,
|
|
Packit |
1c1d7e |
particularly for small read/write operations. isBuffered() tells
|
|
Packit |
1c1d7e |
the user whether a given device is buffered. (This can often be set
|
|
Packit |
1c1d7e |
by the application in the call to open().)
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Synchronicity. Synchronous devices work there and then, for
|
|
Packit |
1c1d7e |
example files. When you read from a file, the file delivers its
|
|
Packit |
1c1d7e |
data right away. Others, such as a socket connected to a HTTP
|
|
Packit |
1c1d7e |
server, may not deliver the data until seconds after you ask to read
|
|
Packit |
1c1d7e |
it. isSynchronous() and isAsynchronous() tells the user how this
|
|
Packit |
1c1d7e |
device operates.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
CR/LF translation. For simplicity, applications often like to
|
|
Packit |
1c1d7e |
see just a single CR/LF style, and QIODevice subclasses can provide
|
|
Packit |
1c1d7e |
that. isTranslated() returns TRUE if this object translates CR/LF
|
|
Packit |
1c1d7e |
to just LF. (This can often be set by the application in the call
|
|
Packit |
1c1d7e |
to open().)
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Accessibility. Some files cannot be written, for example.
|
|
Packit |
1c1d7e |
isReadable(), isWritable and isReadWrite() tells the application
|
|
Packit |
1c1d7e |
whether it can read from and write to a given device. (This can
|
|
Packit |
1c1d7e |
often be set by the application in the call to open().)
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Finally, isOpen() returns TRUE if the device is open. This can
|
|
Packit |
1c1d7e |
quite obviously be set using open() :)
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QIODevice provides numerous pure virtual functions you need to
|
|
Packit |
1c1d7e |
implement when subclassing it. Here is a skeleton subclass with all
|
|
Packit |
1c1d7e |
the members you are certain to need, and some it's likely that you
|
|
Packit |
1c1d7e |
will need:
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
class YourDevice : public QIODevice
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
public:
|
|
Packit |
1c1d7e |
YourDevice();
|
|
Packit |
1c1d7e |
~YourDevice();
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
bool open( int mode );
|
|
Packit |
1c1d7e |
void close();
|
|
Packit |
1c1d7e |
void flush();
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
uint size() const;
|
|
Packit |
1c1d7e |
int at() const; // not a pure virtual function
|
|
Packit |
1c1d7e |
bool at( int ); // not a pure virtual function
|
|
Packit |
1c1d7e |
bool atEnd() const; // not a pure virtual function
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
int readBlock( char *data, uint maxlen );
|
|
Packit |
1c1d7e |
int writeBlock( const char *data, uint len );
|
|
Packit |
1c1d7e |
int readLine( char *data, uint maxlen );
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
int getch();
|
|
Packit |
1c1d7e |
int putch( int );
|
|
Packit |
1c1d7e |
int ungetch( int );
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The three non-pure virtual functions can be ignored if your device
|
|
Packit |
1c1d7e |
is sequential (e.g. an RS-232 port).
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa QDataStream, QTextStream
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Constructs an I/O device.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QIODevice::QIODevice()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
ioMode = 0; // initial mode
|
|
Packit |
1c1d7e |
ioSt = IO_Ok;
|
|
Packit |
1c1d7e |
ioIndex = 0;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Destructs the I/O device.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QIODevice::~QIODevice()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::flags() const
|
|
Packit |
1c1d7e |
Returns the current I/O device flags setting.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Flags consists of mode flags and state flags.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa mode(), state()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::mode() const
|
|
Packit |
1c1d7e |
Returns bits OR'ed together that specify the current operation mode.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
These are the flags that were given to the open() function.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The flags are: \c IO_ReadOnly, \c IO_WriteOnly, \c IO_ReadWrite,
|
|
Packit |
1c1d7e |
\c IO_Append, \c IO_Truncate and \c IO_Translate.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::state() const
|
|
Packit |
1c1d7e |
Returns bits OR'ed together that specify the current state.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The flags are: \c IO_Open.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Subclasses may define more flags.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isDirectAccess() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a direct access (not sequential) device,
|
|
Packit |
1c1d7e |
otherwise FALSE.
|
|
Packit |
1c1d7e |
\sa isSequentialAccess()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isSequentialAccess() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a sequential access (not direct) device,
|
|
Packit |
1c1d7e |
otherwise FALSE. Operations involving size() and at(int) are not valid
|
|
Packit |
1c1d7e |
on sequential devices.
|
|
Packit |
1c1d7e |
\sa isDirectAccess()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isCombinedAccess() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a combined access (both direct and
|
|
Packit |
1c1d7e |
sequential) device, otherwise FALSE.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This access method is currently not in use.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isBuffered() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a buffered (not raw) device, otherwise
|
|
Packit |
1c1d7e |
FALSE.
|
|
Packit |
1c1d7e |
\sa isRaw()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isRaw() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a raw (not buffered) device, otherwise
|
|
Packit |
1c1d7e |
FALSE.
|
|
Packit |
1c1d7e |
\sa isBuffered()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isSynchronous() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a synchronous device, otherwise
|
|
Packit |
1c1d7e |
FALSE.
|
|
Packit |
1c1d7e |
\sa isAsynchronous()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isAsynchronous() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device is a asynchronous device, otherwise
|
|
Packit |
1c1d7e |
FALSE.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This mode is currently not in use.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa isSynchronous()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isTranslated() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device translates carriage-return and linefeed
|
|
Packit |
1c1d7e |
characters.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
A QFile is translated if it is opened with the \c IO_Translate mode
|
|
Packit |
1c1d7e |
flag.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isReadable() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device was opened using \c IO_ReadOnly or
|
|
Packit |
1c1d7e |
\c IO_ReadWrite mode.
|
|
Packit |
1c1d7e |
\sa isWritable(), isReadWrite()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isWritable() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device was opened using \c IO_WriteOnly or
|
|
Packit |
1c1d7e |
\c IO_ReadWrite mode.
|
|
Packit |
1c1d7e |
\sa isReadable(), isReadWrite()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isReadWrite() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device was opened using \c IO_ReadWrite mode.
|
|
Packit |
1c1d7e |
\sa isReadable(), isWritable()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isInactive() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device state is 0, i.e. the device is not open.
|
|
Packit |
1c1d7e |
\sa isOpen()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::isOpen() const
|
|
Packit |
1c1d7e |
Returns TRUE if the I/O device state has been opened, otherwise FALSE.
|
|
Packit |
1c1d7e |
\sa isInactive()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::status() const
|
|
Packit |
1c1d7e |
Returns the I/O device status.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The I/O device status returns an error code. If open() returns FALSE
|
|
Packit |
1c1d7e |
or readBlock() or writeBlock() return -1, this function can be called to
|
|
Packit |
1c1d7e |
get the reason why the operation did not succeed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The status codes are:
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\c IO_Ok The operation was successful.
|
|
Packit |
1c1d7e |
\c IO_ReadError Could not read from the device.
|
|
Packit |
1c1d7e |
\c IO_WriteError Could not write to the device.
|
|
Packit |
1c1d7e |
\c IO_FatalError A fatal unrecoverable error occurred.
|
|
Packit |
1c1d7e |
\c IO_OpenError Could not open the device.
|
|
Packit |
1c1d7e |
\c IO_ConnectError Could not connect to the device.
|
|
Packit |
1c1d7e |
\c IO_AbortError The operation was unexpectedly aborted.
|
|
Packit |
1c1d7e |
\c IO_TimeOutError The operation timed out.
|
|
Packit |
1c1d7e |
\c IO_OnCloseError An unspecified error happened on close.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa resetStatus()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QIODevice::resetStatus()
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Sets the I/O device status to \c IO_Ok.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa status()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QIODevice::setFlags( int f )
|
|
Packit |
1c1d7e |
\internal
|
|
Packit |
1c1d7e |
Used by subclasses to set the device flags.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\internal
|
|
Packit |
1c1d7e |
Used by subclasses to set the device type.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QIODevice::setType( int t )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
#if defined(CHECK_RANGE)
|
|
Packit |
1c1d7e |
if ( (t & IO_TypeMask) != t )
|
|
Packit |
1c1d7e |
qWarning( "QIODevice::setType: Specified type out of range" );
|
|
Packit |
1c1d7e |
#endif
|
|
Packit |
1c1d7e |
ioMode &= ~IO_TypeMask; // reset type bits
|
|
Packit |
1c1d7e |
ioMode |= t;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\internal
|
|
Packit |
1c1d7e |
Used by subclasses to set the device mode.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QIODevice::setMode( int m )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
#if defined(CHECK_RANGE)
|
|
Packit |
1c1d7e |
if ( (m & IO_ModeMask) != m )
|
|
Packit |
1c1d7e |
qWarning( "QIODevice::setMode: Specified mode out of range" );
|
|
Packit |
1c1d7e |
#endif
|
|
Packit |
1c1d7e |
ioMode &= ~IO_ModeMask; // reset mode bits
|
|
Packit |
1c1d7e |
ioMode |= m;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\internal
|
|
Packit |
1c1d7e |
Used by subclasses to set the device state.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QIODevice::setState( int s )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
#if defined(CHECK_RANGE)
|
|
Packit |
1c1d7e |
if ( ((uint)s & IO_StateMask) != (uint)s )
|
|
Packit |
1c1d7e |
qWarning( "QIODevice::setState: Specified state out of range" );
|
|
Packit |
1c1d7e |
#endif
|
|
Packit |
1c1d7e |
ioMode &= ~IO_StateMask; // reset state bits
|
|
Packit |
1c1d7e |
ioMode |= (uint)s;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\internal
|
|
Packit |
1c1d7e |
Used by subclasses to set the device status (not state).
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QIODevice::setStatus( int s )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
ioSt = s;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::open( int mode )
|
|
Packit |
1c1d7e |
Opens the I/O device using the specified \e mode.
|
|
Packit |
1c1d7e |
Returns TRUE if successful, or FALSE if the device could not be opened.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The mode parameter \e m must be a combination of the following flags.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\c IO_Raw specified raw (unbuffered) file access.
|
|
Packit |
1c1d7e |
\c IO_ReadOnly opens a file in read-only mode.
|
|
Packit |
1c1d7e |
\c IO_WriteOnly opens a file in write-only mode.
|
|
Packit |
1c1d7e |
\c IO_ReadWrite opens a file in read/write mode.
|
|
Packit |
1c1d7e |
\c IO_Append sets the file index to the end of the file.
|
|
Packit |
1c1d7e |
\c IO_Truncate truncates the file.
|
|
Packit |
1c1d7e |
\c IO_Translate enables carriage returns and linefeed translation
|
|
Packit |
1c1d7e |
for text files under MS-DOS, Window, OS/2 and Macintosh. On Unix systems
|
|
Packit |
1c1d7e |
this flag has no effect. Use with caution as it will also transform every linefeed
|
|
Packit |
1c1d7e |
written to the file into a CRLF pair. This is likely to corrupt your file when
|
|
Packit |
1c1d7e |
writing binary data to it. Cannot be combined with \c IO_Raw.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa close()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QIODevice::close()
|
|
Packit |
1c1d7e |
Closes the I/O device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa open()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QIODevice::flush()
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Flushes an open I/O device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn uint QIODevice::size() const
|
|
Packit |
1c1d7e |
Virtual function that returns the size of the I/O device.
|
|
Packit |
1c1d7e |
\sa at()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Virtual function that returns the current I/O device index.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This index is the data read/write head of the I/O device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa size()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
int QIODevice::at() const
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
return ioIndex;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Virtual function that sets the I/O device index to \e pos.
|
|
Packit |
1c1d7e |
\sa size()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
bool QIODevice::at( int pos )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
#if defined(CHECK_RANGE)
|
|
Packit |
1c1d7e |
if ( (uint)pos > size() ) {
|
|
Packit |
1c1d7e |
qWarning( "QIODevice::at: Index %d out of range", pos );
|
|
Packit |
1c1d7e |
return FALSE;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
#endif
|
|
Packit |
1c1d7e |
ioIndex = pos;
|
|
Packit |
1c1d7e |
return TRUE;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Virtual function that returns TRUE if the I/O device index is at the
|
|
Packit |
1c1d7e |
end of the input.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
bool QIODevice::atEnd() const
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
if ( isSequentialAccess() || isTranslated() ) {
|
|
Packit |
1c1d7e |
QIODevice* that = (QIODevice*)this;
|
|
Packit |
1c1d7e |
int c = that->getch();
|
|
Packit |
1c1d7e |
bool result = c < 0;
|
|
Packit |
1c1d7e |
that->ungetch(c);
|
|
Packit |
1c1d7e |
return result;
|
|
Packit |
1c1d7e |
} else {
|
|
Packit |
1c1d7e |
return at() == (int)size();
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QIODevice::reset()
|
|
Packit |
1c1d7e |
Sets the device index to 0.
|
|
Packit |
1c1d7e |
\sa at()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::readBlock( char *data, uint maxlen )
|
|
Packit |
1c1d7e |
Reads at most \e maxlen bytes from the I/O device into \e data and
|
|
Packit |
1c1d7e |
returns the number of bytes actually read.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa writeBlock()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
This convenience function returns all of the remaining data in the
|
|
Packit |
1c1d7e |
device. Note that this only works for direct access devices, such
|
|
Packit |
1c1d7e |
as QFile.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa isDirectAccess()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
QByteArray QIODevice::readAll()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
int n = size()-at();
|
|
Packit |
1c1d7e |
QByteArray ba(size()-at());
|
|
Packit |
1c1d7e |
char* c = ba.data();
|
|
Packit |
1c1d7e |
while ( n ) {
|
|
Packit |
1c1d7e |
int r = readBlock( c, n );
|
|
Packit |
1c1d7e |
if ( r < 0 )
|
|
Packit |
1c1d7e |
return QByteArray();
|
|
Packit |
1c1d7e |
n -= r;
|
|
Packit |
1c1d7e |
c += r;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
return ba;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::writeBlock( const char *data, uint len )
|
|
Packit |
1c1d7e |
Writes \e len bytes from \e p to the I/O device and returns the number of
|
|
Packit |
1c1d7e |
bytes actually written.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa readBlock()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
This convenience function is the same as calling
|
|
Packit |
1c1d7e |
writeBlock( data.data(), data.size() ).
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
int QIODevice::writeBlock( const QByteArray& data )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
return writeBlock( data.data(), data.size() );
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
Reads a line of text, up to \e maxlen bytes including a terminating
|
|
Packit |
1c1d7e |
\0. If there is a newline at the end if the line, it is not stripped.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Returns the number of bytes read, or -1 in case of error.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function can be reimplemented much more efficiently by
|
|
Packit |
1c1d7e |
the most subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa readBlock(), QTextStream::readLine()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
int QIODevice::readLine( char *data, uint maxlen )
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
if ( maxlen == 0 ) // application bug?
|
|
Packit |
1c1d7e |
return 0;
|
|
Packit |
1c1d7e |
int pos = at(); // get current position
|
|
Packit |
1c1d7e |
int s = (int)size(); // size of I/O device
|
|
Packit |
1c1d7e |
char *p = data;
|
|
Packit |
1c1d7e |
if ( pos >= s )
|
|
Packit |
1c1d7e |
return 0;
|
|
Packit |
1c1d7e |
while ( pos++ < s && --maxlen ) { // read one byte at a time
|
|
Packit |
1c1d7e |
readBlock( p, 1 );
|
|
Packit |
1c1d7e |
if ( *p++ == '\n' ) // end of line
|
|
Packit |
1c1d7e |
break;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
*p++ = '\0';
|
|
Packit |
1c1d7e |
return (int)((intptr_t)p - (intptr_t)data);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::getch()
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Reads a single byte/character from the I/O device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Returns the byte/character read, or -1 if the end of the I/O device has been
|
|
Packit |
1c1d7e |
reached.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa putch(), ungetch()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::putch( int ch )
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Writes the character \e ch to the I/O device.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Returns \e ch, or -1 if some error occurred.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa getch(), ungetch()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn int QIODevice::ungetch( int ch )
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Puts the character \e ch back into the I/O device and decrements the
|
|
Packit |
1c1d7e |
index if it is not zero.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This function is normally called to "undo" a getch() operation.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Returns \e ch, or -1 if some error occurred.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This virtual function must be reimplemented by all subclasses.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa getch(), putch()
|
|
Packit |
1c1d7e |
*/
|