qtextstream.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
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 #endif
63 
64  // Encoding encoding() const { return cmode; }
65 
66  QTextStream();
68  QTextStream( QString*, int mode );
69  QTextStream( QString&, int mode ); // obsolete
70  QTextStream( QByteArray, int mode );
71  QTextStream( FILE *, int mode );
72  virtual ~QTextStream();
73 
74  QIODevice *device() const;
75  void setDevice( QIODevice * );
76  void unsetDevice();
77 
78  bool atEnd() const;
79  bool eof() const;
80 
82  QTextStream &operator>>( char & );
83  QTextStream &operator>>( signed short & );
84  QTextStream &operator>>( unsigned short & );
85  QTextStream &operator>>( signed int & );
86  QTextStream &operator>>( unsigned int & );
87  QTextStream &operator>>( signed long & );
88  QTextStream &operator>>( unsigned long & );
89  QTextStream &operator>>( float & );
90  QTextStream &operator>>( double & );
91  QTextStream &operator>>( char * );
94 
96  QTextStream &operator<<( char );
97  QTextStream &operator<<( signed short );
98  QTextStream &operator<<( unsigned short );
99  QTextStream &operator<<( signed int );
100  QTextStream &operator<<( unsigned int );
101  QTextStream &operator<<( signed long );
102  QTextStream &operator<<( unsigned long );
103  QTextStream &operator<<( float );
104  QTextStream &operator<<( double );
105  QTextStream &operator<<( const char* );
106  QTextStream &operator<<( const QString & );
107  QTextStream &operator<<( const QCString & );
108  QTextStream &operator<<( void * ); // any pointer
109 
110  QTextStream &readRawBytes( char *, uint len );
111  QTextStream &writeRawBytes( const char* , uint len );
112 
113  QString readLine();
114  QString read();
115  void skipWhiteSpace();
116 
117  enum {
118  skipws = 0x0001, // skip whitespace on input
119  left = 0x0002, // left-adjust output
120  right = 0x0004, // right-adjust output
121  internal = 0x0008, // pad after sign
122  bin = 0x0010, // binary format integer
123  oct = 0x0020, // octal format integer
124  dec = 0x0040, // decimal format integer
125  hex = 0x0080, // hex format integer
126  showbase = 0x0100, // show base indicator
127  showpoint = 0x0200, // force decimal point (float)
128  uppercase = 0x0400, // upper-case hex output
129  showpos = 0x0800, // add '+' to positive integers
130  scientific= 0x1000, // scientific float output
131  fixed = 0x2000 // fixed float output
132  };
133 
134  static const int basefield; // bin | oct | dec | hex
135  static const int adjustfield; // left | right | internal
136  static const int floatfield; // scientific | fixed
137 
138  int flags() const;
139  int flags( int f );
140  int setf( int bits );
141  int setf( int bits, int mask );
142  int unsetf( int bits );
143 
144  void reset();
145 
146  int width() const;
147  int width( int );
148  int fill() const;
149  int fill( int );
150  int precision() const;
151  int precision( int );
152 
153 private:
154  long input_int();
155  void init();
156  QTextStream &output_int( int, ulong, bool );
158  bool isNetworkOrder() { return internalOrder == QChar::networkOrdered(); }
159 
160  int fflags;
161  int fwidth;
162  int fillchar;
163  int fprec;
164  bool fstrm;
165  bool owndev;
169  bool latin1;
173 
174  QChar eat_ws();
175  void ts_ungetc( QChar );
176  QChar ts_getc();
177  uint ts_getbuf( QChar*, uint );
178  void ts_putc(int);
179  void ts_putc(QChar);
180  bool ts_isspace(QChar);
181  bool ts_isdigit(QChar);
182  ulong input_bin();
183  ulong input_oct();
184  ulong input_dec();
185  ulong input_hex();
186  double input_double();
187  QTextStream &writeBlock( const char* p, uint len );
188  QTextStream &writeBlock( const QChar* p, uint len );
189 
190 private: // Disabled copy constructor and operator=
191 #if defined(Q_DISABLE_COPY)
192  QTextStream( const QTextStream & );
193  QTextStream &operator=( const QTextStream & );
194 #endif
195 };
196 
197 typedef QTextStream QTS;
198 
200 public:
202  QTextStream(s,IO_ReadOnly) { }
204  QTextStream(ba,IO_ReadOnly) { }
205  QTextIStream( FILE *f ) :
206  QTextStream(f,IO_ReadOnly) { }
207 };
208 
210 public:
214  QTextStream(ba,IO_WriteOnly) { }
215  QTextOStream( FILE *f ) :
217 };
218 
219 /*****************************************************************************
220  QTextStream inline functions
221  *****************************************************************************/
222 
224 { return dev; }
225 
226 inline bool QTextStream::atEnd() const
227 { return dev ? dev->atEnd() : FALSE; }
228 
229 inline bool QTextStream::eof() const
230 { return atEnd(); }
231 
232 inline int QTextStream::flags() const
233 { return fflags; }
234 
235 inline int QTextStream::flags( int f )
236 { int oldf = fflags; fflags = f; return oldf; }
237 
238 inline int QTextStream::setf( int bits )
239 { int oldf = fflags; fflags |= bits; return oldf; }
240 
241 inline int QTextStream::setf( int bits, int mask )
242 { int oldf = fflags; fflags = (fflags & ~mask) | (bits & mask); return oldf; }
243 
244 inline int QTextStream::unsetf( int bits )
245 { int oldf = fflags; fflags &= ~bits; return oldf; }
246 
247 inline int QTextStream::width() const
248 { return fwidth; }
249 
250 inline int QTextStream::width( int w )
251 { int oldw = fwidth; fwidth = w; return oldw; }
252 
253 inline int QTextStream::fill() const
254 { return fillchar; }
255 
256 inline int QTextStream::fill( int f )
257 { int oldc = fillchar; fillchar = f; return oldc; }
258 
259 inline int QTextStream::precision() const
260 { return fprec; }
261 
262 inline int QTextStream::precision( int p )
263 { int oldp = fprec; fprec = p; return oldp; }
264 
265 /*!
266  Returns one character from the stream, or EOF.
267 */
269 { QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); }
270 
271 /*****************************************************************************
272  QTextStream manipulators
273  *****************************************************************************/
274 
275 typedef QTextStream & (*QTSFUNC)(QTextStream &);// manipulator function
276 typedef int (QTextStream::*QTSMFI)(int); // manipulator w/int argument
277 
278 class Q_EXPORT QTSManip { // text stream manipulator
279 public:
280  QTSManip( QTSMFI m, int a ) { mf=m; arg=a; }
281  void exec( QTextStream &s ) { (s.*mf)(arg); }
282 private:
283  QTSMFI mf; // QTextStream member function
284  int arg; // member function argument
285 };
286 
288 { return (*f)( s ); }
289 
291 { return (*f)( s ); }
292 
294 { m.exec(s); return s; }
295 
296 Q_EXPORT QTextStream &bin( QTextStream &s ); // set bin notation
297 Q_EXPORT QTextStream &oct( QTextStream &s ); // set oct notation
298 Q_EXPORT QTextStream &dec( QTextStream &s ); // set dec notation
299 Q_EXPORT QTextStream &hex( QTextStream &s ); // set hex notation
300 Q_EXPORT QTextStream &endl( QTextStream &s ); // insert EOL ('\n')
301 Q_EXPORT QTextStream &flush( QTextStream &s ); // flush output
302 Q_EXPORT QTextStream &ws( QTextStream &s ); // eat whitespace on input
303 Q_EXPORT QTextStream &reset( QTextStream &s ); // set default flags
304 
305 Q_EXPORT inline QTSManip qSetW( int w )
306 {
308  return QTSManip(func,w);
309 }
310 
312 {
314  return QTSManip(func,f);
315 }
316 
318 {
320  return QTSManip(func,p);
321 }
322 
323 
324 #ifndef QT_ALTERNATE_QTSMANIP
325 
326 // These will go away in Qt 3.0, as they conflict with std libs
327 //
328 // If you get conflicts now, #define QT_ALTERNATE_QTSMANIP before
329 // including this file.
330 
331 Q_EXPORT inline QTSManip setw( int w )
332 {
334  return QTSManip(func,w);
335 }
336 
337 Q_EXPORT inline QTSManip setfill( int f )
338 {
340  return QTSManip(func,f);
341 }
342 
344 {
346  return QTSManip(func,p);
347 }
348 #endif
349 
350 #endif // QT_NO_TEXTSTREAM
351 #endif // QTEXTSTREAM_H
QTextOStream(FILE *f)
Definition: qtextstream.h:215
QChar ts_getc()
Definition: qtextstream.h:268
int setf(int bits)
Definition: qtextstream.h:238
QTextIStream(QByteArray ba)
Definition: qtextstream.h:203
float precision
Definition: makePolycone.py:48
bool isNetworkOrder()
Definition: qtextstream.h:158
Q_EXPORT QTextStream & endl(QTextStream &s)
void exec(QTextStream &s)
Definition: qtextstream.h:281
bool doUnicodeHeader
Definition: qtextstream.h:171
#define IO_WriteOnly
Definition: qiodevice.h:62
bool atEnd() const
Definition: qtextstream.h:226
QTSMFI mf
Definition: qtextstream.h:283
uint ts_getbuf(QChar *, uint)
QTSManip(QTSMFI m, int a)
Definition: qtextstream.h:280
State-based decoder.
Definition: qtextcodec.h:56
const bool FALSE
Definition: qglobal.h:370
int width() const
Definition: qtextstream.h:247
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
Q_EXPORT QTSManip qSetFill(int f)
Definition: qtextstream.h:311
init
Definition: train.py:42
int fill() const
Definition: qtextstream.h:253
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
QChar ungetcBuf
Definition: qtextstream.h:168
#define IO_ReadOnly
Definition: qiodevice.h:61
Q_EXPORT QTextStream & oct(QTextStream &s)
int precision() const
Definition: qtextstream.h:259
int unsetf(int bits)
Definition: qtextstream.h:244
A convenience class for input streams.
Definition: qtextstream.h:199
QIODevice * dev
Definition: qtextstream.h:157
static bool networkOrdered()
Definition: qstring.h:172
Q_EXPORT QTextStream & flush(QTextStream &s)
Q_EXPORT QTSManip qSetPrecision(int p)
Definition: qtextstream.h:317
const double a
bool internalOrder
Definition: qtextstream.h:170
Q_EXPORT QTextStream & hex(QTextStream &s)
int(QTextStream::* QTSMFI)(int)
Definition: qtextstream.h:276
unsigned long ulong
Definition: qglobal.h:352
p
Definition: test.py:223
QTextOStream(QByteArray ba)
Definition: qtextstream.h:213
Q_EXPORT QTSManip qSetW(int w)
Definition: qtextstream.h:305
static const int adjustfield
Definition: qtextstream.h:135
QTextIStream(FILE *f)
Definition: qtextstream.h:205
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Q_EXPORT QTextStream & reset(QTextStream &s)
QTextOStream(QString *s)
Definition: qtextstream.h:211
static const int floatfield
Definition: qtextstream.h:136
The QTextStream class provides basic functions for reading and writing text using a QIODevice...
Definition: qtextstream.h:53
unsigned short ushort
Definition: qglobal.h:350
def fill(s)
Definition: translator.py:93
A convenience class for output streams.
Definition: qtextstream.h:209
static const int basefield
Definition: qtextstream.h:134
Q_EXPORT QTextStream & ws(QTextStream &s)
Q_EXPORT QTextStream & dec(QTextStream &s)
def func()
Definition: docstring.py:7
QTextStream &(* QTSFUNC)(QTextStream &)
Definition: qtextstream.h:275
QTextStreamPrivate * d
Definition: qtextstream.h:167
QTextStream QTS
Definition: qtextstream.h:197
Q_EXPORT QTextStream & operator>>(QTextStream &s, QTSFUNC f)
Definition: qtextstream.h:287
QIODevice * device() const
Definition: qtextstream.h:223
int flags() const
Definition: qtextstream.h:232
int read(int, char *, size_t)
Read bytes from a file descriptor.
Q_EXPORT QTextStream & bin(QTextStream &s)
void * reserved_ptr
Definition: qtextstream.h:172
Provides conversion between text encodings.
Definition: qtextcodec.h:62
QTextIStream(QString *s)
Definition: qtextstream.h:201
Q_EXPORT QTextStream & operator<<(QTextStream &s, QTSFUNC f)
Definition: qtextstream.h:290
The QIODevice class is the base class of I/O devices.
Definition: qiodevice.h:88
unsigned uint
Definition: qglobal.h:351
Q_EXPORT QTSManip setfill(int f)
Definition: qtextstream.h:337
static QCString * s
Definition: config.cpp:1042
#define Q_EXPORT
Definition: qglobal.h:468
QTextCodec * mapper
Definition: qtextstream.h:166
bool eof() const
Definition: qtextstream.h:229