]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/istream.cc
tree-ssa.c (verify_flow_sensitive_alias_info): Don't retrieve annotations or pointer...
[thirdparty/gcc.git] / libstdc++-v3 / src / istream.cc
CommitLineData
adb31ad6
PC
1// Input streams -*- C++ -*-
2
3// Copyright (C) 2004 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.
10
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.
15
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
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.
29
30//
31// ISO C++ 14882: 27.6.1 Input streams
32//
33
34#include <istream>
35
36namespace std
37{
38 template<>
39 basic_istream<char>&
40 basic_istream<char>::
41 getline(char_type* __s, streamsize __n, char_type __delim)
42 {
43 _M_gcount = 0;
44 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
45 sentry __cerb(*this, true);
46 if (__cerb)
47 {
48 try
49 {
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();
54
55 while (_M_gcount + 1 < __n
56 && !traits_type::eq_int_type(__c, __eof)
57 && !traits_type::eq_int_type(__c, __idelim))
58 {
59 streamsize __size = std::min(streamsize(__sb->egptr()
60 - __sb->gptr()),
61 streamsize(__n - _M_gcount
62 - 1));
63 if (__size > 1)
64 {
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();
75 }
76 else
77 {
78 *__s++ = traits_type::to_char_type(__c);
79 ++_M_gcount;
80 __c = __sb->snextc();
81 }
82 }
83
84 if (traits_type::eq_int_type(__c, __eof))
85 __err |= ios_base::eofbit;
86 else if (traits_type::eq_int_type(__c, __idelim))
87 {
88 ++_M_gcount;
89 __sb->sbumpc();
90 }
91 else
92 __err |= ios_base::failbit;
93 }
94 catch(...)
95 { this->_M_setstate(ios_base::badbit); }
96 }
97 *__s = char_type();
98 if (!_M_gcount)
99 __err |= ios_base::failbit;
100 if (__err)
101 this->setstate(__err);
102 return *this;
103 }
104
105 template<>
106 basic_istream<char>&
107 basic_istream<char>::
108 ignore(streamsize __n)
109 {
110 if (__n == 1)
111 return ignore();
112
113 _M_gcount = 0;
114 sentry __cerb(*this, true);
115 if (__cerb && __n > 0)
116 {
117 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
118 try
119 {
120 const int_type __eof = traits_type::eof();
121 __streambuf_type* __sb = this->rdbuf();
122 int_type __c = __sb->sgetc();
123
124 const bool __bound = __n != numeric_limits<streamsize>::max();
125 if (__bound)
126 --__n;
127 while (_M_gcount <= __n
128 && !traits_type::eq_int_type(__c, __eof))
129 {
130 streamsize __size = __sb->egptr() - __sb->gptr();
131 if (__bound)
132 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
133
134 if (__size > 1)
135 {
136 __sb->gbump(__size);
137 _M_gcount += __size;
138 __c = __sb->sgetc();
139 }
140 else
141 {
142 ++_M_gcount;
143 __c = __sb->snextc();
144 }
145 }
146 if (traits_type::eq_int_type(__c, __eof))
147 __err |= ios_base::eofbit;
148 }
149 catch(...)
150 { this->_M_setstate(ios_base::badbit); }
151 if (__err)
152 this->setstate(__err);
153 }
154 return *this;
155 }
156
157 template<>
158 basic_istream<char>&
159 basic_istream<char>::
160 ignore(streamsize __n, int_type __delim)
161 {
162 if (traits_type::eq_int_type(__delim, traits_type::eof()))
163 return ignore(__n);
164
165 _M_gcount = 0;
166 sentry __cerb(*this, true);
167 if (__cerb && __n > 0)
168 {
169 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
170 try
171 {
172 const char_type __cdelim = traits_type::to_char_type(__delim);
173 const int_type __eof = traits_type::eof();
174 __streambuf_type* __sb = this->rdbuf();
175 int_type __c = __sb->sgetc();
176
177 const bool __bound = __n != numeric_limits<streamsize>::max();
178 if (__bound)
179 --__n;
180 while (_M_gcount <= __n
181 && !traits_type::eq_int_type(__c, __eof)
182 && !traits_type::eq_int_type(__c, __delim))
183 {
184 streamsize __size = __sb->egptr() - __sb->gptr();
185 if (__bound)
186 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
187
188 if (__size > 1)
189 {
190 const char_type* __p = traits_type::find(__sb->gptr(),
191 __size,
192 __cdelim);
193 if (__p)
194 __size = __p - __sb->gptr();
195 __sb->gbump(__size);
196 _M_gcount += __size;
197 __c = __sb->sgetc();
198 }
199 else
200 {
201 ++_M_gcount;
202 __c = __sb->snextc();
203 }
204 }
205 if (traits_type::eq_int_type(__c, __eof))
206 __err |= ios_base::eofbit;
207 else if (traits_type::eq_int_type(__c, __delim))
208 {
209 ++_M_gcount;
210 __sb->sbumpc();
211 }
212 }
213 catch(...)
214 { this->_M_setstate(ios_base::badbit); }
215 if (__err)
216 this->setstate(__err);
217 }
218 return *this;
219 }
220
e9fb72e8
PC
221 template<>
222 basic_istream<char>&
223 getline(basic_istream<char>& __in, basic_string<char>& __str,
224 char __delim)
225 {
226 typedef basic_istream<char> __istream_type;
227 typedef __istream_type::int_type __int_type;
228 typedef __istream_type::char_type __char_type;
229 typedef __istream_type::traits_type __traits_type;
230 typedef __istream_type::__streambuf_type __streambuf_type;
231 typedef __istream_type::__ctype_type __ctype_type;
232 typedef basic_string<char> __string_type;
233 typedef __string_type::size_type __size_type;
234
235 __size_type __extracted = 0;
236 const __size_type __n = __str.max_size();
237 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
238 __istream_type::sentry __cerb(__in, true);
239 if (__cerb)
240 {
241 try
242 {
243 __str.erase();
244 const __int_type __idelim = __traits_type::to_int_type(__delim);
245 const __int_type __eof = __traits_type::eof();
246 __streambuf_type* __sb = __in.rdbuf();
247 __int_type __c = __sb->sgetc();
248
249 while (__extracted < __n
250 && !__traits_type::eq_int_type(__c, __eof)
251 && !__traits_type::eq_int_type(__c, __idelim))
252 {
253 streamsize __size = std::min(streamsize(__sb->egptr()
254 - __sb->gptr()),
255 streamsize(__n - __extracted));
256 if (__size > 1)
257 {
258 const __char_type* __p = __traits_type::find(__sb->gptr(),
259 __size,
260 __delim);
261 if (__p)
262 __size = __p - __sb->gptr();
263 __str.append(__sb->gptr(), __size);
264 __sb->gbump(__size);
265 __extracted += __size;
266 __c = __sb->sgetc();
267 }
268 else
269 {
270 __str += __traits_type::to_char_type(__c);
271 ++__extracted;
272 __c = __sb->snextc();
273 }
274 }
275
276 if (__traits_type::eq_int_type(__c, __eof))
277 __err |= ios_base::eofbit;
278 else if (__traits_type::eq_int_type(__c, __idelim))
279 {
280 ++__extracted;
281 __sb->sbumpc();
282 }
283 else
284 __err |= ios_base::failbit;
285 }
286 catch(...)
287 {
288 // _GLIBCXX_RESOLVE_LIB_DEFECTS
289 // 91. Description of operator>> and getline() for string<>
290 // might cause endless loop
291 __in._M_setstate(ios_base::badbit);
292 }
293 }
294 if (!__extracted)
295 __err |= ios_base::failbit;
296 if (__err)
297 __in.setstate(__err);
298 return __in;
299 }
300
adb31ad6
PC
301#ifdef _GLIBCXX_USE_WCHAR_T
302 template<>
303 basic_istream<wchar_t>&
304 basic_istream<wchar_t>::
305 getline(char_type* __s, streamsize __n, char_type __delim)
306 {
307 _M_gcount = 0;
308 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
309 sentry __cerb(*this, true);
310 if (__cerb)
311 {
312 try
313 {
314 const int_type __idelim = traits_type::to_int_type(__delim);
315 const int_type __eof = traits_type::eof();
316 __streambuf_type* __sb = this->rdbuf();
317 int_type __c = __sb->sgetc();
318
319 while (_M_gcount + 1 < __n
320 && !traits_type::eq_int_type(__c, __eof)
321 && !traits_type::eq_int_type(__c, __idelim))
322 {
323 streamsize __size = std::min(streamsize(__sb->egptr()
324 - __sb->gptr()),
325 streamsize(__n - _M_gcount
326 - 1));
327 if (__size > 1)
328 {
329 const char_type* __p = traits_type::find(__sb->gptr(),
330 __size,
331 __delim);
332 if (__p)
333 __size = __p - __sb->gptr();
334 traits_type::copy(__s, __sb->gptr(), __size);
335 __s += __size;
336 __sb->gbump(__size);
337 _M_gcount += __size;
338 __c = __sb->sgetc();
339 }
340 else
341 {
342 *__s++ = traits_type::to_char_type(__c);
343 ++_M_gcount;
344 __c = __sb->snextc();
345 }
346 }
347
348 if (traits_type::eq_int_type(__c, __eof))
349 __err |= ios_base::eofbit;
350 else if (traits_type::eq_int_type(__c, __idelim))
351 {
352 ++_M_gcount;
353 __sb->sbumpc();
354 }
355 else
356 __err |= ios_base::failbit;
357 }
358 catch(...)
359 { this->_M_setstate(ios_base::badbit); }
360 }
361 *__s = char_type();
362 if (!_M_gcount)
363 __err |= ios_base::failbit;
364 if (__err)
365 this->setstate(__err);
366 return *this;
367 }
368
369 template<>
370 basic_istream<wchar_t>&
371 basic_istream<wchar_t>::
372 ignore(streamsize __n)
373 {
374 if (__n == 1)
375 return ignore();
376
377 _M_gcount = 0;
378 sentry __cerb(*this, true);
379 if (__cerb && __n > 0)
380 {
381 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
382 try
383 {
384 const int_type __eof = traits_type::eof();
385 __streambuf_type* __sb = this->rdbuf();
386 int_type __c = __sb->sgetc();
387
388 const bool __bound = __n != numeric_limits<streamsize>::max();
389 if (__bound)
390 --__n;
391 while (_M_gcount <= __n
392 && !traits_type::eq_int_type(__c, __eof))
393 {
394 streamsize __size = __sb->egptr() - __sb->gptr();
395 if (__bound)
396 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
397
398 if (__size > 1)
399 {
400 __sb->gbump(__size);
401 _M_gcount += __size;
402 __c = __sb->sgetc();
403 }
404 else
405 {
406 ++_M_gcount;
407 __c = __sb->snextc();
408 }
409 }
410 if (traits_type::eq_int_type(__c, __eof))
411 __err |= ios_base::eofbit;
412 }
413 catch(...)
414 { this->_M_setstate(ios_base::badbit); }
415 if (__err)
416 this->setstate(__err);
417 }
418 return *this;
419 }
420
421 template<>
422 basic_istream<wchar_t>&
423 basic_istream<wchar_t>::
424 ignore(streamsize __n, int_type __delim)
425 {
426 if (traits_type::eq_int_type(__delim, traits_type::eof()))
427 return ignore(__n);
428
429 _M_gcount = 0;
430 sentry __cerb(*this, true);
431 if (__cerb && __n > 0)
432 {
433 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
434 try
435 {
436 const char_type __cdelim = traits_type::to_char_type(__delim);
437 const int_type __eof = traits_type::eof();
438 __streambuf_type* __sb = this->rdbuf();
439 int_type __c = __sb->sgetc();
440
441 const bool __bound = __n != numeric_limits<streamsize>::max();
442 if (__bound)
443 --__n;
444 while (_M_gcount <= __n
445 && !traits_type::eq_int_type(__c, __eof)
446 && !traits_type::eq_int_type(__c, __delim))
447 {
448 streamsize __size = __sb->egptr() - __sb->gptr();
449 if (__bound)
450 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
451
452 if (__size > 1)
453 {
454 const char_type* __p = traits_type::find(__sb->gptr(),
455 __size,
456 __cdelim);
457 if (__p)
458 __size = __p - __sb->gptr();
459 __sb->gbump(__size);
460 _M_gcount += __size;
461 __c = __sb->sgetc();
462 }
463 else
464 {
465 ++_M_gcount;
466 __c = __sb->snextc();
467 }
468 }
469 if (traits_type::eq_int_type(__c, __eof))
470 __err |= ios_base::eofbit;
471 else if (traits_type::eq_int_type(__c, __delim))
472 {
473 ++_M_gcount;
474 __sb->sbumpc();
475 }
476 }
477 catch(...)
478 { this->_M_setstate(ios_base::badbit); }
479 if (__err)
480 this->setstate(__err);
481 }
482 return *this;
483 }
e9fb72e8
PC
484
485 template<>
486 basic_istream<wchar_t>&
487 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
488 wchar_t __delim)
489 {
490 typedef basic_istream<wchar_t> __istream_type;
491 typedef __istream_type::int_type __int_type;
492 typedef __istream_type::char_type __char_type;
493 typedef __istream_type::traits_type __traits_type;
494 typedef __istream_type::__streambuf_type __streambuf_type;
495 typedef __istream_type::__ctype_type __ctype_type;
496 typedef basic_string<wchar_t> __string_type;
497 typedef __string_type::size_type __size_type;
498
499 __size_type __extracted = 0;
500 const __size_type __n = __str.max_size();
501 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
502 __istream_type::sentry __cerb(__in, true);
503 if (__cerb)
504 {
505 try
506 {
507 __str.erase();
508 const __int_type __idelim = __traits_type::to_int_type(__delim);
509 const __int_type __eof = __traits_type::eof();
510 __streambuf_type* __sb = __in.rdbuf();
511 __int_type __c = __sb->sgetc();
512
513 while (__extracted < __n
514 && !__traits_type::eq_int_type(__c, __eof)
515 && !__traits_type::eq_int_type(__c, __idelim))
516 {
517 streamsize __size = std::min(streamsize(__sb->egptr()
518 - __sb->gptr()),
519 streamsize(__n - __extracted));
520 if (__size > 1)
521 {
522 const __char_type* __p = __traits_type::find(__sb->gptr(),
523 __size,
524 __delim);
525 if (__p)
526 __size = __p - __sb->gptr();
527 __str.append(__sb->gptr(), __size);
528 __sb->gbump(__size);
529 __extracted += __size;
530 __c = __sb->sgetc();
531 }
532 else
533 {
534 __str += __traits_type::to_char_type(__c);
535 ++__extracted;
536 __c = __sb->snextc();
537 }
538 }
539
540 if (__traits_type::eq_int_type(__c, __eof))
541 __err |= ios_base::eofbit;
542 else if (__traits_type::eq_int_type(__c, __idelim))
543 {
544 ++__extracted;
545 __sb->sbumpc();
546 }
547 else
548 __err |= ios_base::failbit;
549 }
550 catch(...)
551 {
552 // _GLIBCXX_RESOLVE_LIB_DEFECTS
553 // 91. Description of operator>> and getline() for string<>
554 // might cause endless loop
555 __in._M_setstate(ios_base::badbit);
556 }
557 }
558 if (!__extracted)
559 __err |= ios_base::failbit;
560 if (__err)
561 __in.setstate(__err);
562 return __in;
563 }
adb31ad6
PC
564#endif
565} // namespace std