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.

364 lines
11 KiB

  1. /***
  2. *ios.h - definitions/declarations for the ios class.
  3. *
  4. * Copyright (c) 1990-2001, 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. *Revision History:
  14. * 01-23-92 KRS Ported from 16-bit version.
  15. * 03-02-92 KRS Added locks for multithread support.
  16. * 06-03-92 KRS Add NULL definition here too, for convenience.
  17. * 08-27-92 KRS Removed bogus 'short' defs from private section.
  18. * 02-23-93 SKS Update copyright to 1993
  19. * 03-23-93 CFW Modified #pragma warnings.
  20. * 10-06-93 GJF Merged Cuda and NT version. Replaced MTHREAD with
  21. * _MT.
  22. * 10-13-93 GJF Deleted obsolete COMBOINC check. Enclose #pragma-s
  23. * in #ifdef _MSC_VER
  24. * 10-28-93 SKS Add _mttermlock() as d-tor versionf _mtinitlock()
  25. * 03-03-94 SKS Add __cdecl keyword to _mt*lock* functions.
  26. * _mtlockinit & _mtlockterm are for internal use only.
  27. * 04-12-94 SKS Add __cdecl keyword to dec/hex/oct functions.
  28. * 08-12-94 GJF Disable warning 4514 instead of 4505.
  29. * 11-03-94 GJF Changed pack pragma to 8 byte alignment.
  30. * 02-11-95 CFW Add _CRTBLD to avoid users getting wrong headers.
  31. * 02-14-95 CFW Clean up Mac merge.
  32. * 02-30-95 CFW Fix _CRT_CRITICAL_SECTION stuff.
  33. * 05-11-95 CFW Only for use by C++ programs.
  34. * 12-14-95 JWM Add "#pragma once".
  35. * 04-09-96 SKS Change _CRTIMP to _CRTIMP1 for special iostream build
  36. * 04-15-96 JWM Remove _OLD_IOSTREAMS, add '#pragma comment(lib,"cirt")'.
  37. * 04-16-96 JWM '#include useoldio.h' replaces '#pragma comment(...)'.
  38. * 02-21-97 GJF Cleaned out obsolete support for _NTSDK. Also,
  39. * detab-ed.
  40. * 05-17-99 PML Remove all Macintosh support.
  41. *
  42. ****/
  43. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  44. #pragma once
  45. #endif
  46. #ifdef __cplusplus
  47. #ifndef _INC_IOS
  48. #define _INC_IOS
  49. #if !defined(_WIN32)
  50. #error ERROR: Only Win32 target supported!
  51. #endif
  52. #ifndef _CRTBLD
  53. /* This version of the header files is NOT for user programs.
  54. * It is intended for use when building the C runtimes ONLY.
  55. * The version intended for public use will not have this message.
  56. */
  57. #error ERROR: Use of C runtime library internal header file.
  58. #endif /* _CRTBLD */
  59. #ifdef _MSC_VER
  60. // Currently, all MS C compilers for Win32 platforms default to 8 byte
  61. // alignment.
  62. #pragma pack(push,8)
  63. #include <useoldio.h>
  64. #endif // _MSC_VER
  65. /* Define _CRTIMP */
  66. #ifndef _CRTIMP
  67. #ifdef CRTDLL
  68. #define _CRTIMP __declspec(dllexport)
  69. #else /* ndef CRTDLL */
  70. #ifdef _DLL
  71. #define _CRTIMP __declspec(dllimport)
  72. #else /* ndef _DLL */
  73. #define _CRTIMP
  74. #endif /* _DLL */
  75. #endif /* CRTDLL */
  76. #endif /* _CRTIMP */
  77. #ifndef _INTERNAL_IFSTRIP_
  78. /* Define _CRTIMP1 */
  79. #ifndef _CRTIMP1
  80. #ifdef CRTDLL1
  81. #define _CRTIMP1 __declspec(dllexport)
  82. #else /* ndef CRTDLL1 */
  83. #define _CRTIMP1 _CRTIMP
  84. #endif /* CRTDLL1 */
  85. #endif /* _CRTIMP1 */
  86. #endif /* _INTERNAL_IFSTRIP_ */
  87. #ifdef _MT
  88. typedef struct __CRT_LIST_ENTRY {
  89. struct __CRT_LIST_ENTRY *Flink;
  90. struct __CRT_LIST_ENTRY *Blink;
  91. } _CRT_LIST_ENTRY;
  92. typedef struct _CRT_CRITICAL_SECTION_DEBUG {
  93. unsigned short Type;
  94. unsigned short CreatorBackTraceIndex;
  95. struct _CRT_CRITICAL_SECTION *CriticalSection;
  96. _CRT_LIST_ENTRY ProcessLocksList;
  97. unsigned long EntryCount;
  98. unsigned long ContentionCount;
  99. unsigned long Depth;
  100. void * OwnerBackTrace[ 5 ];
  101. } _CRT_CRITICAL_SECTION_DEBUG, *_PCRT_CRITICAL_SECTION_DEBUG;
  102. typedef struct _CRT_CRITICAL_SECTION {
  103. _PCRT_CRITICAL_SECTION_DEBUG DebugInfo;
  104. //
  105. // The following three fields control entering and exiting the critical
  106. // section for the resource
  107. //
  108. long LockCount;
  109. long RecursionCount;
  110. void * OwningThread; // from the thread's ClientId->UniqueThread
  111. void * LockSemaphore;
  112. unsigned long Reserved;
  113. } _CRT_CRITICAL_SECTION, *_PCRT_CRITICAL_SECTION;
  114. extern "C" {
  115. #ifndef _INTERNAL_IFSTRIP_
  116. void __cdecl _mtlockinit(_PCRT_CRITICAL_SECTION);
  117. void __cdecl _mtlockterm(_PCRT_CRITICAL_SECTION);
  118. #endif
  119. _CRTIMP1 void __cdecl _mtlock(_PCRT_CRITICAL_SECTION);
  120. _CRTIMP1 void __cdecl _mtunlock(_PCRT_CRITICAL_SECTION);
  121. }
  122. #endif /* _MT */
  123. #ifndef NULL
  124. #define NULL 0
  125. #endif
  126. #ifndef EOF
  127. #define EOF (-1)
  128. #endif
  129. #ifdef _MSC_VER
  130. // C4514: "unreferenced inline function has been removed"
  131. #pragma warning(disable:4514) // disable C4514 warning
  132. // #pragma warning(default:4514) // use this to reenable, if desired
  133. #endif // _MSC_VER
  134. class _CRTIMP1 streambuf;
  135. class _CRTIMP1 ostream;
  136. class _CRTIMP1 ios {
  137. public:
  138. enum io_state { goodbit = 0x00,
  139. eofbit = 0x01,
  140. failbit = 0x02,
  141. badbit = 0x04 };
  142. enum open_mode { in = 0x01,
  143. out = 0x02,
  144. ate = 0x04,
  145. app = 0x08,
  146. trunc = 0x10,
  147. nocreate = 0x20,
  148. noreplace = 0x40,
  149. binary = 0x80 };
  150. enum seek_dir { beg=0, cur=1, end=2 };
  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. static const long basefield; // dec | oct | hex
  168. static const long adjustfield; // left | right | internal
  169. static const long floatfield; // scientific | fixed
  170. ios(streambuf*); // differs from ANSI
  171. virtual ~ios();
  172. inline long flags() const;
  173. inline long flags(long _l);
  174. inline long setf(long _f,long _m);
  175. inline long setf(long _l);
  176. inline long unsetf(long _l);
  177. inline int width() const;
  178. inline int width(int _i);
  179. inline ostream* tie(ostream* _os);
  180. inline ostream* tie() const;
  181. inline char fill() const;
  182. inline char fill(char _c);
  183. inline int precision(int _i);
  184. inline int precision() const;
  185. inline int rdstate() const;
  186. inline void clear(int _i = 0);
  187. // inline operator void*() const;
  188. operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  189. inline int operator!() const;
  190. inline int good() const;
  191. inline int eof() const;
  192. inline int fail() const;
  193. inline int bad() const;
  194. inline streambuf* rdbuf() const;
  195. inline long & iword(int) const;
  196. inline void * & pword(int) const;
  197. static long bitalloc();
  198. static int xalloc();
  199. static void sync_with_stdio();
  200. #ifdef _MT
  201. inline void __cdecl setlock();
  202. inline void __cdecl clrlock();
  203. void __cdecl lock() { if (LockFlg<0) _mtlock(lockptr()); };
  204. void __cdecl unlock() { if (LockFlg<0) _mtunlock(lockptr()); }
  205. inline void __cdecl lockbuf();
  206. inline void __cdecl unlockbuf();
  207. #else
  208. void __cdecl lock() { }
  209. void __cdecl unlock() { }
  210. void __cdecl lockbuf() { }
  211. void __cdecl unlockbuf() { }
  212. #endif
  213. protected:
  214. ios();
  215. ios(const ios&); // treat as private
  216. ios& operator=(const ios&);
  217. void init(streambuf*);
  218. enum { skipping, tied };
  219. streambuf* bp;
  220. int state;
  221. int ispecial; // not used
  222. int ospecial; // not used
  223. int isfx_special; // not used
  224. int osfx_special; // not used
  225. int x_delbuf; // if set, rdbuf() deleted by ~ios
  226. ostream* x_tie;
  227. long x_flags;
  228. int x_precision;
  229. char x_fill;
  230. int x_width;
  231. static void (*stdioflush)(); // not used
  232. #ifdef _MT
  233. static void lockc() { _mtlock(& x_lockc); }
  234. static void unlockc() { _mtunlock( & x_lockc); }
  235. _PCRT_CRITICAL_SECTION lockptr() { return & x_lock; }
  236. #else
  237. static void lockc() { }
  238. static void unlockc() { }
  239. #endif
  240. public:
  241. int delbuf() const { return x_delbuf; }
  242. void delbuf(int _i) { x_delbuf = _i; }
  243. private:
  244. static long x_maxbit;
  245. static int x_curindex;
  246. static int sunk_with_stdio; // make sure sync_with done only once
  247. #ifdef _MT
  248. #define MAXINDEX 7
  249. static long x_statebuf[MAXINDEX+1]; // used by xalloc()
  250. static int fLockcInit; // used to see if x_lockc initialized
  251. static _CRT_CRITICAL_SECTION x_lockc; // used to lock static (class) data members
  252. int LockFlg; // enable locking flag
  253. _CRT_CRITICAL_SECTION x_lock; // used for multi-thread lock on object
  254. #else
  255. static long * x_statebuf; // used by xalloc()
  256. #endif
  257. };
  258. #include <streamb.h>
  259. inline _CRTIMP1 ios& __cdecl dec(ios& _strm) { _strm.setf(ios::dec,ios::basefield); return _strm; }
  260. inline _CRTIMP1 ios& __cdecl hex(ios& _strm) { _strm.setf(ios::hex,ios::basefield); return _strm; }
  261. inline _CRTIMP1 ios& __cdecl oct(ios& _strm) { _strm.setf(ios::oct,ios::basefield); return _strm; }
  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. inline long ios::setf(long _l,long _m){ long _lO; lock(); _lO = x_flags; x_flags = (_l&_m) | (x_flags&(~_m)); unlock(); return _lO; }
  265. inline long ios::setf(long _l){ long _lO; lock(); _lO = x_flags; x_flags |= _l; unlock(); return _lO; }
  266. inline long ios::unsetf(long _l){ long _lO; lock(); _lO = x_flags; x_flags &= (~_l); unlock(); return _lO; }
  267. inline int ios::width() const { return x_width; }
  268. inline int ios::width(int _i){ int _iO; _iO = (int)x_width; x_width = _i; return _iO; }
  269. inline ostream* ios::tie(ostream* _os){ ostream* _osO; _osO = x_tie; x_tie = _os; return _osO; }
  270. inline ostream* ios::tie() const { return x_tie; }
  271. inline char ios::fill() const { return x_fill; }
  272. inline char ios::fill(char _c){ char _cO; _cO = x_fill; x_fill = _c; return _cO; }
  273. inline int ios::precision(int _i){ int _iO; _iO = (int)x_precision; x_precision = _i; return _iO; }
  274. inline int ios::precision() const { return x_precision; }
  275. inline int ios::rdstate() const { return state; }
  276. // inline ios::operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
  277. inline int ios::operator!() const { return state&(badbit|failbit); }
  278. inline int ios::bad() const { return state & badbit; }
  279. inline void ios::clear(int _i){ lock(); state = _i; unlock(); }
  280. inline int ios::eof() const { return state & eofbit; }
  281. inline int ios::fail() const { return state & (badbit | failbit); }
  282. inline int ios::good() const { return state == 0; }
  283. inline streambuf* ios::rdbuf() const { return bp; }
  284. inline long & ios::iword(int _i) const { return x_statebuf[_i] ; }
  285. inline void * & ios::pword(int _i) const { return (void * &)x_statebuf[_i]; }
  286. #ifdef _MT
  287. inline void ios::setlock() { LockFlg--; if (bp) bp->setlock(); }
  288. inline void ios::clrlock() { if (LockFlg <= 0) LockFlg++; if (bp) bp->clrlock(); }
  289. inline void ios::lockbuf() { bp->lock(); }
  290. inline void ios::unlockbuf() { bp->unlock(); }
  291. #endif
  292. #ifdef _MSC_VER
  293. // Restore default packing
  294. #pragma pack(pop)
  295. #endif // _MSC_VER
  296. #endif // _INC_IOS
  297. #endif /* __cplusplus */