1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
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 Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #ifndef QTEXTSTREAM_H |
43 | #define QTEXTSTREAM_H |
44 | |
45 | #include <QtCore/qiodevice.h> |
46 | #include <QtCore/qstring.h> |
47 | #include <QtCore/qchar.h> |
48 | #include <QtCore/qlocale.h> |
49 | #include <QtCore/qscopedpointer.h> |
50 | |
51 | #ifndef QT_NO_TEXTCODEC |
52 | # ifdef QT3_SUPPORT |
53 | # include <QtCore/qtextcodec.h> |
54 | # endif |
55 | #endif |
56 | |
57 | #include <stdio.h> |
58 | |
59 | #ifdef Status |
60 | #error qtextstream.h must be included before any header file that defines Status |
61 | #endif |
62 | |
63 | QT_BEGIN_HEADER |
64 | |
65 | QT_BEGIN_NAMESPACE |
66 | |
67 | QT_MODULE(Core) |
68 | |
69 | class QTextCodec; |
70 | class QTextDecoder; |
71 | |
72 | class QTextStreamPrivate; |
73 | class Q_CORE_EXPORT QTextStream // text stream class |
74 | { |
75 | Q_DECLARE_PRIVATE(QTextStream) |
76 | |
77 | public: |
78 | enum RealNumberNotation { |
79 | SmartNotation, |
80 | FixedNotation, |
81 | ScientificNotation |
82 | }; |
83 | enum FieldAlignment { |
84 | AlignLeft, |
85 | AlignRight, |
86 | AlignCenter, |
87 | AlignAccountingStyle |
88 | }; |
89 | enum Status { |
90 | Ok, |
91 | ReadPastEnd, |
92 | ReadCorruptData, |
93 | WriteFailed |
94 | }; |
95 | enum NumberFlag { |
96 | ShowBase = 0x1, |
97 | ForcePoint = 0x2, |
98 | ForceSign = 0x4, |
99 | UppercaseBase = 0x8, |
100 | UppercaseDigits = 0x10 |
101 | }; |
102 | Q_DECLARE_FLAGS(NumberFlags, NumberFlag) |
103 | |
104 | QTextStream(); |
105 | explicit QTextStream(QIODevice *device); |
106 | explicit QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite); |
107 | explicit QTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite); |
108 | explicit QTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite); |
109 | explicit QTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly); |
110 | virtual ~QTextStream(); |
111 | |
112 | #ifndef QT_NO_TEXTCODEC |
113 | void setCodec(QTextCodec *codec); |
114 | void setCodec(const char *codecName); |
115 | QTextCodec *codec() const; |
116 | void setAutoDetectUnicode(bool enabled); |
117 | bool autoDetectUnicode() const; |
118 | void setGenerateByteOrderMark(bool generate); |
119 | bool generateByteOrderMark() const; |
120 | #endif |
121 | |
122 | void setLocale(const QLocale &locale); |
123 | QLocale locale() const; |
124 | |
125 | void setDevice(QIODevice *device); |
126 | QIODevice *device() const; |
127 | |
128 | void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite); |
129 | QString *string() const; |
130 | |
131 | Status status() const; |
132 | void setStatus(Status status); |
133 | void resetStatus(); |
134 | |
135 | bool atEnd() const; |
136 | void reset(); |
137 | void flush(); |
138 | bool seek(qint64 pos); |
139 | qint64 pos() const; |
140 | |
141 | void skipWhiteSpace(); |
142 | |
143 | QString readLine(qint64 maxlen = 0); |
144 | QString readAll(); |
145 | QString read(qint64 maxlen); |
146 | |
147 | void setFieldAlignment(FieldAlignment alignment); |
148 | FieldAlignment fieldAlignment() const; |
149 | |
150 | void setPadChar(QChar ch); |
151 | QChar padChar() const; |
152 | |
153 | void setFieldWidth(int width); |
154 | int fieldWidth() const; |
155 | |
156 | void setNumberFlags(NumberFlags flags); |
157 | NumberFlags numberFlags() const; |
158 | |
159 | void setIntegerBase(int base); |
160 | int integerBase() const; |
161 | |
162 | void setRealNumberNotation(RealNumberNotation notation); |
163 | RealNumberNotation realNumberNotation() const; |
164 | |
165 | void setRealNumberPrecision(int precision); |
166 | int realNumberPrecision() const; |
167 | |
168 | QTextStream &operator>>(QChar &ch); |
169 | QTextStream &operator>>(char &ch); |
170 | QTextStream &operator>>(signed short &i); |
171 | QTextStream &operator>>(unsigned short &i); |
172 | QTextStream &operator>>(signed int &i); |
173 | QTextStream &operator>>(unsigned int &i); |
174 | QTextStream &operator>>(signed long &i); |
175 | QTextStream &operator>>(unsigned long &i); |
176 | QTextStream &operator>>(qlonglong &i); |
177 | QTextStream &operator>>(qulonglong &i); |
178 | QTextStream &operator>>(float &f); |
179 | QTextStream &operator>>(double &f); |
180 | QTextStream &operator>>(QString &s); |
181 | QTextStream &operator>>(QByteArray &array); |
182 | QTextStream &operator>>(char *c); |
183 | |
184 | QTextStream &operator<<(QBool b); |
185 | QTextStream &operator<<(QChar ch); |
186 | QTextStream &operator<<(char ch); |
187 | QTextStream &operator<<(signed short i); |
188 | QTextStream &operator<<(unsigned short i); |
189 | QTextStream &operator<<(signed int i); |
190 | QTextStream &operator<<(unsigned int i); |
191 | QTextStream &operator<<(signed long i); |
192 | QTextStream &operator<<(unsigned long i); |
193 | QTextStream &operator<<(qlonglong i); |
194 | QTextStream &operator<<(qulonglong i); |
195 | QTextStream &operator<<(float f); |
196 | QTextStream &operator<<(double f); |
197 | QTextStream &operator<<(const QString &s); |
198 | QTextStream &operator<<(const QByteArray &array); |
199 | QTextStream &operator<<(const char *c); |
200 | QTextStream &operator<<(const void *ptr); |
201 | |
202 | #ifdef QT3_SUPPORT |
203 | // not marked as QT3_SUPPORT to avoid double compiler warnings, as |
204 | // they are used in the QT3_SUPPORT functions below. |
205 | inline QT3_SUPPORT int flags() const { return flagsInternal(); } |
206 | inline QT3_SUPPORT int flags(int f) { return flagsInternal(f); } |
207 | |
208 | inline QT3_SUPPORT int setf(int bits) |
209 | { int old = flagsInternal(); flagsInternal(flagsInternal() | bits); return old; } |
210 | inline QT3_SUPPORT int setf(int bits, int mask) |
211 | { int old = flagsInternal(); flagsInternal(flagsInternal() | (bits & mask)); return old; } |
212 | inline QT3_SUPPORT int unsetf(int bits) |
213 | { int old = flagsInternal(); flagsInternal(flagsInternal() & ~bits); return old; } |
214 | |
215 | inline QT3_SUPPORT int width(int w) |
216 | { int old = fieldWidth(); setFieldWidth(w); return old; } |
217 | inline QT3_SUPPORT int fill(int f) |
218 | { QChar ch = padChar(); setPadChar(QChar(f)); return ch.unicode(); } |
219 | inline QT3_SUPPORT int precision(int p) |
220 | { int old = realNumberPrecision(); setRealNumberPrecision(p); return old; } |
221 | |
222 | enum { |
223 | skipws = 0x0001, // skip whitespace on input |
224 | left = 0x0002, // left-adjust output |
225 | right = 0x0004, // right-adjust output |
226 | internal = 0x0008, // pad after sign |
227 | bin = 0x0010, // binary format integer |
228 | oct = 0x0020, // octal format integer |
229 | dec = 0x0040, // decimal format integer |
230 | hex = 0x0080, // hex format integer |
231 | showbase = 0x0100, // show base indicator |
232 | showpoint = 0x0200, // force decimal point (float) |
233 | uppercase = 0x0400, // upper-case hex output |
234 | showpos = 0x0800, // add '+' to positive integers |
235 | scientific = 0x1000, // scientific float output |
236 | fixed = 0x2000 // fixed float output |
237 | }; |
238 | enum { |
239 | basefield = bin | oct | dec | hex, |
240 | adjustfield = left | right | internal, |
241 | floatfield = scientific | fixed |
242 | }; |
243 | |
244 | #ifndef QT_NO_TEXTCODEC |
245 | enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder, |
246 | UnicodeReverse, RawUnicode, UnicodeUTF8 }; |
247 | QT3_SUPPORT void setEncoding(Encoding encoding); |
248 | #endif |
249 | inline QT3_SUPPORT QString read() { return readAll(); } |
250 | inline QT3_SUPPORT void unsetDevice() { setDevice(0); } |
251 | #endif |
252 | |
253 | private: |
254 | #ifdef QT3_SUPPORT |
255 | int flagsInternal() const; |
256 | int flagsInternal(int flags); |
257 | #endif |
258 | |
259 | Q_DISABLE_COPY(QTextStream) |
260 | |
261 | QScopedPointer<QTextStreamPrivate> d_ptr; |
262 | }; |
263 | |
264 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags) |
265 | |
266 | /***************************************************************************** |
267 | QTextStream manipulators |
268 | *****************************************************************************/ |
269 | |
270 | typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function |
271 | typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument |
272 | typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument |
273 | |
274 | class Q_CORE_EXPORT QTextStreamManipulator |
275 | { |
276 | public: |
277 | QTextStreamManipulator(QTSMFI m, int a) { mf = m; mc = 0; arg = a; } |
278 | QTextStreamManipulator(QTSMFC m, QChar c) { mf = 0; mc = m; ch = c; arg = -1; } |
279 | void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } } |
280 | |
281 | private: |
282 | QTSMFI mf; // QTextStream member function |
283 | QTSMFC mc; // QTextStream member function |
284 | int arg; // member function argument |
285 | QChar ch; |
286 | }; |
287 | |
288 | inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f) |
289 | { return (*f)(s); } |
290 | |
291 | inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f) |
292 | { return (*f)(s); } |
293 | |
294 | inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m) |
295 | { m.exec(s); return s; } |
296 | |
297 | Q_CORE_EXPORT QTextStream &bin(QTextStream &s); |
298 | Q_CORE_EXPORT QTextStream &oct(QTextStream &s); |
299 | Q_CORE_EXPORT QTextStream &dec(QTextStream &s); |
300 | Q_CORE_EXPORT QTextStream &hex(QTextStream &s); |
301 | |
302 | Q_CORE_EXPORT QTextStream &showbase(QTextStream &s); |
303 | Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s); |
304 | Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s); |
305 | Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s); |
306 | Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s); |
307 | Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s); |
308 | |
309 | Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s); |
310 | Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s); |
311 | Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s); |
312 | Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s); |
313 | |
314 | Q_CORE_EXPORT QTextStream &fixed(QTextStream &s); |
315 | Q_CORE_EXPORT QTextStream &scientific(QTextStream &s); |
316 | |
317 | Q_CORE_EXPORT QTextStream &left(QTextStream &s); |
318 | Q_CORE_EXPORT QTextStream &right(QTextStream &s); |
319 | Q_CORE_EXPORT QTextStream ¢er(QTextStream &s); |
320 | |
321 | Q_CORE_EXPORT QTextStream &endl(QTextStream &s); |
322 | Q_CORE_EXPORT QTextStream &flush(QTextStream &s); |
323 | Q_CORE_EXPORT QTextStream &reset(QTextStream &s); |
324 | |
325 | Q_CORE_EXPORT QTextStream &bom(QTextStream &s); |
326 | |
327 | Q_CORE_EXPORT QTextStream &ws(QTextStream &s); |
328 | |
329 | inline QTextStreamManipulator qSetFieldWidth(int width) |
330 | { |
331 | QTSMFI func = &QTextStream::setFieldWidth; |
332 | return QTextStreamManipulator(func,width); |
333 | } |
334 | |
335 | inline QTextStreamManipulator qSetPadChar(QChar ch) |
336 | { |
337 | QTSMFC func = &QTextStream::setPadChar; |
338 | return QTextStreamManipulator(func, ch); |
339 | } |
340 | |
341 | inline QTextStreamManipulator qSetRealNumberPrecision(int precision) |
342 | { |
343 | QTSMFI func = &QTextStream::setRealNumberPrecision; |
344 | return QTextStreamManipulator(func, precision); |
345 | } |
346 | |
347 | #ifdef QT3_SUPPORT |
348 | typedef QTextStream QTS; |
349 | |
350 | class Q_CORE_EXPORT QTextIStream : public QTextStream |
351 | { |
352 | public: |
353 | inline explicit QTextIStream(const QString *s) : QTextStream(const_cast<QString *>(s), QIODevice::ReadOnly) {} |
354 | inline explicit QTextIStream(QByteArray *a) : QTextStream(a, QIODevice::ReadOnly) {} |
355 | inline QTextIStream(FILE *f) : QTextStream(f, QIODevice::ReadOnly) {} |
356 | |
357 | private: |
358 | Q_DISABLE_COPY(QTextIStream) |
359 | }; |
360 | |
361 | class Q_CORE_EXPORT QTextOStream : public QTextStream |
362 | { |
363 | public: |
364 | inline explicit QTextOStream(QString *s) : QTextStream(s, QIODevice::WriteOnly) {} |
365 | inline explicit QTextOStream(QByteArray *a) : QTextStream(a, QIODevice::WriteOnly) {} |
366 | inline QTextOStream(FILE *f) : QTextStream(f, QIODevice::WriteOnly) {} |
367 | |
368 | private: |
369 | Q_DISABLE_COPY(QTextOStream) |
370 | }; |
371 | #endif |
372 | |
373 | QT_END_NAMESPACE |
374 | |
375 | QT_END_HEADER |
376 | |
377 | #endif // QTEXTSTREAM_H |
378 | |