home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / INCLUDE.ZIP / STREAM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  8.7 KB  |  316 lines

  1. /* C++ Stream I/O header file
  2.  
  3.     Copyright (c) Borland International 1987,1988,1990
  4.     All Rights Reserved.
  5.  
  6.     %W%  %G% %U%
  7.  
  8.     NOTE: The data and function variables are declared so that any
  9.     combination of alignment and size-of-enum compiler options will
  10.     produce the same results.
  11. */
  12.  
  13. #if ! defined(_I_STREAM_H_)
  14. // protect against multiple inclusion
  15. #define _I_STREAM_H_
  16. #include <stdio.h>
  17.  
  18. /*
  19.  * whitespace
  20.  */
  21. struct whitespace { char x; };
  22. extern struct whitespace WS;
  23.  
  24. class istream;
  25. class ostream;
  26.  
  27. /*
  28.  * basic stream buffer class
  29.  */
  30. class streambuf {
  31.   friend istream;
  32.   friend ostream;
  33.  
  34.   protected:
  35.     FILE *file;            // if non-0, connected stdio file
  36.     char *base;            // start of buffer
  37.     char *pptr;            // next free char (for output)
  38.     char *gptr;            // next avail char (for input)
  39.     char *eptr;            // one off end of buffer
  40.     int didalloc;        // non-0 if we allocated a buffer
  41.  
  42.     // empty full output buffer, put c in it
  43.     virtual int _Cdecl overflow( int = EOF );
  44.  
  45.     // fill empty input buffer, get first char
  46.     virtual int _Cdecl underflow();
  47.  
  48.     // allocate a buffer
  49.     int _Cdecl allocate();
  50.  
  51.     // specify a buffer to use, possibly with data in it (offset > 0 )
  52.     streambuf * _Cdecl setbuf( char *, int, unsigned = 0 );
  53.  
  54.     // add a null byte to a string without advancing pointer
  55.     virtual void _Cdecl terminate();
  56.  
  57.   public:
  58.     // constructor: no buffer
  59.     _Cdecl streambuf()
  60.     { file = 0; didalloc = 0; base = pptr = gptr = eptr = 0; }
  61.  
  62.     // constructor: user buffer
  63.     _Cdecl streambuf( char *buf, int len )
  64.     { file = 0; didalloc = 0; setbuf(buf, len); }
  65.  
  66.     // destructor
  67.     virtual _Cdecl ~streambuf();
  68.  
  69.     // get next char from input buffer
  70.     virtual int _Cdecl snextc();
  71.  
  72.     // write a char to output buffer
  73.     virtual int _Cdecl sputc( int );
  74.  
  75.     // return a char to input buffer
  76.     virtual void _Cdecl sputbackc( char );
  77. };
  78.  
  79.  
  80. /*
  81.  * file buffer class
  82.  */
  83. enum open_mode { input, output, append }; // mode for opening a file
  84.  
  85. class filebuf: public streambuf {
  86.  
  87.   private:
  88.     int fd;            // external file id
  89.     int isopen;            // non-0 if open
  90.  
  91.   protected:
  92.  
  93.     // we don't need to add a null byte to a file
  94.     void _Cdecl terminate() { };
  95.  
  96.   public:
  97.     _Cdecl filebuf() { isopen = 0; fd = 0; } // basic constructor
  98.     _Cdecl filebuf( FILE * );        // tie to existing stdio file
  99.     _Cdecl filebuf( int );        // tie to existing file
  100.     _Cdecl filebuf( int , char *, int ); // tie to file, user buffer
  101.     _Cdecl ~filebuf() { close(); }    // destructor (virtual)
  102.  
  103.     // open a named external file
  104.     filebuf * _Cdecl open( char *, int );
  105.  
  106.     // close the external file
  107.     int _Cdecl close();
  108.  
  109.     // get next char from input buffer
  110.     int _Cdecl snextc() { return ::fgetc(file); }
  111.  
  112.     // write a char to output buffer
  113.     int _Cdecl sputc( int _C ) { return ::fputc(_C, file); }
  114.  
  115.     // return a char to input buffer
  116.     void _Cdecl sputbackc( char _C ) { (void) ::ungetc(_C, file); }
  117. };
  118.  
  119.  
  120. /*
  121.  * formatting functions
  122.  */
  123. char * _Cdecl dec( long, int = 0 );    // internal to decimal text
  124. char * _Cdecl hex( long, int = 0 );    // internal to hex text
  125. char * _Cdecl oct( long, int = 0 );    // internal to octal text
  126.  
  127. char * _Cdecl chr( int, int = 0 );    // char to string
  128. char * _Cdecl str( const char *, int = 0 ); // make fixed-width string
  129. char * _Cdecl form( char * ... );    // general formating
  130.  
  131.  
  132. /*
  133.  * state of istream or ostream
  134.  */
  135. enum stream_state { _good, _eof, _fail, _bad };
  136.  
  137. /*
  138.  * class ostream
  139.  */
  140. class ostream {
  141.   friend istream;
  142.  
  143.   private:
  144.     streambuf *stream;            // connected streambuf or filebuf
  145.     char mystream;            // non-0 if stream was alloc'd here
  146.     char state;                // state of stream
  147.  
  148.   public:
  149.     _Cdecl ostream( streambuf * );        // connect to existing streambuf
  150.     _Cdecl ostream( int );            // connect to open file
  151.     _Cdecl ostream( int, char * );        // connect to char array
  152.     _Cdecl ~ostream() {            // destructor
  153.     (void) flush();
  154.     if( mystream ) delete stream;
  155.     }
  156.  
  157.     // test the stream
  158.     _Cdecl operator void*() { return (state==_good) ? this : 0; }
  159.     int _Cdecl operator !() { return (state != _good); }
  160.  
  161.     ostream& _Cdecl operator << ( int );        // write a char, uns char, short, or int
  162.     ostream& _Cdecl operator << ( long );        // write a long
  163.     ostream& _Cdecl operator << ( unsigned int _U ) {    // write uns short, uns int
  164.     return *this << ((long) _U);
  165.     }
  166.     ostream& _Cdecl operator << ( unsigned long );    // write unsigned long
  167.     ostream& _Cdecl operator << ( const char * );    // write a string
  168.     ostream& _Cdecl operator << ( double );        // write floating-point
  169.     ostream& _Cdecl operator << ( long double );    // write floating-point
  170.  
  171.     ostream& _Cdecl put( char _C ) {        // write char as a char
  172.     if( stream->sputc(_C) == EOF )
  173.         state = _fail;
  174.     return *this;
  175.     }
  176.  
  177.     ostream& _Cdecl flush();            // flush output buffer
  178.  
  179.     // return stream state
  180.     int _Cdecl rdstate() { return state; }
  181.  
  182.     // set stream state
  183.     void _Cdecl clear( int _S = _good ) { state = _S; }
  184.  
  185.     // non-0 if bad
  186.     int _Cdecl bad() { return (state >= _bad); }
  187.  
  188.     // non-0 if bad or failed
  189.     int _Cdecl fail() { return (state >= _fail); }
  190.  
  191.     // non-0 if end of input
  192.     int _Cdecl eof() { return (state == _eof); }
  193.  
  194.     // non-0 if ok
  195.     int _Cdecl good() { return (state == _good); }
  196. };
  197.  
  198.  
  199. /*
  200.  * class istream
  201.  */
  202. class istream {
  203.   friend ostream;
  204.   friend void _Cdecl eatwhite( istream & _S ) { _S >> WS; }
  205.  
  206.   private:
  207.     ostream *tied_to;            // tied ostream, if non-0
  208.     streambuf *stream;            // connected streambuf or filebuf
  209.     char skipping;            // non-0 if skipping whitespace
  210.     char mystream;            // non-0 if stream was alloc'd here
  211.     int state;                // state of stream
  212.  
  213.     void _Cdecl checkskip( int&, int& );    // used internally
  214.     long _Cdecl get_long( int );        // used internally
  215.  
  216.   public:
  217.     // connect to streambuf or filebuf
  218.     _Cdecl istream( streambuf *, int = 1, ostream * = 0 );
  219.     // connect to string
  220.     _Cdecl istream( int, char *, int = 1 );
  221.     // connect to open file
  222.     _Cdecl istream( int, int = 1, ostream * = 0 );
  223.     // destructor
  224.     _Cdecl ~istream() {
  225.     if( mystream ) delete stream;
  226.     }
  227.  
  228.     // test the stream
  229.     _Cdecl operator void*() { return (state==_good) ? this : 0; }
  230.     int _Cdecl operator !() { return (state != _good); }
  231.  
  232.     /*
  233.      * input functions
  234.      */
  235.     istream& _Cdecl operator >> ( signed char& _C ) {
  236.     unsigned char _X;
  237.     *this >> _X;
  238.     if( state == _good ) _C = (signed char) _X;
  239.     return *this;
  240.     }
  241.     istream& _Cdecl operator >> ( short& _S ) {
  242.     short _T = (short) get_long(0);
  243.     if( state == _good ) _S = _T;
  244.     return *this;
  245.     }
  246.     istream& _Cdecl operator >> ( int& _I ) {
  247.     int _T = (int) get_long(0);
  248.     if( state == _good ) _I = _T;
  249.     return *this;
  250.     }
  251.     istream& _Cdecl operator >> ( long& _L ) {
  252.     long _T = get_long(0);
  253.     if( state == _good ) _L = _T;
  254.     return *this;
  255.     }
  256.     istream& _Cdecl operator >> ( unsigned char& );
  257.     istream& _Cdecl operator >> ( unsigned short& _S ) {
  258.     unsigned short _T = (unsigned short) get_long(1);
  259.     if( state == _good ) _S = _T;
  260.     return *this;
  261.     }
  262.     istream& _Cdecl operator >> ( unsigned int& _I ) {
  263.     unsigned int _T = (unsigned int) get_long(1);
  264.     if( state == _good ) _I = _T;
  265.     return *this;
  266.     }
  267.     istream& _Cdecl operator >> ( unsigned long& _L ) {
  268.     unsigned long _T = (unsigned long) get_long(1);
  269.     if( state == _good ) _L = _T;
  270.     return *this;
  271.     }
  272.     istream& _Cdecl operator >> ( float& );
  273.     istream& _Cdecl operator >> ( double& );
  274.     istream& _Cdecl operator >> ( long double& );
  275.     istream& _Cdecl operator >> ( char * );
  276.     istream& _Cdecl operator >> ( whitespace& );
  277.     istream& _Cdecl get(char& );
  278.     istream& _Cdecl get(char *, int, int = '\n' );
  279.  
  280.     // set skipping state
  281.     int _Cdecl skip( int _S ) {
  282.     short _T = skipping; skipping = _S; return _T;
  283.     }
  284.  
  285.     // tie to ostream
  286.     ostream * _Cdecl tie( ostream *_To ) {
  287.     ostream *_T = tied_to; tied_to = _To; return _T;
  288.     }
  289.  
  290.     // return a character to input
  291.     void _Cdecl putback( char _C ) { stream->sputbackc(_C); };
  292.  
  293.     // return stream state
  294.     int _Cdecl rdstate() { return state; }
  295.  
  296.     // set stream state
  297.     void _Cdecl clear( int _S = _good ) { state = _S; }
  298.  
  299.     // non-0 if bad
  300.     int _Cdecl bad() { return (state >= _bad); }
  301.  
  302.     // non-0 if bad or failed
  303.     int _Cdecl fail() { return (state >= _fail); }
  304.  
  305.     // non-0 if end of input
  306.     int _Cdecl eof() { return (state == _eof); }
  307.  
  308.     // non-0 if ok
  309.     int _Cdecl good() { return (state == _good); }
  310. };
  311.  
  312. extern istream _Cdecl cin;
  313. extern ostream _Cdecl cout;
  314. extern ostream _Cdecl cerr;
  315. #endif
  316.