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.

250 lines
4.5 KiB

  1. /***
  2. * istream.cpp - definitions for istream and istream_withassign classes
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Definitions of member functions for istream and istream_withassign
  8. * classes.
  9. * [AT&T C++]
  10. *
  11. *Revision History:
  12. * 07-15-91 KRS Created.
  13. * 08-15-91 KRS Fix handling of 1-length strings in get(char*,int,int)
  14. * 08-20-91 KRS Make read() not do text translation (for filebufs)
  15. * 08-21-91 KRS Fix >>(streambuf *) to advance pointer properly.
  16. * 08-22-91 KRS Fix octal error in getint().
  17. * 08-26-91 KRS Fix for Windows DLL's and set max. ibuffer[] lengths.
  18. * 09-05-91 KRS Don't special-case 0x in getint(). Spec. conformance...
  19. * 09-10-91 KRS Reinstate text translation (by default) for read().
  20. * 09-12-91 KRS Treat count as unsigned in get() and read().
  21. * 09-16-91 KRS Fix get(char *, int lim, char) for lim=0 case.
  22. * 09-23-91 KRS Split up flie for granularity purposes.
  23. * 10-21-91 KRS Make eatwhite() return void again.
  24. * 10-24-91 KRS Move istream_withassign::operator=(streambuf*) here.
  25. * 11-04-91 KRS Make constructors work with virtual base.
  26. * Fix whitespace error handling in ipfx().
  27. * 11-20-91 KRS Add/fix copy constructor and assignment operators.
  28. * 01-23-92 KRS C700 #5883: Fix behaviour of peek() to call ipfx(1).
  29. * 03-28-95 CFW Fix debug delete scheme.
  30. * 03-21-95 CFW Remove _delete_crt.
  31. * 06-14-95 CFW Comment cleanup.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <internal.h>
  36. #include <stdlib.h>
  37. #include <ctype.h>
  38. #include <iostream.h>
  39. #include <dbgint.h>
  40. #pragma hdrstop
  41. istream::istream()
  42. // : ios()
  43. {
  44. x_flags |= ios::skipws;
  45. x_gcount = 0;
  46. _fGline = 0;
  47. }
  48. istream::istream(streambuf* _inistbf)
  49. // : ios()
  50. {
  51. init(_inistbf);
  52. x_flags |= ios::skipws;
  53. x_gcount = 0;
  54. _fGline = 0;
  55. }
  56. istream::istream(const istream& _istrm)
  57. // : ios()
  58. {
  59. init(_istrm.rdbuf());
  60. x_flags |= ios::skipws;
  61. x_gcount = 0;
  62. _fGline = 0;
  63. }
  64. istream::~istream()
  65. // : ~ios()
  66. {
  67. }
  68. // used by ios::sync_with_stdio()
  69. istream& istream::operator=(streambuf * _sbuf)
  70. {
  71. if (delbuf() && rdbuf())
  72. delete rdbuf();
  73. bp = 0;
  74. this->ios::operator=(ios()); // initialize ios members
  75. delbuf(0); // important!
  76. init(_sbuf); // set up bp
  77. x_flags |= ios::skipws; // init istream members too
  78. x_gcount = 0;
  79. return *this;
  80. }
  81. int istream::ipfx(int need)
  82. {
  83. lock();
  84. if (need) // reset gcount if unformatted input
  85. x_gcount = 0;
  86. if (state) // return 0 iff error condition
  87. {
  88. state |= ios::failbit; // solves cin>>buf problem
  89. unlock();
  90. return 0;
  91. }
  92. if (x_tie && ((need==0) || (need > bp->in_avail())))
  93. {
  94. x_tie->flush();
  95. }
  96. lockbuf();
  97. if ((need==0) && (x_flags & ios::skipws))
  98. {
  99. eatwhite();
  100. if (state) // eof or error
  101. {
  102. state |= ios::failbit;
  103. unlockbuf();
  104. unlock();
  105. return 0;
  106. }
  107. }
  108. // leave locked ; isfx() will unlock
  109. return 1; // return nz if okay
  110. }
  111. // formatted input functions
  112. istream& istream::operator>>(char * s)
  113. {
  114. int c;
  115. unsigned int i, lim;
  116. if (ipfx(0))
  117. {
  118. lim = (unsigned)(x_width-1);
  119. x_width = 0;
  120. if (!s)
  121. {
  122. state |= ios::failbit;
  123. }
  124. else
  125. {
  126. for (i=0; i< lim; i++)
  127. {
  128. c=bp->sgetc();
  129. if (c==EOF)
  130. {
  131. state |= ios::eofbit;
  132. if (!i)
  133. state |= ios::failbit|ios::badbit;
  134. break;
  135. }
  136. else if (isspace(c))
  137. {
  138. break;
  139. }
  140. else
  141. {
  142. s[i] = (char)c;
  143. bp->stossc(); // advance pointer
  144. }
  145. }
  146. if (!i)
  147. state |= ios::failbit;
  148. else
  149. s[i] = '\0';
  150. }
  151. isfx();
  152. }
  153. return *this;
  154. }
  155. int istream::peek()
  156. {
  157. int retval;
  158. if (ipfx(1))
  159. {
  160. retval = (bp->sgetc());
  161. isfx();
  162. }
  163. else
  164. retval = EOF;
  165. return retval;
  166. }
  167. istream& istream::putback(char c)
  168. {
  169. if (good())
  170. {
  171. lockbuf();
  172. if (bp->sputbackc(c)==EOF)
  173. {
  174. clear(state | ios::failbit);
  175. }
  176. unlockbuf();
  177. }
  178. return *this;
  179. }
  180. int istream::sync()
  181. {
  182. int retval;
  183. lockbuf();
  184. if ((retval=bp->sync())==EOF)
  185. {
  186. clear(state | (ios::failbit|ios::badbit));
  187. }
  188. unlockbuf();
  189. return retval;
  190. }
  191. void istream::eatwhite()
  192. {
  193. int c;
  194. lockbuf();
  195. c = bp->sgetc();
  196. for ( ; ; )
  197. {
  198. if (c==EOF)
  199. {
  200. clear(state | ios::eofbit);
  201. break;
  202. }
  203. if (isspace(c))
  204. {
  205. c = bp->snextc();
  206. }
  207. else
  208. {
  209. break;
  210. }
  211. }
  212. unlockbuf();
  213. }
  214. istream_withassign::istream_withassign()
  215. : istream()
  216. {
  217. }
  218. istream_withassign::istream_withassign(streambuf* _is)
  219. : istream(_is)
  220. {
  221. }
  222. istream_withassign::~istream_withassign()
  223. // : ~istream()
  224. {
  225. }