Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.6 KiB

  1. /***
  2. *ios.h - definitions/declarations for the ios class.
  3. *
  4. * Copyright (c) 1990-1994, 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. ****/
  12. #ifndef _INC_IOS
  13. #define _INC_IOS
  14. /* Define _CRTIMP */
  15. #ifndef _CRTIMP
  16. #ifdef _NTSDK
  17. /* definition compatible with NT SDK */
  18. #define _CRTIMP
  19. #else /* ndef _NTSDK */
  20. /* current definition */
  21. #ifdef _DLL
  22. #define _CRTIMP __declspec(dllimport)
  23. #else /* ndef _DLL */
  24. #define _CRTIMP
  25. #endif /* _DLL */
  26. #endif /* _NTSDK */
  27. #endif /* _CRTIMP */
  28. #ifdef _MT
  29. #include <windows.h> // critical section declarations
  30. extern "C" {
  31. _CRTIMP void __cdecl _mtlock(PRTL_CRITICAL_SECTION);
  32. _CRTIMP void __cdecl _mtunlock(PRTL_CRITICAL_SECTION);
  33. }
  34. #endif
  35. #ifndef NULL
  36. #define NULL 0
  37. #endif
  38. #ifndef EOF
  39. #define EOF (-1)
  40. #endif
  41. #ifdef _MSC_VER
  42. // C4505: "unreferenced local function has been removed"
  43. #pragma warning(disable:4505) // disable C4505 warning
  44. // #pragma warning(default:4505) // use this to reenable, if desired
  45. // Force word packing to avoid possible -Zp override
  46. #pragma pack(push,4)
  47. #endif // _MSC_VER
  48. class _CRTIMP streambuf;
  49. class _CRTIMP ostream;
  50. class _CRTIMP ios {
  51. public:
  52. enum io_state { goodbit = 0x00,
  53. eofbit = 0x01,
  54. failbit = 0x02,
  55. badbit = 0x04 };
  56. enum open_mode { in = 0x01,
  57. out = 0x02,
  58. ate = 0x04,
  59. app = 0x08,
  60. trunc = 0x10,
  61. nocreate = 0x20,
  62. noreplace = 0x40,
  63. binary = 0x80 }; // CONSIDER: not in latest spec.
  64. enum seek_dir { beg=0, cur=1, end=2 };
  65. enum { skipws = 0x0001,
  66. left = 0x0002,
  67. right = 0x0004,
  68. internal = 0x0008,
  69. dec = 0x0010,
  70. oct = 0x0020,
  71. hex = 0x0040,
  72. showbase = 0x0080,
  73. showpoint = 0x0100,
  74. uppercase = 0x0200,
  75. showpos = 0x0400,
  76. scientific = 0x0800,
  77. fixed = 0x1000,
  78. unitbuf = 0x2000,
  79. stdio = 0x4000
  80. };
  81. static const long basefield; // dec | oct | hex
  82. static const long adjustfield; // left | right | internal
  83. static const long floatfield; // scientific | fixed
  84. ios(streambuf*); // differs from ANSI
  85. virtual ~ios();
  86. inline long flags() const;
  87. inline long flags(long _l);
  88. inline long setf(long _f,long _m);
  89. inline long setf(long _l);
  90. inline long unsetf(long _l);
  91. inline int width() const;
  92. inline int width(int _i);
  93. inline ostream* tie(ostream* _os);
  94. inline ostream* tie() const;
  95. inline char fill() const;
  96. inline char fill(char _c);
  97. inline int precision(int _i);
  98. inline int precision() const;
  99. inline int rdstate() const;
  100. inline void clear(int _i = 0);
  101. // inline operator void*() const;
  102. operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  103. inline int operator!() const;
  104. inline int good() const;
  105. inline int eof() const;
  106. inline int fail() const;
  107. inline int bad() const;
  108. inline streambuf* rdbuf() const;
  109. inline long & iword(int) const;
  110. inline void * & pword(int) const;
  111. static long bitalloc();
  112. static int xalloc();
  113. static void sync_with_stdio();
  114. #ifdef _MT
  115. inline void __cdecl setlock();
  116. inline void __cdecl clrlock();
  117. void __cdecl lock() { if (LockFlg<0) _mtlock(lockptr()); };
  118. void __cdecl unlock() { if (LockFlg<0) _mtunlock(lockptr()); }
  119. inline void __cdecl lockbuf();
  120. inline void __cdecl unlockbuf();
  121. #else
  122. void __cdecl lock() { }
  123. void __cdecl unlock() { }
  124. void __cdecl lockbuf() { }
  125. void __cdecl unlockbuf() { }
  126. #endif
  127. protected:
  128. ios();
  129. ios(const ios&); // treat as private
  130. ios& operator=(const ios&);
  131. void init(streambuf*);
  132. enum { skipping, tied };
  133. streambuf* bp;
  134. int state;
  135. int ispecial; // not used
  136. int ospecial; // not used
  137. int isfx_special; // not used
  138. int osfx_special; // not used
  139. int x_delbuf; // if set, rdbuf() deleted by ~ios
  140. ostream* x_tie;
  141. long x_flags;
  142. int x_precision;
  143. char x_fill;
  144. int x_width;
  145. static void (*stdioflush)(); // not used
  146. #ifdef _MT
  147. static void lockc() { _mtlock(& x_lockc); }
  148. static void unlockc() { _mtunlock( & x_lockc); }
  149. PRTL_CRITICAL_SECTION lockptr() { return & x_lock; }
  150. #else
  151. static void lockc() { }
  152. static void unlockc() { }
  153. #endif
  154. public:
  155. int delbuf() const { return x_delbuf; }
  156. void delbuf(int _i) { x_delbuf = _i; }
  157. private:
  158. static long x_maxbit;
  159. static int x_curindex;
  160. // consider: make interal static to ios::sync_with_stdio()
  161. static int sunk_with_stdio; // make sure sync_with done only once
  162. #ifdef _MT
  163. #define MAXINDEX 7
  164. static long x_statebuf[MAXINDEX+1]; // used by xalloc()
  165. static int fLockcInit; // used to see if x_lockc initialized
  166. static RTL_CRITICAL_SECTION x_lockc; // used to lock static (class) data members
  167. // consider: make pointer and allocate elsewhere
  168. int LockFlg; // enable locking flag
  169. RTL_CRITICAL_SECTION x_lock; // used for multi-thread lock on object
  170. #else
  171. static long * x_statebuf; // used by xalloc()
  172. #endif
  173. };
  174. #include <streamb.h>
  175. inline _CRTIMP ios& __cdecl dec(ios& _strm) { _strm.setf(ios::dec,ios::basefield); return _strm; }
  176. inline _CRTIMP ios& __cdecl hex(ios& _strm) { _strm.setf(ios::hex,ios::basefield); return _strm; }
  177. inline _CRTIMP ios& __cdecl oct(ios& _strm) { _strm.setf(ios::oct,ios::basefield); return _strm; }
  178. inline long ios::flags() const { return x_flags; }
  179. inline long ios::flags(long _l){ long _lO; _lO = x_flags; x_flags = _l; return _lO; }
  180. inline long ios::setf(long _l,long _m){ long _lO; lock(); _lO = x_flags; x_flags = (_l&_m) | (x_flags&(~_m)); unlock(); return _lO; }
  181. inline long ios::setf(long _l){ long _lO; lock(); _lO = x_flags; x_flags |= _l; unlock(); return _lO; }
  182. inline long ios::unsetf(long _l){ long _lO; lock(); _lO = x_flags; x_flags &= (~_l); unlock(); return _lO; }
  183. inline int ios::width() const { return x_width; }
  184. inline int ios::width(int _i){ int _iO; _iO = (int)x_width; x_width = _i; return _iO; }
  185. inline ostream* ios::tie(ostream* _os){ ostream* _osO; _osO = x_tie; x_tie = _os; return _osO; }
  186. inline ostream* ios::tie() const { return x_tie; }
  187. inline char ios::fill() const { return x_fill; }
  188. inline char ios::fill(char _c){ char _cO; _cO = x_fill; x_fill = _c; return _cO; }
  189. inline int ios::precision(int _i){ int _iO; _iO = (int)x_precision; x_precision = _i; return _iO; }
  190. inline int ios::precision() const { return x_precision; }
  191. inline int ios::rdstate() const { return state; }
  192. // inline ios::operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  193. inline int ios::operator!() const { return state&(badbit|failbit); }
  194. inline int ios::bad() const { return state & badbit; }
  195. // consider: are locks needed on clear() ?
  196. inline void ios::clear(int _i){ lock(); state = _i; unlock(); }
  197. inline int ios::eof() const { return state & eofbit; }
  198. inline int ios::fail() const { return state & (badbit | failbit); }
  199. inline int ios::good() const { return state == 0; }
  200. inline streambuf* ios::rdbuf() const { return bp; }
  201. inline long & ios::iword(int _i) const { return x_statebuf[_i] ; }
  202. inline void * & ios::pword(int _i) const { return (void * &)x_statebuf[_i]; }
  203. #ifdef _MT
  204. inline void ios::setlock() { LockFlg--; if (bp) bp->setlock(); }
  205. inline void ios::clrlock() { if (LockFlg <= 0) LockFlg++; if (bp) bp->clrlock(); }
  206. inline void ios::lockbuf() { bp->lock(); }
  207. inline void ios::unlockbuf() { bp->unlock(); }
  208. #endif
  209. #ifdef _MSC_VER
  210. // Restore default packing
  211. #pragma pack(pop)
  212. #endif // _MSC_VER
  213. #endif // !_INC_IOS