~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/io/qtextstream.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QTEXTSTREAM_H
 
30
#define QTEXTSTREAM_H
 
31
 
 
32
#include "QtCore/qiodevice.h"
 
33
#include "QtCore/qstring.h"
 
34
#include "QtCore/qchar.h"
 
35
 
 
36
#ifndef QT_NO_TEXTCODEC
 
37
#  ifdef QT3_SUPPORT
 
38
#    include "QtCore/qtextcodec.h"
 
39
#  endif
 
40
#endif
 
41
 
 
42
#include <stdio.h>
 
43
 
 
44
class QTextCodec;
 
45
class QTextDecoder;
 
46
 
 
47
class QTextStreamPrivate;
 
48
class Q_CORE_EXPORT QTextStream                                // text stream class
 
49
{
 
50
    Q_DECLARE_PRIVATE(QTextStream)
 
51
 
 
52
public:
 
53
    enum RealNumberNotation {
 
54
        SmartNotation,
 
55
        FixedNotation,
 
56
        ScientificNotation
 
57
    };
 
58
    enum FieldAlignment {
 
59
        AlignLeft,
 
60
        AlignRight,
 
61
        AlignCenter,
 
62
        AlignAccountingStyle
 
63
    };
 
64
    enum NumberFlag {
 
65
        ShowBase = 0x1,
 
66
        ForcePoint = 0x2,
 
67
        ForceSign = 0x4,
 
68
        UppercaseBase = 0x8,
 
69
        UppercaseDigits = 0x10
 
70
    };
 
71
    Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
 
72
 
 
73
    QTextStream();
 
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();
 
80
 
 
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;
 
89
#endif
 
90
 
 
91
    void setDevice(QIODevice *device);
 
92
    QIODevice *device() const;
 
93
 
 
94
    void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
 
95
    QString *string() const;
 
96
 
 
97
    bool atEnd() const;
 
98
    void reset();
 
99
    void flush();
 
100
    bool seek(qint64 pos);
 
101
 
 
102
    void skipWhiteSpace();
 
103
 
 
104
    QString readLine(qint64 maxlen = 0);
 
105
    QString readAll();
 
106
 
 
107
    void setFieldAlignment(FieldAlignment alignment);
 
108
    FieldAlignment fieldAlignment() const;
 
109
 
 
110
    void setPadChar(QChar ch);
 
111
    QChar padChar() const;
 
112
 
 
113
    void setFieldWidth(int width);
 
114
    int fieldWidth() const;
 
115
 
 
116
    void setNumberFlags(NumberFlags flags);
 
117
    NumberFlags numberFlags() const;
 
118
 
 
119
    void setIntegerBase(int base);
 
120
    int integerBase() const;
 
121
 
 
122
    void setRealNumberNotation(RealNumberNotation notation);
 
123
    RealNumberNotation realNumberNotation() const;
 
124
 
 
125
    void setRealNumberPrecision(int precision);
 
126
    int realNumberPrecision() const;
 
127
 
 
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);
 
143
 
 
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);
 
160
 
 
161
#ifdef QT3_SUPPORT
 
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); }
 
166
 
 
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; }
 
173
 
 
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; }
 
180
 
 
181
    enum {
 
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
 
196
    };
 
197
    enum {
 
198
        basefield = bin | oct | dec | hex,
 
199
        adjustfield = left | right | internal,
 
200
        floatfield = scientific | fixed
 
201
    };
 
202
 
 
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); }
 
208
#endif
 
209
 
 
210
private:
 
211
#ifdef QT3_SUPPORT
 
212
    int flagsInternal() const;
 
213
    int flagsInternal(int flags);
 
214
#endif
 
215
 
 
216
    Q_DISABLE_COPY(QTextStream)
 
217
 
 
218
    QTextStreamPrivate *d_ptr;
 
219
};
 
220
 
 
221
Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
 
222
 
 
223
/*****************************************************************************
 
224
  QTextStream manipulators
 
225
 *****************************************************************************/
 
226
 
 
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
 
230
 
 
231
class Q_CORE_EXPORT QTextStreamManipulator
 
232
{
 
233
public:
 
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); } }
 
237
 
 
238
private:
 
239
    QTSMFI mf;                                        // QTextStream member function
 
240
    QTSMFC mc;                                        // QTextStream member function
 
241
    int arg;                                          // member function argument
 
242
    QChar ch;
 
243
};
 
244
 
 
245
inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
 
246
{ return (*f)(s); }
 
247
 
 
248
inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
 
249
{ return (*f)(s); }
 
250
 
 
251
inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
 
252
{ m.exec(s); return s; }
 
253
 
 
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);
 
258
 
 
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);
 
265
 
 
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);
 
270
 
 
271
Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
 
272
Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
 
273
 
 
274
Q_CORE_EXPORT QTextStream &left(QTextStream &s);
 
275
Q_CORE_EXPORT QTextStream &right(QTextStream &s);
 
276
Q_CORE_EXPORT QTextStream &center(QTextStream &s);
 
277
 
 
278
Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
 
279
Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
 
280
Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
 
281
 
 
282
Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
 
283
 
 
284
Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
 
285
 
 
286
inline QTextStreamManipulator qSetFieldWidth(int width)
 
287
{
 
288
    QTSMFI func = &QTextStream::setFieldWidth;
 
289
    return QTextStreamManipulator(func,width);
 
290
}
 
291
 
 
292
inline QTextStreamManipulator qSetPadChar(QChar ch)
 
293
{
 
294
    QTSMFC func = &QTextStream::setPadChar;
 
295
    return QTextStreamManipulator(func, ch);
 
296
}
 
297
 
 
298
inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
 
299
{
 
300
    QTSMFI func = &QTextStream::setRealNumberPrecision;
 
301
    return QTextStreamManipulator(func, precision);
 
302
}
 
303
 
 
304
#ifdef QT3_SUPPORT
 
305
typedef QTextStream QTS;
 
306
 
 
307
class Q_CORE_EXPORT QTextIStream : public QTextStream
 
308
{
 
309
public:
 
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) {}
 
313
 
 
314
private:
 
315
    Q_DISABLE_COPY(QTextIStream)
 
316
};
 
317
 
 
318
class Q_CORE_EXPORT QTextOStream : public QTextStream
 
319
{
 
320
public:
 
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) {}
 
324
 
 
325
private:
 
326
    Q_DISABLE_COPY(QTextOStream)
 
327
};
 
328
#endif
 
329
 
 
330
 
 
331
#endif // QTEXTSTREAM_H