xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/src/c++98/istream.cc (revision 2dd295436a0082eb4f8d294f4aa73c223413d0f2)
1 // Input streams -*- C++ -*-
2 
3 // Copyright (C) 2004-2022 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 //
26 // ISO C++ 14882: 27.6.1  Input streams
27 //
28 
29 #include <istream>
30 
31 namespace std _GLIBCXX_VISIBILITY(default)
32 {
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
34 
35   template<>
36     basic_istream<char>&
37     basic_istream<char>::
38     getline(char_type* __s, streamsize __n, char_type __delim)
39     {
40       _M_gcount = 0;
41       ios_base::iostate __err = ios_base::goodbit;
42       sentry __cerb(*this, true);
43       if (__cerb)
44 	{
45           __try
46 	    {
47 	      const int_type __idelim = traits_type::to_int_type(__delim);
48 	      const int_type __eof = traits_type::eof();
49 	      __streambuf_type* __sb = this->rdbuf();
50 	      int_type __c = __sb->sgetc();
51 
52 	      while (_M_gcount + 1 < __n
53 		     && !traits_type::eq_int_type(__c, __eof)
54 		     && !traits_type::eq_int_type(__c, __idelim))
55 		{
56 		  streamsize __size = std::min(streamsize(__sb->egptr()
57 							  - __sb->gptr()),
58 					       streamsize(__n - _M_gcount
59 							  - 1));
60 		  if (__size > 1)
61 		    {
62 		      const char_type* __p = traits_type::find(__sb->gptr(),
63 							       __size,
64 							       __delim);
65 		      if (__p)
66 			__size = __p - __sb->gptr();
67 		      traits_type::copy(__s, __sb->gptr(), __size);
68 		      __s += __size;
69 		      __sb->__safe_gbump(__size);
70 		      _M_gcount += __size;
71 		      __c = __sb->sgetc();
72 		    }
73 		  else
74 		    {
75 		      *__s++ = traits_type::to_char_type(__c);
76 		      ++_M_gcount;
77 		      __c = __sb->snextc();
78 		    }
79 		}
80 
81 	      if (traits_type::eq_int_type(__c, __eof))
82 		__err |= ios_base::eofbit;
83 	      else if (traits_type::eq_int_type(__c, __idelim))
84 		{
85 		  ++_M_gcount;
86 		  __sb->sbumpc();
87 		}
88 	      else
89 		__err |= ios_base::failbit;
90 	    }
91 	  __catch(__cxxabiv1::__forced_unwind&)
92 	    {
93 	      this->_M_setstate(ios_base::badbit);
94 	      __throw_exception_again;
95 	    }
96 	  __catch(...)
97 	    { this->_M_setstate(ios_base::badbit); }
98 	}
99       // _GLIBCXX_RESOLVE_LIB_DEFECTS
100       // 243. get and getline when sentry reports failure.
101       if (__n > 0)
102 	*__s = char_type();
103       if (!_M_gcount)
104 	__err |= ios_base::failbit;
105       if (__err)
106 	this->setstate(__err);
107       return *this;
108     }
109 
110   template<>
111     basic_istream<char>&
112     basic_istream<char>::
113     ignore(streamsize __n, int_type __delim)
114     {
115 #ifdef _GLIBCXX_COMPAT_
116       if (traits_type::eq_int_type(__delim, traits_type::eof()))
117 	return ignore(__n);
118 #endif
119 
120       _M_gcount = 0;
121       sentry __cerb(*this, true);
122       if (__n > 0 && __cerb)
123 	{
124 	  ios_base::iostate __err = ios_base::goodbit;
125 	  __try
126 	    {
127 	      const char_type __cdelim = traits_type::to_char_type(__delim);
128 	      const int_type __eof = traits_type::eof();
129 	      __streambuf_type* __sb = this->rdbuf();
130 	      int_type __c = __sb->sgetc();
131 
132 	      bool __large_ignore = false;
133 	      while (true)
134 		{
135 		  while (_M_gcount < __n
136 			 && !traits_type::eq_int_type(__c, __eof)
137 			 && !traits_type::eq_int_type(__c, __delim))
138 		    {
139 		      streamsize __size = std::min(streamsize(__sb->egptr()
140 							      - __sb->gptr()),
141 						   streamsize(__n - _M_gcount));
142 		      if (__size > 1)
143 			{
144 			  const char_type* __p = traits_type::find(__sb->gptr(),
145 								   __size,
146 								   __cdelim);
147 			  if (__p)
148 			    __size = __p - __sb->gptr();
149 			  __sb->__safe_gbump(__size);
150 			  _M_gcount += __size;
151 			  __c = __sb->sgetc();
152 			}
153 		      else
154 			{
155 			  ++_M_gcount;
156 			  __c = __sb->snextc();
157 			}
158 		    }
159 		  if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
160 		      && !traits_type::eq_int_type(__c, __eof)
161 		      && !traits_type::eq_int_type(__c, __delim))
162 		    {
163 		      _M_gcount =
164 			__gnu_cxx::__numeric_traits<streamsize>::__min;
165 		      __large_ignore = true;
166 		    }
167 		  else
168 		    break;
169 		}
170 
171 	      if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max)
172 		{
173 		  if (__large_ignore)
174 		    _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
175 
176 		  if (traits_type::eq_int_type(__c, __eof))
177 		    __err |= ios_base::eofbit;
178 		  else
179 		    {
180 		      if (_M_gcount != __n)
181 			++_M_gcount;
182 		      __sb->sbumpc();
183 		    }
184 		}
185 	      else if (_M_gcount < __n) // implies __c == __delim or EOF
186 		{
187 		  if (traits_type::eq_int_type(__c, __eof))
188 		    __err |= ios_base::eofbit;
189 		  else
190 		    {
191 		      ++_M_gcount;
192 		      __sb->sbumpc();
193 		    }
194 		}
195 	    }
196 	  __catch(__cxxabiv1::__forced_unwind&)
197 	    {
198 	      this->_M_setstate(ios_base::badbit);
199 	      __throw_exception_again;
200 	    }
201 	  __catch(...)
202 	    { this->_M_setstate(ios_base::badbit); }
203 	  if (__err)
204 	    this->setstate(__err);
205 	}
206       return *this;
207     }
208 
209     void
210     __istream_extract(istream& __in, char* __s, streamsize __num)
211     {
212       typedef basic_istream<char>       	__istream_type;
213       typedef __istream_type::int_type		__int_type;
214       typedef __istream_type::char_type		__char_type;
215       typedef __istream_type::traits_type	__traits_type;
216       typedef __istream_type::__streambuf_type  __streambuf_type;
217       typedef __istream_type::__ctype_type	__ctype_type;
218 
219       streamsize __extracted = 0;
220       ios_base::iostate __err = ios_base::goodbit;
221       __istream_type::sentry __cerb(__in, false);
222       if (__cerb)
223 	{
224 	  __try
225 	    {
226 	      // Figure out how many characters to extract.
227 	      streamsize __width = __in.width();
228 	      if (0 < __width && __width < __num)
229 		__num = __width;
230 
231 	      const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
232 
233 	      const __int_type __eof = __traits_type::eof();
234 	      __streambuf_type* __sb = __in.rdbuf();
235 	      __int_type __c = __sb->sgetc();
236 
237 	      while (__extracted < __num - 1
238 		     && !__traits_type::eq_int_type(__c, __eof)
239 		     && !__ct.is(ctype_base::space,
240 				 __traits_type::to_char_type(__c)))
241 		{
242 		  streamsize __size = std::min(streamsize(__sb->egptr()
243 							  - __sb->gptr()),
244 					       streamsize(__num - __extracted
245 							  - 1));
246 		  if (__size > 1)
247 		    {
248 		      __size = (__ct.scan_is(ctype_base::space,
249 					     __sb->gptr() + 1,
250 					     __sb->gptr() + __size)
251 				- __sb->gptr());
252 		      __traits_type::copy(__s, __sb->gptr(), __size);
253 		      __s += __size;
254 		      __sb->__safe_gbump(__size);
255 		      __extracted += __size;
256 		      __c = __sb->sgetc();
257 		    }
258 		  else
259 		    {
260 		      *__s++ = __traits_type::to_char_type(__c);
261 		      ++__extracted;
262 		      __c = __sb->snextc();
263 		    }
264 		}
265 
266 	      if (__extracted < __num - 1
267 		  && __traits_type::eq_int_type(__c, __eof))
268 		__err |= ios_base::eofbit;
269 
270 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
271 	      // 68.  Extractors for char* should store null at end
272 	      *__s = __char_type();
273 	      __in.width(0);
274 	    }
275 	  __catch(__cxxabiv1::__forced_unwind&)
276 	    {
277 	      __in._M_setstate(ios_base::badbit);
278 	      __throw_exception_again;
279 	    }
280 	  __catch(...)
281 	    { __in._M_setstate(ios_base::badbit); }
282 	}
283       if (!__extracted)
284 	__err |= ios_base::failbit;
285       if (__err)
286 	__in.setstate(__err);
287     }
288 
289 #ifdef _GLIBCXX_USE_WCHAR_T
290   template<>
291     basic_istream<wchar_t>&
292     basic_istream<wchar_t>::
293     getline(char_type* __s, streamsize __n, char_type __delim)
294     {
295       _M_gcount = 0;
296       ios_base::iostate __err = ios_base::goodbit;
297       sentry __cerb(*this, true);
298       if (__cerb)
299 	{
300           __try
301 	    {
302 	      const int_type __idelim = traits_type::to_int_type(__delim);
303 	      const int_type __eof = traits_type::eof();
304 	      __streambuf_type* __sb = this->rdbuf();
305 	      int_type __c = __sb->sgetc();
306 
307 	      while (_M_gcount + 1 < __n
308 		     && !traits_type::eq_int_type(__c, __eof)
309 		     && !traits_type::eq_int_type(__c, __idelim))
310 		{
311 		  streamsize __size = std::min(streamsize(__sb->egptr()
312 							  - __sb->gptr()),
313 					       streamsize(__n - _M_gcount
314 							  - 1));
315 		  if (__size > 1)
316 		    {
317 		      const char_type* __p = traits_type::find(__sb->gptr(),
318 							       __size,
319 							       __delim);
320 		      if (__p)
321 			__size = __p - __sb->gptr();
322 		      traits_type::copy(__s, __sb->gptr(), __size);
323 		      __s += __size;
324 		      __sb->__safe_gbump(__size);
325 		      _M_gcount += __size;
326 		      __c = __sb->sgetc();
327 		    }
328 		  else
329 		    {
330 		      *__s++ = traits_type::to_char_type(__c);
331 		      ++_M_gcount;
332 		      __c = __sb->snextc();
333 		    }
334 		}
335 
336 	      if (traits_type::eq_int_type(__c, __eof))
337 		__err |= ios_base::eofbit;
338 	      else if (traits_type::eq_int_type(__c, __idelim))
339 		{
340 		  ++_M_gcount;
341 		  __sb->sbumpc();
342 		}
343 	      else
344 		__err |= ios_base::failbit;
345 	    }
346 	  __catch(__cxxabiv1::__forced_unwind&)
347 	    {
348 	      this->_M_setstate(ios_base::badbit);
349 	      __throw_exception_again;
350 	    }
351 	  __catch(...)
352 	    { this->_M_setstate(ios_base::badbit); }
353 	}
354       // _GLIBCXX_RESOLVE_LIB_DEFECTS
355       // 243. get and getline when sentry reports failure.
356       if (__n > 0)
357 	*__s = char_type();
358       if (!_M_gcount)
359 	__err |= ios_base::failbit;
360       if (__err)
361 	this->setstate(__err);
362       return *this;
363     }
364 
365   template<>
366     basic_istream<wchar_t>&
367     basic_istream<wchar_t>::
368     ignore(streamsize __n, int_type __delim)
369     {
370 #ifdef _GLIBCXX_COMPAT_
371       if (traits_type::eq_int_type(__delim, traits_type::eof()))
372 	return ignore(__n);
373 #endif
374 
375       _M_gcount = 0;
376       sentry __cerb(*this, true);
377       if (__n > 0 && __cerb)
378 	{
379 	  ios_base::iostate __err = ios_base::goodbit;
380 	  __try
381 	    {
382 	      const char_type __cdelim = traits_type::to_char_type(__delim);
383 	      const int_type __eof = traits_type::eof();
384 	      __streambuf_type* __sb = this->rdbuf();
385 	      int_type __c = __sb->sgetc();
386 
387 	      bool __large_ignore = false;
388 	      while (true)
389 		{
390 		  while (_M_gcount < __n
391 			 && !traits_type::eq_int_type(__c, __eof)
392 			 && !traits_type::eq_int_type(__c, __delim))
393 		    {
394 		      streamsize __size = std::min(streamsize(__sb->egptr()
395 							      - __sb->gptr()),
396 						   streamsize(__n - _M_gcount));
397 		      if (__size > 1)
398 			{
399 			  const char_type* __p = traits_type::find(__sb->gptr(),
400 								   __size,
401 								   __cdelim);
402 			  if (__p)
403 			    __size = __p - __sb->gptr();
404 			  __sb->__safe_gbump(__size);
405 			  _M_gcount += __size;
406 			  __c = __sb->sgetc();
407 			}
408 		      else
409 			{
410 			  ++_M_gcount;
411 			  __c = __sb->snextc();
412 			}
413 		    }
414 		  if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
415 		      && !traits_type::eq_int_type(__c, __eof)
416 		      && !traits_type::eq_int_type(__c, __delim))
417 		    {
418 		      _M_gcount =
419 			__gnu_cxx::__numeric_traits<streamsize>::__min;
420 		      __large_ignore = true;
421 		    }
422 		  else
423 		    break;
424 		}
425 
426 	      if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max)
427 		{
428 		  if (__large_ignore)
429 		    _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
430 
431 		  if (traits_type::eq_int_type(__c, __eof))
432 		    __err |= ios_base::eofbit;
433 		  else
434 		    {
435 		      if (_M_gcount != __n)
436 			++_M_gcount;
437 		      __sb->sbumpc();
438 		    }
439 		}
440 	      else if (_M_gcount < __n) // implies __c == __delim or EOF
441 		{
442 		  if (traits_type::eq_int_type(__c, __eof))
443 		    __err |= ios_base::eofbit;
444 		  else
445 		    {
446 		      ++_M_gcount;
447 		      __sb->sbumpc();
448 		    }
449 		}
450 	    }
451 	  __catch(__cxxabiv1::__forced_unwind&)
452 	    {
453 	      this->_M_setstate(ios_base::badbit);
454 	      __throw_exception_again;
455 	    }
456 	  __catch(...)
457 	    { this->_M_setstate(ios_base::badbit); }
458 	  if (__err)
459 	    this->setstate(__err);
460 	}
461       return *this;
462     }
463 #endif
464 
465 
466 _GLIBCXX_END_NAMESPACE_VERSION
467 } // namespace
468