1 // Input streams operating on strings-*- C++ -*-
3 // Copyright (C) 2004-2016 Free Software Foundation, Inc.
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)
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 // 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.
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/>.
26 // ISO C++ 14882: 27.6.1 Input streams
29 #ifndef _GLIBCXX_USE_CXX11_ABI
30 // Instantiations in this file use the new SSO std::string ABI unless included
31 // by another file which defines _GLIBCXX_USE_CXX11_ABI=0.
32 # define _GLIBCXX_USE_CXX11_ABI 1
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 operator>>(basic_istream
<char>& __in
, basic_string
<char>& __str
)
45 typedef basic_istream
<char> __istream_type
;
46 typedef __istream_type::int_type __int_type
;
47 typedef __istream_type::traits_type __traits_type
;
48 typedef __istream_type::__streambuf_type __streambuf_type
;
49 typedef __istream_type::__ctype_type __ctype_type
;
50 typedef basic_string
<char> __string_type
;
51 typedef __string_type::size_type __size_type
;
53 __size_type __extracted
= 0;
54 ios_base::iostate __err
= ios_base::goodbit
;
55 __istream_type::sentry
__cerb(__in
, false);
61 const streamsize __w
= __in
.width();
62 const __size_type __n
= __w
> 0 ? static_cast<__size_type
>(__w
)
64 const __ctype_type
& __ct
= use_facet
<__ctype_type
>(__in
.getloc());
65 const __int_type __eof
= __traits_type::eof();
66 __streambuf_type
* __sb
= __in
.rdbuf();
67 __int_type __c
= __sb
->sgetc();
69 while (__extracted
< __n
70 && !__traits_type::eq_int_type(__c
, __eof
)
71 && !__ct
.is(ctype_base::space
,
72 __traits_type::to_char_type(__c
)))
74 streamsize __size
= std::min(streamsize(__sb
->egptr()
76 streamsize(__n
- __extracted
));
79 __size
= (__ct
.scan_is(ctype_base::space
,
81 __sb
->gptr() + __size
)
83 __str
.append(__sb
->gptr(), __size
);
84 __sb
->__safe_gbump(__size
);
85 __extracted
+= __size
;
90 __str
+= __traits_type::to_char_type(__c
);
96 if (__traits_type::eq_int_type(__c
, __eof
))
97 __err
|= ios_base::eofbit
;
100 __catch(__cxxabiv1::__forced_unwind
&)
102 __in
._M_setstate(ios_base::badbit
);
103 __throw_exception_again
;
107 // _GLIBCXX_RESOLVE_LIB_DEFECTS
108 // 91. Description of operator>> and getline() for string<>
109 // might cause endless loop
110 __in
._M_setstate(ios_base::badbit
);
114 __err
|= ios_base::failbit
;
116 __in
.setstate(__err
);
122 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
125 typedef basic_istream
<char> __istream_type
;
126 typedef __istream_type::int_type __int_type
;
127 typedef __istream_type::char_type __char_type
;
128 typedef __istream_type::traits_type __traits_type
;
129 typedef __istream_type::__streambuf_type __streambuf_type
;
130 typedef basic_string
<char> __string_type
;
131 typedef __string_type::size_type __size_type
;
133 __size_type __extracted
= 0;
134 const __size_type __n
= __str
.max_size();
135 ios_base::iostate __err
= ios_base::goodbit
;
136 __istream_type::sentry
__cerb(__in
, true);
142 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
143 const __int_type __eof
= __traits_type::eof();
144 __streambuf_type
* __sb
= __in
.rdbuf();
145 __int_type __c
= __sb
->sgetc();
147 while (__extracted
< __n
148 && !__traits_type::eq_int_type(__c
, __eof
)
149 && !__traits_type::eq_int_type(__c
, __idelim
))
151 streamsize __size
= std::min(streamsize(__sb
->egptr()
153 streamsize(__n
- __extracted
));
156 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
160 __size
= __p
- __sb
->gptr();
161 __str
.append(__sb
->gptr(), __size
);
162 __sb
->__safe_gbump(__size
);
163 __extracted
+= __size
;
168 __str
+= __traits_type::to_char_type(__c
);
170 __c
= __sb
->snextc();
174 if (__traits_type::eq_int_type(__c
, __eof
))
175 __err
|= ios_base::eofbit
;
176 else if (__traits_type::eq_int_type(__c
, __idelim
))
182 __err
|= ios_base::failbit
;
184 __catch(__cxxabiv1::__forced_unwind
&)
186 __in
._M_setstate(ios_base::badbit
);
187 __throw_exception_again
;
191 // _GLIBCXX_RESOLVE_LIB_DEFECTS
192 // 91. Description of operator>> and getline() for string<>
193 // might cause endless loop
194 __in
._M_setstate(ios_base::badbit
);
198 __err
|= ios_base::failbit
;
200 __in
.setstate(__err
);
204 #ifdef _GLIBCXX_USE_WCHAR_T
206 basic_istream
<wchar_t>&
207 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
210 typedef basic_istream
<wchar_t> __istream_type
;
211 typedef __istream_type::int_type __int_type
;
212 typedef __istream_type::char_type __char_type
;
213 typedef __istream_type::traits_type __traits_type
;
214 typedef __istream_type::__streambuf_type __streambuf_type
;
215 typedef basic_string
<wchar_t> __string_type
;
216 typedef __string_type::size_type __size_type
;
218 __size_type __extracted
= 0;
219 const __size_type __n
= __str
.max_size();
220 ios_base::iostate __err
= ios_base::goodbit
;
221 __istream_type::sentry
__cerb(__in
, true);
227 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
228 const __int_type __eof
= __traits_type::eof();
229 __streambuf_type
* __sb
= __in
.rdbuf();
230 __int_type __c
= __sb
->sgetc();
232 while (__extracted
< __n
233 && !__traits_type::eq_int_type(__c
, __eof
)
234 && !__traits_type::eq_int_type(__c
, __idelim
))
236 streamsize __size
= std::min(streamsize(__sb
->egptr()
238 streamsize(__n
- __extracted
));
241 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
245 __size
= __p
- __sb
->gptr();
246 __str
.append(__sb
->gptr(), __size
);
247 __sb
->__safe_gbump(__size
);
248 __extracted
+= __size
;
253 __str
+= __traits_type::to_char_type(__c
);
255 __c
= __sb
->snextc();
259 if (__traits_type::eq_int_type(__c
, __eof
))
260 __err
|= ios_base::eofbit
;
261 else if (__traits_type::eq_int_type(__c
, __idelim
))
267 __err
|= ios_base::failbit
;
269 __catch(__cxxabiv1::__forced_unwind
&)
271 __in
._M_setstate(ios_base::badbit
);
272 __throw_exception_again
;
276 // _GLIBCXX_RESOLVE_LIB_DEFECTS
277 // 91. Description of operator>> and getline() for string<>
278 // might cause endless loop
279 __in
._M_setstate(ios_base::badbit
);
283 __err
|= ios_base::failbit
;
285 __in
.setstate(__err
);
290 _GLIBCXX_END_NAMESPACE_VERSION