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

  1. /***
  2. * ostream.cpp - definitions for ostream and ostream_withassign classes
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains the core member function definitions for ostream and
  8. *       ostream_withassign classes.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <internal.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <iostream.h>
  18. #include <dbgint.h>
  19. #pragma hdrstop
  20.  
  21. int ostream::opfx()
  22. {
  23.     lock();
  24.     if (state)
  25.         {
  26.         state |= ios::failbit;
  27.         unlock();
  28.         return 0;
  29.         }
  30.     if (x_tie)
  31.         {
  32.         x_tie->flush();
  33.         }
  34.     lockbuf();
  35.     return(1);  // return non-zero
  36. }
  37.  
  38. void ostream::osfx()
  39. {
  40.     x_width = 0;
  41.     if (x_flags & unitbuf)
  42.         {
  43.         if (bp->sync()==EOF)
  44.             state = failbit | badbit;
  45.         }
  46.     if (x_flags & ios::stdio)
  47.         {
  48.         if (fflush(stdout)==EOF)
  49.             state |= failbit;
  50.         if (fflush(stderr)==EOF)
  51.             state |= failbit;
  52.         }
  53.     unlockbuf();
  54.     unlock();
  55. }
  56.  
  57. // note: called inline by unsigned char * and signed char * versions:
  58. ostream& ostream::operator<<(const char * s)
  59. {
  60.     if (opfx()) {
  61.         writepad("",s);
  62.         osfx();
  63.     }
  64.     return *this;
  65. }
  66.  
  67. ostream& ostream::flush()
  68. {
  69.     lock();
  70.     lockbuf();
  71.     if (bp->sync()==EOF)
  72.         state |= ios::failbit;
  73.     unlockbuf();
  74.     unlock();
  75.     return(*this);
  76. }
  77.  
  78.         ostream::ostream()
  79. // : ios()
  80. {
  81.         x_floatused = 0;
  82. }
  83.  
  84.         ostream::ostream(streambuf* _inistbf)
  85. // : ios()
  86. {
  87.         init(_inistbf);
  88.  
  89.         x_floatused = 0;
  90. }
  91.  
  92.         ostream::ostream(const ostream& _ostrm)
  93. // : ios()
  94. {
  95.         init(_ostrm.rdbuf());
  96.  
  97.         x_floatused = 0;
  98. }
  99.  
  100.         ostream::~ostream()
  101. // : ~ios()
  102. {
  103. }
  104.  
  105. // used in ios::sync_with_stdio()
  106. ostream& ostream::operator=(streambuf * _sbuf)
  107. {
  108.  
  109.         if (delbuf() && rdbuf())
  110.             delete rdbuf();
  111.  
  112.         bp = 0;
  113.  
  114.         this->ios::operator=(ios());    // initialize ios members
  115.         delbuf(0);                      // important!
  116.         init(_sbuf);
  117.  
  118.         return *this;
  119. }
  120.  
  121.  
  122.         ostream_withassign::ostream_withassign()
  123. : ostream()
  124. {
  125. }
  126.  
  127.         ostream_withassign::ostream_withassign(streambuf* _os)
  128. : ostream(_os)
  129. {
  130. }
  131.  
  132.         ostream_withassign::~ostream_withassign()
  133. // : ~ostream()
  134. {
  135. }
  136.  
  137. ostream& ostream::writepad(const char * leader, const char * value)
  138.  
  139.         {
  140. unsigned int len, leadlen;
  141. long padlen;
  142.         leadlen = strlen(leader);
  143.         len = strlen(value);
  144.         padlen = (((unsigned)x_width) > (len+leadlen)) ? ((unsigned)x_width) - (len + leadlen) : 0;
  145.         if (!(x_flags & (left|internal)))  // default is right-adjustment
  146.             {
  147.             while (padlen-- >0)
  148.                 {
  149.                 if (bp->sputc((unsigned char)x_fill)==EOF)
  150.                     state |= (ios::failbit|ios::badbit);
  151.                 }
  152.             }
  153.         if (leadlen)
  154.             {
  155.             if ((unsigned)bp->sputn(leader,leadlen)!=leadlen)
  156.                 state |= (failbit|badbit);
  157.             }
  158.         if (x_flags & internal)
  159.             {
  160.             while (padlen-- >0)
  161.                 {
  162.                 if (bp->sputc((unsigned char)x_fill)==EOF)
  163.                     state |= (ios::failbit|ios::badbit);
  164.                 }
  165.             }
  166.         if ((unsigned)bp->sputn(value,len)!=len)
  167.             state |= (failbit|badbit);
  168.         if (x_flags & left)
  169.             {
  170.             while ((padlen--)>0)        // left-adjust if necessary
  171.                 {
  172.                 if (bp->sputc((unsigned char)x_fill)==EOF)
  173.                     state |= (ios::failbit|ios::badbit);
  174.                 }
  175.             }
  176.         return (*this);
  177.         }
  178.