summaryrefslogtreecommitdiffstats
path: root/src/qt3support/text/q3textstream.h
blob: d0c4f48aab76621b66a9483917b96274c70f4e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef Q3TEXTSTREAM_H
#define Q3TEXTSTREAM_H

#include <QtCore/qiodevice.h>
#include <QtCore/qstring.h>
#ifndef QT_NO_TEXTCODEC
#include <QtCore/qtextcodec.h>
#endif
#include <Qt3Support/q3cstring.h>

#include <stdio.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Qt3SupportLight)

class Q3TextStreamPrivate;

class Q_COMPAT_EXPORT Q3TextStream				// text stream class
{
public:
    enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder,
		    UnicodeReverse, RawUnicode, UnicodeUTF8 };

    void	setEncoding( Encoding );
#ifndef QT_NO_TEXTCODEC
    void	setCodec( QTextCodec* );
    QTextCodec *codec();
#endif

    Q3TextStream();
    Q3TextStream( QIODevice * );
    Q3TextStream( QString*, int mode );
    Q3TextStream( QString&, int mode );		// obsolete
    Q3TextStream( QByteArray&, int mode );
    Q3TextStream( FILE *, int mode );
    virtual ~Q3TextStream();

    QIODevice	*device() const;
    void	 setDevice( QIODevice * );
    void	 unsetDevice();

    bool	 atEnd() const;
    bool	 eof() const;

    Q3TextStream &operator>>( QChar & );
    Q3TextStream &operator>>( char & );
    Q3TextStream &operator>>( signed short & );
    Q3TextStream &operator>>( unsigned short & );
    Q3TextStream &operator>>( signed int & );
    Q3TextStream &operator>>( unsigned int & );
    Q3TextStream &operator>>( signed long & );
    Q3TextStream &operator>>( unsigned long & );
    Q3TextStream &operator>>( float & );
    Q3TextStream &operator>>( double & );
    Q3TextStream &operator>>( char * );
    Q3TextStream &operator>>( QString & );
    Q3TextStream &operator>>( Q3CString & );

    Q3TextStream &operator<<( QChar );
    Q3TextStream &operator<<( char );
    Q3TextStream &operator<<( signed short );
    Q3TextStream &operator<<( unsigned short );
    Q3TextStream &operator<<( signed int );
    Q3TextStream &operator<<( unsigned int );
    Q3TextStream &operator<<( signed long );
    Q3TextStream &operator<<( unsigned long );
    Q3TextStream &operator<<( float );
    Q3TextStream &operator<<( double );
    Q3TextStream &operator<<( const char* );
    Q3TextStream &operator<<( const QString & );
    Q3TextStream &operator<<( const Q3CString & );
    Q3TextStream &operator<<( void * );		// any pointer

    Q3TextStream &readRawBytes( char *, uint len );
    Q3TextStream &writeRawBytes( const char* , uint len );

    QString	readLine();
    QString	read();
    void	skipWhiteSpace();

    enum {
	skipws	  = 0x0001,			// skip whitespace on input
	left	  = 0x0002,			// left-adjust output
	right	  = 0x0004,			// right-adjust output
	internal  = 0x0008,			// pad after sign
	bin	  = 0x0010,			// binary format integer
	oct	  = 0x0020,			// octal format integer
	dec	  = 0x0040,			// decimal format integer
	hex	  = 0x0080,			// hex format integer
	showbase  = 0x0100,			// show base indicator
	showpoint = 0x0200,			// force decimal point (float)
	uppercase = 0x0400,			// upper-case hex output
	showpos	  = 0x0800,			// add '+' to positive integers
	scientific= 0x1000,			// scientific float output
	fixed	  = 0x2000			// fixed float output
    };

    static const int basefield;			// bin | oct | dec | hex
    static const int adjustfield;		// left | right | internal
    static const int floatfield;		// scientific | fixed

    int	  flags() const;
    int	  flags( int f );
    int	  setf( int bits );
    int	  setf( int bits, int mask );
    int	  unsetf( int bits );

    void  reset();

    int	  width()	const;
    int	  width( int );
    int	  fill()	const;
    int	  fill( int );
    int	  precision()	const;
    int	  precision( int );

