1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include "QtCore/qiodevice.h"
33
#include "QtCore/qstring.h"
34
#include "QtCore/qchar.h"
36
#ifndef QT_NO_TEXTCODEC
38
# include "QtCore/qtextcodec.h"
47
class QTextStreamPrivate;
48
class Q_CORE_EXPORT QTextStream // text stream class
50
Q_DECLARE_PRIVATE(QTextStream)
53
enum RealNumberNotation {
69
UppercaseDigits = 0x10
71
Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
74
explicit QTextStream(QIODevice *device);
75
explicit QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
76
explicit QTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
77
explicit QTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
78
explicit QTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly);
79
virtual ~QTextStream();
81
#ifndef QT_NO_TEXTCODEC
82
void setCodec(QTextCodec *codec);
83
void setCodec(const char *codecName);
84
QTextCodec *codec() const;
85
void setAutoDetectUnicode(bool enabled);
86
bool autoDetectUnicode() const;
87
void setGenerateByteOrderMark(bool generate);
88
bool generateByteOrderMark() const;
91
void setDevice(QIODevice *device);
92
QIODevice *device() const;
94
void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
95
QString *string() const;
100
bool seek(qint64 pos);
102
void skipWhiteSpace();
104
QString readLine(qint64 maxlen = 0);
107
void setFieldAlignment(FieldAlignment alignment);
108
FieldAlignment fieldAlignment() const;
110
void setPadChar(QChar ch);
111
QChar padChar() const;
113
void setFieldWidth(int width);
114
int fieldWidth() const;
116
void setNumberFlags(NumberFlags flags);
117
NumberFlags numberFlags() const;
119
void setIntegerBase(int base);
120
int integerBase() const;
122
void setRealNumberNotation(RealNumberNotation notation);
123
RealNumberNotation realNumberNotation() const;
125
void setRealNumberPrecision(int precision);
126
int realNumberPrecision() const;
128
QTextStream &operator>>(QChar &ch);
129
QTextStream &operator>>(char &ch);
130
QTextStream &operator>>(signed short &i);
131
QTextStream &operator>>(unsigned short &i);
132
QTextStream &operator>>(signed int &i);
133
QTextStream &operator>>(unsigned int &i);
134
QTextStream &operator>>(signed long &i);
135
QTextStream &operator>>(unsigned long &i);
136
QTextStream &operator>>(qlonglong &i);
137
QTextStream &operator>>(qulonglong &i);
138
QTextStream &operator>>(float &f);
139
QTextStream &operator>>(double &f);
140
QTextStream &operator>>(QString &s);
141
QTextStream &operator>>(QByteArray &array);
142
QTextStream &operator>>(char *c);
144
QTextStream &operator<<(QChar ch);
145
QTextStream &operator<<(char ch);
146
QTextStream &operator<<(signed short i);
147
QTextStream &operator<<(unsigned short i);
148
QTextStream &operator<<(signed int i);
149
QTextStream &operator<<(unsigned int i);
150
QTextStream &operator<<(signed long i);
151
QTextStream &operator<<(unsigned long i);
152
QTextStream &operator<<(qlonglong i);
153
QTextStream &operator<<(qulonglong i);
154
QTextStream &operator<<(float f);
155
QTextStream &operator<<(double f);
156
QTextStream &operator<<(const QString &s);
157
QTextStream &operator<<(const QByteArray &array);
158
QTextStream &operator<<(const char *c);
159
QTextStream &operator<<(const void *ptr);
162
// not marked as QT3_SUPPORT to avoid double compiler warnings, as
163
// they are used in the QT3_SUPPORT functions below.
164
inline QT3_SUPPORT int flags() const { return flagsInternal(); }
165
inline QT3_SUPPORT int flags(int f) { return flagsInternal(f); }
167
inline QT3_SUPPORT int setf(int bits)
168
{ int old = flagsInternal(); flagsInternal(flagsInternal() | bits); return old; }
169
inline QT3_SUPPORT int setf(int bits, int mask)
170
{ int old = flagsInternal(); flagsInternal(flagsInternal() | (bits & mask)); return old; }
171
inline QT3_SUPPORT int unsetf(int bits)
172
{ int old = flagsInternal(); flagsInternal(flagsInternal() & ~bits); return old; }
174
inline QT3_SUPPORT int width(int w)
175
{ int old = fieldWidth(); setFieldWidth(w); return old; }
176
inline QT3_SUPPORT int fill(int f)
177
{ QChar ch = padChar(); setPadChar(QChar(f)); return ch.unicode(); }
178
inline QT3_SUPPORT int precision(int p)
179
{ int old = realNumberPrecision(); setRealNumberPrecision(p); return old; }
182
skipws = 0x0001, // skip whitespace on input
183
left = 0x0002, // left-adjust output
184
right = 0x0004, // right-adjust output
185
internal = 0x0008, // pad after sign
186
bin = 0x0010, // binary format integer
187
oct = 0x0020, // octal format integer
188
dec = 0x0040, // decimal format integer
189
hex = 0x0080, // hex format integer
190
showbase = 0x0100, // show base indicator
191
showpoint = 0x0200, // force decimal point (float)
192
uppercase = 0x0400, // upper-case hex output
193
showpos = 0x0800, // add '+' to positive integers
194
scientific = 0x1000, // scientific float output
195
fixed = 0x2000 // fixed float output
198
basefield = bin | oct | dec | hex,
199
adjustfield = left | right | internal,
200
floatfield = scientific | fixed
203
enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder,
204
UnicodeReverse, RawUnicode, UnicodeUTF8 };
205
QT3_SUPPORT void setEncoding(Encoding encoding);
206
inline QT3_SUPPORT QString read() { return readAll(); }
207
inline QT3_SUPPORT void unsetDevice() { setDevice(0); }
212
int flagsInternal() const;
213
int flagsInternal(int flags);
216
Q_DISABLE_COPY(QTextStream)
218
QTextStreamPrivate *d_ptr;
221
Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
223
/*****************************************************************************
224
QTextStream manipulators
225
*****************************************************************************/
227
typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
228
typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
229
typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
231
class Q_CORE_EXPORT QTextStreamManipulator
234
QTextStreamManipulator(QTSMFI m, int a) { mf = m; mc = 0; arg = a; }
235
QTextStreamManipulator(QTSMFC m, QChar c) { mf = 0; mc = m; ch = c; }
236
void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
239
QTSMFI mf; // QTextStream member function
240
QTSMFC mc; // QTextStream member function
241
int arg; // member function argument
245
inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
248
inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
251
inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
252
{ m.exec(s); return s; }
254
Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
255
Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
256
Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
257
Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
259
Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
260
Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
261
Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
262
Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
263
Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
264
Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
266
Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
267
Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
268
Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
269
Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
271
Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
272
Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
274
Q_CORE_EXPORT QTextStream &left(QTextStream &s);
275
Q_CORE_EXPORT QTextStream &right(QTextStream &s);
276
Q_CORE_EXPORT QTextStream ¢er(QTextStream &s);
278
Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
279
Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
280
Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
282
Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
284
Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
286
inline QTextStreamManipulator qSetFieldWidth(int width)
288
QTSMFI func = &QTextStream::setFieldWidth;
289
return QTextStreamManipulator(func,width);
292
inline QTextStreamManipulator qSetPadChar(QChar ch)
294
QTSMFC func = &QTextStream::setPadChar;
295
return QTextStreamManipulator(func, ch);
298
inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
300
QTSMFI func = &QTextStream::setRealNumberPrecision;
301
return QTextStreamManipulator(func, precision);
305
typedef QTextStream QTS;
307
class Q_CORE_EXPORT QTextIStream : public QTextStream
310
inline explicit QTextIStream(const QString *s) : QTextStream(const_cast<QString *>(s), QIODevice::ReadOnly) {}
311
inline explicit QTextIStream(QByteArray *a) : QTextStream(a, QIODevice::ReadOnly) {}
312
inline QTextIStream(FILE *f) : QTextStream(f, QIODevice::ReadOnly) {}
315
Q_DISABLE_COPY(QTextIStream)
318
class Q_CORE_EXPORT QTextOStream : public QTextStream
321
inline explicit QTextOStream(QString *s) : QTextStream(s, QIODevice::WriteOnly) {}
322
inline explicit QTextOStream(QByteArray *a) : QTextStream(a, QIODevice::WriteOnly) {}
323
inline QTextOStream(FILE *f) : QTextStream(f, QIODevice::WriteOnly) {}
326
Q_DISABLE_COPY(QTextOStream)
331
#endif // QTEXTSTREAM_H