qtextstream.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QTEXTSTREAM_H
00043 #define QTEXTSTREAM_H
00044 
00045 #include <QtCore/qiodevice.h>
00046 #include <QtCore/qstring.h>
00047 #include <QtCore/qchar.h>
00048 #include <QtCore/qlocale.h>
00049 #include <QtCore/qscopedpointer.h>
00050 
00051 #ifndef QT_NO_TEXTCODEC
00052 #  ifdef QT3_SUPPORT
00053 #    include <QtCore/qtextcodec.h>
00054 #  endif
00055 #endif
00056 
00057 #include <stdio.h>
00058 
00059 #ifdef Status
00060 #error qtextstream.h must be included before any header file that defines Status
00061 #endif
00062 
00063 QT_BEGIN_HEADER
00064 
00065 QT_BEGIN_NAMESPACE
00066 
00067 QT_MODULE(Core)
00068 
00069 class QTextCodec;
00070 class QTextDecoder;
00071 
00072 class QTextStreamPrivate;
00073 class Q_CORE_EXPORT QTextStream                                // text stream class
00074 {
00075     Q_DECLARE_PRIVATE(QTextStream)
00076 
00077 public:
00078     enum RealNumberNotation {
00079         SmartNotation,
00080         FixedNotation,
00081         ScientificNotation
00082     };
00083     enum FieldAlignment {
00084         AlignLeft,
00085         AlignRight,
00086         AlignCenter,
00087         AlignAccountingStyle
00088     };
00089     enum Status {
00090         Ok,
00091         ReadPastEnd,
00092         ReadCorruptData
00093     };
00094     enum NumberFlag {
00095         ShowBase = 0x1,
00096         ForcePoint = 0x2,
00097         ForceSign = 0x4,
00098         UppercaseBase = 0x8,
00099         UppercaseDigits = 0x10
00100     };
00101     Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
00102 
00103     QTextStream();
00104     explicit QTextStream(QIODevice *device);
00105     explicit QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
00106     explicit QTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
00107     explicit QTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
00108     explicit QTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly);
00109     virtual ~QTextStream();
00110 
00111 #ifndef QT_NO_TEXTCODEC
00112     void setCodec(QTextCodec *codec);
00113     void setCodec(const char *codecName);
00114     QTextCodec *codec() const;
00115     void setAutoDetectUnicode(bool enabled);
00116     bool autoDetectUnicode() const;
00117     void setGenerateByteOrderMark(bool generate);
00118     bool generateByteOrderMark() const;
00119 #endif
00120 
00121     void setLocale(const QLocale &locale);
00122     QLocale locale() const;
00123 
00124     void setDevice(QIODevice *device);
00125     QIODevice *device() const;
00126 
00127     void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
00128     QString *string() const;
00129 
00130     Status status() const;
00131     void setStatus(Status status);
00132     void resetStatus();
00133 
00134     bool atEnd() const;
00135     void reset();
00136     void flush();
00137     bool seek(qint64 pos);
00138     qint64 pos() const;
00139 
00140     void skipWhiteSpace();
00141 
00142     QString readLine(qint64 maxlen = 0);
00143     QString readAll();
00144     QString read(qint64 maxlen);
00145 
00146     void setFieldAlignment(FieldAlignment alignment);
00147     FieldAlignment fieldAlignment() const;
00148 
00149     void setPadChar(QChar ch);
00150     QChar padChar() const;
00151 
00152     void setFieldWidth(int width);
00153     int fieldWidth() const;
00154 
00155     void setNumberFlags(NumberFlags flags);
00156     NumberFlags numberFlags() const;
00157 
00158     void setIntegerBase(int base);
00159     int integerBase() const;
00160 
00161     void setRealNumberNotation(RealNumberNotation notation);
00162     RealNumberNotation realNumberNotation() const;
00163 
00164     void setRealNumberPrecision(int precision);
00165     int realNumberPrecision() const;
00166 
00167     QTextStream &operator>>(QChar &ch);
00168     QTextStream &operator>>(char &ch);
00169     QTextStream &operator>>(signed short &i);
00170     QTextStream &operator>>(unsigned short &i);
00171     QTextStream &operator>>(signed int &i);
00172     QTextStream &operator>>(unsigned int &i);
00173     QTextStream &operator>>(signed long &i);
00174     QTextStream &operator>>(unsigned long &i);
00175     QTextStream &operator>>(qlonglong &i);
00176     QTextStream &operator>>(qulonglong &i);
00177     QTextStream &operator>>(float &f);
00178     QTextStream &operator>>(double &f);
00179     QTextStream &operator>>(QString &s);
00180     QTextStream &operator>>(QByteArray &array);
00181     QTextStream &operator>>(char *c);
00182 
00183     QTextStream &operator<<(QBool b);
00184     QTextStream &operator<<(QChar ch);
00185     QTextStream &operator<<(char ch);
00186     QTextStream &operator<<(signed short i);
00187     QTextStream &operator<<(unsigned short i);
00188     QTextStream &operator<<(signed int i);
00189     QTextStream &operator<<(unsigned int i);
00190     QTextStream &operator<<(signed long i);
00191     QTextStream &operator<<(unsigned long i);
00192     QTextStream &operator<<(qlonglong i);
00193     QTextStream &operator<<(qulonglong i);
00194     QTextStream &operator<<(float f);
00195     QTextStream &operator<<(double f);
00196     QTextStream &operator<<(const QString &s);
00197     QTextStream &operator<<(const QByteArray &array);
00198     QTextStream &operator<<(const char *c);
00199     QTextStream &operator<<(const void *ptr);
00200 
00201 #ifdef QT3_SUPPORT
00202     // not marked as QT3_SUPPORT to avoid double compiler warnings, as
00203     // they are used in the QT3_SUPPORT functions below.
00204     inline QT3_SUPPORT int flags() const { return flagsInternal(); }
00205     inline QT3_SUPPORT int flags(int f) { return flagsInternal(f); }
00206 
00207     inline QT3_SUPPORT int setf(int bits)
00208     { int old = flagsInternal(); flagsInternal(flagsInternal() | bits); return old; }
00209     inline QT3_SUPPORT int setf(int bits, int mask)
00210     { int old = flagsInternal(); flagsInternal(flagsInternal() | (bits & mask)); return old; }
00211     inline QT3_SUPPORT int unsetf(int bits)
00212     { int old = flagsInternal(); flagsInternal(flagsInternal() & ~bits); return old; }
00213 
00214     inline QT3_SUPPORT int width(int w)
00215     { int old = fieldWidth(); setFieldWidth(w); return old; }
00216     inline QT3_SUPPORT int fill(int f)
00217     { QChar ch = padChar(); setPadChar(QChar(f)); return ch.unicode(); }
00218     inline QT3_SUPPORT int precision(int p)
00219     { int old = realNumberPrecision(); setRealNumberPrecision(p); return old; }
00220 
00221     enum {
00222         skipws       = 0x0001,                        // skip whitespace on input
00223         left         = 0x0002,                        // left-adjust output
00224         right        = 0x0004,                        // right-adjust output
00225         internal     = 0x0008,                        // pad after sign
00226         bin          = 0x0010,                        // binary format integer
00227         oct          = 0x0020,                        // octal format integer
00228         dec          = 0x0040,                        // decimal format integer
00229         hex          = 0x0080,                        // hex format integer
00230         showbase     = 0x0100,                        // show base indicator
00231         showpoint    = 0x0200,                        // force decimal point (float)
00232         uppercase    = 0x0400,                        // upper-case hex output
00233         showpos      = 0x0800,                        // add '+' to positive integers
00234         scientific   = 0x1000,                        // scientific float output
00235         fixed        = 0x2000                         // fixed float output
00236     };
00237     enum {
00238         basefield = bin | oct | dec | hex,
00239         adjustfield = left | right | internal,
00240         floatfield = scientific | fixed
00241     };
00242 
00243 #ifndef QT_NO_TEXTCODEC
00244     enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder,
00245                     UnicodeReverse, RawUnicode, UnicodeUTF8 };
00246     QT3_SUPPORT void setEncoding(Encoding encoding);
00247 #endif
00248     inline QT3_SUPPORT QString read() { return readAll(); }
00249     inline QT3_SUPPORT void unsetDevice() { setDevice(0); }
00250 #endif
00251 
00252 private:
00253 #ifdef QT3_SUPPORT
00254     int flagsInternal() const;
00255     int flagsInternal(int flags);
00256 #endif
00257 
00258     Q_DISABLE_COPY(QTextStream)
00259 
00260     QScopedPointer<QTextStreamPrivate> d_ptr;
00261 };
00262 
00263 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
00264 
00265 /*****************************************************************************
00266   QTextStream manipulators
00267  *****************************************************************************/
00268 
00269 typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
00270 typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
00271 typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
00272 
00273 class Q_CORE_EXPORT QTextStreamManipulator
00274 {
00275 public:
00276     QTextStreamManipulator(QTSMFI m, int a) { mf = m; mc = 0; arg = a; }
00277     QTextStreamManipulator(QTSMFC m, QChar c) { mf = 0; mc = m; ch = c; arg = -1; }
00278     void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
00279 
00280 private:
00281     QTSMFI mf;                                        // QTextStream member function
00282     QTSMFC mc;                                        // QTextStream member function
00283     int arg;                                          // member function argument
00284     QChar ch;
00285 };
00286 
00287 inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
00288 { return (*f)(s); }
00289 
00290 inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
00291 { return (*f)(s); }
00292 
00293 inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
00294 { m.exec(s); return s; }
00295 
00296 Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
00297 Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
00298 Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
00299 Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
00300 
00301 Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
00302 Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
00303 Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
00304 Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
00305 Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
00306 Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
00307 
00308 Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
00309 Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
00310 Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
00311 Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
00312 
00313 Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
00314 Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
00315 
00316 Q_CORE_EXPORT QTextStream &left(QTextStream &s);
00317 Q_CORE_EXPORT QTextStream &right(QTextStream &s);
00318 Q_CORE_EXPORT QTextStream &center(QTextStream &s);
00319 
00320 Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
00321 Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
00322 Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
00323 
00324 Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
00325 
00326 Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
00327 
00328 inline QTextStreamManipulator qSetFieldWidth(int width)
00329 {
00330     QTSMFI func = &QTextStream::setFieldWidth;
00331     return QTextStreamManipulator(func,width);
00332 }
00333 
00334 inline QTextStreamManipulator qSetPadChar(QChar ch)
00335 {
00336     QTSMFC func = &QTextStream::setPadChar;
00337     return QTextStreamManipulator(func, ch);
00338 }
00339 
00340 inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
00341 {
00342     QTSMFI func = &QTextStream::setRealNumberPrecision;
00343     return QTextStreamManipulator(func, precision);
00344 }
00345 
00346 #ifdef QT3_SUPPORT
00347 typedef QTextStream QTS;
00348 
00349 class Q_CORE_EXPORT QTextIStream : public QTextStream
00350 {
00351 public:
00352     inline explicit QTextIStream(const QString *s) : QTextStream(const_cast<QString *>(s), QIODevice::ReadOnly) {}
00353     inline explicit QTextIStream(QByteArray *a) : QTextStream(a, QIODevice::ReadOnly) {}
00354     inline QTextIStream(FILE *f) : QTextStream(f, QIODevice::ReadOnly) {}
00355 
00356 private:
00357     Q_DISABLE_COPY(QTextIStream)
00358 };
00359 
00360 class Q_CORE_EXPORT QTextOStream : public QTextStream
00361 {
00362 public:
00363     inline explicit QTextOStream(QString *s) : QTextStream(s, QIODevice::WriteOnly) {}
00364     inline explicit QTextOStream(QByteArray *a) : QTextStream(a, QIODevice::WriteOnly) {}
00365     inline QTextOStream(FILE *f) : QTextStream(f, QIODevice::WriteOnly) {}
00366 
00367 private:
00368     Q_DISABLE_COPY(QTextOStream)
00369 };
00370 #endif
00371 
00372 QT_END_NAMESPACE
00373 
00374 QT_END_HEADER
00375 
00376 #endif // QTEXTSTREAM_H