* config/xtensa/lib1funcs.asm (__umodsi3, __modsi3): Rearrange so that
[official-gcc.git] / libstdc++-v3 / src / istream.cc
blob0f24340e031b6e3c225f231dff2cbfd194eb9caf
1 // Input streams -*- C++ -*-
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.6.1 Input streams
34 #include <istream>
36 _GLIBCXX_BEGIN_NAMESPACE(std)
38 template<>
39 basic_istream<char>&
40 basic_istream<char>::
41 getline(char_type* __s, streamsize __n, char_type __delim)
43 _M_gcount = 0;
44 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
45 sentry __cerb(*this, true);
46 if (__cerb)
48 try
50 const int_type __idelim = traits_type::to_int_type(__delim);
51 const int_type __eof = traits_type::eof();
52 __streambuf_type* __sb = this->rdbuf();
53 int_type __c = __sb->sgetc();
55 while (_M_gcount + 1 < __n
56 && !traits_type::eq_int_type(__c, __eof)
57 && !traits_type::eq_int_type(__c, __idelim))
59 streamsize __size = std::min(streamsize(__sb->egptr()
60 - __sb->gptr()),
61 streamsize(__n - _M_gcount
62 - 1));
63 if (__size > 1)
65 const char_type* __p = traits_type::find(__sb->gptr(),
66 __size,
67 __delim);
68 if (__p)
69 __size = __p - __sb->gptr();
70 traits_type::copy(__s, __sb->gptr(), __size);
71 __s += __size;
72 __sb->gbump(__size);
73 _M_gcount += __size;
74 __c = __sb->sgetc();
76 else
78 *__s++ = traits_type::to_char_type(__c);
79 ++_M_gcount;
80 __c = __sb->snextc();
84 if (traits_type::eq_int_type(__c, __eof))
85 __err |= ios_base::eofbit;
86 else if (traits_type::eq_int_type(__c, __idelim))
88 ++_M_gcount;
89 __sb->sbumpc();
91 else
92 __err |= ios_base::failbit;
94 catch(...)
95 { this->_M_setstate(ios_base::badbit); }
97 // _GLIBCXX_RESOLVE_LIB_DEFECTS
98 // 243. get and getline when sentry reports failure.
99 if (__n > 0)
100 *__s = char_type();
101 if (!_M_gcount)
102 __err |= ios_base::failbit;
103 if (__err)
104 this->setstate(__err);
105 return *this;
108 template<>
109 basic_istream<char>&
110 basic_istream<char>::
111 ignore(streamsize __n, int_type __delim)
113 if (traits_type::eq_int_type(__delim, traits_type::eof()))
114 return ignore(__n);
116 _M_gcount = 0;
117 sentry __cerb(*this, true);
118 if (__cerb && __n > 0)
120 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123 const char_type __cdelim = traits_type::to_char_type(__delim);
124 const int_type __eof = traits_type::eof();
125 __streambuf_type* __sb = this->rdbuf();
126 int_type __c = __sb->sgetc();
128 bool __large_ignore = false;
129 while (true)
131 while (_M_gcount < __n
132 && !traits_type::eq_int_type(__c, __eof)
133 && !traits_type::eq_int_type(__c, __delim))
135 streamsize __size = std::min(streamsize(__sb->egptr()
136 - __sb->gptr()),
137 streamsize(__n - _M_gcount));
138 if (__size > 1)
140 const char_type* __p = traits_type::find(__sb->gptr(),
141 __size,
142 __cdelim);
143 if (__p)
144 __size = __p - __sb->gptr();
145 __sb->gbump(__size);
146 _M_gcount += __size;
147 __c = __sb->sgetc();
149 else
151 ++_M_gcount;
152 __c = __sb->snextc();
155 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
156 && !traits_type::eq_int_type(__c, __eof)
157 && !traits_type::eq_int_type(__c, __delim))
159 _M_gcount =
160 __gnu_cxx::__numeric_traits<streamsize>::__min;
161 __large_ignore = true;
163 else
164 break;
167 if (__large_ignore)
168 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
170 if (traits_type::eq_int_type(__c, __eof))
171 __err |= ios_base::eofbit;
172 else if (traits_type::eq_int_type(__c, __delim))
174 if (_M_gcount
175 < __gnu_cxx::__numeric_traits<streamsize>::__max)
176 ++_M_gcount;
177 __sb->sbumpc();
180 catch(...)
181 { this->_M_setstate(ios_base::badbit); }
182 if (__err)
183 this->setstate(__err);
185 return *this;
188 template<>
189 basic_istream<char>&
190 operator>>(basic_istream<char>& __in, char* __s)
192 typedef basic_istream<char> __istream_type;
193 typedef __istream_type::int_type __int_type;
194 typedef __istream_type::char_type __char_type;
195 typedef __istream_type::traits_type __traits_type;
196 typedef __istream_type::__streambuf_type __streambuf_type;
197 typedef __istream_type::__ctype_type __ctype_type;
199 streamsize __extracted = 0;
200 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
201 __istream_type::sentry __cerb(__in, false);
202 if (__cerb)
206 // Figure out how many characters to extract.
207 streamsize __num = __in.width();
208 if (__num <= 0)
209 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
211 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
213 const __int_type __eof = __traits_type::eof();
214 __streambuf_type* __sb = __in.rdbuf();
215 __int_type __c = __sb->sgetc();
217 while (__extracted < __num - 1
218 && !__traits_type::eq_int_type(__c, __eof)
219 && !__ct.is(ctype_base::space,
220 __traits_type::to_char_type(__c)))
222 streamsize __size = std::min(streamsize(__sb->egptr()
223 - __sb->gptr()),
224 streamsize(__num - __extracted
225 - 1));
226 if (__size > 1)
228 __size = (__ct.scan_is(ctype_base::space,
229 __sb->gptr() + 1,
230 __sb->gptr() + __size)
231 - __sb->gptr());
232 __traits_type::copy(__s, __sb->gptr(), __size);
233 __s += __size;
234 __sb->gbump(__size);
235 __extracted += __size;
236 __c = __sb->sgetc();
238 else
240 *__s++ = __traits_type::to_char_type(__c);
241 ++__extracted;
242 __c = __sb->snextc();
246 if (__traits_type::eq_int_type(__c, __eof))
247 __err |= ios_base::eofbit;
249 // _GLIBCXX_RESOLVE_LIB_DEFECTS
250 // 68. Extractors for char* should store null at end
251 *__s = __char_type();
252 __in.width(0);
254 catch(...)
255 { __in._M_setstate(ios_base::badbit); }
257 if (!__extracted)
258 __err |= ios_base::failbit;
259 if (__err)
260 __in.setstate(__err);
261 return __in;
264 template<>
265 basic_istream<char>&
266 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
268 typedef basic_istream<char> __istream_type;
269 typedef __istream_type::int_type __int_type;
270 typedef __istream_type::char_type __char_type;
271 typedef __istream_type::traits_type __traits_type;
272 typedef __istream_type::__streambuf_type __streambuf_type;
273 typedef __istream_type::__ctype_type __ctype_type;
274 typedef basic_string<char> __string_type;
275 typedef __string_type::size_type __size_type;
277 __size_type __extracted = 0;
278 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
279 __istream_type::sentry __cerb(__in, false);
280 if (__cerb)
284 __str.erase();
285 const streamsize __w = __in.width();
286 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
287 : __str.max_size();
288 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
289 const __int_type __eof = __traits_type::eof();
290 __streambuf_type* __sb = __in.rdbuf();
291 __int_type __c = __sb->sgetc();
293 while (__extracted < __n
294 && !__traits_type::eq_int_type(__c, __eof)
295 && !__ct.is(ctype_base::space,
296 __traits_type::to_char_type(__c)))
298 streamsize __size = std::min(streamsize(__sb->egptr()
299 - __sb->gptr()),
300 streamsize(__n - __extracted));
301 if (__size > 1)
303 __size = (__ct.scan_is(ctype_base::space,
304 __sb->gptr() + 1,
305 __sb->gptr() + __size)
306 - __sb->gptr());
307 __str.append(__sb->gptr(), __size);
308 __sb->gbump(__size);
309 __extracted += __size;
310 __c = __sb->sgetc();
312 else
314 __str += __traits_type::to_char_type(__c);
315 ++__extracted;
316 __c = __sb->snextc();
320 if (__traits_type::eq_int_type(__c, __eof))
321 __err |= ios_base::eofbit;
322 __in.width(0);
324 catch(...)
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 91. Description of operator>> and getline() for string<>
328 // might cause endless loop
329 __in._M_setstate(ios_base::badbit);
332 if (!__extracted)
333 __err |= ios_base::failbit;
334 if (__err)
335 __in.setstate(__err);
336 return __in;
339 template<>
340 basic_istream<char>&
341 getline(basic_istream<char>& __in, basic_string<char>& __str,
342 char __delim)
344 typedef basic_istream<char> __istream_type;
345 typedef __istream_type::int_type __int_type;
346 typedef __istream_type::char_type __char_type;
347 typedef __istream_type::traits_type __traits_type;
348 typedef __istream_type::__streambuf_type __streambuf_type;
349 typedef __istream_type::__ctype_type __ctype_type;
350 typedef basic_string<char> __string_type;
351 typedef __string_type::size_type __size_type;
353 __size_type __extracted = 0;
354 const __size_type __n = __str.max_size();
355 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
356 __istream_type::sentry __cerb(__in, true);
357 if (__cerb)
361 __str.erase();
362 const __int_type __idelim = __traits_type::to_int_type(__delim);
363 const __int_type __eof = __traits_type::eof();
364 __streambuf_type* __sb = __in.rdbuf();
365 __int_type __c = __sb->sgetc();
367 while (__extracted < __n
368 && !__traits_type::eq_int_type(__c, __eof)
369 && !__traits_type::eq_int_type(__c, __idelim))
371 streamsize __size = std::min(streamsize(__sb->egptr()
372 - __sb->gptr()),
373 streamsize(__n - __extracted));
374 if (__size > 1)
376 const __char_type* __p = __traits_type::find(__sb->gptr(),
377 __size,
378 __delim);
379 if (__p)
380 __size = __p - __sb->gptr();
381 __str.append(__sb->gptr(), __size);
382 __sb->gbump(__size);
383 __extracted += __size;
384 __c = __sb->sgetc();
386 else
388 __str += __traits_type::to_char_type(__c);
389 ++__extracted;
390 __c = __sb->snextc();
394 if (__traits_type::eq_int_type(__c, __eof))
395 __err |= ios_base::eofbit;
396 else if (__traits_type::eq_int_type(__c, __idelim))
398 ++__extracted;
399 __sb->sbumpc();
401 else
402 __err |= ios_base::failbit;
404 catch(...)
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 91. Description of operator>> and getline() for string<>
408 // might cause endless loop
409 __in._M_setstate(ios_base::badbit);
412 if (!__extracted)
413 __err |= ios_base::failbit;
414 if (__err)
415 __in.setstate(__err);
416 return __in;
419 #ifdef _GLIBCXX_USE_WCHAR_T
420 template<>
421 basic_istream<wchar_t>&
422 basic_istream<wchar_t>::
423 getline(char_type* __s, streamsize __n, char_type __delim)
425 _M_gcount = 0;
426 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
427 sentry __cerb(*this, true);
428 if (__cerb)
432 const int_type __idelim = traits_type::to_int_type(__delim);
433 const int_type __eof = traits_type::eof();
434 __streambuf_type* __sb = this->rdbuf();
435 int_type __c = __sb->sgetc();
437 while (_M_gcount + 1 < __n
438 && !traits_type::eq_int_type(__c, __eof)
439 && !traits_type::eq_int_type(__c, __idelim))
441 streamsize __size = std::min(streamsize(__sb->egptr()
442 - __sb->gptr()),
443 streamsize(__n - _M_gcount
444 - 1));
445 if (__size > 1)
447 const char_type* __p = traits_type::find(__sb->gptr(),
448 __size,
449 __delim);
450 if (__p)
451 __size = __p - __sb->gptr();
452 traits_type::copy(__s, __sb->gptr(), __size);
453 __s += __size;
454 __sb->gbump(__size);
455 _M_gcount += __size;
456 __c = __sb->sgetc();
458 else
460 *__s++ = traits_type::to_char_type(__c);
461 ++_M_gcount;
462 __c = __sb->snextc();
466 if (traits_type::eq_int_type(__c, __eof))
467 __err |= ios_base::eofbit;
468 else if (traits_type::eq_int_type(__c, __idelim))
470 ++_M_gcount;
471 __sb->sbumpc();
473 else
474 __err |= ios_base::failbit;
476 catch(...)
477 { this->_M_setstate(ios_base::badbit); }
479 // _GLIBCXX_RESOLVE_LIB_DEFECTS
480 // 243. get and getline when sentry reports failure.
481 if (__n > 0)
482 *__s = char_type();
483 if (!_M_gcount)
484 __err |= ios_base::failbit;
485 if (__err)
486 this->setstate(__err);
487 return *this;
490 template<>
491 basic_istream<wchar_t>&
492 basic_istream<wchar_t>::
493 ignore(streamsize __n, int_type __delim)
495 if (traits_type::eq_int_type(__delim, traits_type::eof()))
496 return ignore(__n);
498 _M_gcount = 0;
499 sentry __cerb(*this, true);
500 if (__cerb && __n > 0)
502 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
505 const char_type __cdelim = traits_type::to_char_type(__delim);
506 const int_type __eof = traits_type::eof();
507 __streambuf_type* __sb = this->rdbuf();
508 int_type __c = __sb->sgetc();
510 bool __large_ignore = false;
511 while (true)
513 while (_M_gcount < __n
514 && !traits_type::eq_int_type(__c, __eof)
515 && !traits_type::eq_int_type(__c, __delim))
517 streamsize __size = std::min(streamsize(__sb->egptr()
518 - __sb->gptr()),
519 streamsize(__n - _M_gcount));
520 if (__size > 1)
522 const char_type* __p = traits_type::find(__sb->gptr(),
523 __size,
524 __cdelim);
525 if (__p)
526 __size = __p - __sb->gptr();
527 __sb->gbump(__size);
528 _M_gcount += __size;
529 __c = __sb->sgetc();
531 else
533 ++_M_gcount;
534 __c = __sb->snextc();
537 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
538 && !traits_type::eq_int_type(__c, __eof)
539 && !traits_type::eq_int_type(__c, __delim))
541 _M_gcount =
542 __gnu_cxx::__numeric_traits<streamsize>::__min;
543 __large_ignore = true;
545 else
546 break;
549 if (__large_ignore)
550 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
552 if (traits_type::eq_int_type(__c, __eof))
553 __err |= ios_base::eofbit;
554 else if (traits_type::eq_int_type(__c, __delim))
556 if (_M_gcount
557 < __gnu_cxx::__numeric_traits<streamsize>::__max)
558 ++_M_gcount;
559 __sb->sbumpc();
562 catch(...)
563 { this->_M_setstate(ios_base::badbit); }
564 if (__err)
565 this->setstate(__err);
567 return *this;
570 template<>
571 basic_istream<wchar_t>&
572 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
573 wchar_t __delim)
575 typedef basic_istream<wchar_t> __istream_type;
576 typedef __istream_type::int_type __int_type;
577 typedef __istream_type::char_type __char_type;
578 typedef __istream_type::traits_type __traits_type;
579 typedef __istream_type::__streambuf_type __streambuf_type;
580 typedef __istream_type::__ctype_type __ctype_type;
581 typedef basic_string<wchar_t> __string_type;
582 typedef __string_type::size_type __size_type;
584 __size_type __extracted = 0;
585 const __size_type __n = __str.max_size();
586 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
587 __istream_type::sentry __cerb(__in, true);
588 if (__cerb)
592 __str.erase();
593 const __int_type __idelim = __traits_type::to_int_type(__delim);
594 const __int_type __eof = __traits_type::eof();
595 __streambuf_type* __sb = __in.rdbuf();
596 __int_type __c = __sb->sgetc();
598 while (__extracted < __n
599 && !__traits_type::eq_int_type(__c, __eof)
600 && !__traits_type::eq_int_type(__c, __idelim))
602 streamsize __size = std::min(streamsize(__sb->egptr()
603 - __sb->gptr()),
604 streamsize(__n - __extracted));
605 if (__size > 1)
607 const __char_type* __p = __traits_type::find(__sb->gptr(),
608 __size,
609 __delim);
610 if (__p)
611 __size = __p - __sb->gptr();
612 __str.append(__sb->gptr(), __size);
613 __sb->gbump(__size);
614 __extracted += __size;
615 __c = __sb->sgetc();
617 else
619 __str += __traits_type::to_char_type(__c);
620 ++__extracted;
621 __c = __sb->snextc();
625 if (__traits_type::eq_int_type(__c, __eof))
626 __err |= ios_base::eofbit;
627 else if (__traits_type::eq_int_type(__c, __idelim))
629 ++__extracted;
630 __sb->sbumpc();
632 else
633 __err |= ios_base::failbit;
635 catch(...)
637 // _GLIBCXX_RESOLVE_LIB_DEFECTS
638 // 91. Description of operator>> and getline() for string<>
639 // might cause endless loop
640 __in._M_setstate(ios_base::badbit);
643 if (!__extracted)
644 __err |= ios_base::failbit;
645 if (__err)
646 __in.setstate(__err);
647 return __in;
649 #endif
651 _GLIBCXX_END_NAMESPACE