src/qt3support/text/q3textstream.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef Q3TEXTSTREAM_H
       
    43 #define Q3TEXTSTREAM_H
       
    44 
       
    45 #include <QtCore/qiodevice.h>
       
    46 #include <QtCore/qstring.h>
       
    47 #ifndef QT_NO_TEXTCODEC
       
    48 #include <QtCore/qtextcodec.h>
       
    49 #endif
       
    50 #include <Qt3Support/q3cstring.h>
       
    51 
       
    52 #include <stdio.h>
       
    53 
       
    54 QT_BEGIN_HEADER
       
    55 
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 QT_MODULE(Qt3SupportLight)
       
    59 
       
    60 class Q3TextStreamPrivate;
       
    61 
       
    62 class Q_COMPAT_EXPORT Q3TextStream				// text stream class
       
    63 {
       
    64 public:
       
    65     enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder,
       
    66 		    UnicodeReverse, RawUnicode, UnicodeUTF8 };
       
    67 
       
    68     void	setEncoding( Encoding );
       
    69 #ifndef QT_NO_TEXTCODEC
       
    70     void	setCodec( QTextCodec* );
       
    71     QTextCodec *codec();
       
    72 #endif
       
    73 
       
    74     Q3TextStream();
       
    75     Q3TextStream( QIODevice * );
       
    76     Q3TextStream( QString*, int mode );
       
    77     Q3TextStream( QString&, int mode );		// obsolete
       
    78     Q3TextStream( QByteArray&, int mode );
       
    79     Q3TextStream( FILE *, int mode );
       
    80     virtual ~Q3TextStream();
       
    81 
       
    82     QIODevice	*device() const;
       
    83     void	 setDevice( QIODevice * );
       
    84     void	 unsetDevice();
       
    85 
       
    86     bool	 atEnd() const;
       
    87     bool	 eof() const;
       
    88 
       
    89     Q3TextStream &operator>>( QChar & );
       
    90     Q3TextStream &operator>>( char & );
       
    91     Q3TextStream &operator>>( signed short & );
       
    92     Q3TextStream &operator>>( unsigned short & );
       
    93     Q3TextStream &operator>>( signed int & );
       
    94     Q3TextStream &operator>>( unsigned int & );
       
    95     Q3TextStream &operator>>( signed long & );
       
    96     Q3TextStream &operator>>( unsigned long & );
       
    97     Q3TextStream &operator>>( float & );
       
    98     Q3TextStream &operator>>( double & );
       
    99     Q3TextStream &operator>>( char * );
       
   100     Q3TextStream &operator>>( QString & );
       
   101     Q3TextStream &operator>>( Q3CString & );
       
   102 
       
   103     Q3TextStream &operator<<( QChar );
       
   104     Q3TextStream &operator<<( char );
       
   105     Q3TextStream &operator<<( signed short );
       
   106     Q3TextStream &operator<<( unsigned short );
       
   107     Q3TextStream &operator<<( signed int );
       
   108     Q3TextStream &operator<<( unsigned int );
       
   109     Q3TextStream &operator<<( signed long );
       
   110     Q3TextStream &operator<<( unsigned long );
       
   111     Q3TextStream &operator<<( float );
       
   112     Q3TextStream &operator<<( double );
       
   113     Q3TextStream &operator<<( const char* );
       
   114     Q3TextStream &operator<<( const QString & );
       
   115     Q3TextStream &operator<<( const Q3CString & );
       
   116     Q3TextStream &operator<<( void * );		// any pointer
       
   117 
       
   118     Q3TextStream &readRawBytes( char *, uint len );
       
   119     Q3TextStream &writeRawBytes( const char* , uint len );
       
   120 
       
   121     QString	readLine();
       
   122     QString	read();
       
   123     void	skipWhiteSpace();
       
   124 
       
   125     enum {
       
   126 	skipws	  = 0x0001,			// skip whitespace on input
       
   127 	left	  = 0x0002,			// left-adjust output
       
   128 	right	  = 0x0004,			// right-adjust output
       
   129 	internal  = 0x0008,			// pad after sign
       
   130 	bin	  = 0x0010,			// binary format integer
       
   131 	oct	  = 0x0020,			// octal format integer
       
   132 	dec	  = 0x0040,			// decimal format integer
       
   133 	hex	  = 0x0080,			// hex format integer
       
   134 	showbase  = 0x0100,			// show base indicator
       
   135 	showpoint = 0x0200,			// force decimal point (float)
       
   136 	uppercase = 0x0400,			// upper-case hex output
       
   137 	showpos	  = 0x0800,			// add '+' to positive integers
       
   138 	scientific= 0x1000,			// scientific float output
       
   139 	fixed	  = 0x2000			// fixed float output
       
   140     };
       
   141 
       
   142     static const int basefield;			// bin | oct | dec | hex
       
   143     static const int adjustfield;		// left | right | internal
       
   144     static const int floatfield;		// scientific | fixed
       
   145 
       
   146     int	  flags() const;
       
   147     int	  flags( int f );
       
   148     int	  setf( int bits );
       
   149     int	  setf( int bits, int mask );
       
   150     int	  unsetf( int bits );
       
   151 
       
   152     void  reset();
       
   153 
       
   154     int	  width()	const;
       
   155     int	  width( int );
       
   156     int	  fill()	const;
       
   157     int	  fill( int );
       
   158     int	  precision()	const;
       
   159     int	  precision( int );
       
   160 
       
   161 private:
       
   162     long	input_int();
       
   163     void	init();
       
   164     Q3TextStream &output_int( int, ulong, bool );
       
   165     QIODevice	*dev;
       
   166 
       
   167     int		fflags;
       
   168     int		fwidth;
       
   169     int		fillchar;
       
   170     int		fprec;
       
   171     bool	doUnicodeHeader;
       
   172     bool	owndev;
       
   173     QTextCodec 	*mapper;
       
   174     QTextCodec::ConverterState mapperReadState;
       
   175     QTextCodec::ConverterState mapperWriteState;
       
   176     Q3TextStreamPrivate * d;
       
   177     QChar	unused1; // ### remove in Qt 4.0
       
   178     bool	latin1;
       
   179     bool 	internalOrder;
       
   180     bool	networkOrder;
       
   181     void	*unused2; // ### remove in Qt 4.0
       
   182 
       
   183     QChar	eat_ws();
       
   184     uint 	ts_getline( QChar* );
       
   185     void	ts_ungetc( QChar );
       
   186     QChar	ts_getc();
       
   187     uint	ts_getbuf( QChar*, uint );
       
   188     void	ts_putc(int);
       
   189     void	ts_putc(QChar);
       
   190     bool	ts_isspace(QChar);
       
   191     bool	ts_isdigit(QChar);
       
   192     ulong	input_bin();
       
   193     ulong	input_oct();
       
   194     ulong	input_dec();
       
   195     ulong	input_hex();
       
   196     double	input_double();
       
   197     Q3TextStream &writeBlock( const char* p, uint len );
       
   198     Q3TextStream &writeBlock( const QChar* p, uint len );
       
   199 
       
   200 private:	// Disabled copy constructor and operator=
       
   201 #if defined(Q_DISABLE_COPY)
       
   202     Q3TextStream( const Q3TextStream & );
       
   203     Q3TextStream &operator=( const Q3TextStream & );
       
   204 #endif
       
   205 };
       
   206 
       
   207 /*****************************************************************************
       
   208   Q3TextStream inline functions
       
   209  *****************************************************************************/
       
   210 
       
   211 inline QIODevice *Q3TextStream::device() const
       
   212 { return dev; }
       
   213 
       
   214 inline bool Q3TextStream::atEnd() const
       
   215 { return dev ? dev->atEnd() : FALSE; }
       
   216 
       
   217 inline bool Q3TextStream::eof() const
       
   218 { return atEnd(); }
       
   219 
       
   220 inline int Q3TextStream::flags() const
       
   221 { return fflags; }
       
   222 
       
   223 inline int Q3TextStream::flags( int f )
       
   224 { int oldf = fflags;  fflags = f;  return oldf; }
       
   225 
       
   226 inline int Q3TextStream::setf( int bits )
       
   227 { int oldf = fflags;  fflags |= bits;  return oldf; }
       
   228 
       
   229 inline int Q3TextStream::setf( int bits, int mask )
       
   230 { int oldf = fflags;  fflags = (fflags & ~mask) | (bits & mask); return oldf; }
       
   231 
       
   232 inline int Q3TextStream::unsetf( int bits )
       
   233 { int oldf = fflags;  fflags &= ~bits;	return oldf; }
       
   234 
       
   235 inline int Q3TextStream::width() const
       
   236 { return fwidth; }
       
   237 
       
   238 inline int Q3TextStream::width( int w )
       
   239 { int oldw = fwidth;  fwidth = w;  return oldw;	 }
       
   240 
       
   241 inline int Q3TextStream::fill() const
       
   242 { return fillchar; }
       
   243 
       
   244 inline int Q3TextStream::fill( int f )
       
   245 { int oldc = fillchar;	fillchar = f;  return oldc;  }
       
   246 
       
   247 inline int Q3TextStream::precision() const
       
   248 { return fprec; }
       
   249 
       
   250 inline int Q3TextStream::precision( int p )
       
   251 { int oldp = fprec;  fprec = p;	 return oldp;  }
       
   252 
       
   253 /*!
       
   254   Returns one character from the stream, or EOF.
       
   255 */
       
   256 inline QChar Q3TextStream::ts_getc()
       
   257 { QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); }
       
   258 
       
   259 /*****************************************************************************
       
   260   Q3TextStream manipulators
       
   261  *****************************************************************************/
       
   262 
       
   263 typedef Q3TextStream & (*Q3TSFUNC)(Q3TextStream &);// manipulator function
       
   264 typedef int (Q3TextStream::*Q3TSMFI)(int);	// manipulator w/int argument
       
   265 
       
   266 class Q_COMPAT_EXPORT Q3TSManip {			// text stream manipulator
       
   267 public:
       
   268     Q3TSManip( Q3TSMFI m, int a ) { mf=m; arg=a; }
       
   269     void exec( Q3TextStream &s ) { (s.*mf)(arg); }
       
   270 private:
       
   271     Q3TSMFI mf;					// Q3TextStream member function
       
   272     int	   arg;					// member function argument
       
   273 };
       
   274 
       
   275 Q_COMPAT_EXPORT inline Q3TextStream &operator>>( Q3TextStream &s, Q3TSFUNC f )
       
   276 { return (*f)( s ); }
       
   277 
       
   278 Q_COMPAT_EXPORT inline Q3TextStream &operator<<( Q3TextStream &s, Q3TSFUNC f )
       
   279 { return (*f)( s ); }
       
   280 
       
   281 Q_COMPAT_EXPORT inline Q3TextStream &operator<<( Q3TextStream &s, Q3TSManip m )
       
   282 { m.exec(s); return s; }
       
   283 
       
   284 Q_COMPAT_EXPORT Q3TextStream &bin( Q3TextStream &s );	// set bin notation
       
   285 Q_COMPAT_EXPORT Q3TextStream &oct( Q3TextStream &s );	// set oct notation
       
   286 Q_COMPAT_EXPORT Q3TextStream &dec( Q3TextStream &s );	// set dec notation
       
   287 Q_COMPAT_EXPORT Q3TextStream &hex( Q3TextStream &s );	// set hex notation
       
   288 Q_COMPAT_EXPORT Q3TextStream &endl( Q3TextStream &s );	// insert EOL ('\n')
       
   289 Q_COMPAT_EXPORT Q3TextStream &flush( Q3TextStream &s );	// flush output
       
   290 Q_COMPAT_EXPORT Q3TextStream &ws( Q3TextStream &s );	// eat whitespace on input
       
   291 Q_COMPAT_EXPORT Q3TextStream &reset( Q3TextStream &s );	// set default flags
       
   292 
       
   293 QT_END_NAMESPACE
       
   294 
       
   295 QT_END_HEADER
       
   296 
       
   297 #endif // Q3TEXTSTREAM_H