1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QIODEVICE_H
41 #define QIODEVICE_H
42 
43 #include <QtCore/qglobal.h>
44 #ifndef QT_NO_QOBJECT
45 #include <QtCore/qobject.h>
46 #else
47 #include <QtCore/qobjectdefs.h>
48 #include <QtCore/qscopedpointer.h>
49 #endif
50 #include <QtCore/qstring.h>
51 
52 #ifdef open
53 #error qiodevice.h must be included before any header file that defines open
54 #endif
55 
56 QT_BEGIN_NAMESPACE
57 
58 
59 class QByteArray;
60 class QIODevicePrivate;
61 
62 class Q_CORE_EXPORT QIODevice
63 #ifndef QT_NO_QOBJECT
64     : public QObject
65 #endif
66 {
67 #ifndef QT_NO_QOBJECT
68     Q_OBJECT
69 #endif
70 public:
71     enum OpenModeFlag {
72         NotOpen = 0x0000,
73         ReadOnly = 0x0001,
74         WriteOnly = 0x0002,
75         ReadWrite = ReadOnly | WriteOnly,
76         Append = 0x0004,
77         Truncate = 0x0008,
78         Text = 0x0010,
79         Unbuffered = 0x0020,
80         NewOnly = 0x0040,
81         ExistingOnly = 0x0080
82     };
83     Q_DECLARE_FLAGS(OpenMode, OpenModeFlag)
84 
85     QIODevice();
86 #ifndef QT_NO_QOBJECT
87     explicit QIODevice(QObject *parent);
88 #endif
89     virtual ~QIODevice();
90 
91     OpenMode openMode() const;
92 
93     void setTextModeEnabled(bool enabled);
94     bool isTextModeEnabled() const;
95 
96     bool isOpen() const;
97     bool isReadable() const;
98     bool isWritable() const;
99     virtual bool isSequential() const;
100 
101     int readChannelCount() const;
102     int writeChannelCount() const;
103     int currentReadChannel() const;
104     void setCurrentReadChannel(int channel);
105     int currentWriteChannel() const;
106     void setCurrentWriteChannel(int channel);
107 
108     virtual bool open(OpenMode mode);
109     virtual void close();
110 
111     // ### Qt 6: pos() and seek() should not be virtual, and
112     // ### seek() should call a virtual seekData() function.
113     virtual qint64 pos() const;
114     virtual qint64 size() const;
115     virtual bool seek(qint64 pos);
116     virtual bool atEnd() const;
117     virtual bool reset();
118 
119     virtual qint64 bytesAvailable() const;
120     virtual qint64 bytesToWrite() const;
121 
122     qint64 read(char *data, qint64 maxlen);
123     QByteArray read(qint64 maxlen);
124     QByteArray readAll();
125     qint64 readLine(char *data, qint64 maxlen);
126     QByteArray readLine(qint64 maxlen = 0);
127     virtual bool canReadLine() const;
128 
129     void startTransaction();
130     void commitTransaction();
131     void rollbackTransaction();
132     bool isTransactionStarted() const;
133 
134     qint64 write(const char *data, qint64 len);
135     qint64 write(const char *data);
write(const QByteArray & data)136     inline qint64 write(const QByteArray &data)
137     { return write(data.constData(), data.size()); }
138 
139     qint64 peek(char *data, qint64 maxlen);
140     QByteArray peek(qint64 maxlen);
141     qint64 skip(qint64 maxSize);
142 
143     virtual bool waitForReadyRead(int msecs);
144     virtual bool waitForBytesWritten(int msecs);
145 
146     void ungetChar(char c);
147     bool putChar(char c);
148     bool getChar(char *c);
149 
150     QString errorString() const;
151 
152 #ifndef QT_NO_QOBJECT
153 Q_SIGNALS:
154     void readyRead();
155     void channelReadyRead(int channel);
156     void bytesWritten(qint64 bytes);
157     void channelBytesWritten(int channel, qint64 bytes);
158     void aboutToClose();
159     void readChannelFinished();
160 #endif
161 
162 protected:
163 #ifdef QT_NO_QOBJECT
164     QIODevice(QIODevicePrivate &dd);
165 #else
166     QIODevice(QIODevicePrivate &dd, QObject *parent = nullptr);
167 #endif
168     virtual qint64 readData(char *data, qint64 maxlen) = 0;
169     virtual qint64 readLineData(char *data, qint64 maxlen);
170     virtual qint64 writeData(const char *data, qint64 len) = 0;
171 
172     void setOpenMode(OpenMode openMode);
173 
174     void setErrorString(const QString &errorString);
175 
176 #ifdef QT_NO_QOBJECT
177     QScopedPointer<QIODevicePrivate> d_ptr;
178 #endif
179 
180 private:
181     Q_DECLARE_PRIVATE(QIODevice)
182     Q_DISABLE_COPY(QIODevice)
183 };
184 
185 Q_DECLARE_OPERATORS_FOR_FLAGS(QIODevice::OpenMode)
186 
187 #if !defined(QT_NO_DEBUG_STREAM)
188 class QDebug;
189 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QIODevice::OpenMode modes);
190 #endif
191 
192 QT_END_NAMESPACE
193 
194 #endif // QIODEVICE_H
195