1
/****************************************************************************
2
**
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
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
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
62
QT_BEGIN_HEADER
63
64
QT_BEGIN_NAMESPACE
65
66
QT_MODULE(Core)
67
68
class QTextCodec;
69
class QTextDecoder;
70
71
class QTextStreamPrivate;
72
class Q_CORE_EXPORT QTextStream                                // text stream class
73
{
74
    Q_DECLARE_PRIVATE(QTextStream)
75
76
public:
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
251
private:
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
262
Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
263
264
/*****************************************************************************
265
  QTextStream manipulators
266
 *****************************************************************************/
267
268
typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
269
typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
270
typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
271
272
class Q_CORE_EXPORT QTextStreamManipulator
273
{
274
public:
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
279
private:
280
    QTSMFI mf;                                        // QTextStream member function
281
    QTSMFC mc;                                        // QTextStream member function
282
    int arg;                                          // member function argument
283
    QChar ch;
284
};
285
286
inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
287
{ return (*f)(s); }
288
289
inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
290
{ return (*f)(s); }
291
292
inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
293
{ m.exec(s); return s; }
294
295
Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
296
Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
297
Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
298
Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
299
300
Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
301
Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
302
Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
303
Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
304
Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
305
Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
306
307
Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
308
Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
309
Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
310
Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
311
312
Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
313
Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
314
315
Q_CORE_EXPORT QTextStream &left(QTextStream &s);
316
Q_CORE_EXPORT QTextStream &right(QTextStream &s);
317
Q_CORE_EXPORT QTextStream &center(QTextStream &s);
318
319
Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
320
Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
321
Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
322
323
Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
324
325
Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
326
327
inline QTextStreamManipulator qSetFieldWidth(int width)
328
{
329
    QTSMFI func = &QTextStream::setFieldWidth;
330
    return QTextStreamManipulator(func,width);
331
}
332
333
inline QTextStreamManipulator qSetPadChar(QChar ch)
334
{
335
    QTSMFC func = &QTextStream::setPadChar;
336
    return QTextStreamManipulator(func, ch);
337
}
338
339
inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
340
{
341
    QTSMFI func = &QTextStream::setRealNumberPrecision;
342
    return QTextStreamManipulator(func, precision);
343
}
344
345
#ifdef QT3_SUPPORT
346
typedef QTextStream QTS;
347
348
class Q_CORE_EXPORT QTextIStream : public QTextStream
349
{
350
public:
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
355
private:
356
    Q_DISABLE_COPY(QTextIStream)
357
};
358
359
class Q_CORE_EXPORT QTextOStream : public QTextStream
360
{
361
public:
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
366
private:
367
    Q_DISABLE_COPY(QTextOStream)
368
};
369
#endif
370
371
QT_END_NAMESPACE
372
373
QT_END_HEADER
374
375
#endif // QTEXTSTREAM_H