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