home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qtextstream.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  10KB  |  328 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qtextstream.h   3.0.0   edited Oct 5 17:52 $
  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. #ifndef QT_NO_COMPAT    
  159.     bool    isNetworkOrder() { return internalOrder == FALSE; }
  160. #endif
  161.  
  162.     int         fflags;
  163.     int         fwidth;
  164.     int         fillchar;
  165.     int         fprec;
  166.     bool     fstrm;
  167.     bool     owndev;
  168.     QTextCodec     *mapper;
  169.     QTextStreamPrivate * d;
  170.     QChar    ungetcBuf;
  171.     bool    latin1;
  172.     bool     internalOrder;
  173.     bool    doUnicodeHeader;
  174.     void    *reserved_ptr;
  175.  
  176.     QChar    eat_ws();
  177.     uint     ts_getline( QChar* );
  178.     void    ts_ungetc( QChar );
  179.     QChar    ts_getc();
  180.     uint    ts_getbuf( QChar*, uint );
  181.     void    ts_putc(int);
  182.     void    ts_putc(QChar);
  183.     bool    ts_isspace(QChar);
  184.     bool    ts_isdigit(QChar);
  185.     ulong    input_bin();
  186.     ulong    input_oct();
  187.     ulong    input_dec();
  188.     ulong    input_hex();
  189.     double    input_double();
  190.     QTextStream &writeBlock( const char* p, uint len );
  191.     QTextStream &writeBlock( const QChar* p, uint len );
  192.  
  193. private:    // Disabled copy constructor and operator=
  194. #if defined(Q_DISABLE_COPY)
  195.     QTextStream( const QTextStream & );
  196.     QTextStream &operator=( const QTextStream & );
  197. #endif
  198. };
  199.  
  200. typedef QTextStream QTS;
  201.  
  202. class Q_EXPORT QTextIStream : public QTextStream {
  203. public:
  204.     QTextIStream( const QString* s ) :
  205.     QTextStream((QString*)s,IO_ReadOnly) { }
  206.     QTextIStream( QByteArray ba ) :
  207.     QTextStream(ba,IO_ReadOnly) { }
  208.     QTextIStream( FILE *f ) :
  209.     QTextStream(f,IO_ReadOnly) { }
  210. };
  211.  
  212. class Q_EXPORT QTextOStream : public QTextStream {
  213. public:
  214.     QTextOStream( QString* s ) :
  215.     QTextStream(s,IO_WriteOnly) { }
  216.     QTextOStream( QByteArray ba ) :
  217.     QTextStream(ba,IO_WriteOnly) { }
  218.     QTextOStream( FILE *f ) :
  219.     QTextStream(f,IO_WriteOnly) { }
  220. };
  221.  
  222. /*****************************************************************************
  223.   QTextStream inline functions
  224.  *****************************************************************************/
  225.  
  226. inline QIODevice *QTextStream::device() const
  227. { return dev; }
  228.  
  229. inline bool QTextStream::atEnd() const
  230. { return dev ? dev->atEnd() : FALSE; }
  231.  
  232. inline bool QTextStream::eof() const
  233. { return atEnd(); }
  234.  
  235. inline int QTextStream::flags() const
  236. { return fflags; }
  237.  
  238. inline int QTextStream::flags( int f )
  239. { int oldf = fflags;  fflags = f;  return oldf; }
  240.  
  241. inline int QTextStream::setf( int bits )
  242. { int oldf = fflags;  fflags |= bits;  return oldf; }
  243.  
  244. inline int QTextStream::setf( int bits, int mask )
  245. { int oldf = fflags;  fflags = (fflags & ~mask) | (bits & mask); return oldf; }
  246.  
  247. inline int QTextStream::unsetf( int bits )
  248. { int oldf = fflags;  fflags &= ~bits;    return oldf; }
  249.  
  250. inline int QTextStream::width() const
  251. { return fwidth; }
  252.  
  253. inline int QTextStream::width( int w )
  254. { int oldw = fwidth;  fwidth = w;  return oldw;     }
  255.  
  256. inline int QTextStream::fill() const
  257. { return fillchar; }
  258.  
  259. inline int QTextStream::fill( int f )
  260. { int oldc = fillchar;    fillchar = f;  return oldc;  }
  261.  
  262. inline int QTextStream::precision() const
  263. { return fprec; }
  264.  
  265. inline int QTextStream::precision( int p )
  266. { int oldp = fprec;  fprec = p;     return oldp;  }
  267.  
  268. /*!
  269.   Returns one character from the stream, or EOF.
  270. */
  271. inline QChar QTextStream::ts_getc()
  272. { QChar r; return ( ts_getbuf( &r,1 ) == 1 ? r : QChar((ushort)0xffff) ); }
  273.  
  274. /*****************************************************************************
  275.   QTextStream manipulators
  276.  *****************************************************************************/
  277.  
  278. typedef QTextStream & (*QTSFUNC)(QTextStream &);// manipulator function
  279. typedef int (QTextStream::*QTSMFI)(int);    // manipulator w/int argument
  280.  
  281. class Q_EXPORT QTSManip {            // text stream manipulator
  282. public:
  283.     QTSManip( QTSMFI m, int a ) { mf=m; arg=a; }
  284.     void exec( QTextStream &s ) { (s.*mf)(arg); }
  285. private:
  286.     QTSMFI mf;                    // QTextStream member function
  287.     int       arg;                    // member function argument
  288. };
  289.  
  290. Q_EXPORT inline QTextStream &operator>>( QTextStream &s, QTSFUNC f )
  291. { return (*f)( s ); }
  292.  
  293. Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSFUNC f )
  294. { return (*f)( s ); }
  295.  
  296. Q_EXPORT inline QTextStream &operator<<( QTextStream &s, QTSManip m )
  297. { m.exec(s); return s; }
  298.  
  299. Q_EXPORT QTextStream &bin( QTextStream &s );    // set bin notation
  300. Q_EXPORT QTextStream &oct( QTextStream &s );    // set oct notation
  301. Q_EXPORT QTextStream &dec( QTextStream &s );    // set dec notation
  302. Q_EXPORT QTextStream &hex( QTextStream &s );    // set hex notation
  303. Q_EXPORT QTextStream &endl( QTextStream &s );    // insert EOL ('\n')
  304. Q_EXPORT QTextStream &flush( QTextStream &s );    // flush output
  305. Q_EXPORT QTextStream &ws( QTextStream &s );    // eat whitespace on input
  306. Q_EXPORT QTextStream &reset( QTextStream &s );    // set default flags
  307.  
  308. Q_EXPORT inline QTSManip qSetW( int w )
  309. {
  310.     QTSMFI func = &QTextStream::width;
  311.     return QTSManip(func,w);
  312. }
  313.  
  314. Q_EXPORT inline QTSManip qSetFill( int f )
  315. {
  316.     QTSMFI func = &QTextStream::fill;
  317.     return QTSManip(func,f);
  318. }
  319.  
  320. Q_EXPORT inline QTSManip qSetPrecision( int p )
  321. {
  322.     QTSMFI func = &QTextStream::precision;
  323.     return QTSManip(func,p);
  324. }
  325.  
  326. #endif // QT_NO_TEXTSTREAM
  327. #endif // QTEXTSTREAM_H
  328.