?? qiodevice.cpp
字號(hào):
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************///#define QIODEVICE_DEBUG#include "qbytearray.h"#include "qdebug.h"#include "qiodevice_p.h"#include "qfile.h"#include "qstringlist.h"#include <limits.h>#ifdef QIODEVICE_DEBUGvoid debugBinaryString(const QByteArray &input){ QByteArray tmp; int startOffset = 0; for (int i = 0; i < input.size(); ++i) { tmp += input[i]; if ((i % 16) == 15 || i == (input.size() - 1)) { printf("\n%15d:", startOffset); startOffset += tmp.size(); for (int j = 0; j < tmp.size(); ++j) printf(" %02x", int(uchar(tmp[j]))); for (int j = tmp.size(); j < 16 + 1; ++j) printf(" "); for (int j = 0; j < tmp.size(); ++j) printf("%c", isprint(int(uchar(tmp[j]))) ? tmp[j] : '.'); tmp.clear(); } } printf("\n\n");}void debugBinaryString(const char *data, qint64 maxlen){ debugBinaryString(QByteArray(data, maxlen));}#endifstatic const qint64 QIODEVICE_BUFFERSIZE = 16384;#define Q_VOID#define CHECK_MAXLEN(function, returnType) \ do { \ if (maxSize < 0) { \ qWarning("QIODevice::"#function": Called with maxSize < 0"); \ return returnType; \ } \ } while (0)#define CHECK_WRITABLE(function, returnType) \ do { \ if ((d->openMode & WriteOnly) == 0) { \ qWarning("QIODevice::"#function": ReadOnly device"); \ return returnType; \ } \ } while (0)#define CHECK_READABLE(function, returnType) \ do { \ if ((d->openMode & ReadOnly) == 0) { \ qWarning("QIODevice::"#function": WriteOnly device"); \ return returnType; \ } \ } while (0)#define CHECK_OPEN(function, returnType) \ do { \ if (d->openMode == NotOpen) { \ return returnType; \ } \ } while (0)/*! \internal */QIODevicePrivate::QIODevicePrivate() : openMode(QIODevice::NotOpen), pos(0), devicePos(0), accessMode(Unset){}/*! \internal */QIODevicePrivate::~QIODevicePrivate(){}/*! \class QIODevice \reentrant \brief The QIODevice class is the base interface class of all I/O devices in Qt. \ingroup io QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as QFile, QBuffer and QTcpSocket. QIODevice is abstract and can not be instantiated, but it is common to use the interface it defines to provide device-independent I/O features. For example, Qt's XML classes operate on a QIODevice pointer, allowing them to be used with various devices (such as files and buffers). Before accessing the device, open() must be called to set the correct OpenMode (such as ReadOnly or ReadWrite). You can then write to the device with write() or putChar(), and read by calling either read(), readLine(), or readAll(). Call close() when you are done with the device. QIODevice distinguishes between two types of devices: random-access devices and sequential devices. \list \o Random-access devices support seeking to arbitrary positions using seek(). The current position in the file is available by calling pos(). QFile and QBuffer are examples of random-access devices. \o Sequential devices don't support seeking to arbitrary positions. The data must be read in one pass. The functions pos() and size() don't work for sequential devices. QTcpSocket and QProcess are examples of sequential devices. \endlist You can use isSequential() to determine the type of device. QIODevice emits readyRead() when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can call bytesAvailable() to determine the number of bytes that currently available for reading. It's common to use bytesAvailable() together with the readyRead() signal when programming with asynchronous devices such as QTcpSocket, where fragments of data can arrive at arbitrary points in time. QIODevice emits the bytesWritten() signal every time a payload of data has been written to the device. Use bytesToWrite() to determine the current amount of data waiting to be written. Certain subclasses of QIODevice, such as QTcpSocket and QProcess, are asynchronous. This means that I/O functions such as write() or read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows QIODevice subclasses to be used without an event loop, or in a separate thread: \list \o waitForReadyRead() - This function suspends operation in the calling thread until new data is available for reading. \o waitForBytesWritten() - This function suspends operation in the calling thread until one payload of data has been written to the device. \o waitFor....() - Subclasses of QIODevice implement blocking functions for device-specific operations. For example, QProcess has a function called waitForStarted() which suspends operation in the calling thread until the process has started. \endlist Calling these functions from the main, GUI thread, may cause your user interface to freeze. Example: \code QProcess gzip; gzip.start("gzip", QStringList() << "-c"); if (!gzip.waitForStarted()) return false; gzip.write("uncompressed data"); QByteArray compressed; while (gzip.waitForReadyRead()) compressed += gzip.readAll(); \endcode By subclassing QIODevice, you can provide the same interface to your own I/O devices. Subclasses of QIODevice are only required to implement the protected readData() and writeData() functions. QIODevice uses these functions to implement all its convenience functions, such as getChar(), readLine() and write(). QIODevice also handles access control for you, so you can safely assume that the device is opened in write mode if writeData() is called. Some subclasses, such as QFile and QTcpSocket, are implemented using a memory buffer for intermediate storing of data. This reduces the number of required device accessing calls, which are often very slow. Buffering makes functions like getChar() and putChar() fast, as they can operate on the memory buffer instead of directly on the device itself. Certain I/O operations, however, don't work well with a buffer. For example, if several users open the same device and read it character by character, they may end up reading the same data when they meant to read a separate chunk each. For this reason, QIODevice allows you to bypass any buffering by passing the Unbuffered flag to open(). When subclassing QIODevice, remember to bypass any buffer you may use when the device is open in Unbuffered mode. \sa QBuffer QFile QTcpSocket*//*! \typedef QIODevice::Offset \compat Use \c qint64 instead.*//*! \typedef QIODevice::Status \compat Use QIODevice::OpenMode instead, or see the documentation for specific devices.*//*! \enum QIODevice::OpenModeFlag This enum is used with open() to describe the mode in which a device is opened. It is also returned by openMode(). \value NotOpen The device is not open. \value ReadOnly The device is open for reading. \value WriteOnly The device is open for writing. \value ReadWrite The device is open for reading and writing. \value Append The device is opened in append mode, so that all data is written to the end of the file. \value Truncate If possible, the device is truncated before it is opened. All earlier contents of the device are lost. \value Text When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32. \value Unbuffered Any buffer in the device is bypassed. Certain flags, such as QIODevice::Unbuffered and QIODevice::Truncate, might be meaningless for some subclasses. (For example, access to a QBuffer is always unbuffered.)*//*! \fn QIODevice::bytesWritten(qint64 bytes) This signal is emitted every time a payload of data has been written to the device. The \a bytes argument is set to the number of bytes that were written in this payload. bytesWritten() is not emitted recursively; if you reenter the event loop or call waitForBytesWritten() inside a slot connected to the bytesWritten() signal, the signal will not be reemitted (although waitForBytesWritten() may still return true). \sa readyRead()*//*! \fn QIODevice::readyRead() This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device. readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true). \sa bytesWritten()*//*! \fn QIODevice::aboutToClose() This signal is emitted when the device is about to close. Connect this signal if you have operations that need to be performed before the device closes (e.g., if you have data in a separate buffer that needs to be written to the device).*/#ifdef QT_NO_QOBJECTQIODevice::QIODevice() : d_ptr(new QIODevicePrivate){ d_ptr->q_ptr = this;}/*! \internal*/QIODevice::QIODevice(QIODevicePrivate &dd) : d_ptr(&dd){ d_ptr->q_ptr = this;}#else/*! Constructs a QIODevice object.*/QIODevice::QIODevice() : QObject(*new QIODevicePrivate, 0){#if defined QIODEVICE_DEBUG QFile *file = qobject_cast<QFile *>(this); printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, className(), qPrintable(file ? file->fileName() : QString()));#endif}/*! Constructs a QIODevice object with the given \a parent.*/QIODevice::QIODevice(QObject *parent) : QObject(*new QIODevicePrivate, parent){#if defined QIODEVICE_DEBUG printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, className());#endif}/*! \internal*/QIODevice::QIODevice(QIODevicePrivate &dd, QObject *parent) : QObject(dd, parent){}#endif/*! Destructs the QIODevice object.*/QIODevice::~QIODevice(){#if defined QIODEVICE_DEBUG printf("%p QIODevice::~QIODevice()\n", this);#endif}/*! Returns true if this device is sequential; otherwise returns false. Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential. Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential. \sa bytesAvailable()*/bool QIODevice::isSequential() const{ return false;}/*! Returns the mode in which the device has been opened; i.e. ReadOnly or WriteOnly. \sa OpenMode*/QIODevice::OpenMode QIODevice::openMode() const{ return d_func()->openMode;}/*! Sets the OpenMode of the device to \a openMode. Call this function to set the open mode when reimplementing open(). \sa openMode() OpenMode*/void QIODevice::setOpenMode(OpenMode openMode){#if defined QIODEVICE_DEBUG
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -