home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / _ios.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  313 lines

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