source: trunk/src/corelib/io/qtextstream.h@ 477

Last change on this file since 477 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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