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.

333 lines
6.2 KiB

  1. /***
  2. *ios.cpp - fuctions for ios class.
  3. *
  4. * Copyright (c) 1990-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Functions for ios class.
  8. *
  9. *Revision History:
  10. * 09-10-90 WAJ Initial version.
  11. * 07-02-91 KRS Initial version completed.
  12. * 09-19-91 KRS Make use of delbuf() in destructor.
  13. * 11-04-91 KRS Change init(). Add operator=. Fix constructors.
  14. * 11-11-91 KRS Change xalloc() to conform to AT&T usage.
  15. * 11-20-91 KRS Add copy constructor.
  16. * 02-12-92 KRS Fix init of delbuf in ios::ios(streambuf*).
  17. * 03-30-92 KRS Add MTHREAD lock init calls to constructors.
  18. * 04-06-93 JWM Changed constructors to enable locking by default.
  19. * 10-28-93 SKS Add call to _mttermlock() in ios::~ios to clean up
  20. * o.s. resources associated with a Critical Section.
  21. * 01-17-94 SKS Change creation of crit. sects. and locks in ios
  22. * to avoid excess creating/destructing of class locks.
  23. * 09-06-94 CFW Replace MTHREAD with _MT.
  24. * 01-12-95 CFW Debug CRT allocs.
  25. * 03-17-95 CFW Change debug delete scheme.
  26. * 03-28-95 CFW Fix debug delete scheme.
  27. * 03-21-95 CFW Remove _delete_crt.
  28. * 06-14-95 CFW Comment cleanup.
  29. *
  30. *******************************************************************************/
  31. #include <cruntime.h>
  32. #include <internal.h>
  33. #include <stdlib.h>
  34. #include <iostream.h>
  35. #include <dbgint.h>
  36. #pragma hdrstop
  37. const long ios::basefield = (ios::dec | ios::oct | ios::hex);
  38. const long ios::adjustfield = (ios::left | ios::right | ios::internal);
  39. const long ios::floatfield = (ios::scientific | ios::fixed);
  40. long ios::x_maxbit = 0x8000; // ios::openprot
  41. int ios::x_curindex = -1;
  42. #ifdef _MT
  43. #define MAXINDEX 7
  44. long ios::x_statebuf[MAXINDEX+1] = { 0,0,0,0,0,0,0,0 }; // MAXINDEX * 0
  45. int ios::fLockcInit = 0; // nonzero = static lock initialized
  46. _CRT_CRITICAL_SECTION ios::x_lockc;
  47. #else // _MT
  48. long * ios::x_statebuf = NULL;
  49. #endif // _MT
  50. /***
  51. *ios::ios() - default constructor.
  52. *
  53. *Purpose:
  54. * Initializes an ios.
  55. *
  56. *Entry:
  57. *
  58. *Exit:
  59. *
  60. *Exceptions:
  61. *
  62. *******************************************************************************/
  63. ios::ios()
  64. {
  65. bp = NULL;
  66. state = ios::badbit;
  67. ispecial = 0;
  68. ospecial = 0;
  69. x_tie = (0);
  70. x_flags = 0;
  71. x_precision = 6;
  72. x_fill = ' ';
  73. x_width = 0;
  74. x_delbuf = 0;
  75. #ifdef _MT
  76. LockFlg = -1; // default is now : locking
  77. _mtlockinit(lockptr());
  78. if (!fLockcInit++)
  79. {
  80. _mtlockinit(&x_lockc);
  81. }
  82. #endif /* _MT */
  83. }
  84. /***
  85. *ios::ios( streambuf* pSB ) - constructor.
  86. *
  87. *Purpose:
  88. * Initializes an ios.
  89. *
  90. *Entry:
  91. *
  92. *Exit:
  93. *
  94. *Exceptions:
  95. *
  96. *******************************************************************************/
  97. ios::ios( streambuf* pSB )
  98. {
  99. // this->ios();
  100. bp = pSB;
  101. state = (bp) ? 0 : ios::badbit;
  102. ispecial = 0;
  103. ospecial = 0;
  104. x_tie = (0);
  105. x_flags = 0;
  106. x_precision = 6;
  107. x_fill = ' ';
  108. x_width = 0;
  109. x_delbuf = 0;
  110. #ifdef _MT
  111. LockFlg = -1; // default is now : locking
  112. _mtlockinit(lockptr());
  113. if (!fLockcInit++)
  114. {
  115. _mtlockinit(&x_lockc);
  116. }
  117. #endif /* _MT */
  118. }
  119. /***
  120. *ios::ios(const ios& _strm) - copy constructor.
  121. *
  122. *Purpose:
  123. * Copy constructor.
  124. *
  125. *Entry:
  126. * _strm = ios to copy data members from.
  127. *
  128. *Exit:
  129. *
  130. *Exceptions:
  131. *
  132. *******************************************************************************/
  133. ios::ios(const ios& _strm) // copy constructor
  134. {
  135. bp = NULL;
  136. x_delbuf = 0;
  137. *this = _strm; // invoke assignment operator
  138. #ifdef _MT
  139. LockFlg = -1; // default is now : locking
  140. _mtlockinit(lockptr());
  141. if (!fLockcInit++)
  142. {
  143. _mtlockinit(&x_lockc);
  144. }
  145. #endif /* _MT */
  146. }
  147. /***
  148. *virtual ios::~ios() - default destructor.
  149. *
  150. *Purpose:
  151. * Terminates an ios.
  152. *
  153. *Entry:
  154. *
  155. *Exit:
  156. *
  157. *Exceptions:
  158. *
  159. *******************************************************************************/
  160. ios::~ios()
  161. {
  162. #ifdef _MT
  163. LockFlg = -1; // default is now : locking
  164. if (!--fLockcInit)
  165. {
  166. _mtlockterm(&x_lockc);
  167. }
  168. _mtlockterm(lockptr());
  169. #endif /* _MT */
  170. if ((x_delbuf) && (bp))
  171. delete bp;
  172. bp = NULL;
  173. state = badbit;
  174. }
  175. /***
  176. *void ios::init( streambuf* pSB ) - initializes ios
  177. *
  178. *Purpose:
  179. * Initializes an ios.
  180. *
  181. *Entry:
  182. *
  183. *Exit:
  184. *
  185. *Exceptions:
  186. *
  187. *******************************************************************************/
  188. void ios::init( streambuf* pSB )
  189. {
  190. if (delbuf() && (bp)) // delete previous bp if necessary
  191. delete bp;
  192. bp = pSB;
  193. if (bp)
  194. state &= ~ios::badbit;
  195. else
  196. state |= ios::badbit;
  197. }
  198. /***
  199. *ios& ios::operator=( const ios& _strm ) - copy an ios.
  200. *
  201. *Purpose:
  202. * Copy an ios.
  203. *
  204. *Entry:
  205. *
  206. *Exit:
  207. *
  208. *Exceptions:
  209. *
  210. *******************************************************************************/
  211. ios& ios::operator=(const ios& _strm)
  212. {
  213. x_tie = _strm.tie();
  214. x_flags = _strm.flags();
  215. x_precision = (char)_strm.precision();
  216. x_fill = _strm.fill();
  217. x_width = (char)_strm.width();
  218. state = _strm.rdstate();
  219. if (!bp)
  220. state |= ios::badbit; // adjust state for uninitialized bp
  221. return *this;
  222. }
  223. /***
  224. *int ios::xalloc() - ios xalloc member function
  225. *
  226. *Purpose:
  227. *
  228. *Entry:
  229. * None.
  230. *
  231. *Exit:
  232. * Returns index of of new entry in new buffer, or EOF if error.
  233. *
  234. *Exceptions:
  235. * Returns EOF if OM error.
  236. *
  237. *******************************************************************************/
  238. int ios::xalloc()
  239. {
  240. #ifdef _MT
  241. // buffer must be static if multithread, since thread can't keep track of
  242. // validity of pointer otherwise
  243. int index;
  244. lockc();
  245. if (x_curindex >= MAXINDEX)
  246. index = EOF;
  247. else
  248. {
  249. index = ++x_curindex;
  250. }
  251. unlockc();
  252. return index;
  253. #else // _MT
  254. long * tptr;
  255. int i;
  256. if (!(tptr=_new_crt long[x_curindex+2])) // allocate new buffer
  257. return EOF;
  258. for (i=0; i <= x_curindex; i++) // copy old buffer, if any
  259. tptr[i] = x_statebuf[i];
  260. tptr[++x_curindex] = 0L; // init new entry, bump size
  261. if (x_statebuf) // delete old buffer, if any
  262. delete x_statebuf;
  263. x_statebuf = tptr; // and assign new buffer
  264. return x_curindex;
  265. #endif // _MT
  266. }
  267. /***
  268. *long ios::bitalloc() - ios bitalloc member function
  269. *
  270. *Purpose:
  271. * Returns a unused bit mask for flags().
  272. *
  273. *Entry:
  274. * None.
  275. *
  276. *Exit:
  277. * Returns next available bit maskf.
  278. *
  279. *Exceptions:
  280. *
  281. *******************************************************************************/
  282. long ios::bitalloc()
  283. {
  284. long b;
  285. lockc(); // lock to make sure mask in unique (_MT)
  286. b = (x_maxbit<<=1);
  287. unlockc();
  288. return b;
  289. }