Leaked source code of windows server 2003
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.

200 lines
5.1 KiB

  1. /***
  2. * ostream.cpp - definitions for ostream and ostream_withassign classes
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains the core member function definitions for ostream and
  8. * ostream_withassign classes.
  9. *
  10. *Revision History:
  11. * 07-01-91 KRS Created.
  12. * 08-19-91 KRS Corrected my interpretation of the spec. for negative
  13. * hex or octal integers.
  14. * 08-20-91 KRS Replace 'clear(x)' with 'state |= x'.
  15. * Skip text translation for write().
  16. * 08-26-91 KRS Modify to work with DLL's/MTHREAD.
  17. * 09-05-91 KRS Fix opfx() to flush tied ostream, not current one.
  18. * 09-09-91 KRS Remove sync_with_stdio() call from Iostream_init().
  19. * Reinstate text-translation (by default) for write().
  20. * 09-19-91 KRS Add opfx()/osfx() calls to put() and write().
  21. * Schedule destruction of predefined streams.
  22. * 09-23-91 KRS Split up for granularity.
  23. * 10-04-91 KRS Use bp->sputc, not put(), in writepad().
  24. * 10-24-91 KRS Added initialization of x_floatused.
  25. * 11-04-91 KRS Make constructors work with virtual base.
  26. * 11-20-91 KRS Add/fix copy constructor and assignment operators.
  27. * 03-28-95 CFW Fix debug delete scheme.
  28. * 03-21-95 CFW Remove _delete_crt.
  29. * 06-14-95 CFW Comment cleanup.
  30. * 01-05-99 GJF Changes for 64-bit size_t.
  31. *
  32. *******************************************************************************/
  33. #include <cruntime.h>
  34. #include <internal.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <iostream.h>
  39. #include <dbgint.h>
  40. #pragma hdrstop
  41. int ostream::opfx()
  42. {
  43. lock();
  44. if (state)
  45. {
  46. state |= ios::failbit;
  47. unlock();
  48. return 0;
  49. }
  50. if (x_tie)
  51. {
  52. x_tie->flush();
  53. }
  54. lockbuf();
  55. return(1); // return non-zero
  56. }
  57. void ostream::osfx()
  58. {
  59. x_width = 0;
  60. if (x_flags & unitbuf)
  61. {
  62. if (bp->sync()==EOF)
  63. state = failbit | badbit;
  64. }
  65. #ifndef _WINDLL
  66. if (x_flags & ios::stdio)
  67. {
  68. if (fflush(stdout)==EOF)
  69. state |= failbit;
  70. if (fflush(stderr)==EOF)
  71. state |= failbit;
  72. }
  73. #endif
  74. unlockbuf();
  75. unlock();
  76. }
  77. // note: called inline by unsigned char * and signed char * versions:
  78. ostream& ostream::operator<<(const char * s)
  79. {
  80. if (opfx()) {
  81. writepad("",s);
  82. osfx();
  83. }
  84. return *this;
  85. }
  86. ostream& ostream::flush()
  87. {
  88. lock();
  89. lockbuf();
  90. if (bp->sync()==EOF)
  91. state |= ios::failbit;
  92. unlockbuf();
  93. unlock();
  94. return(*this);
  95. }
  96. ostream::ostream()
  97. // : ios()
  98. {
  99. x_floatused = 0;
  100. }
  101. ostream::ostream(streambuf* _inistbf)
  102. // : ios()
  103. {
  104. init(_inistbf);
  105. x_floatused = 0;
  106. }
  107. ostream::ostream(const ostream& _ostrm)
  108. // : ios()
  109. {
  110. init(_ostrm.rdbuf());
  111. x_floatused = 0;
  112. }
  113. ostream::~ostream()
  114. // : ~ios()
  115. {
  116. }
  117. // used in ios::sync_with_stdio()
  118. ostream& ostream::operator=(streambuf * _sbuf)
  119. {
  120. if (delbuf() && rdbuf())
  121. delete rdbuf();
  122. bp = 0;
  123. this->ios::operator=(ios()); // initialize ios members
  124. delbuf(0); // important!
  125. init(_sbuf);
  126. return *this;
  127. }
  128. ostream_withassign::ostream_withassign()
  129. : ostream()
  130. {
  131. }
  132. ostream_withassign::ostream_withassign(streambuf* _os)
  133. : ostream(_os)
  134. {
  135. }
  136. ostream_withassign::~ostream_withassign()
  137. // : ~ostream()
  138. {
  139. }
  140. ostream& ostream::writepad(const char * leader, const char * value)
  141. {
  142. unsigned int len, leadlen;
  143. long padlen;
  144. leadlen = (unsigned int)strlen(leader);
  145. len = (unsigned int)strlen(value);
  146. padlen = (((unsigned)x_width) > (len+leadlen)) ? ((unsigned)x_width) - (len + leadlen) : 0;
  147. if (!(x_flags & (left|internal))) // default is right-adjustment
  148. {
  149. while (padlen-- >0)
  150. {
  151. if (bp->sputc((unsigned char)x_fill)==EOF)
  152. state |= (ios::failbit|ios::badbit);
  153. }
  154. }
  155. if (leadlen)
  156. {
  157. if ((unsigned)bp->sputn(leader,leadlen)!=leadlen)
  158. state |= (failbit|badbit);
  159. }
  160. if (x_flags & internal)
  161. {
  162. while (padlen-- >0)
  163. {
  164. if (bp->sputc((unsigned char)x_fill)==EOF)
  165. state |= (ios::failbit|ios::badbit);
  166. }
  167. }
  168. if ((unsigned)bp->sputn(value,len)!=len)
  169. state |= (failbit|badbit);
  170. if (x_flags & left)
  171. {
  172. while ((padlen--)>0) // left-adjust if necessary
  173. {
  174. if (bp->sputc((unsigned char)x_fill)==EOF)
  175. state |= (ios::failbit|ios::badbit);
  176. }
  177. }
  178. return (*this);
  179. }