home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 2.ddi / INCLUDE.ZIP / STREAM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  9.6 KB  |  335 lines

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