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

  1. /***
  2. *ios.h - definitions/declarations for the ios 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 ios 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_IOS
  22. #define _INC_IOS
  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. #ifdef  _MT
  50.  
  51. typedef struct __CRT_LIST_ENTRY {
  52.    struct __CRT_LIST_ENTRY *Flink;
  53.    struct __CRT_LIST_ENTRY *Blink;
  54. } _CRT_LIST_ENTRY;
  55.  
  56. typedef struct _CRT_CRITICAL_SECTION_DEBUG {
  57.     unsigned short Type;
  58.     unsigned short CreatorBackTraceIndex;
  59.     struct _CRT_CRITICAL_SECTION *CriticalSection;
  60.     _CRT_LIST_ENTRY ProcessLocksList;
  61.     unsigned long EntryCount;
  62.     unsigned long ContentionCount;
  63.     unsigned long Depth;
  64.     void * OwnerBackTrace[ 5 ];
  65. } _CRT_CRITICAL_SECTION_DEBUG, *_PCRT_CRITICAL_SECTION_DEBUG;
  66.  
  67. typedef struct _CRT_CRITICAL_SECTION {
  68.     _PCRT_CRITICAL_SECTION_DEBUG DebugInfo;
  69.  
  70.     //
  71.     //  The following three fields control entering and exiting the critical
  72.     //  section for the resource
  73.     //
  74.  
  75.     long LockCount;
  76.     long RecursionCount;
  77.     void * OwningThread;        // from the thread's ClientId->UniqueThread
  78.     void * LockSemaphore;
  79.     unsigned long Reserved;
  80. } _CRT_CRITICAL_SECTION, *_PCRT_CRITICAL_SECTION;
  81.  
  82. extern "C" {
  83. _CRTIMP void __cdecl _mtlock(_PCRT_CRITICAL_SECTION);
  84. _CRTIMP void __cdecl _mtunlock(_PCRT_CRITICAL_SECTION);
  85. }
  86.  
  87. #endif  /* _MT */
  88.  
  89. #ifndef NULL
  90. #define NULL    0
  91. #endif
  92.  
  93. #ifndef EOF
  94. #define EOF     (-1)
  95. #endif
  96.  
  97. #ifdef  _MSC_VER
  98. // C4514: "unreferenced inline function has been removed"
  99. #pragma warning(disable:4514) // disable C4514 warning
  100. // #pragma warning(default:4514)        // use this to reenable, if desired
  101. #endif  // _MSC_VER
  102.  
  103. class _CRTIMP streambuf;
  104. class _CRTIMP ostream;
  105.  
  106. class _CRTIMP ios {
  107.  
  108. public:
  109.     enum io_state {  goodbit = 0x00,
  110.                      eofbit  = 0x01,
  111.                      failbit = 0x02,
  112.                      badbit  = 0x04 };
  113.  
  114.     enum open_mode { in        = 0x01,
  115.                      out       = 0x02,
  116.                      ate       = 0x04,
  117.                      app       = 0x08,
  118.                      trunc     = 0x10,
  119.                      nocreate  = 0x20,
  120.                      noreplace = 0x40,
  121.                      binary    = 0x80 };
  122.  
  123.     enum seek_dir { beg=0, cur=1, end=2 };
  124.  
  125.     enum {  skipws     = 0x0001,
  126.             left       = 0x0002,
  127.             right      = 0x0004,
  128.             internal   = 0x0008,
  129.             dec        = 0x0010,
  130.             oct        = 0x0020,
  131.             hex        = 0x0040,
  132.             showbase   = 0x0080,
  133.             showpoint  = 0x0100,
  134.             uppercase  = 0x0200,
  135.             showpos    = 0x0400,
  136.             scientific = 0x0800,
  137.             fixed      = 0x1000,
  138.             unitbuf    = 0x2000,
  139.             stdio      = 0x4000
  140.                                  };
  141.  
  142.     static const long basefield;        // dec | oct | hex
  143.     static const long adjustfield;      // left | right | internal
  144.     static const long floatfield;       // scientific | fixed
  145.  
  146.     ios(streambuf*);                    // differs from ANSI
  147.     virtual ~ios();
  148.  
  149.     inline long flags() const;
  150.     inline long flags(long _l);
  151.  
  152.     inline long setf(long _f,long _m);
  153.     inline long setf(long _l);
  154.     inline long unsetf(long _l);
  155.  
  156.     inline int width() const;
  157.     inline int width(int _i);
  158.  
  159.     inline ostream* tie(ostream* _os);
  160.     inline ostream* tie() const;
  161.  
  162.     inline char fill() const;
  163.     inline char fill(char _c);
  164.  
  165.     inline int precision(int _i);
  166.     inline int precision() const;
  167.  
  168.     inline int rdstate() const;
  169.     inline void clear(int _i = 0);
  170.  
  171. //  inline operator void*() const;
  172.     operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  173.     inline int operator!() const;
  174.  
  175.     inline int  good() const;
  176.     inline int  eof() const;
  177.     inline int  fail() const;
  178.     inline int  bad() const;
  179.  
  180.     inline streambuf* rdbuf() const;
  181.  
  182.     inline long & iword(int) const;
  183.     inline void * & pword(int) const;
  184.  
  185.     static long bitalloc();
  186.     static int xalloc();
  187.     static void sync_with_stdio();
  188.  
  189. #ifdef  _MT
  190.     inline void __cdecl setlock();
  191.     inline void __cdecl clrlock();
  192.     void __cdecl lock() { if (LockFlg<0) _mtlock(lockptr()); };
  193.     void __cdecl unlock() { if (LockFlg<0) _mtunlock(lockptr()); }
  194.     inline void __cdecl lockbuf();
  195.     inline void __cdecl unlockbuf();
  196. #else
  197.     void __cdecl lock() { }
  198.     void __cdecl unlock() { }
  199.     void __cdecl lockbuf() { }
  200.     void __cdecl unlockbuf() { }
  201. #endif
  202.  
  203. protected:
  204.     ios();
  205.     ios(const ios&);                    // treat as private
  206.     ios& operator=(const ios&);
  207.     void init(streambuf*);
  208.  
  209.     enum { skipping, tied };
  210.     streambuf*  bp;
  211.  
  212.     int     state;
  213.     int     ispecial;                   // not used
  214.     int     ospecial;                   // not used
  215.     int     isfx_special;               // not used
  216.     int     osfx_special;               // not used
  217.     int     x_delbuf;                   // if set, rdbuf() deleted by ~ios
  218.  
  219.     ostream* x_tie;
  220.     long    x_flags;
  221.     int     x_precision;
  222.     char    x_fill;
  223.     int     x_width;
  224.  
  225.     static void (*stdioflush)();        // not used
  226.  
  227. #ifdef  _MT
  228.     static void lockc() { _mtlock(& x_lockc); }
  229.     static void unlockc() { _mtunlock( & x_lockc); }
  230.     _PCRT_CRITICAL_SECTION lockptr() { return & x_lock; }
  231. #else
  232.     static void lockc() { }
  233.     static void unlockc() { }
  234. #endif
  235.  
  236. public:
  237.     int delbuf() const { return x_delbuf; }
  238.     void    delbuf(int _i) { x_delbuf = _i; }
  239.  
  240. private:
  241.     static long x_maxbit;
  242.     static int x_curindex;
  243.     static int sunk_with_stdio;         // make sure sync_with done only once
  244. #ifdef  _MT
  245. #define MAXINDEX 7
  246.     static long x_statebuf[MAXINDEX+1];  // used by xalloc()
  247.     static int fLockcInit;              // used to see if x_lockc initialized
  248.     static _CRT_CRITICAL_SECTION x_lockc; // used to lock static (class) data members
  249.     int LockFlg;                        // enable locking flag
  250.     _CRT_CRITICAL_SECTION x_lock;       // used for multi-thread lock on object
  251. #else
  252.     static long * x_statebuf;  // used by xalloc()
  253. #endif
  254. };
  255.  
  256. #include <streamb.h>
  257.  
  258. inline _CRTIMP ios& __cdecl dec(ios& _strm) { _strm.setf(ios::dec,ios::basefield); return _strm; }
  259. inline _CRTIMP ios& __cdecl hex(ios& _strm) { _strm.setf(ios::hex,ios::basefield); return _strm; }
  260. inline _CRTIMP ios& __cdecl oct(ios& _strm) { _strm.setf(ios::oct,ios::basefield); return _strm; }
  261.  
  262. inline long ios::flags() const { return x_flags; }
  263. inline long ios::flags(long _l){ long _lO; _lO = x_flags; x_flags = _l; return _lO; }
  264.  
  265. inline long ios::setf(long _l,long _m){ long _lO; lock(); _lO = x_flags; x_flags = (_l&_m) | (x_flags&(~_m)); unlock(); return _lO; }
  266. inline long ios::setf(long _l){ long _lO; lock(); _lO = x_flags; x_flags |= _l; unlock(); return _lO; }
  267. inline long ios::unsetf(long _l){ long _lO; lock(); _lO = x_flags; x_flags &= (~_l); unlock(); return _lO; }
  268.  
  269. inline int ios::width() const { return x_width; }
  270. inline int ios::width(int _i){ int _iO; _iO = (int)x_width; x_width = _i; return _iO; }
  271.  
  272. inline ostream* ios::tie(ostream* _os){ ostream* _osO; _osO = x_tie; x_tie = _os; return _osO; }
  273. inline ostream* ios::tie() const { return x_tie; }
  274. inline char ios::fill() const { return x_fill; }
  275. inline char ios::fill(char _c){ char _cO; _cO = x_fill; x_fill = _c; return _cO; }
  276. inline int ios::precision(int _i){ int _iO; _iO = (int)x_precision; x_precision = _i; return _iO; }
  277. inline int ios::precision() const { return x_precision; }
  278.  
  279. inline int ios::rdstate() const { return state; }
  280.  
  281. // inline ios::operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  282. inline int ios::operator!() const { return state&(badbit|failbit); }
  283.  
  284. inline int  ios::bad() const { return state & badbit; }
  285. inline void ios::clear(int _i){ lock(); state = _i; unlock(); }
  286. inline int  ios::eof() const { return state & eofbit; }
  287. inline int  ios::fail() const { return state & (badbit | failbit); }
  288. inline int  ios::good() const { return state == 0; }
  289.  
  290. inline streambuf* ios::rdbuf() const { return bp; }
  291.  
  292. inline long & ios::iword(int _i) const { return x_statebuf[_i] ; }
  293. inline void * & ios::pword(int _i) const { return (void * &)x_statebuf[_i]; }
  294.  
  295. #ifdef  _MT
  296.     inline void ios::setlock() { LockFlg--; if (bp) bp->setlock(); }
  297.     inline void ios::clrlock() { if (LockFlg <= 0) LockFlg++; if (bp) bp->clrlock(); }
  298.     inline void ios::lockbuf() { bp->lock(); }
  299.     inline void ios::unlockbuf() { bp->unlock(); }
  300. #endif
  301.  
  302. #ifdef  _MSC_VER
  303. // Restore default packing
  304. #pragma pack(pop)
  305. #endif  // _MSC_VER
  306.  
  307. #endif  // _INC_IOS
  308.  
  309. #endif  /* __cplusplus */
  310.