home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / streamb.h < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  214 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  /* _MSC_VER > 1000 */
  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  /* !defined (_WIN32) && !defined (_MAC) */
  27.  
  28. #ifndef _CRTBLD
  29. /* This version of the header files is NOT for user programs.
  30.  * It is intended for use when building the C runtimes ONLY.
  31.  * The version intended for public use will not have this message.
  32.  */
  33. #error ERROR: Use of C runtime library internal header file.
  34. #endif  /* _CRTBLD */
  35.  
  36. #ifdef _MSC_VER
  37. // Currently, all MS C compilers for Win32 platforms default to 8 byte
  38. // alignment.
  39. #pragma pack(push,8)
  40.  
  41. #include <useoldio.h>
  42.  
  43. #endif  /* _MSC_VER */
  44.  
  45. /* Define _CRTIMP */
  46.  
  47. #ifndef _CRTIMP
  48. #ifdef CRTDLL
  49. #define _CRTIMP __declspec(dllexport)
  50. #else  /* CRTDLL */
  51. #ifdef _DLL
  52. #define _CRTIMP __declspec(dllimport)
  53. #else  /* _DLL */
  54. #define _CRTIMP
  55. #endif  /* _DLL */
  56. #endif  /* CRTDLL */
  57. #endif  /* _CRTIMP */
  58.  
  59. #ifndef _INTERNAL_IFSTRIP_
  60. /* Define _CRTIMP1 */
  61.  
  62. #ifndef _CRTIMP1
  63. #ifdef CRTDLL1
  64. #define _CRTIMP1 __declspec(dllexport)
  65. #else  /* CRTDLL1 */
  66. #define _CRTIMP1 _CRTIMP
  67. #endif  /* CRTDLL1 */
  68. #endif  /* _CRTIMP1 */
  69. #endif  /* _INTERNAL_IFSTRIP_ */
  70.  
  71. #include <ios.h>        // need ios::seek_dir definition
  72.  
  73. #ifndef NULL
  74. #define NULL    0
  75. #endif  /* NULL */
  76.  
  77. #ifndef EOF
  78. #define EOF     (-1)
  79. #endif  /* EOF */
  80.  
  81. #ifdef _MSC_VER
  82. // C4514: "unreferenced inline function has been removed"
  83. #pragma warning(disable:4514) // disable C4514 warning
  84. // #pragma warning(default:4514)        // use this to reenable, if desired
  85. #endif  /* _MSC_VER */
  86.  
  87. typedef long streampos, streamoff;
  88.  
  89. class _CRTIMP1 ios;
  90.  
  91. class _CRTIMP1 streambuf {
  92. public:
  93.  
  94.     virtual ~streambuf();
  95.  
  96.     inline int in_avail() const;
  97.     inline int out_waiting() const;
  98.     int sgetc();
  99.     int snextc();
  100.     int sbumpc();
  101.     void stossc();
  102.  
  103.     inline int sputbackc(char);
  104.  
  105.     inline int sputc(int);
  106.     inline int sputn(const char *,int);
  107.     inline int sgetn(char *,int);
  108.  
  109.     virtual int sync();
  110.  
  111.     virtual streambuf* setbuf(char *, int);
  112.     virtual streampos seekoff(streamoff,ios::seek_dir,int =ios::in|ios::out);
  113.     virtual streampos seekpos(streampos,int =ios::in|ios::out);
  114.  
  115.     virtual int xsputn(const char *,int);
  116.     virtual int xsgetn(char *,int);
  117.  
  118.     virtual int overflow(int =EOF) = 0; // pure virtual function
  119.     virtual int underflow() = 0;        // pure virtual function
  120.  
  121.     virtual int pbackfail(int);
  122.  
  123.     void dbp();
  124.  
  125. #ifdef _MT
  126.     void setlock() { LockFlg--; }       // <0 indicates lock required;
  127.     void clrlock() { if (LockFlg <= 0) LockFlg++; }
  128.     void lock() { if (LockFlg<0) _mtlock(lockptr()); };
  129.     void unlock() { if (LockFlg<0) _mtunlock(lockptr()); }
  130. #else  /* _MT */
  131.     void lock() { }
  132.     void unlock() { }
  133. #endif  /* _MT */
  134.  
  135. protected:
  136.     streambuf();
  137.     streambuf(char *,int);
  138.  
  139.     inline char * base() const;
  140.     inline char * ebuf() const;
  141.     inline char * pbase() const;
  142.     inline char * pptr() const;
  143.     inline char * epptr() const;
  144.     inline char * eback() const;
  145.     inline char * gptr() const;
  146.     inline char * egptr() const;
  147.     inline int blen() const;
  148.     inline void setp(char *,char *);
  149.     inline void setg(char *,char *,char *);
  150.     inline void pbump(int);
  151.     inline void gbump(int);
  152.  
  153.     void setb(char *,char *,int =0);
  154.     inline int unbuffered() const;
  155.     inline void unbuffered(int);
  156.     int allocate();
  157.     virtual int doallocate();
  158. #ifdef _MT
  159.     _PCRT_CRITICAL_SECTION lockptr() { return & x_lock; }
  160. #endif  /* _MT */
  161.  
  162. private:
  163.     int _fAlloc;
  164.     int _fUnbuf;
  165.     int x_lastc;
  166.     char * _base;
  167.     char * _ebuf;
  168.     char * _pbase;
  169.     char * _pptr;
  170.     char * _epptr;
  171.     char * _eback;
  172.     char * _gptr;
  173.     char * _egptr;
  174. #ifdef _MT
  175.     int LockFlg;                // <0 indicates locking required
  176.    _CRT_CRITICAL_SECTION x_lock;        // lock needed only for multi-thread operation
  177. #endif  /* _MT */
  178. };
  179.  
  180. inline int streambuf::in_avail() const { return (gptr()<_egptr) ? (_egptr-gptr()) : 0; }
  181. inline int streambuf::out_waiting() const { return (_pptr>=_pbase) ? (_pptr-_pbase) : 0; }
  182.  
  183. inline int streambuf::sputbackc(char _c){ return (_eback<gptr()) ? *(--_gptr)=_c : pbackfail(_c); }
  184.  
  185. inline int streambuf::sputc(int _i){ return (_pptr<_epptr) ? (unsigned char)(*(_pptr++)=(char)_i) : overflow(_i); }
  186.  
  187. inline int streambuf::sputn(const char * _str,int _n) { return xsputn(_str, _n); }
  188. inline int streambuf::sgetn(char * _str,int _n) { return xsgetn(_str, _n); }
  189.  
  190. inline char * streambuf::base() const { return _base; }
  191. inline char * streambuf::ebuf() const { return _ebuf; }
  192. inline int streambuf::blen() const  {return ((_ebuf > _base) ? (_ebuf-_base) : 0); }
  193. inline char * streambuf::pbase() const { return _pbase; }
  194. inline char * streambuf::pptr() const { return _pptr; }
  195. inline char * streambuf::epptr() const { return _epptr; }
  196. inline char * streambuf::eback() const { return _eback; }
  197. inline char * streambuf::gptr() const { return _gptr; }
  198. inline char * streambuf::egptr() const { return _egptr; }
  199. inline void streambuf::gbump(int _n) { if (_egptr) _gptr += _n; }
  200. inline void streambuf::pbump(int _n) { if (_epptr) _pptr += _n; }
  201. inline void streambuf::setg(char * _eb, char * _g, char * _eg) {_eback=_eb; _gptr=_g; _egptr=_eg; x_lastc=EOF; }
  202. inline void streambuf::setp(char * _p, char * _ep) {_pptr=_pbase=_p; _epptr=_ep; }
  203. inline int streambuf::unbuffered() const { return _fUnbuf; }
  204. inline void streambuf::unbuffered(int _f) { _fUnbuf = _f; }
  205.  
  206. #ifdef _MSC_VER
  207. // Restore previous packing
  208. #pragma pack(pop)
  209. #endif  /* _MSC_VER */
  210.  
  211. #endif  /* _INC_STREAMB */
  212.  
  213. #endif  /* __cplusplus */
  214.