home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / STREAMBF.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.4 KB  |  104 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - streambf.cpp
  3.  * C++ stream I/O functions for handling streambufs
  4.  *-----------------------------------------------------------------------*/
  5.  
  6. /*[]------------------------------------------------------------[]*/
  7. /*|                                                              |*/
  8. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  9. /*|                                                              |*/
  10. /*|                                                              |*/
  11. /*|     Copyright (c) 1990 by Borland International              |*/
  12. /*|     All Rights Reserved.                                     |*/
  13. /*|                                                              |*/
  14. /*[]------------------------------------------------------------[]*/
  15.  
  16. #include <stream.h>
  17.  
  18.  
  19. // destructor (virtual)
  20. _Cdecl streambuf::~streambuf()
  21. {
  22.     if( didalloc ) {
  23.     delete base;
  24.     didalloc = 0;
  25.     }
  26. }
  27.  
  28.  
  29. // default overflow: do nothing
  30. #pragma argsused
  31. int _Cdecl streambuf::overflow( int c)
  32. {
  33.     return EOF;
  34. }
  35.  
  36.  
  37. // default underflow: do nothing
  38. int _Cdecl streambuf::underflow()
  39. {
  40.     return EOF;
  41. }
  42.  
  43.  
  44. // allocate a standard size buffer
  45. int _Cdecl streambuf::allocate()
  46. {
  47.     int ret = 0;
  48.     if( ! didalloc ) {        // no action if we did a previous alloc
  49.     char *p = new char[BUFSIZ];
  50.  
  51.     if( p == 0 ) ret = EOF;    // no change if we can't allocate space
  52.     else {
  53.         pptr = p;
  54.         gptr = p;
  55.         base = p;
  56.         eptr = p + BUFSIZ;
  57.         didalloc = 1;
  58.     }
  59.     }
  60.     return ret;
  61. }
  62.  
  63.  
  64. // specify a buffer to use, possibly with data in it (offset > 0 )
  65. streambuf * _Cdecl streambuf::setbuf( char *buf, int len, unsigned offset )
  66. {
  67.     if( didalloc ) {
  68.     delete base;
  69.     didalloc = 0;
  70.     }
  71.     base = buf;
  72.     gptr = buf;
  73.     pptr = buf + (offset < len ? offset : len );
  74.     eptr = buf + len;
  75.     return this;
  76. }
  77.  
  78.  
  79. // return a char to input buffer
  80. void _Cdecl streambuf::sputbackc( char c )
  81. {
  82.     if( c != EOF )
  83.     if( gptr > base ) *--gptr = c;
  84. }
  85.  
  86.  
  87. int _Cdecl streambuf::snextc()
  88. {
  89.     return (gptr >= pptr) ? underflow() : (*(gptr++) & 0x7f);
  90. }
  91.  
  92.  
  93. int _Cdecl streambuf::sputc( int c )
  94. {
  95.     return (pptr >= eptr) ? overflow(c) : (*(pptr++) = c);
  96. }
  97.  
  98.  
  99. // add a null byte without advancing pointer
  100. void _Cdecl streambuf::terminate( )
  101. {
  102.     if( pptr < eptr ) *pptr = 0;
  103. }
  104.