| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: qtextstream.h 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Definition of QTextStream class | 
|---|
| 5 | ** | 
|---|
| 6 | ** Created : 940922 | 
|---|
| 7 | ** | 
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 9 | ** | 
|---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit. | 
|---|
| 11 | ** | 
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License | 
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file | 
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file. | 
|---|
| 15 | ** | 
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the | 
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 19 | ** packaging of this file. | 
|---|
| 20 | ** | 
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | 
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License | 
|---|
| 23 | ** Agreement provided with the Software. | 
|---|
| 24 | ** | 
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 
|---|
| 27 | ** | 
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | 
|---|
| 29 | **   information about Qt Commercial License Agreements. | 
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | 
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 
|---|
| 32 | ** | 
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are | 
|---|
| 34 | ** not clear to you. | 
|---|
| 35 | ** | 
|---|
| 36 | **********************************************************************/ | 
|---|
| 37 |  | 
|---|
| 38 | #ifndef QTEXTSTREAM_H | 
|---|
| 39 | #define QTEXTSTREAM_H | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef QT_H | 
|---|
| 42 | #include "qiodevice.h" | 
|---|
| 43 | #include "qstring.h" | 
|---|
| 44 | #include <stdio.h> | 
|---|
| 45 | #endif // QT_H | 
|---|
| 46 |  | 
|---|
| 47 | #ifndef QT_NO_TEXTSTREAM | 
|---|
| 48 | class QTextCodec; | 
|---|
| 49 | class QTextDecoder; | 
|---|
| 50 |  | 
|---|
| 51 | class QTextStreamPrivate; | 
|---|
| 52 |  | 
|---|
| 53 | class Q_EXPORT QTextStream                              // text stream class | 
|---|
| 54 | { | 
|---|
| 55 | public: | 
|---|
| 56 | enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder, | 
|---|
| 57 | UnicodeReverse, RawUnicode, UnicodeUTF8 }; | 
|---|
| 58 |  | 
|---|
| 59 | void        setEncoding( Encoding ); | 
|---|
| 60 | #ifndef QT_NO_TEXTCODEC | 
|---|
| 61 | void        setCodec( QTextCodec* ); | 
|---|
| 62 | QTextCodec *codec(); | 
|---|
| 63 | #endif | 
|---|
| 64 |  | 
|---|
| 65 | QTextStream(); | 
|---|
| 66 | QTextStream( QIODevice * ); | 
|---|
| 67 | QTextStream( QString*, int mode ); | 
|---|
| 68 | QTextStream( QString&, int mode );          // obsolete | 
|---|
| 69 | QTextStream( QByteArray, int mode ); | 
|---|
| 70 | QTextStream( FILE *, int mode ); | 
|---|
| 71 | virtual ~QTextStream(); | 
|---|
| 72 |  | 
|---|
| 73 | QIODevice   *device() const; | 
|---|
| 74 | void         setDevice( QIODevice * ); | 
|---|
| 75 | void         unsetDevice(); | 
|---|
| 76 |  | 
|---|
| 77 | bool         atEnd() const; | 
|---|
| 78 | bool         eof() const; | 
|---|
| 79 |  | 
|---|
| 80 | QTextStream &operator>>( QChar & ); | 
|---|
| 81 | QTextStream &operator>>( char & ); | 
|---|
| 82 | QTextStream &operator>>( signed short & ); | 
|---|
| 83 | QTextStream &operator>>( unsigned short & ); | 
|---|
| 84 | QTextStream &operator>>( signed int & ); | 
|---|
| 85 | QTextStream &operator>>( unsigned int & ); | 
|---|
| 86 | QTextStream &operator>>( signed long & ); | 
|---|
| 87 | QTextStream &operator>>( unsigned long & ); | 
|---|
| 88 | QTextStream &operator>>( float & ); | 
|---|
| 89 | QTextStream &operator>>( double & ); | 
|---|
| 90 | QTextStream &operator>>( char * ); | 
|---|
| 91 | QTextStream &operator>>( QString & ); | 
|---|
| 92 | QTextStream &operator>>( QCString & ); | 
|---|
| 93 |  | 
|---|
| 94 | QTextStream &operator<<( QChar ); | 
|---|
| 95 | QTextStream &operator<<( char ); | 
|---|
| 96 | QTextStream &operator<<( signed short ); | 
|---|
| 97 | QTextStream &operator<<( unsigned short ); | 
|---|
| 98 | QTextStream &operator<<( signed int ); | 
|---|
| 99 | QTextStream &operator<<( unsigned int ); | 
|---|
| 100 | QTextStream &operator<<( signed long ); | 
|---|
| 101 | QTextStream &operator<<( unsigned long ); | 
|---|
| 102 | QTextStream &operator<<( float ); | 
|---|
| 103 | QTextStream &operator<<( double ); | 
|---|
| 104 | QTextStream &operator<<( const char* ); | 
|---|
| 105 | QTextStream &operator<<( const QString & ); | 
|---|
| 106 | QTextStream &operator<<( const QCString & ); | 
|---|
| 107 | QTextStream &operator<<( void * );          // any pointer | 
|---|
| 108 |  | 
|---|
| 109 | QTextStream &readRawBytes( char *, uint len ); | 
|---|
| 110 | QTextStream &writeRawBytes( const char* , uint len ); | 
|---|
| 111 |  | 
|---|
| 112 | QString     readLine(); | 
|---|
| 113 | QString     read(); | 
|---|
| 114 | void        skipWhiteSpace(); | 
|---|
| 115 |  | 
|---|
| 116 | enum { | 
|---|
| 117 | skipws    = 0x0001,                     // skip whitespace on input | 
|---|
| 118 | left      = 0x0002,                     // left-adjust output | 
|---|
| 119 | right     = 0x0004,                     // right-adjust output | 
|---|
| 120 | internal  = 0x0008,                     // pad after sign | 
|---|
| 121 | bin       = 0x0010,                     // binary format integer | 
|---|
| 122 | oct       = 0x0020,                     // octal format integer | 
|---|
| 123 | dec       = 0x0040,                     // decimal format integer | 
|---|
| 124 | hex       = 0x0080,                     // hex format integer | 
|---|
| 125 | showbase  = 0x0100,                     // show base indicator | 
|---|
| 126 | showpoint = 0x0200,                     // force decimal point (float) | 
|---|
| 127 | uppercase = 0x0400,                     // upper-case hex output | 
|---|
| 128 | showpos   = 0x0800,                     // add '+' to positive integers | 
|---|
| 129 | scientific= 0x1000,                     // scientific float output | 
|---|
| 130 | fixed     = 0x2000                      // fixed float output | 
|---|
| 131 | }; | 
|---|
| 132 |  | 
|---|
| 133 | static const int basefield;                 // bin | oct | dec | hex | 
|---|
| 134 | static const int adjustfield;               // left | right | internal | 
|---|
| 135 | static const int floatfield;                // scientific | fixed | 
|---|
| 136 |  | 
|---|
| 137 | int   flags() const; | 
|---|
| 138 | int   flags( int f ); | 
|---|
| 139 | int   setf( int bits ); | 
|---|
| 140 | int   setf( int bits, int mask ); | 
|---|
| 141 | int   unsetf( int bits ); | 
|---|
| 142 |  | 
|---|
| 143 | void  reset(); | 
|---|
| 144 |  | 
|---|
| 145 | int   width()       const; | 
|---|
| 146 | int   width( int ); | 
|---|
| 147 | int   fill()        const; | 
|---|
| 148 | int   fill( int ); | 
|---|
| 149 | int   precision()   const; | 
|---|
| 150 | int   precision( int ); | 
|---|
| 151 |  | 
|---|
| 152 | private: | 
|---|
| 153 | long        input_int(); | 
|---|
| 154 | void        init(); | 
|---|
| 155 | QTextStream &output_int( int, ulong, bool ); | 
|---|
| 156 | QIODevice   *dev; | 
|---|
| 157 |  | 
|---|
| 158 | int         fflags; | 
|---|
| 159 | int         fwidth; | 
|---|
| 160 | int         fillchar; | 
|---|
| 161 | int         fprec; | 
|---|
| 162 | bool        doUnicodeHeader; | 
|---|
| 163 | bool        owndev; | 
|---|
| 164 | QTextCodec  *mapper; | 
|---|
| 165 | QTextStreamPrivate * d; | 
|---|
| 166 | QChar       unused1; // ### remove in Qt 4.0 | 
|---|
| 167 | bool        latin1; | 
|---|
| 168 | bool        internalOrder; | 
|---|
| 169 | bool        networkOrder; | 
|---|
| 170 | void        *unused2; // ### remove in Qt 4.0 | 
|---|
| 171 |  | 
|---|
| 172 | QChar       eat_ws(); | 
|---|
| 173 | uint        ts_getline( QChar* ); | 
|---|
| 174 | void        ts_ungetc( QChar ); | 
|---|
| 175 | QChar       ts_getc(); | 
|---|
| 176 | uint        ts_getbuf( QChar*, uint ); | 
|---|
| 177 | void        ts_putc(int); | 
|---|
| 178 | void        ts_putc(QChar); | 
|---|
| 179 | bool        ts_isspace(QChar); | 
|---|
| 180 | bool        ts_isdigit(QChar); | 
|---|
| 181 | ulong       input_bin(); | 
|---|
| 182 | ulong       input_oct(); | 
|---|
| 183 | ulong       input_dec(); | 
|---|
| 184 | ulong       input_hex(); | 
|---|
| 185 | double      input_double(); | 
|---|
| 186 | QTextStream &writeBlock( const char* p, uint len ); | 
|---|
| 187 | QTextStream &writeBlock( const QChar* p, uint len ); | 
|---|
| 188 |  | 
|---|
| 189 | private:        // Disabled copy constructor and operator= | 
|---|
| 190 | #if defined(Q_DISABLE_COPY) | 
|---|
| 191 | QTextStream( const QTextStream & ); | 
|---|
| 192 | QTextStream &operator=( const QTextStream & ); | 
|---|
| 193 | #endif | 
|---|
| 194 | }; | 
|---|
| 195 |  | 
|---|
| 196 | typedef QTextStream QTS; | 
|---|
| 197 |  | 
|---|
| 198 | class Q_EXPORT QTextIStream : public QTextStream { | 
|---|
| 199 | public: | 
|---|
| 200 | QTextIStream( const QString* s ) : | 
|---|
| 201 | QTextStream((QString*)s,IO_ReadOnly) { } | 
|---|
| 202 | QTextIStream( QByteArray ba ) : | 
|---|
| 203 | QTextStream(ba,IO_ReadOnly) { } | 
|---|
| 204 | QTextIStream( FILE *f ) : | 
|---|
| 205 | QTextStream(f,IO_ReadOnly) { } | 
|---|
| 206 |  | 
|---|
| 207 | private:        // Disabled copy constructor and operator= | 
|---|
| 208 | #if defined(Q_DISABLE_COPY) | 
|---|
| 209 | QTextIStream( const QTextIStream & ); | 
|---|
| 210 | QTextIStream &operator=( const QTextIStream & ); | 
|---|
| 211 | #endif | 
|---|
| 212 | }; | 
|---|
| 213 |  | 
|---|
| 214 | class Q_EXPORT QTextOStream : public QTextStream { | 
|---|
| 215 | public: | 
|---|
| 216 | QTextOStream( QString* s ) : | 
|---|
| 217 | QTextStream(s,IO_WriteOnly) { } | 
|---|
| 218 | QTextOStream( QByteArray ba ) : | 
|---|
| 219 | QTextStream(ba,IO_WriteOnly) { } | 
|---|
| 220 | QTextOStream( FILE *f ) : | 
|---|
| 221 | QTextStream(f,IO_WriteOnly) { } | 
|---|
| 222 |  | 
|---|
| 223 | private:        // Disabled copy constructor and operator= | 
|---|
| 224 | #if defined(Q_DISABLE_COPY) | 
|---|
| 225 | QTextOStream( const QTextOStream & ); | 
|---|
| 226 | QTextOStream &operator=( const QTextOStream & ); | 
|---|
| 227 | #endif | 
|---|
| 228 | }; | 
|---|
| 229 |  | 
|---|
| 230 | /***************************************************************************** | 
|---|
| 231 | QTextStream inline functions | 
|---|
| 232 | *****************************************************************************/ | 
|---|
| 233 |  | 
|---|
| 234 | inline QIODevice *QTextStream::device() const | 
|---|
| 235 | { return dev; } | 
|---|
| 236 |  | 
|---|
| 237 | inline bool QTextStream::atEnd() const | 
|---|
| 238 | { return dev ? dev->atEnd() : FALSE; } | 
|---|
| 239 |  | 
|---|
| 240 | inline bool QTextStream::eof() const | 
|---|
| 241 | { return atEnd(); } | 
|---|
| 242 |  | 
|---|
| 243 | inline int QTextStream::flags() const | 
|---|
| 244 | { return fflags; } | 
|---|
| 245 |  | 
|---|
| 246 | inline int QTextStream::flags( int f ) | 
|---|
| 247 | { int oldf = fflags;  fflags = f;  return oldf; } | 
|---|
| 248 |  | 
|---|
| 249 | inline int QTextStream::setf( int bits ) | 
|---|
| 250 | { int oldf = fflags;  fflags |= bits;  return oldf; } | 
|---|
| 251 |  | 
|---|
| 252 | inline int QTextStream::setf( int bits, int mask ) | 
|---|
| 253 | { int oldf = fflags;  fflags = (fflags & ~mask) | (bits & mask); return oldf; } | 
|---|
| 254 |  | 
|---|
| 255 | inline int QTextStream::unsetf( int bits ) | 
|---|
| 256 | { int oldf = fflags;  fflags &= ~bits;  return oldf; } | 
|---|
| 257 |  | 
|---|
| 258 | inline int QTextStream::width() const | 
|---|
| 259 | { return fwidth; } | 
|---|
| 260 |  | 
|---|
| 261 | inline int QTextStream::width( int w ) | 
|---|
| 262 | { int oldw = fwidth;  fwidth = w;  return oldw;  } | 
|---|
| 263 |  | 
|---|
| 264 | inline int QTextStream::fill() const | 
|---|
| 265 | { return fillchar; } | 
|---|
| 266 |  | 
|---|
| 267 | inline int QTextStream::fill( int f ) | 
|---|
| 268 | { int oldc = fillchar;  fillchar = f;  return oldc;  } | 
|---|
| 269 |  | 
|---|
| 270 | inline int QTextStream::precision() const | 
|---|
| 271 | { return fprec; } | 
|---|
| 272 |  | 
|---|
| 273 | inline int QTextStream::precision( int p ) | 
|---|
| 274 | { int oldp = fprec;  fprec = p;  return oldp;  } | 
|---|
| 275 |  | 
|---|
| 276 | /*! | 
|---|
| 277 | Returns one character from the stream, or EOF. | 
|---|
| 278 | */ | 
|---|
| 279 | inline QChar QTextStream::ts_getc() | 
|---|
| 280 | { QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); } | 
|---|
| 281 |  | 
|---|
| 282 | /***************************************************************************** | 
|---|
| 283 | QTextStream manipulators | 
|---|
| 284 | *****************************************************************************/ | 
|---|
| 285 |  | 
|---|
| 286 | typedef QTextStream & (*QTSFUNC)(QTextStream &);// manipulator function | 
|---|
| 287 | typedef int (QTextStream::*QTSMFI)(int);        // manipulator w/int argument | 
|---|
| 288 |  | 
|---|
| 289 | class Q_EXPORT QTSManip {                       // text stream manipulator | 
|---|
| 290 | public: | 
|---|
| 291 | QTSManip( QTSMFI m, int a ) { mf=m; arg=a; } | 
|---|
| 292 | void exec( QTextStream &s ) { (s.*mf)(arg); } | 
|---|
| 293 | private: | 
|---|
| 294 | QTSMFI mf;                                  // QTextStream member function | 
|---|
| 295 | int    arg;                                 // member function argument | 
|---|
| 296 | }; | 
|---|
| 297 |  | 
|---|
| 298 | Q_EXPORT inline QTextStream &operator>>( QTextStream &s, QTSFUNC f ) | 
|---|
| 299 | { return (*f)( s ); } | 
|---|
| 300 |  | 
|---|
| 301 | Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSFUNC f ) | 
|---|
| 302 | { return (*f)( s ); } | 
|---|
| 303 |  | 
|---|
| 304 | Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSManip m ) | 
|---|
| 305 | { m.exec(s); return s; } | 
|---|
| 306 |  | 
|---|
| 307 | Q_EXPORT QTextStream &bin( QTextStream &s );    // set bin notation | 
|---|
| 308 | Q_EXPORT QTextStream &oct( QTextStream &s );    // set oct notation | 
|---|
| 309 | Q_EXPORT QTextStream &dec( QTextStream &s );    // set dec notation | 
|---|
| 310 | Q_EXPORT QTextStream &hex( QTextStream &s );    // set hex notation | 
|---|
| 311 | Q_EXPORT QTextStream &endl( QTextStream &s );   // insert EOL ('\n') | 
|---|
| 312 | Q_EXPORT QTextStream &flush( QTextStream &s );  // flush output | 
|---|
| 313 | Q_EXPORT QTextStream &ws( QTextStream &s );     // eat whitespace on input | 
|---|
| 314 | Q_EXPORT QTextStream &reset( QTextStream &s );  // set default flags | 
|---|
| 315 |  | 
|---|
| 316 | Q_EXPORT inline QTSManip qSetW( int w ) | 
|---|
| 317 | { | 
|---|
| 318 | QTSMFI func = &QTextStream::width; | 
|---|
| 319 | return QTSManip(func,w); | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | Q_EXPORT inline QTSManip qSetFill( int f ) | 
|---|
| 323 | { | 
|---|
| 324 | QTSMFI func = &QTextStream::fill; | 
|---|
| 325 | return QTSManip(func,f); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | Q_EXPORT inline QTSManip qSetPrecision( int p ) | 
|---|
| 329 | { | 
|---|
| 330 | QTSMFI func = &QTextStream::precision; | 
|---|
| 331 | return QTSManip(func,p); | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | #endif // QT_NO_TEXTSTREAM | 
|---|
| 335 | #endif // QTEXTSTREAM_H | 
|---|