home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / h.z / STREAMBU.H < prev    next >
C/C++ Source or Header  |  1996-07-24  |  8KB  |  292 lines

  1. //
  2. //  streambu.h    Stream buffer
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //
  6. #ifndef _STREAMBUF_H_INCLUDED
  7. #define _STREAMBUF_H_INCLUDED
  8.  
  9. #ifndef __cplusplus
  10. #error streambu.h is for use with C++
  11. #endif
  12.  
  13. #ifndef _IOSTREAM_H_INCLUDED
  14. #error streambu.h should only be included by iostream.h
  15. #endif
  16.  
  17. #ifndef _COMDEF_H_INCLUDED
  18.  #include <_comdef.h>
  19. #endif
  20. #ifndef _STDDEF_H_INCLUDED
  21.  #include <stddef.h>
  22. #endif
  23. #ifndef _STRING_H_INCLUDED
  24.  #include <string.h>
  25. #endif
  26.  
  27. // For multi-thread support.
  28. #ifndef __lock_it
  29.  
  30. class __lock;
  31.  
  32. #ifdef __SW_BM
  33. class _WPRTLINK __get_lock {
  34. public:
  35.     __get_lock( __lock * );
  36.     ~__get_lock();
  37. private:
  38.     __lock *__lk;
  39. };
  40. #define __lock_it( __l ) __get_lock __lock_name( __LINE__ )( __l )
  41. #define __lock_name( __ln ) __lock_glue( __lock__, __ln )
  42. #define __lock_glue( __pre, __lin ) __pre ## __lin
  43. #else 
  44. #define __lock_it( __l )
  45. #endif
  46. #endif
  47.  
  48. // **************************** STREAMBUF ************************************
  49. #if defined(_M_IX86)
  50.   #pragma pack(__push,1);
  51. #else
  52.   #pragma pack(__push,8);
  53. #endif
  54. class _WPRTLINK streambuf {
  55. public:
  56.     int  in_avail() const;
  57.     int  out_waiting() const;
  58.     int  snextc();
  59.     int  sgetn( char *__buf, int __len );
  60.     int  speekc();
  61.     int  sgetc();
  62.     int  sgetchar();
  63.     int  sbumpc();
  64.     void stossc();
  65.     int  sputbackc( char __c );
  66.     int  sputc( int __c );
  67.     int  sputn( char const *__buf, int __len );
  68.     void dbp();
  69.  
  70.     virtual int        do_sgetn( char *__buf, int __len );
  71.     virtual int        do_sputn( char const *__buf, int __len );
  72.     virtual int        pbackfail( int __ignored );
  73.     virtual int        overflow( int = EOF ) = 0;
  74.     virtual int        underflow() = 0;
  75.     virtual streambuf *setbuf( char *__buf, int __len );
  76.     virtual streampos  seekoff( streamoff     __ignored1,
  77.                                 ios::seekdir  __ignored2,
  78.                                 ios::openmode __ignored3 = ios::in | ios::out );
  79.     virtual streampos  seekpos( streampos     __position,
  80.                                 ios::openmode __mode = ios::in | ios::out );
  81.     virtual int        sync();
  82.  
  83.     __lock *__b_lock;        // streambuf data member operations
  84.  
  85. protected:
  86.     streambuf();
  87.     streambuf( char *__buf, int __len );
  88.     virtual ~streambuf();
  89.  
  90.     int   allocate();
  91.     char *base() const;
  92.     char *ebuf() const;
  93.     int   blen() const;
  94.     void  setb( char *__buf, char *__endbuf, int __autodelete = 0 );
  95.     char *eback() const;
  96.     char *gptr() const;
  97.     char *egptr() const;
  98.     void  gbump( streamoff __offset );
  99.     void  setg( char *__eback, char *__gptr, char *__egptr );
  100.     char *pbase() const;
  101.     char *pptr() const;
  102.     char *epptr() const;
  103.     void  pbump( streamoff __offset );
  104.     void  setp( char *__pptr, char *__epptr );
  105.     int   unbuffered( int __unbuffered );
  106.     int   unbuffered() const;
  107.  
  108.     virtual int doallocate();
  109.  
  110. private:
  111.     // Declared but not defined, to prevent copying a streambuf.
  112.     streambuf( streambuf & );
  113.     void operator = ( streambuf & );
  114.  
  115.     char *__reserve_base;
  116.     char *__reserve_end;
  117.     char *__get_base;
  118.     char *__get_end;
  119.     char *__get_ptr;
  120.     char *__put_base;
  121.     char *__put_end;
  122.     char *__put_ptr;
  123.     unsigned  __unbuffered_state : 1;
  124.     unsigned  __delete_reserve   : 1;
  125. };
  126. #pragma pack(__pop);
  127.  
  128. // *********************** Reserve area inline functions *********************
  129.  
  130. inline char *streambuf::base() const {
  131.     return( __reserve_base );
  132. }
  133.  
  134. inline char *streambuf::ebuf() const {
  135.     return( __reserve_end );
  136. }
  137.  
  138. inline int streambuf::blen() const {
  139.     __lock_it( __b_lock );
  140.     return( (int)(__reserve_end - __reserve_base) );
  141. }
  142.  
  143. inline int streambuf::allocate() {
  144.     __lock_it( __b_lock );
  145.     return( (base() != NULL || unbuffered()) ? __NOT_EOF
  146.                                              : doallocate() );
  147. }
  148.  
  149. inline int streambuf::unbuffered() const {
  150.     return( __unbuffered_state );
  151. }
  152.  
  153. inline int streambuf::unbuffered( int __unbuffered ) {
  154.     __lock_it( __b_lock );
  155.     int __old_unbuffered = __unbuffered_state;
  156.     __unbuffered_state   = (char)(__unbuffered ? 1 : 0);
  157.     return( __old_unbuffered );
  158. }
  159.  
  160. // *********************** Get area inline functions *************************
  161.  
  162. inline char * streambuf::eback() const {
  163.     return( __get_base );
  164. }
  165.  
  166. inline char * streambuf::gptr() const {
  167.     return( __get_ptr );
  168. }
  169.  
  170. inline char * streambuf::egptr() const {
  171.     return( __get_end );
  172. }
  173.  
  174. inline void streambuf::gbump( streamoff __offset ) {
  175.     __lock_it( __b_lock );
  176.     __get_ptr += __offset;
  177. }
  178.  
  179. inline void streambuf::setg( char *__eback, char *__gptr, char *__egptr ) {
  180.     __lock_it( __b_lock );
  181.     __get_base = __eback;
  182.     __get_ptr  = __gptr;
  183.     __get_end  = __egptr;
  184. }
  185.  
  186. // *********************** Put area inline functions *************************
  187.  
  188. inline char * streambuf::pbase() const {
  189.     return( __put_base );
  190. }
  191.  
  192. inline char * streambuf::pptr() const {
  193.     return( __put_ptr );
  194. }
  195.  
  196. inline char * streambuf::epptr() const {
  197.     return( __put_end );
  198. }
  199.  
  200. inline void streambuf::pbump( streamoff __offset ) {
  201.     __lock_it( __b_lock );
  202.     __put_ptr += __offset;
  203. }
  204.  
  205. inline void streambuf::setp( char *__pptr, char *__epptr ) {
  206.     __lock_it( __b_lock );
  207.     __put_base = __pptr;
  208.     __put_ptr  = __pptr;
  209.     __put_end  = __epptr;
  210. }
  211.  
  212. // *********************** Inline input functions ****************************
  213.  
  214. inline int streambuf::in_avail() const {
  215.     __lock_it( __b_lock );
  216.     return( (int)(__get_end - __get_ptr) );
  217. }
  218.  
  219. inline int streambuf::sgetchar() {
  220.     __lock_it( __b_lock );
  221.     return( (__get_ptr >= __get_end) && (underflow() == EOF) ? EOF
  222.                                                              : *__get_ptr++ );
  223. }
  224.  
  225. inline int streambuf::sbumpc() {
  226.     return( sgetchar() );
  227. }
  228.  
  229. inline int streambuf::snextc() {
  230.     __lock_it( __b_lock );
  231.     return( (__get_ptr) && (++__get_ptr < __get_end) ? *__get_ptr
  232.                                                      : underflow() );
  233. }
  234.  
  235. inline int streambuf::speekc() {
  236.     __lock_it( __b_lock );
  237.     return( (__get_ptr < __get_end) ? *__get_ptr
  238.                                     : underflow() );
  239. }
  240.  
  241. inline int streambuf::sgetc() {
  242.     return( speekc() );
  243. }
  244.  
  245. inline int streambuf::sgetn( char *__buf, int __len ) {
  246.     __lock_it( __b_lock );
  247.     if( __len < (int)(__get_end - __get_ptr) ) {
  248.     ::memcpy( __buf, __get_ptr, __len );
  249.     gbump( __len );
  250.     return( __len );
  251.     }
  252.     return( do_sgetn( __buf, __len ) );
  253. }
  254.  
  255. inline void streambuf::stossc() {
  256.     __lock_it( __b_lock );
  257.     if( (__get_ptr < __get_end) || (underflow() != EOF) ) {
  258.         ++__get_ptr;
  259.     }
  260. }
  261.  
  262. // *********************** Inline output functions ***************************
  263.  
  264. inline int streambuf::out_waiting() const {
  265.     __lock_it( __b_lock );
  266.     return( (int)(__put_ptr - __put_base) );
  267. }
  268.  
  269. inline int streambuf::sputbackc( char __c ) {
  270.     __lock_it( __b_lock );
  271.     return( (__get_ptr > __get_base) ? (*--__get_ptr = __c)
  272.                                      : pbackfail( __c ) );
  273. }
  274.  
  275. inline int streambuf::sputc( int __c ) {
  276.     __lock_it( __b_lock );
  277.     return( (__put_ptr >= __put_end) ? overflow( __c )
  278.                                      : (*__put_ptr++ = (char)__c) );
  279. }
  280.  
  281. inline int streambuf::sputn( char const *__buf, int __len ) {
  282.     __lock_it( __b_lock );
  283.     if( __len < (int)(__put_end - __put_ptr) ) {
  284.     ::memcpy( __put_ptr, __buf, __len );
  285.     pbump( __len );
  286.     return( __len );
  287.     }
  288.     return( do_sputn( __buf, __len ) );
  289. }
  290.  
  291. #endif
  292.