private:
    long	input_int();
    void	init();
    Q3TextStream &output_int( int, ulong, bool );
    QIODevice	*dev;

    int		fflags;
    int		fwidth;
    int		fillchar;
    int		fprec;
    bool	doUnicodeHeader;
    bool	owndev;
    QTextCodec 	*mapper;
    QTextCodec::ConverterState mapperReadState;
    QTextCodec::ConverterState mapperWriteState;
    Q3TextStreamPrivate * d;
    QChar	unused1; // ### remove in Qt 4.0
    bool	latin1;
    bool 	internalOrder;
    bool	networkOrder;
    void	*unused2; // ### remove in Qt 4.0

    QChar	eat_ws();
    uint 	ts_getline( QChar* );
    void	ts_ungetc( QChar );
    QChar	ts_getc();
    uint	ts_getbuf( QChar*, uint );
    void	ts_putc(int);
    void	ts_putc(QChar);
    bool	ts_isspace(QChar);
    bool	ts_isdigit(QChar);
    ulong	input_bin();
    ulong	input_oct();
    ulong	input_dec();
    ulong	input_hex();
    double	input_double();
    Q3TextStream &writeBlock( const char* p, uint len );
    Q3TextStream &writeBlock( const QChar* p, uint len );

private:	// Disabled copy constructor and operator=
#if defined(Q_DISABLE_COPY)
    Q3TextStream( const Q3TextStream & );
    Q3TextStream &operator=( const Q3TextStream & );
#endif
};

/*****************************************************************************
  Q3TextStream inline functions
 *****************************************************************************/

inline QIODevice *Q3TextStream::device() const
{ return dev; }

inline bool Q3TextStream::atEnd() const
{ return dev ? dev->atEnd() : FALSE; }

inline bool Q3TextStream::eof() const
{ return atEnd(); }

inline int Q3TextStream::flags() const
{ return fflags; }

inline int Q3TextStream::flags( int f )
{ int oldf = fflags;  fflags = f;  return oldf; }

inline int Q3TextStream::setf( int bits )
{ int oldf = fflags;  fflags |= bits;  return oldf; }

inline int Q3TextStream::setf( int bits, int mask )
{ int oldf = fflags;  fflags = (fflags & ~mask) | (bits & mask); return oldf; }

inline int Q3TextStream::unsetf( int bits )
{ int oldf = fflags;  fflags &= ~bits;	return oldf; }

inline int Q3TextStream::width() const
{ return fwidth; }

inline int Q3TextStream::width( int w )
{ int oldw = fwidth;  fwidth = w;  return oldw;	 }

inline int Q3TextStream::fill() const
{ return fillchar; }

inline int Q3TextStream::fill( int f )
{ int oldc = fillchar;	fillchar = f;  return oldc;  }

inline int Q3TextStream::precision() const
{ return fprec; }

inline int Q3TextStream::precision( int p )
{ int oldp = fprec;  fprec = p;	 return oldp;  }

/*!
  Returns one character from the stream, or EOF.
*/
inline QChar Q3TextStream::ts_getc()
{ QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); }

/*****************************************************************************
  Q3TextStream manipulators
 *****************************************************************************/

typedef Q3TextStream & (*Q3TSFUNC)(Q3TextStream &);// manipulator function
typedef int (Q3TextStream::*Q3TSMFI)(int);	// manipulator w/int argument

class Q_COMPAT_EXPORT Q3TSManip {			// text stream manipulator
public:
    Q3TSManip( Q3TSMFI m, int a ) { mf=m; arg=a; }
    void exec( Q3TextStream &s ) { (s.*mf)(arg); }
private:
    Q3TSMFI mf;					// Q3TextStream member function
    int	   arg;					// member function argument
};

Q_COMPAT_EXPORT inline Q3TextStream &operator>>( Q3TextStream &s, Q3TSFUNC f )
{ return (*f)( s ); }

Q_COMPAT_EXPORT inline Q3TextStream &operator<<( Q3TextStream &s, Q3TSFUNC f )
{ return (*f)( s ); }

Q_COMPAT_EXPORT inline Q3TextStream &operator<<( Q3TextStream &s, Q3TSManip m )
{ m.exec(s); return s; }

Q_COMPAT_EXPORT Q3TextStream &bin( Q3TextStream &s );	// set bin notation
Q_COMPAT_EXPORT Q3TextStream &oct( Q3TextStream &s );	// set oct notation
Q_COMPAT_EXPORT Q3TextStream &dec( Q3TextStream &s );	// set dec notation
Q_COMPAT_EXPORT Q3TextStream &hex( Q3TextStream &s );	// set hex notation
Q_COMPAT_EXPORT Q3TextStream &endl( Q3TextStream &s );	// insert EOL ('\n')
Q_COMPAT_EXPORT Q3TextStream &flush( Q3TextStream &s );	// flush output
Q_COMPAT_EXPORT Q3TextStream &ws( Q3TextStream &s );	// eat whitespace on input
Q_COMPAT_EXPORT Q3TextStream &reset( Q3TextStream &s );	// set default flags

QT_END_NAMESPACE

QT_END_HEADER

#endif // Q3TEXTSTREAM_H