|
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(); |
|
67 QTextStream( QIODevice * ); |
|
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 |
|
81 QTextStream &operator>>( QChar & ); |
|
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 * ); |
|
92 QTextStream &operator>>( QString & ); |
|
93 QTextStream &operator>>( QCString & ); |
|
94 |
|
95 QTextStream &operator<<( QChar ); |
|
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 ); |
|
157 QIODevice *dev; |
|
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; |
|
166 QTextCodec *mapper; |
|
167 QTextStreamPrivate * d; |
|
168 QChar ungetcBuf; |
|
169 bool latin1; |
|
170 bool internalOrder; |
|
171 bool doUnicodeHeader; |
|
172 void *reserved_ptr; |
|
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 |
|
199 class Q_EXPORT QTextIStream : public QTextStream { |
|
200 public: |
|
201 QTextIStream( QString* s ) : |
|
202 QTextStream(s,IO_ReadOnly) { } |
|
203 QTextIStream( QByteArray ba ) : |
|
204 QTextStream(ba,IO_ReadOnly) { } |
|
205 QTextIStream( FILE *f ) : |
|
206 QTextStream(f,IO_ReadOnly) { } |
|
207 }; |
|
208 |
|
209 class Q_EXPORT QTextOStream : public QTextStream { |
|
210 public: |
|
211 QTextOStream( QString* s ) : |
|
212 QTextStream(s,IO_WriteOnly) { } |
|
213 QTextOStream( QByteArray ba ) : |
|
214 QTextStream(ba,IO_WriteOnly) { } |
|
215 QTextOStream( FILE *f ) : |
|
216 QTextStream(f,IO_WriteOnly) { } |
|
217 }; |
|
218 |
|
219 /***************************************************************************** |
|
220 QTextStream inline functions |
|
221 *****************************************************************************/ |
|
222 |
|
223 inline QIODevice *QTextStream::device() const |
|
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 */ |
|
268 inline QChar QTextStream::ts_getc() |
|
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 |
|
287 Q_EXPORT inline QTextStream &operator>>( QTextStream &s, QTSFUNC f ) |
|
288 { return (*f)( s ); } |
|
289 |
|
290 Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSFUNC f ) |
|
291 { return (*f)( s ); } |
|
292 |
|
293 Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSManip m ) |
|
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 { |
|
307 QTSMFI func = &QTextStream::width; |
|
308 return QTSManip(func,w); |
|
309 } |
|
310 |
|
311 Q_EXPORT inline QTSManip qSetFill( int f ) |
|
312 { |
|
313 QTSMFI func = &QTextStream::fill; |
|
314 return QTSManip(func,f); |
|
315 } |
|
316 |
|
317 Q_EXPORT inline QTSManip qSetPrecision( int p ) |
|
318 { |
|
319 QTSMFI func = &QTextStream::precision; |
|
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 { |
|
333 QTSMFI func = &QTextStream::width; |
|
334 return QTSManip(func,w); |
|
335 } |
|
336 |
|
337 Q_EXPORT inline QTSManip setfill( int f ) |
|
338 { |
|
339 QTSMFI func = &QTextStream::fill; |
|
340 return QTSManip(func,f); |
|
341 } |
|
342 |
|
343 Q_EXPORT inline QTSManip setprecision( int p ) |
|
344 { |
|
345 QTSMFI func = &QTextStream::precision; |
|
346 return QTSManip(func,p); |
|
347 } |
|
348 #endif |
|
349 |
|
350 #endif // QT_NO_TEXTSTREAM |
|
351 #endif // QTEXTSTREAM_H |