_istream.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_ISTREAM_C
  19. #define _STLP_ISTREAM_C
  20. #ifndef _STLP_INTERNAL_ISTREAM
  21. # include <stl/_istream.h>
  22. #endif
  23. #ifndef _STLP_INTERNAL_LIMITS
  24. # include <stl/_limits.h>
  25. #endif
  26. #ifndef _STLP_INTERNAL_NUM_GET_H
  27. # include <stl/_num_get.h>
  28. #endif
  29. #if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
  30. // no wchar_t is supported for this mode
  31. # define __BIS_int_type__ int
  32. # define __BIS_pos_type__ streampos
  33. # define __BIS_off_type__ streamoff
  34. #else
  35. # define __BIS_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::int_type
  36. # define __BIS_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::pos_type
  37. # define __BIS_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::off_type
  38. #endif
  39. _STLP_BEGIN_NAMESPACE
  40. //----------------------------------------------------------------------
  41. // Function object structs used by some member functions.
  42. _STLP_MOVE_TO_PRIV_NAMESPACE
  43. template <class _Traits>
  44. struct _Is_not_wspace {
  45. typedef typename _Traits::char_type argument_type;
  46. typedef bool result_type;
  47. const ctype<argument_type>* _M_ctype;
  48. _Is_not_wspace(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
  49. bool operator()(argument_type __c) const
  50. { return !_M_ctype->is(ctype_base::space, __c); }
  51. };
  52. template <class _Traits>
  53. struct _Is_wspace_null {
  54. typedef typename _Traits::char_type argument_type;
  55. typedef bool result_type;
  56. const ctype<argument_type>* _M_ctype;
  57. _Is_wspace_null(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
  58. bool operator()(argument_type __c) const {
  59. return _Traits::eq(__c, argument_type()) ||
  60. _M_ctype->is(ctype_base::space, __c);
  61. }
  62. };
  63. template <class _Traits>
  64. struct _Scan_for_wspace {
  65. typedef typename _Traits::char_type char_type;
  66. typedef char_type* first_argument_type;
  67. typedef char_type* second_argument_type;
  68. typedef char_type* result_type;
  69. const ctype<char_type>* _M_ctype;
  70. _Scan_for_wspace(const ctype<char_type>* __ctype) : _M_ctype(__ctype) {}
  71. const char_type*
  72. operator()(const char_type* __first, const char_type* __last) const {
  73. return _M_ctype->scan_is(ctype_base::space, __first, __last);
  74. }
  75. };
  76. template <class _Traits>
  77. struct _Scan_wspace_null {
  78. typedef typename _Traits::char_type char_type;
  79. typedef char_type* first_argument_type;
  80. typedef char_type* second_argument_type;
  81. typedef char_type* result_type;
  82. const ctype<char_type>* _M_ctype;
  83. _Scan_wspace_null(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
  84. const char_type*
  85. operator()(const char_type* __first, const char_type* __last) const {
  86. __last = find_if(__first, __last,
  87. _Eq_char_bound<_Traits>(char_type()));
  88. return _M_ctype->scan_is(ctype_base::space, __first, __last);
  89. }
  90. };
  91. template <class _Traits>
  92. struct _Scan_for_not_wspace {
  93. typedef typename _Traits::char_type char_type;
  94. typedef char_type* first_argument_type;
  95. typedef char_type* second_argument_type;
  96. typedef char_type* result_type;
  97. const ctype<char_type>* _M_ctype;
  98. _Scan_for_not_wspace(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
  99. const char_type*
  100. operator()(const char_type* __first, const char_type* __last) const {
  101. return _M_ctype->scan_not(ctype_base::space, __first, __last);
  102. }
  103. };
  104. template <class _Traits>
  105. struct _Scan_for_char_val {
  106. typedef typename _Traits::char_type char_type;
  107. typedef char_type* first_argument_type;
  108. typedef char_type* second_argument_type;
  109. typedef char_type* result_type;
  110. char_type _M_val;
  111. _Scan_for_char_val(char_type __val) : _M_val(__val) {}
  112. const char_type*
  113. operator()(const char_type* __first, const char_type* __last) const {
  114. return find_if(__first, __last, _Eq_char_bound<_Traits>(_M_val));
  115. }
  116. };
  117. template <class _Traits>
  118. struct _Scan_for_int_val {
  119. typedef typename _Traits::char_type char_type;
  120. typedef typename _Traits::int_type int_type;
  121. typedef char_type* first_argument_type;
  122. typedef char_type* second_argument_type;
  123. typedef char_type* result_type;
  124. int_type _M_val;
  125. _Scan_for_int_val(int_type __val) : _M_val(__val) {}
  126. const char_type*
  127. operator()(const char_type* __first, const char_type* __last) const {
  128. return find_if(__first, __last,
  129. _Eq_int_bound<_Traits>(_M_val));
  130. }
  131. };
  132. // Helper function: try to push back a character to a streambuf,
  133. // return true if the pushback succeeded. Does not throw.
  134. template <class _CharT, class _Traits>
  135. bool _STLP_CALL
  136. __pushback(basic_streambuf<_CharT, _Traits>* __buf, _CharT __c) {
  137. bool ret;
  138. _STLP_TRY {
  139. const typename _Traits::int_type __eof = _Traits::eof();
  140. ret = !_Traits::eq_int_type(__buf->sputbackc(__c), __eof);
  141. }
  142. _STLP_CATCH_ALL {
  143. ret = false;
  144. }
  145. return ret;
  146. }
  147. //----------------------------------------------------------------------
  148. // Definitions of basic_istream<>'s noninline member functions.
  149. // Helper function for formatted input of numbers.
  150. template <class _CharT, class _Traits, class _Number>
  151. ios_base::iostate _STLP_CALL
  152. __get_num(basic_istream<_CharT, _Traits>& __that, _Number& __val) {
  153. typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
  154. ios_base::iostate __err = 0;
  155. _Sentry __sentry( __that ); // Skip whitespace.
  156. if (__sentry) {
  157. typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > _Num_get;
  158. _STLP_TRY {
  159. // Do not remove additional parenthesis around use_facet instanciation, some compilers (VC6)
  160. // require it when building the library.
  161. (use_facet<_Num_get>(__that.getloc())).get(istreambuf_iterator<_CharT, _Traits>(__that.rdbuf()),
  162. 0, __that, __err, __val);
  163. }
  164. _STLP_CATCH_ALL {
  165. __that._M_handle_exception(ios_base::badbit);
  166. }
  167. if (__err) __that.setstate(__err);
  168. }
  169. return __err;
  170. }
  171. _STLP_MOVE_TO_STD_NAMESPACE
  172. template <class _CharT, class _Traits>
  173. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (short& __val) {
  174. long __lval;
  175. _STLP_PRIV __get_num(*this, __lval);
  176. if ( this->fail() ) {
  177. return *this;
  178. }
  179. short __tmp = __STATIC_CAST(short, __lval);
  180. unsigned short __uval = __STATIC_CAST(unsigned short, __lval);
  181. // check if we lose digits
  182. // if ((__val != __lval) && ((unsigned short)__val != __lval))
  183. if ((__tmp != __lval) && ((long)__uval != __lval))
  184. this->setstate(ios_base::failbit);
  185. else
  186. __val = __tmp;
  187. return *this;
  188. }
  189. template <class _CharT, class _Traits>
  190. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (int& __val) {
  191. long __lval;
  192. _STLP_PRIV __get_num(*this, __lval);
  193. if ( this->fail() ) {
  194. return *this;
  195. }
  196. int __tmp = __lval;
  197. unsigned int __uval = __lval;
  198. // check if we lose digits
  199. // if ((__val != __lval) && ((unsigned int)__val != __lval))
  200. if ((__tmp != __lval) && ((long)__uval != __lval))
  201. this->setstate(ios_base::failbit);
  202. else
  203. __val = __tmp;
  204. return *this;
  205. }
  206. template <class _CharT, class _Traits>
  207. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned short& __val) {
  208. _STLP_PRIV __get_num(*this, __val);
  209. return *this;
  210. }
  211. template <class _CharT, class _Traits>
  212. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned int& __val) {
  213. _STLP_PRIV __get_num(*this, __val);
  214. return *this;
  215. }
  216. template <class _CharT, class _Traits>
  217. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long& __val) {
  218. _STLP_PRIV __get_num(*this, __val);
  219. return *this;
  220. }
  221. template <class _CharT, class _Traits>
  222. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned long& __val) {
  223. _STLP_PRIV __get_num(*this, __val);
  224. return *this;
  225. }
  226. #if defined (_STLP_LONG_LONG)
  227. template <class _CharT, class _Traits>
  228. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (_STLP_LONG_LONG& __val) {
  229. _STLP_PRIV __get_num(*this, __val);
  230. return *this;
  231. }
  232. template <class _CharT, class _Traits>
  233. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned _STLP_LONG_LONG& __val) {
  234. _STLP_PRIV __get_num(*this, __val);
  235. return *this;
  236. }
  237. #endif
  238. template <class _CharT, class _Traits>
  239. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (float& __val) {
  240. _STLP_PRIV __get_num(*this, __val);
  241. return *this;
  242. }
  243. template <class _CharT, class _Traits>
  244. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (double& __val) {
  245. _STLP_PRIV __get_num(*this, __val);
  246. return *this;
  247. }
  248. #if !defined (_STLP_NO_LONG_DOUBLE)
  249. template <class _CharT, class _Traits>
  250. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long double& __val) {
  251. _STLP_PRIV __get_num(*this, __val);
  252. return *this;
  253. }
  254. #endif
  255. #if !defined (_STLP_NO_BOOL)
  256. template <class _CharT, class _Traits>
  257. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (bool& __val) {
  258. _STLP_PRIV __get_num(*this, __val);
  259. return *this;
  260. }
  261. #endif
  262. template <class _CharT, class _Traits>
  263. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (void*& __val) {
  264. _STLP_PRIV __get_num(*this, __val);
  265. return *this;
  266. }
  267. // Unformatted input
  268. template <class _CharT, class _Traits>
  269. __BIS_int_type__
  270. basic_istream<_CharT, _Traits>::peek() {
  271. typename _Traits::int_type __tmp = _Traits::eof();
  272. this->_M_gcount = 0;
  273. sentry __sentry(*this, _No_Skip_WS());
  274. if (__sentry) {
  275. _STLP_TRY {
  276. __tmp = this->rdbuf()->sgetc();
  277. }
  278. _STLP_CATCH_ALL {
  279. this->_M_handle_exception(ios_base::badbit);
  280. }
  281. if (this->_S_eof(__tmp))
  282. this->setstate(ios_base::eofbit);
  283. }
  284. return __tmp;
  285. }
  286. template <class _CharT, class _Traits>
  287. __BIS_int_type__
  288. basic_istream<_CharT, _Traits>::get() {
  289. typename _Traits::int_type __tmp = _Traits::eof();
  290. sentry __sentry(*this, _No_Skip_WS());
  291. this->_M_gcount = 0;
  292. if (__sentry) {
  293. _STLP_TRY {
  294. __tmp = this->rdbuf()->sbumpc();
  295. }
  296. _STLP_CATCH_ALL {
  297. this->_M_handle_exception(ios_base::badbit);
  298. }
  299. if (!this->_S_eof(__tmp))
  300. this->_M_gcount = 1;
  301. }
  302. if (_M_gcount == 0)
  303. this->setstate(ios_base::eofbit | ios_base::failbit);
  304. return __tmp;
  305. }
  306. template <class _CharT, class _Traits>
  307. basic_istream<_CharT, _Traits>&
  308. basic_istream<_CharT, _Traits>::get(_CharT& __c) {
  309. sentry __sentry(*this, _No_Skip_WS());
  310. this->_M_gcount = 0;
  311. if (__sentry) {
  312. typename _Traits::int_type __tmp = _Traits::eof();
  313. _STLP_TRY {
  314. __tmp = this->rdbuf()->sbumpc();
  315. }
  316. _STLP_CATCH_ALL {
  317. this->_M_handle_exception(ios_base::badbit);
  318. }
  319. if (!this->_S_eof(__tmp)) {
  320. this->_M_gcount = 1;
  321. __c = _Traits::to_char_type(__tmp);
  322. }
  323. }
  324. if (this->_M_gcount == 0)
  325. this->setstate(ios_base::eofbit | ios_base::failbit);
  326. return *this;
  327. }
  328. // Read characters and discard them. The standard specifies a single
  329. // function with two arguments, each with a default. We instead use
  330. // three overloded functions, because it's possible to implement the
  331. // first two more efficiently than the fully general third version.
  332. template <class _CharT, class _Traits>
  333. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore() {
  334. sentry __sentry(*this, _No_Skip_WS());
  335. this->_M_gcount = 0;
  336. if (__sentry) {
  337. int_type __c;
  338. _STLP_TRY {
  339. __c = this->rdbuf()->sbumpc();
  340. }
  341. _STLP_CATCH_ALL {
  342. this->_M_handle_exception(ios_base::badbit);
  343. return *this;
  344. }
  345. if (!this->_S_eof(__c))
  346. this->_M_gcount = 1;
  347. else
  348. this->setstate(ios_base::eofbit);
  349. }
  350. return *this;
  351. }
  352. // Putback
  353. template <class _CharT, class _Traits>
  354. basic_istream<_CharT, _Traits>&
  355. basic_istream<_CharT, _Traits>::putback(_CharT __c) {
  356. this->_M_gcount = 0;
  357. sentry __sentry(*this, _No_Skip_WS());
  358. if (__sentry) {
  359. typename _Traits::int_type __tmp = _Traits::eof();
  360. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  361. // if (!__buf || this->_S_eof(__buf->sputbackc(__c)))
  362. if (__buf) {
  363. _STLP_TRY {
  364. __tmp = __buf->sputbackc(__c);
  365. }
  366. _STLP_CATCH_ALL {
  367. this->_M_handle_exception(ios_base::badbit);
  368. }
  369. }
  370. if (this->_S_eof(__tmp))
  371. this->setstate(ios_base::badbit);
  372. }
  373. else
  374. this->setstate(ios_base::failbit);
  375. return *this;
  376. }
  377. template <class _CharT, class _Traits>
  378. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
  379. this->_M_gcount = 0;
  380. sentry __sentry(*this, _No_Skip_WS());
  381. if (__sentry) {
  382. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  383. // if (!__buf || _Traits::eq_int_type(__buf->sungetc(), _Traits::eof()))
  384. if (__buf) {
  385. _STLP_TRY {
  386. if (this->_S_eof(__buf->sungetc()))
  387. this->setstate(ios_base::badbit);
  388. }
  389. _STLP_CATCH_ALL {
  390. this->_M_handle_exception(ios_base::badbit);
  391. }
  392. } else
  393. this->setstate(ios_base::badbit);
  394. }
  395. else
  396. this->setstate(ios_base::failbit);
  397. return *this;
  398. }
  399. // Positioning and buffer control.
  400. template <class _CharT, class _Traits>
  401. int basic_istream<_CharT, _Traits>::sync() {
  402. sentry __sentry(*this, _No_Skip_WS());
  403. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  404. if (__buf) {
  405. if (__buf->pubsync() == -1) {
  406. this->setstate(ios_base::badbit);
  407. return -1;
  408. }
  409. else
  410. return 0;
  411. }
  412. else
  413. return -1;
  414. }
  415. template <class _CharT, class _Traits>
  416. __BIS_pos_type__
  417. basic_istream<_CharT, _Traits>::tellg() {
  418. sentry __sentry(*this, _No_Skip_WS());
  419. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  420. return (__buf && !this->fail()) ? __buf->pubseekoff(0, ios_base::cur, ios_base::in)
  421. : pos_type(-1);
  422. }
  423. template <class _CharT, class _Traits>
  424. basic_istream<_CharT, _Traits>&
  425. basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
  426. sentry __sentry(*this, _No_Skip_WS());
  427. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  428. if (!this->fail() && __buf) {
  429. if (__buf->pubseekpos(__pos, ios_base::in) == pos_type(-1)) {
  430. this->setstate(ios_base::failbit);
  431. }
  432. }
  433. return *this;
  434. }
  435. template <class _CharT, class _Traits>
  436. basic_istream<_CharT, _Traits>&
  437. basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
  438. sentry __sentry(*this, _No_Skip_WS());
  439. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  440. if (!this->fail() && __buf)
  441. __buf->pubseekoff(__off, __dir, ios_base::in);
  442. return *this;
  443. }
  444. // Formatted input of characters and character arrays.
  445. template <class _CharT, class _Traits>
  446. void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT& __c) {
  447. // typename _Traits::int_type __tmp = _Traits::eof();
  448. sentry __sentry(*this); // Skip whitespace.
  449. if (__sentry) {
  450. typename _Traits::int_type __tmp;// = _Traits::eof();
  451. _STLP_TRY {
  452. __tmp = this->rdbuf()->sbumpc();
  453. }
  454. _STLP_CATCH_ALL {
  455. this->_M_handle_exception(ios_base::badbit);
  456. return;
  457. }
  458. if (!this->_S_eof(__tmp))
  459. __c = _Traits::to_char_type(__tmp);
  460. else
  461. this->setstate(ios_base::eofbit | ios_base::failbit);
  462. }
  463. }
  464. //---------------------------------------------------------------------------
  465. // istream's helper functions.
  466. // A generic function for unbuffered input. We stop when we reach EOF,
  467. // or when we have extracted _Num characters, or when the function object
  468. // __is_delim return true. In the last case, it extracts the character
  469. // for which __is_delim is true, if and only if __extract_delim is true.
  470. // It appends a null character to the end of the string; this means that
  471. // it may store up to _Num + 1 characters.
  472. //
  473. // __is_getline governs two corner cases: reading _Num characters without
  474. // encountering delim or eof (in which case failbit is set if __is_getline
  475. // is true); and reading _Num characters where the _Num+1'st character is
  476. // eof (in which case eofbit is set if __is_getline is true).
  477. //
  478. // It is assumed that __is_delim never throws.
  479. //
  480. // Return value is the number of characters extracted, including the
  481. // delimiter if it is extracted. Note that the number of characaters
  482. // extracted isn't necessarily the same as the number stored.
  483. _STLP_MOVE_TO_PRIV_NAMESPACE
  484. template < class _CharT, class _Traits, class _Is_Delim>
  485. streamsize _STLP_CALL
  486. __read_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
  487. streamsize _Num, _CharT* __s,
  488. _Is_Delim __is_delim,
  489. bool __extract_delim, bool __append_null,
  490. bool __is_getline)
  491. {
  492. streamsize __n = 0;
  493. ios_base::iostate __status = 0;
  494. typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  495. // The operations that can potentially throw are sbumpc, snextc, and sgetc.
  496. _STLP_TRY {
  497. for (;;) {
  498. if (__n == _Num) {
  499. if (__is_getline) // didn't find delimiter as one of the _Num chars
  500. __status |= ios_base::failbit;
  501. break;
  502. }
  503. int_type __c = __buf->sbumpc(); // sschwarz
  504. if (__that->_S_eof(__c)) {
  505. if (__n < _Num || __is_getline)
  506. __status |= ios_base::eofbit;
  507. break;
  508. } else if (__is_delim(_Traits::to_char_type(__c))) {
  509. if (__extract_delim) { // Extract and discard current character.
  510. ++__n;
  511. } else if ( !__pushback(__buf, _Traits::to_char_type(__c)) ) { // leave delimiter
  512. __status |= ios_base::failbit;
  513. }
  514. break;
  515. }
  516. // regular character
  517. *__s++ = _Traits::to_char_type(__c);
  518. ++__n;
  519. }
  520. }
  521. _STLP_CATCH_ALL {
  522. __that->_M_handle_exception(ios_base::badbit);
  523. *__s = _STLP_DEFAULT_CONSTRUCTED(_CharT);
  524. return __n;
  525. }
  526. if (__append_null)
  527. *__s = _STLP_DEFAULT_CONSTRUCTED(_CharT);
  528. if (__status)
  529. __that->setstate(__status); // This might throw.
  530. return __n;
  531. }
  532. // Much like __read_unbuffered, but with one additional function object:
  533. // __scan_delim(first, last) returns the first pointer p in [first, last)
  534. // such that __is_delim(p) is true.
  535. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  536. streamsize _STLP_CALL
  537. __read_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
  538. streamsize _Num, _CharT* __s,
  539. _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  540. bool __extract_delim, bool __append_null,
  541. bool __is_getline) {
  542. streamsize __n = 0;
  543. ios_base::iostate __status = 0;
  544. bool __done = false;
  545. _STLP_TRY {
  546. while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
  547. const _CharT* __first = __buf->_M_gptr();
  548. const _CharT* __last = __buf->_M_egptr();
  549. //casting numeric_limits<ptrdiff_t>::max to streamsize only works is ptrdiff_t is signed or streamsize representation
  550. //is larger than ptrdiff_t one.
  551. _STLP_STATIC_ASSERT((sizeof(streamsize) > sizeof(ptrdiff_t)) ||
  552. ((sizeof(streamsize) == sizeof(ptrdiff_t)) && numeric_limits<ptrdiff_t>::is_signed))
  553. ptrdiff_t __request = __STATIC_CAST(ptrdiff_t, (min) (__STATIC_CAST(streamsize, (numeric_limits<ptrdiff_t>::max)()), _Num - __n));
  554. const _CharT* __p = __scan_delim(__first, __last);
  555. ptrdiff_t __chunk = (min) (ptrdiff_t(__p - __first), __request);
  556. _Traits::copy(__s, __first, __chunk);
  557. __s += __chunk;
  558. __n += __chunk;
  559. __buf->_M_gbump((int)__chunk);
  560. // We terminated by finding delim.
  561. if (__p != __last && __p - __first <= __request) {
  562. if (__extract_delim) {
  563. __n += 1;
  564. __buf->_M_gbump(1);
  565. }
  566. __done = true;
  567. }
  568. // We terminated by reading all the characters we were asked for.
  569. else if (__n == _Num) {
  570. // Find out if we have reached eof. This matters for getline.
  571. if (__is_getline) {
  572. if (__chunk == __last - __first) {
  573. if (__that->_S_eof(__buf->sgetc()))
  574. __status |= ios_base::eofbit;
  575. }
  576. else
  577. __status |= ios_base::failbit;
  578. }
  579. __done = true;
  580. }
  581. // The buffer contained fewer than _Num - __n characters. Either we're
  582. // at eof, or we should refill the buffer and try again.
  583. else {
  584. if (__that->_S_eof(__buf->sgetc())) {
  585. __status |= ios_base::eofbit;
  586. __done = true;
  587. }
  588. }
  589. } // Close the while loop.
  590. }
  591. _STLP_CATCH_ALL {
  592. __that->_M_handle_exception(ios_base::badbit);
  593. __done = true;
  594. }
  595. if (__done) {
  596. if (__append_null)
  597. *__s = _STLP_DEFAULT_CONSTRUCTED(_CharT);
  598. if (__status != 0)
  599. __that->setstate(__status); // This might throw.
  600. return __n;
  601. }
  602. // If execution has reached this point, then we have an empty buffer but
  603. // we have not reached eof. What that means is that the streambuf has
  604. // decided to switch from buffered to unbuffered input. We switch to
  605. // to __read_unbuffered.
  606. return __n + __read_unbuffered(__that, __buf, _Num - __n, __s, __is_delim,
  607. __extract_delim,__append_null,__is_getline);
  608. }
  609. _STLP_MOVE_TO_STD_NAMESPACE
  610. template <class _CharT, class _Traits>
  611. basic_istream<_CharT, _Traits>&
  612. basic_istream<_CharT, _Traits>::get(_CharT* __s, streamsize __n,
  613. _CharT __delim) {
  614. sentry __sentry(*this, _No_Skip_WS());
  615. this->_M_gcount = 0;
  616. if (__sentry) {
  617. if (__n > 0) {
  618. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  619. if (__buf->egptr() != __buf->gptr())
  620. this->_M_gcount =
  621. _STLP_PRIV __read_buffered(this, __buf, __n - 1, __s,
  622. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  623. _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
  624. false, true, false);
  625. else
  626. this->_M_gcount =
  627. _STLP_PRIV __read_unbuffered(this, __buf, __n - 1, __s,
  628. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  629. false, true, false);
  630. }
  631. }
  632. if (this->_M_gcount == 0)
  633. this->setstate(ios_base::failbit);
  634. return *this;
  635. }
  636. // Getline is essentially identical to get, except that it extracts
  637. // the delimiter.
  638. template <class _CharT, class _Traits>
  639. basic_istream<_CharT, _Traits>&
  640. basic_istream<_CharT, _Traits>::getline(_CharT* __s, streamsize __n,
  641. _CharT __delim) {
  642. sentry __sentry(*this, _No_Skip_WS());
  643. this->_M_gcount = 0;
  644. if (__sentry) {
  645. if (__n > 0) {
  646. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  647. this->_M_gcount = __buf->egptr() != __buf->gptr()
  648. ? _STLP_PRIV __read_buffered(this, __buf, __n - 1, __s,
  649. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  650. _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
  651. true, true, true)
  652. : _STLP_PRIV __read_unbuffered(this, __buf, __n - 1, __s,
  653. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  654. true, true, true);
  655. }
  656. }
  657. if (this->_M_gcount == 0)
  658. this->setstate(ios_base::failbit);
  659. return *this;
  660. }
  661. // Read n characters. We don't look for any delimiter, and we don't
  662. // put in a terminating null character.
  663. template <class _CharT, class _Traits>
  664. basic_istream<_CharT, _Traits>&
  665. basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
  666. sentry __sentry(*this, _No_Skip_WS());
  667. this->_M_gcount = 0;
  668. if (__sentry && !this->eof()) {
  669. basic_streambuf<_CharT, _Traits>*__buf = this->rdbuf();
  670. if (__buf->gptr() != __buf->egptr())
  671. _M_gcount
  672. = _STLP_PRIV __read_buffered(this, __buf, __n, __s,
  673. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  674. _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
  675. false, false, false);
  676. else
  677. _M_gcount
  678. = _STLP_PRIV __read_unbuffered(this, __buf, __n, __s,
  679. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  680. false, false, false);
  681. }
  682. else
  683. this->setstate(ios_base::failbit);
  684. if (this->eof())
  685. this->setstate(ios_base::eofbit | ios_base::failbit);
  686. return *this;
  687. }
  688. // Read n or fewer characters. We don't look for any delimiter, and
  689. // we don't put in a terminating null character.
  690. template <class _CharT, class _Traits>
  691. streamsize
  692. basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __nmax) {
  693. sentry __sentry(*this, _No_Skip_WS());
  694. this->_M_gcount = 0;
  695. if (__sentry && !this->eof() && __nmax >= 0) {
  696. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  697. streamsize __avail = __buf->in_avail();
  698. // fbp : isn't full-blown setstate required here ?
  699. if (__avail == -1)
  700. this->_M_setstate_nothrow(ios_base::eofbit);
  701. else if (__avail != 0) {
  702. if (__buf->gptr() != __buf->egptr())
  703. _M_gcount
  704. = _STLP_PRIV __read_buffered(this, __buf, (min) (__avail, __nmax), __s,
  705. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  706. _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
  707. false, false, false);
  708. else
  709. _M_gcount
  710. = _STLP_PRIV __read_unbuffered(this, __buf, (min) (__avail, __nmax), __s,
  711. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  712. false, false, false);
  713. }
  714. }
  715. else {
  716. // fbp : changed so that failbit is set only there, to pass Dietmar's test
  717. if (this->eof())
  718. this->setstate(ios_base::eofbit | ios_base::failbit);
  719. else
  720. this->setstate(ios_base::failbit);
  721. }
  722. // if (this->eof())
  723. // this->setstate(ios_base::eofbit | ios_base::failbit);
  724. return _M_gcount;
  725. }
  726. template <class _CharT, class _Traits>
  727. void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT* __s) {
  728. sentry __sentry(*this); // Skip whitespace.
  729. if (__sentry) {
  730. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  731. streamsize __nmax = this->width() > 0
  732. ? this->width() - 1
  733. : ((numeric_limits<streamsize>::max)() / sizeof(_CharT)) - 1;
  734. streamsize __n = __buf->gptr() != __buf->egptr()
  735. ? _STLP_PRIV __read_buffered(this, __buf, __nmax, __s,
  736. _STLP_PRIV _Is_wspace_null<_Traits>(this->_M_ctype_facet()),
  737. _STLP_PRIV _Scan_wspace_null<_Traits>(this->_M_ctype_facet()),
  738. false, true, false)
  739. : _STLP_PRIV __read_unbuffered(this, __buf, __nmax, __s,
  740. _STLP_PRIV _Is_wspace_null<_Traits>(this->_M_ctype_facet()),
  741. false, true, false);
  742. if (__n == 0)
  743. this->setstate(ios_base::failbit);
  744. }
  745. this->width(0);
  746. }
  747. // A generic unbuffered function for ignoring characters. We stop
  748. // when we reach EOF, or when the function object __is_delim returns
  749. // true. In the last case, it extracts the character for which
  750. // __is_delim is true, if and only if __extract_delim is true.
  751. template < class _CharT, class _Traits, class _Is_Delim>
  752. void _STLP_CALL
  753. _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that,
  754. basic_streambuf<_CharT, _Traits>* __buf,
  755. _Is_Delim __is_delim,
  756. bool __extract_delim, bool __set_failbit) {
  757. bool __done = false;
  758. ios_base::iostate __status = 0;
  759. typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  760. _STLP_TRY {
  761. while (!__done) {
  762. int_type __c = __buf->sbumpc();
  763. if (__that->_S_eof(__c)) {
  764. __done = true;
  765. __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
  766. : ios_base::eofbit;
  767. }
  768. else if (__is_delim(_Traits::to_char_type(__c))) {
  769. __done = true;
  770. if (!__extract_delim)
  771. if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
  772. __status |= ios_base::failbit;
  773. }
  774. }
  775. }
  776. _STLP_CATCH_ALL {
  777. __that->_M_handle_exception(ios_base::badbit);
  778. }
  779. __that->setstate(__status);
  780. }
  781. // A generic buffered function for ignoring characters. Much like
  782. // _M_ignore_unbuffered, but with one additional function object:
  783. // __scan_delim(first, last) returns the first pointer p in [first,
  784. // last) such that __is_delim(p) is true.
  785. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  786. void _STLP_CALL
  787. _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that,
  788. basic_streambuf<_CharT, _Traits>* __buf,
  789. _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  790. bool __extract_delim, bool __set_failbit) {
  791. bool __at_eof = false;
  792. bool __found_delim = false;
  793. _STLP_TRY {
  794. while (__buf->_M_egptr() != __buf->_M_gptr() && !__at_eof && !__found_delim) {
  795. const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
  796. __buf->_M_gbump((int)(__p - __buf->_M_gptr()));
  797. if (__p != __buf->_M_egptr()) { // We found delim, so we're done.
  798. if (__extract_delim)
  799. __buf->_M_gbump(1);
  800. __found_delim = true;
  801. }
  802. else // No delim. Try to refil the buffer.
  803. __at_eof = __that->_S_eof(__buf->sgetc());
  804. } // Close the while loop.
  805. }
  806. _STLP_CATCH_ALL {
  807. __that->_M_handle_exception(ios_base::badbit);
  808. return;
  809. }
  810. if (__at_eof) {
  811. __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
  812. : ios_base::eofbit);
  813. return;
  814. }
  815. if (__found_delim)
  816. return;
  817. // If execution has reached this point, then we have an empty buffer but
  818. // we have not reached eof. What that means is that the streambuf has
  819. // decided to switch from a buffered to an unbuffered mode. We switch
  820. // to _M_ignore_unbuffered.
  821. _M_ignore_unbuffered(__that, __buf, __is_delim, __extract_delim, __set_failbit);
  822. }
  823. // Overloaded versions of _M_ignore_unbuffered and _M_ignore_unbuffered
  824. // with an explicit count _Num. Return value is the number of
  825. // characters extracted.
  826. //
  827. // The function object __max_chars takes two arguments, _Num and __n
  828. // (the latter being the number of characters we have already read),
  829. // and returns the maximum number of characters to read from the buffer.
  830. // We parameterize _M_ignore_buffered so that we can use it for both
  831. // bounded and unbounded input; for the former the function object should
  832. // be minus<>, and for the latter it should return a constant maximum value.
  833. template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim>
  834. streamsize _STLP_CALL
  835. _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that,
  836. basic_streambuf<_CharT, _Traits>* __buf,
  837. streamsize _Num, _Max_Chars __max_chars,
  838. _Is_Delim __is_delim,
  839. bool __extract_delim, bool __set_failbit) {
  840. streamsize __n = 0;
  841. ios_base::iostate __status = 0;
  842. typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  843. _STLP_TRY {
  844. while (__max_chars(_Num, __n) > 0) {
  845. int_type __c = __buf->sbumpc();
  846. if (__that->_S_eof(__c)) {
  847. __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
  848. : ios_base::eofbit;
  849. break;
  850. }
  851. else if (__is_delim(_Traits::to_char_type(__c))) {
  852. if (__extract_delim)
  853. ++__n;
  854. else if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
  855. __status |= ios_base::failbit;
  856. break;
  857. }
  858. // fbp : added counter increment to pass Dietmar's test
  859. ++__n;
  860. }
  861. }
  862. _STLP_CATCH_ALL {
  863. __that->_M_handle_exception(ios_base::badbit);
  864. }
  865. if (__status)
  866. __that->setstate(__status); // This might throw.
  867. return __n;
  868. }
  869. template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim, class _Scan_Delim>
  870. streamsize _STLP_CALL
  871. _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that,
  872. basic_streambuf<_CharT, _Traits>* __buf,
  873. streamsize _Num,
  874. _Max_Chars __max_chars,
  875. _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  876. bool __extract_delim, bool __set_failbit) {
  877. streamsize __n = 0;
  878. bool __at_eof = false;
  879. bool __done = false;
  880. _STLP_TRY {
  881. while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
  882. ptrdiff_t __avail = __buf->_M_egptr() - __buf->_M_gptr();
  883. streamsize __m = __max_chars(_Num, __n);
  884. if (__avail >= __m) { // We have more characters than we need.
  885. const _CharT* __last = __buf->_M_gptr() + __STATIC_CAST(ptrdiff_t, __m);
  886. const _CharT* __p = __scan_delim(__buf->_M_gptr(), __last);
  887. ptrdiff_t __chunk = __p - __buf->_M_gptr();
  888. __n += __chunk;
  889. __buf->_M_gbump((int)__chunk);
  890. if (__extract_delim && __p != __last) {
  891. __n += 1;
  892. __buf->_M_gbump(1);
  893. }
  894. __done = true;
  895. }
  896. else {
  897. const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
  898. ptrdiff_t __chunk = __p - __buf->_M_gptr();
  899. __n += __chunk;
  900. __buf->_M_gbump((int)__chunk);
  901. if (__p != __buf->_M_egptr()) { // We found delim.
  902. if (__extract_delim) {
  903. __n += 1;
  904. __buf->_M_gbump(1);
  905. }
  906. __done = true;
  907. }
  908. // We didn't find delim. Try to refill the buffer.
  909. else if (__that->_S_eof(__buf->sgetc())) {
  910. __done = true;
  911. __at_eof = true;
  912. }
  913. }
  914. } // Close the while loop.
  915. }
  916. _STLP_CATCH_ALL {
  917. __that->_M_handle_exception(ios_base::badbit);
  918. return __n;
  919. }
  920. if (__at_eof)
  921. __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
  922. : ios_base::eofbit);
  923. if (__done)
  924. return __n;
  925. // If execution has reached this point, then we have an empty buffer but
  926. // we have not reached eof. What that means is that the streambuf has
  927. // decided to switch from buffered to unbuffered input. We switch to
  928. // to _M_ignore_unbuffered.
  929. return __n + _M_ignore_unbuffered(__that, __buf, _Num, __max_chars,
  930. __is_delim, __extract_delim, __set_failbit);
  931. }
  932. template <class _CharT, class _Traits>
  933. basic_istream<_CharT, _Traits>&
  934. basic_istream<_CharT, _Traits>::ignore(streamsize __n) {
  935. sentry __sentry(*this, _No_Skip_WS());
  936. this->_M_gcount = 0;
  937. if (__sentry) {
  938. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  939. typedef _STLP_PRIV _Constant_unary_fun<bool, int_type> _Const_bool;
  940. typedef _STLP_PRIV _Constant_binary_fun<streamsize, streamsize, streamsize> _Const_streamsize;
  941. const streamsize __maxss = (numeric_limits<streamsize>::max)();
  942. if (__n == (numeric_limits<int>::max)()) {
  943. if (__buf->gptr() != __buf->egptr())
  944. _M_gcount = _M_ignore_buffered(this, __buf,
  945. __maxss, _Const_streamsize(__maxss),
  946. _Const_bool(false),
  947. _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
  948. false, false);
  949. else
  950. _M_gcount = _M_ignore_unbuffered(this, __buf,
  951. __maxss, _Const_streamsize(__maxss),
  952. _Const_bool(false), false, false);
  953. }
  954. else {
  955. if (__buf->gptr() != __buf->egptr())
  956. _M_gcount = _M_ignore_buffered(this, __buf,
  957. __n, minus<streamsize>(),
  958. _Const_bool(false),
  959. _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
  960. false, false);
  961. else
  962. _M_gcount = _M_ignore_unbuffered(this, __buf, __n, minus<streamsize>(),
  963. _Const_bool(false), false, false);
  964. }
  965. }
  966. return *this;
  967. }
  968. template <class _CharT, class _Traits>
  969. basic_istream<_CharT, _Traits>&
  970. basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __delim) {
  971. sentry __sentry(*this, _No_Skip_WS());
  972. this->_M_gcount = 0;
  973. if (__sentry) {
  974. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  975. typedef _STLP_PRIV _Constant_unary_fun<bool, int_type> _Const_bool;
  976. typedef _STLP_PRIV _Constant_binary_fun<streamsize, streamsize, streamsize>
  977. _Const_streamsize;
  978. const streamsize __maxss = (numeric_limits<streamsize>::max)();
  979. if (__n == (numeric_limits<int>::max)()) {
  980. if (__buf->gptr() != __buf->egptr())
  981. _M_gcount = _M_ignore_buffered(this, __buf,
  982. __maxss, _Const_streamsize(__maxss),
  983. _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
  984. _STLP_PRIV _Scan_for_int_val<_Traits>(__delim),
  985. true, false);
  986. else
  987. _M_gcount = _M_ignore_unbuffered(this, __buf,
  988. __maxss, _Const_streamsize(__maxss),
  989. _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
  990. true, false);
  991. }
  992. else {
  993. if (__buf->gptr() != __buf->egptr())
  994. _M_gcount = _M_ignore_buffered(this, __buf,
  995. __n, minus<streamsize>(),
  996. _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
  997. _STLP_PRIV _Scan_for_int_val<_Traits>(__delim),
  998. true, false);
  999. else
  1000. _M_gcount = _M_ignore_unbuffered(this, __buf, __n, minus<streamsize>(),
  1001. _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
  1002. true, false);
  1003. }
  1004. }
  1005. return *this;
  1006. }
  1007. // This member function does not construct a sentry object, because
  1008. // it is called from sentry's constructor.
  1009. template <class _CharT, class _Traits>
  1010. void basic_istream<_CharT, _Traits>::_M_skip_whitespace(bool __set_failbit) {
  1011. basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  1012. if (!__buf)
  1013. this->setstate(ios_base::badbit);
  1014. else if (__buf->gptr() != __buf->egptr())
  1015. _M_ignore_buffered(this, __buf,
  1016. _STLP_PRIV _Is_not_wspace<_Traits>(this->_M_ctype_facet()),
  1017. _STLP_PRIV _Scan_for_not_wspace<_Traits>(this->_M_ctype_facet()),
  1018. false, __set_failbit);
  1019. else
  1020. _M_ignore_unbuffered(this, __buf,
  1021. _STLP_PRIV _Is_not_wspace<_Traits>(this->_M_ctype_facet()),
  1022. false, __set_failbit);
  1023. }
  1024. // This is a very simple loop that reads characters from __src and puts
  1025. // them into __dest. It looks complicated because of the (standard-
  1026. // mandated) exception handling policy.
  1027. //
  1028. // We stop when we get an exception, when we fail to insert into the
  1029. // output streambuf, or when __is_delim is true.
  1030. _STLP_MOVE_TO_PRIV_NAMESPACE
  1031. template < class _CharT, class _Traits, class _Is_Delim>
  1032. streamsize _STLP_CALL
  1033. __copy_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
  1034. basic_streambuf<_CharT, _Traits>* __dest,
  1035. _Is_Delim __is_delim,
  1036. bool __extract_delim, bool __rethrow) {
  1037. streamsize __extracted = 0;
  1038. ios_base::iostate __status = 0;
  1039. typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  1040. int_type __c;
  1041. _STLP_TRY {
  1042. for (;;) {
  1043. // Get a character. If there's an exception, catch and (maybe) rethrow it.
  1044. __c = __src->sbumpc();
  1045. // If we failed to get a character, then quit.
  1046. if (__that->_S_eof(__c)) {
  1047. __status |= ios_base::eofbit;
  1048. break;
  1049. }
  1050. // If it's the delimiter, then quit.
  1051. else if (__is_delim(_Traits::to_char_type(__c))) {
  1052. if (!__extract_delim && !__pushback(__src, _Traits::to_char_type(__c)))
  1053. __status |= ios_base::failbit;
  1054. break;
  1055. }
  1056. else {
  1057. // Try to put the character in the output streambuf.
  1058. bool __failed = false;
  1059. _STLP_TRY {
  1060. if (!__that->_S_eof(__dest->sputc(_Traits::to_char_type(__c))))
  1061. ++__extracted;
  1062. else
  1063. __failed = true;
  1064. }
  1065. _STLP_CATCH_ALL {
  1066. __failed = true;
  1067. }
  1068. // If we failed to put the character in the output streambuf, then
  1069. // try to push it back to the input streambuf.
  1070. if (__failed && !__pushback(__src, _Traits::to_char_type(__c)))
  1071. __status |= ios_base::failbit;
  1072. // fbp : avoiding infinite loop in io-27-6-1-2-3.exp
  1073. if (__failed)
  1074. break;
  1075. }
  1076. } /* for (;;) */
  1077. }
  1078. // fbp : this try/catch moved here in reasonable assumption
  1079. // __is_delim never throw (__pushback is guaranteed not to)
  1080. _STLP_CATCH_ALL {
  1081. // See 27.6.1.2.3, paragraph 13.
  1082. if (__rethrow && __extracted == 0)
  1083. __that->_M_handle_exception(ios_base::failbit);
  1084. }
  1085. __that->setstate(__status);
  1086. return __extracted;
  1087. }
  1088. // Buffered copying from one streambuf to another. We copy the characters
  1089. // in chunks, rather than one at a time. We still have to worry about all
  1090. // of the error conditions we checked in __copy_unbuffered, plus one more:
  1091. // the streambuf might decide to switch from a buffered to an unbuffered mode.
  1092. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  1093. streamsize _STLP_CALL
  1094. __copy_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
  1095. basic_streambuf<_CharT, _Traits>* __dest,
  1096. _Scan_Delim __scan_delim, _Is_Delim __is_delim,
  1097. bool __extract_delim, bool __rethrow) {
  1098. streamsize __extracted = 0;
  1099. ios_base::iostate __status = 0;
  1100. typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  1101. //Borland compiler generates a warning if assignment because value is never used:
  1102. int_type __c /*= _Traits::eof()*/;
  1103. _CharT* __first = __src->_M_gptr();
  1104. ptrdiff_t __avail = __src->_M_egptr() - __first;
  1105. // fbp : introduced to move catch/try blocks out of the loop
  1106. bool __do_handle_exceptions = false;
  1107. _STLP_TRY {
  1108. for (;;) {
  1109. const _CharT* __last = __scan_delim(__first, __src->_M_egptr());
  1110. // Try to copy the entire input buffer to the output buffer.
  1111. streamsize __n = __dest->sputn(__first, __extract_delim && __last != __src->_M_egptr()
  1112. ? (__last - __first) + 1
  1113. : (__last - __first));
  1114. __src->_M_gbump((int)__n);
  1115. __extracted += __n;
  1116. // from this on, catch() will call _M_handle_exceptions()
  1117. __do_handle_exceptions = true;
  1118. if (__n < __avail) // We found the delimiter, or else failed to
  1119. break; // copy some characters.
  1120. __c = __src->sgetc();
  1121. // Three possibilities: we succeeded in refilling the buffer, or
  1122. // we got EOF, or the streambuf has switched to unbuffered mode.
  1123. __first = __src->_M_gptr();
  1124. __avail = __src->_M_egptr() - __first;
  1125. if (__avail > 0)
  1126. {} // dwa 1/16/00 -- suppress a Metrowerks warning
  1127. else if (__that->_S_eof(__c)) {
  1128. __status |= ios_base::eofbit;
  1129. break;
  1130. }
  1131. else {
  1132. return __extracted + __copy_unbuffered(__that, __src, __dest, __is_delim,
  1133. __extract_delim, __rethrow);
  1134. }
  1135. __do_handle_exceptions = false;
  1136. }
  1137. }
  1138. _STLP_CATCH_ALL {
  1139. // See 27.6.1.2.3, paragraph 13.
  1140. if (__rethrow && __do_handle_exceptions && __extracted == 0)
  1141. __that->_M_handle_exception(ios_base::failbit);
  1142. }
  1143. if (__status)
  1144. __that->setstate(__status); // This might throw.
  1145. return __extracted;
  1146. }
  1147. _STLP_MOVE_TO_STD_NAMESPACE
  1148. template <class _CharT, class _Traits>
  1149. basic_istream<_CharT, _Traits>&
  1150. basic_istream<_CharT, _Traits>
  1151. ::get(basic_streambuf<_CharT, _Traits>& __dest, _CharT __delim) {
  1152. sentry __sentry(*this, _No_Skip_WS());
  1153. this->_M_gcount = 0;
  1154. if (__sentry) {
  1155. basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
  1156. if (__src)
  1157. this->_M_gcount = __src->egptr() != __src->gptr()
  1158. ? _STLP_PRIV __copy_buffered(this, __src, &__dest,
  1159. _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
  1160. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  1161. false, false)
  1162. : _STLP_PRIV __copy_unbuffered(this, __src, &__dest,
  1163. _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
  1164. false, false);
  1165. }
  1166. if (this->_M_gcount == 0)
  1167. this->setstate(ios_base::failbit);
  1168. return *this;
  1169. }
  1170. // Copying characters into a streambuf.
  1171. template <class _CharT, class _Traits>
  1172. basic_istream<_CharT, _Traits>&
  1173. basic_istream<_CharT, _Traits>
  1174. ::operator>>(basic_streambuf<_CharT, _Traits>* __dest) {
  1175. streamsize __n = 0;
  1176. typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
  1177. _Sentry __sentry(*this);
  1178. if (__sentry) {
  1179. basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
  1180. if (__src && __dest)
  1181. __n = __src->egptr() != __src->gptr()
  1182. ? _STLP_PRIV __copy_buffered(this, __src, __dest,
  1183. _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
  1184. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  1185. false, true)
  1186. : _STLP_PRIV __copy_unbuffered(this, __src, __dest,
  1187. _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
  1188. false, true);
  1189. }
  1190. if (__n == 0)
  1191. this->setstate(ios_base::failbit);
  1192. return *this;
  1193. }
  1194. // ----------------------------------------------------------------
  1195. // basic_iostream<> class
  1196. // ----------------------------------------------------------------
  1197. template <class _CharT, class _Traits>
  1198. basic_iostream<_CharT, _Traits>
  1199. ::basic_iostream(basic_streambuf<_CharT, _Traits>* __buf)
  1200. : basic_ios<_CharT, _Traits>(),
  1201. basic_istream<_CharT, _Traits>(__buf),
  1202. basic_ostream<_CharT, _Traits>(__buf) {
  1203. this->init(__buf);
  1204. }
  1205. template <class _CharT, class _Traits>
  1206. basic_iostream<_CharT, _Traits>::~basic_iostream()
  1207. {}
  1208. _STLP_END_NAMESPACE
  1209. #undef __BIS_int_type__
  1210. #undef __BIS_pos_type__
  1211. #undef __BIS_off_type__
  1212. #endif /* _STLP_ISTREAM_C */
  1213. // Local Variables:
  1214. // mode:C++
  1215. // End: