home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / streamb.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  192 lines

  1. /***
  2. *streamb.h - definitions/declarations for the streambuf class
  3. *
  4. *       Copyright (c) 1990-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file defines the classes, values, macros, and functions
  8. *       used by the streambuf class.
  9. *       [AT&T C++]
  10. *
  11. *       [Public]
  12. *
  13. ****/
  14.  
  15. #if     _MSC_VER > 1000
  16. #pragma once
  17. #endif
  18.  
  19. #ifdef  __cplusplus
  20.  
  21. #ifndef _INC_STREAMB
  22. #define _INC_STREAMB
  23.  
  24. #if     !defined(_WIN32) && !defined(_MAC)
  25. #error ERROR: Only Mac or Win32 targets supported!
  26. #endif
  27.  
  28.  
  29. #ifdef  _MSC_VER
  30. // Currently, all MS C compilers for Win32 platforms default to 8 byte
  31. // alignment.
  32. #pragma pack(push,8)
  33.  
  34. #include <useoldio.h>
  35.  
  36. #endif  // _MSC_VER
  37.  
  38. /* Define _CRTIMP */
  39.  
  40. #ifndef _CRTIMP
  41. #ifdef  _DLL
  42. #define _CRTIMP __declspec(dllimport)
  43. #else   /* ndef _DLL */
  44. #define _CRTIMP
  45. #endif  /* _DLL */
  46. #endif  /* _CRTIMP */
  47.  
  48.  
  49. #include <ios.h>        // need ios::seek_dir definition
  50.  
  51. #ifndef NULL
  52. #define NULL    0
  53. #endif
  54.  
  55. #ifndef EOF
  56. #define EOF     (-1)
  57. #endif
  58.  
  59. #ifdef  _MSC_VER
  60. // C4514: "unreferenced inline function has been removed"
  61. #pragma warning(disable:4514) // disable C4514 warning
  62. // #pragma warning(default:4514)        // use this to reenable, if desired
  63. #endif  // _MSC_VER
  64.  
  65. typedef long streampos, streamoff;
  66.  
  67. class _CRTIMP ios;
  68.  
  69. class _CRTIMP streambuf {
  70. public:
  71.  
  72.     virtual ~streambuf();
  73.  
  74.     inline int in_avail() const;
  75.     inline int out_waiting() const;
  76.     int sgetc();
  77.     int snextc();
  78.     int sbumpc();
  79.     void stossc();
  80.  
  81.     inline int sputbackc(char);
  82.  
  83.     inline int sputc(int);
  84.     inline int sputn(const char *,int);
  85.     inline int sgetn(char *,int);
  86.  
  87.     virtual int sync();
  88.  
  89.     virtual streambuf* setbuf(char *, int);
  90.     virtual streampos seekoff(streamoff,ios::seek_dir,int =ios::in|ios::out);
  91.     virtual streampos seekpos(streampos,int =ios::in|ios::out);
  92.  
  93.     virtual int xsputn(const char *,int);
  94.     virtual int xsgetn(char *,int);
  95.  
  96.     virtual int overflow(int =EOF) = 0; // pure virtual function
  97.     virtual int underflow() = 0;        // pure virtual function
  98.  
  99.     virtual int pbackfail(int);
  100.  
  101.     void dbp();
  102.  
  103. #ifdef  _MT
  104.     void setlock() { LockFlg--; }       // <0 indicates lock required;
  105.     void clrlock() { if (LockFlg <= 0) LockFlg++; }
  106.     void lock() { if (LockFlg<0) _mtlock(lockptr()); };
  107.     void unlock() { if (LockFlg<0) _mtunlock(lockptr()); }
  108. #else
  109.     void lock() { }
  110.     void unlock() { }
  111. #endif
  112.  
  113. protected:
  114.     streambuf();
  115.     streambuf(char *,int);
  116.  
  117.     inline char * base() const;
  118.     inline char * ebuf() const;
  119.     inline char * pbase() const;
  120.     inline char * pptr() const;
  121.     inline char * epptr() const;
  122.     inline char * eback() const;
  123.     inline char * gptr() const;
  124.     inline char * egptr() const;
  125.     inline int blen() const;
  126.     inline void setp(char *,char *);
  127.     inline void setg(char *,char *,char *);
  128.     inline void pbump(int);
  129.     inline void gbump(int);
  130.  
  131.     void setb(char *,char *,int =0);
  132.     inline int unbuffered() const;
  133.     inline void unbuffered(int);
  134.     int allocate();
  135.     virtual int doallocate();
  136. #ifdef  _MT
  137.     _PCRT_CRITICAL_SECTION lockptr() { return & x_lock; }
  138. #endif
  139.  
  140. private:
  141.     int _fAlloc;
  142.     int _fUnbuf;
  143.     int x_lastc;
  144.     char * _base;
  145.     char * _ebuf;
  146.     char * _pbase;
  147.     char * _pptr;
  148.     char * _epptr;
  149.     char * _eback;
  150.     char * _gptr;
  151.     char * _egptr;
  152. #ifdef  _MT
  153.     int LockFlg;                // <0 indicates locking required
  154.    _CRT_CRITICAL_SECTION x_lock;        // lock needed only for multi-thread operation
  155. #endif
  156. };
  157.  
  158. inline int streambuf::in_avail() const { return (gptr()<_egptr) ? (_egptr-gptr()) : 0; }
  159. inline int streambuf::out_waiting() const { return (_pptr>=_pbase) ? (_pptr-_pbase) : 0; }
  160.  
  161. inline int streambuf::sputbackc(char _c){ return (_eback<gptr()) ? *(--_gptr)=_c : pbackfail(_c); }
  162.  
  163. inline int streambuf::sputc(int _i){ return (_pptr<_epptr) ? (unsigned char)(*(_pptr++)=(char)_i) : overflow(_i); }
  164.  
  165. inline int streambuf::sputn(const char * _str,int _n) { return xsputn(_str, _n); }
  166. inline int streambuf::sgetn(char * _str,int _n) { return xsgetn(_str, _n); }
  167.  
  168. inline char * streambuf::base() const { return _base; }
  169. inline char * streambuf::ebuf() const { return _ebuf; }
  170. inline int streambuf::blen() const  {return ((_ebuf > _base) ? (_ebuf-_base) : 0); }
  171. inline char * streambuf::pbase() const { return _pbase; }
  172. inline char * streambuf::pptr() const { return _pptr; }
  173. inline char * streambuf::epptr() const { return _epptr; }
  174. inline char * streambuf::eback() const { return _eback; }
  175. inline char * streambuf::gptr() const { return _gptr; }
  176. inline char * streambuf::egptr() const { return _egptr; }
  177. inline void streambuf::gbump(int _n) { if (_egptr) _gptr += _n; }
  178. inline void streambuf::pbump(int _n) { if (_epptr) _pptr += _n; }
  179. inline void streambuf::setg(char * _eb, char * _g, char * _eg) {_eback=_eb; _gptr=_g; _egptr=_eg; x_lastc=EOF; }
  180. inline void streambuf::setp(char * _p, char * _ep) {_pptr=_pbase=_p; _epptr=_ep; }
  181. inline int streambuf::unbuffered() const { return _fUnbuf; }
  182. inline void streambuf::unbuffered(int _f) { _fUnbuf = _f; }
  183.  
  184. #ifdef  _MSC_VER
  185. // Restore previous packing
  186. #pragma pack(pop)
  187. #endif  // _MSC_VER
  188.  
  189. #endif  // _INC_STREAMB
  190.  
  191. #endif  /* __cplusplus */
  192.