1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_ISTREAM
|
---|
30 | #define LIBCPP_ISTREAM
|
---|
31 |
|
---|
32 | #include <ios>
|
---|
33 | #include <iosfwd>
|
---|
34 | #include <limits>
|
---|
35 | #include <locale>
|
---|
36 | #include <ostream>
|
---|
37 | #include <utility>
|
---|
38 |
|
---|
39 | namespace std
|
---|
40 | {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * 27.7.2.1, class template basic_stream:
|
---|
44 | */
|
---|
45 |
|
---|
46 | template<class Char, class Traits>
|
---|
47 | class basic_istream: virtual public basic_ios<Char, Traits>
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | using traits_type = Traits;
|
---|
51 | using char_type = Char;
|
---|
52 | using int_type = typename traits_type::int_type;
|
---|
53 | using pos_type = typename traits_type::pos_type;
|
---|
54 | using off_type = typename traits_type::off_type;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * 27.7.2.1.1, constructor/destructor:
|
---|
58 | */
|
---|
59 |
|
---|
60 | explicit basic_istream(basic_streambuf<Char, Traits>* sb)
|
---|
61 | : gcount_{0}
|
---|
62 | {
|
---|
63 | basic_ios<Char, Traits>::init(sb);
|
---|
64 | }
|
---|
65 |
|
---|
66 | virtual ~basic_istream()
|
---|
67 | { /* DUMMY BODY */ }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * 27.7.2.1.3, prefix/suffix:
|
---|
71 | */
|
---|
72 |
|
---|
73 | class sentry
|
---|
74 | {
|
---|
75 | public:
|
---|
76 | explicit sentry(basic_istream<Char, Traits>& is, bool noskipws = false)
|
---|
77 | : ok_{false}
|
---|
78 | {
|
---|
79 | if (!is.good())
|
---|
80 | is.setstate(ios_base::failbit);
|
---|
81 | else
|
---|
82 | {
|
---|
83 | if (is.tie())
|
---|
84 | is.tie()->flush();
|
---|
85 |
|
---|
86 | if (!noskipws && ((is.flags() & ios_base::skipws) != 0))
|
---|
87 | {
|
---|
88 | const auto& ct = use_facet<ctype<Char>>(is.getloc());
|
---|
89 | while (true)
|
---|
90 | {
|
---|
91 | auto i = is.rdbuf()->sgetc();
|
---|
92 | if (Traits::eq_int_type(i, Traits::eof()))
|
---|
93 | {
|
---|
94 | is.setstate(ios_base::failbit | ios_base::eofbit);
|
---|
95 | break;
|
---|
96 | }
|
---|
97 |
|
---|
98 | auto c = Traits::to_char_type(i);
|
---|
99 | if (!ct.is(ctype_base::space, c))
|
---|
100 | break;
|
---|
101 | else
|
---|
102 | is.rdbuf()->sbumpc();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (is.good())
|
---|
108 | ok_ = true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | ~sentry() = default;
|
---|
112 |
|
---|
113 | explicit operator bool() const
|
---|
114 | {
|
---|
115 | return ok_;
|
---|
116 | }
|
---|
117 |
|
---|
118 | sentry(const sentry&) = delete;
|
---|
119 | sentry& operator=(const sentry&) = delete;
|
---|
120 |
|
---|
121 | private:
|
---|
122 | using traits_type = Traits;
|
---|
123 | bool ok_;
|
---|
124 | };
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * 27.7.2.2, formatted input:
|
---|
128 | */
|
---|
129 |
|
---|
130 | basic_istream<Char, Traits>& operator>>(
|
---|
131 | basic_istream<Char, Traits>& (*pf)(basic_istream<Char, Traits>&)
|
---|
132 | )
|
---|
133 | {
|
---|
134 | return pf(*this);
|
---|
135 | }
|
---|
136 |
|
---|
137 | basic_istream<Char, Traits>& operator>>(
|
---|
138 | basic_ios<Char, Traits>& (*pf)(basic_ios<Char, Traits>&)
|
---|
139 | )
|
---|
140 | {
|
---|
141 | pf(*this);
|
---|
142 |
|
---|
143 | return *this;
|
---|
144 | }
|
---|
145 |
|
---|
146 | basic_istream<Char, Traits>& operator>>(
|
---|
147 | ios_base& (*pf)(ios_base&)
|
---|
148 | )
|
---|
149 | {
|
---|
150 | pf(*this);
|
---|
151 |
|
---|
152 | return *this;
|
---|
153 | }
|
---|
154 |
|
---|
155 | basic_istream<Char, Traits>& operator>>(bool& x)
|
---|
156 | {
|
---|
157 | sentry sen{*this, false};
|
---|
158 |
|
---|
159 | if (sen)
|
---|
160 | {
|
---|
161 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
162 | auto err = ios_base::goodbit;
|
---|
163 |
|
---|
164 | auto loc = this->getloc();
|
---|
165 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
166 | this->setstate(err);
|
---|
167 | }
|
---|
168 |
|
---|
169 | return *this;
|
---|
170 | }
|
---|
171 |
|
---|
172 | basic_istream<Char, Traits>& operator>>(short& x)
|
---|
173 | {
|
---|
174 | sentry sen{*this, false};
|
---|
175 |
|
---|
176 | if (sen)
|
---|
177 | {
|
---|
178 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
179 | auto err = ios_base::goodbit;
|
---|
180 |
|
---|
181 | long tmp{};
|
---|
182 | auto loc = this->getloc();
|
---|
183 | use_facet<num_get>(loc).get(*this, 0, *this, err, tmp);
|
---|
184 |
|
---|
185 | if (tmp < numeric_limits<short>::min())
|
---|
186 | {
|
---|
187 | err |= ios_base::failbit;
|
---|
188 | x = numeric_limits<short>::min();
|
---|
189 | }
|
---|
190 | else if (numeric_limits<short>::max() < tmp)
|
---|
191 | {
|
---|
192 | err |= ios_base::failbit;
|
---|
193 | x = numeric_limits<short>::max();
|
---|
194 | }
|
---|
195 | else
|
---|
196 | x = static_cast<short>(tmp);
|
---|
197 |
|
---|
198 | this->setstate(err);
|
---|
199 | }
|
---|
200 |
|
---|
201 | return *this;
|
---|
202 | }
|
---|
203 |
|
---|
204 | basic_istream<Char, Traits>& operator>>(unsigned short& x)
|
---|
205 | {
|
---|
206 | sentry sen{*this, false};
|
---|
207 |
|
---|
208 | if (sen)
|
---|
209 | {
|
---|
210 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
211 | auto err = ios_base::goodbit;
|
---|
212 |
|
---|
213 | auto loc = this->getloc();
|
---|
214 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
215 | this->setstate(err);
|
---|
216 | }
|
---|
217 |
|
---|
218 | return *this;
|
---|
219 | }
|
---|
220 |
|
---|
221 | basic_istream<Char, Traits>& operator>>(int& x)
|
---|
222 | {
|
---|
223 | sentry sen{*this, false};
|
---|
224 |
|
---|
225 | if (sen)
|
---|
226 | {
|
---|
227 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
228 | auto err = ios_base::goodbit;
|
---|
229 |
|
---|
230 | long tmp{};
|
---|
231 | auto loc = this->getloc();
|
---|
232 | use_facet<num_get>(loc).get(*this, 0, *this, err, tmp);
|
---|
233 |
|
---|
234 | if (tmp < numeric_limits<int>::min())
|
---|
235 | {
|
---|
236 | err |= ios_base::failbit;
|
---|
237 | x = numeric_limits<int>::min();
|
---|
238 | }
|
---|
239 | else if (numeric_limits<int>::max() < tmp)
|
---|
240 | {
|
---|
241 | err |= ios_base::failbit;
|
---|
242 | x = numeric_limits<int>::max();
|
---|
243 | }
|
---|
244 | else
|
---|
245 | x = static_cast<int>(tmp);
|
---|
246 |
|
---|
247 | this->setstate(err);
|
---|
248 | }
|
---|
249 |
|
---|
250 | return *this;
|
---|
251 | }
|
---|
252 |
|
---|
253 | basic_istream<Char, Traits>& operator>>(unsigned int& x)
|
---|
254 | {
|
---|
255 | sentry sen{*this, false};
|
---|
256 |
|
---|
257 | if (sen)
|
---|
258 | {
|
---|
259 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
260 | auto err = ios_base::goodbit;
|
---|
261 |
|
---|
262 | auto loc = this->getloc();
|
---|
263 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
264 | this->setstate(err);
|
---|
265 | }
|
---|
266 |
|
---|
267 | return *this;
|
---|
268 | }
|
---|
269 |
|
---|
270 | basic_istream<Char, Traits>& operator>>(long& x)
|
---|
271 | {
|
---|
272 | sentry sen{*this, false};
|
---|
273 |
|
---|
274 | if (sen)
|
---|
275 | {
|
---|
276 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
277 | auto err = ios_base::goodbit;
|
---|
278 |
|
---|
279 | auto loc = this->getloc();
|
---|
280 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
281 | this->setstate(err);
|
---|
282 | }
|
---|
283 |
|
---|
284 | return *this;
|
---|
285 | }
|
---|
286 |
|
---|
287 | basic_istream<Char, Traits>& operator>>(unsigned long& x)
|
---|
288 | {
|
---|
289 | sentry sen{*this, false};
|
---|
290 |
|
---|
291 | if (sen)
|
---|
292 | {
|
---|
293 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
294 | auto err = ios_base::goodbit;
|
---|
295 |
|
---|
296 | auto loc = this->getloc();
|
---|
297 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
298 | this->setstate(err);
|
---|
299 | }
|
---|
300 |
|
---|
301 | return *this;
|
---|
302 | }
|
---|
303 |
|
---|
304 | basic_istream<Char, Traits>& operator>>(long long& x)
|
---|
305 | {
|
---|
306 | sentry sen{*this, false};
|
---|
307 |
|
---|
308 | if (sen)
|
---|
309 | {
|
---|
310 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
311 | auto err = ios_base::goodbit;
|
---|
312 |
|
---|
313 | auto loc = this->getloc();
|
---|
314 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
315 | this->setstate(err);
|
---|
316 | }
|
---|
317 |
|
---|
318 | return *this;
|
---|
319 | }
|
---|
320 |
|
---|
321 | basic_istream<Char, Traits>& operator>>(unsigned long long& x)
|
---|
322 | {
|
---|
323 | sentry sen{*this, false};
|
---|
324 |
|
---|
325 | if (sen)
|
---|
326 | {
|
---|
327 | using num_get = num_get<Char, istreambuf_iterator<Char, Traits>>;
|
---|
328 | auto err = ios_base::goodbit;
|
---|
329 |
|
---|
330 | auto loc = this->getloc();
|
---|
331 | use_facet<num_get>(loc).get(*this, 0, *this, err, x);
|
---|
332 | this->setstate(err);
|
---|
333 | }
|
---|
334 |
|
---|
335 | return *this;
|
---|
336 | }
|
---|
337 |
|
---|
338 | basic_istream<Char, Traits>& operator>>(float& x)
|
---|
339 | {
|
---|
340 | // TODO: implement
|
---|
341 | }
|
---|
342 |
|
---|
343 | basic_istream<Char, Traits>& operator>>(double& x)
|
---|
344 | {
|
---|
345 | // TODO: implement
|
---|
346 | }
|
---|
347 |
|
---|
348 | basic_istream<Char, Traits>& operator>>(long double& x)
|
---|
349 | {
|
---|
350 | // TODO: implement
|
---|
351 | }
|
---|
352 |
|
---|
353 | basic_istream<Char, Traits>& operator>>(void*& p)
|
---|
354 | {
|
---|
355 | // TODO: implement
|
---|
356 | }
|
---|
357 |
|
---|
358 | basic_istream<Char, Traits>& operator>>(basic_streambuf<Char, Traits>* sb)
|
---|
359 | {
|
---|
360 | if (!sb)
|
---|
361 | {
|
---|
362 | this->setstate(ios_base::failbit);
|
---|
363 | return *this;
|
---|
364 | }
|
---|
365 |
|
---|
366 | gcount_ = 0;
|
---|
367 | sentry sen{*this, false};
|
---|
368 |
|
---|
369 | if (sen)
|
---|
370 | {
|
---|
371 | while (true)
|
---|
372 | {
|
---|
373 | auto ic = this->rdbuf()->sgetc();
|
---|
374 | if (traits_type::eq_int_type(ic, traits_type::eof()))
|
---|
375 | {
|
---|
376 | this->setstate(ios_base::eofbit);
|
---|
377 | break;
|
---|
378 | }
|
---|
379 |
|
---|
380 | auto res = sb->sputc(traits_type::to_char_type(ic));
|
---|
381 | if (traits_type::eq_int_type(res, traits_type::eof()))
|
---|
382 | break;
|
---|
383 |
|
---|
384 | ++gcount_;
|
---|
385 | this->rdbuf()->sbumpc();
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | return *this;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * 27.7.2.3, unformatted input:
|
---|
394 | * TODO: Once we have exceptions, implement
|
---|
395 | * 27.7.2.3 paragraph 1.
|
---|
396 | */
|
---|
397 |
|
---|
398 | streamsize gcount() const
|
---|
399 | {
|
---|
400 | return gcount_;
|
---|
401 | }
|
---|
402 |
|
---|
403 | int_type get()
|
---|
404 | {
|
---|
405 | gcount_ = 0;
|
---|
406 | sentry sen{*this, true};
|
---|
407 |
|
---|
408 | if (sen)
|
---|
409 | {
|
---|
410 | auto res = this->rdbuf()->sbumpc();
|
---|
411 | if (!traits_type::eq_int_type(res, traits_type::eof()))
|
---|
412 | {
|
---|
413 | gcount_ = 1;
|
---|
414 | return res;
|
---|
415 | }
|
---|
416 |
|
---|
417 | this->setstate(ios_base::failbit | ios_base::eofbit);
|
---|
418 | }
|
---|
419 |
|
---|
420 | return traits_type::eof();
|
---|
421 | }
|
---|
422 |
|
---|
423 | basic_istream<Char, Traits>& get(char_type& c)
|
---|
424 | {
|
---|
425 | auto res = get();
|
---|
426 | if (res != traits_type::eof())
|
---|
427 | c = traits_type::to_char_type(res);
|
---|
428 |
|
---|
429 | return *this;
|
---|
430 | }
|
---|
431 |
|
---|
432 | basic_istream<Char, Traits>& get(char_type* s, streamsize n, char_type delim)
|
---|
433 | {
|
---|
434 | gcount_ = 0;
|
---|
435 | sentry sen{*this, true};
|
---|
436 |
|
---|
437 | if (sen && n > 0)
|
---|
438 | {
|
---|
439 | while(gcount_ < n - 1)
|
---|
440 | {
|
---|
441 | auto c = this->rdbuf()->sbumpc();
|
---|
442 |
|
---|
443 | if (traits_type::eq_int_type(c, traits_type::eof()))
|
---|
444 | {
|
---|
445 | this->setstate(ios_base::eofbit);
|
---|
446 | break;
|
---|
447 | }
|
---|
448 |
|
---|
449 | s[gcount_++] = traits_type::to_char_type(c);
|
---|
450 |
|
---|
451 | auto peek = traits_type::to_char_type(this->rdbuf()->sgetc());
|
---|
452 | if (traits_type::eq(peek, delim))
|
---|
453 | break;
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (gcount_ == 0)
|
---|
457 | this->setstate(ios_base::failbit);
|
---|
458 | s[n] = char_type{};
|
---|
459 | }
|
---|
460 |
|
---|
461 | return *this;
|
---|
462 | }
|
---|
463 |
|
---|
464 | basic_istream<Char, Traits>& get(char_type* s, streamsize n)
|
---|
465 | {
|
---|
466 | return get(s, n, this->widen('\n'));
|
---|
467 | }
|
---|
468 |
|
---|
469 | basic_istream<Char, Traits>& get(basic_streambuf<Char, Traits>& sb)
|
---|
470 | {
|
---|
471 | get(sb, this->widen('\n'));
|
---|
472 | }
|
---|
473 |
|
---|
474 | basic_istream<Char, Traits>& get(basic_streambuf<Char, Traits>& sb, char_type delim)
|
---|
475 | {
|
---|
476 | gcount_ = 0;
|
---|
477 | sentry sen{*this, true};
|
---|
478 |
|
---|
479 | if (sen)
|
---|
480 | {
|
---|
481 | while (true)
|
---|
482 | {
|
---|
483 | auto i = this->rdbuf()->sgetc();
|
---|
484 | if (traits_type::eq_int_type(i, traits_type::eof()))
|
---|
485 | {
|
---|
486 | this->setstate(ios_base::eofbit);
|
---|
487 | break;
|
---|
488 | }
|
---|
489 |
|
---|
490 | auto c = traits_type::to_char_type(i);
|
---|
491 | if (traits_type::eq(c, delim))
|
---|
492 | break;
|
---|
493 |
|
---|
494 | auto insert_ret = sb.sputc(c);
|
---|
495 | if (traits_type::eq_int_type(insert_ret, traits_type::eof()))
|
---|
496 | break;
|
---|
497 |
|
---|
498 | this->rdbuf()->sbumpc();
|
---|
499 | ++gcount_;
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (gcount_ == 0)
|
---|
503 | this->setstate(ios_base::failbit);
|
---|
504 | }
|
---|
505 |
|
---|
506 | return *this;
|
---|
507 | }
|
---|
508 |
|
---|
509 | basic_istream<Char, Traits>& getline(char_type* s, streamsize n)
|
---|
510 | {
|
---|
511 | return getline(s, n, this->widen('\n'));
|
---|
512 | }
|
---|
513 |
|
---|
514 | basic_istream<Char, Traits>& getline(char_type* s, streamsize n, char_type delim)
|
---|
515 | {
|
---|
516 | gcount_ = 0;
|
---|
517 | sentry sen{*this, true};
|
---|
518 |
|
---|
519 | if (sen)
|
---|
520 | {
|
---|
521 | while (true)
|
---|
522 | { // We have exactly specified order of checks, easier to do them in the body.
|
---|
523 | auto c = this->rdbuf()->sbumpc();
|
---|
524 |
|
---|
525 | if (traits_type::eq_int_type(c, traits_type::eof()))
|
---|
526 | {
|
---|
527 | this->setstate(ios_base::eofbit);
|
---|
528 | break;
|
---|
529 | }
|
---|
530 |
|
---|
531 | if (traits_type::eq_int_type(c, traits_type::to_int_type(delim)))
|
---|
532 | break;
|
---|
533 |
|
---|
534 | if (n < 1 || gcount_ >= n - 1)
|
---|
535 | {
|
---|
536 | this->setstate(ios_base::failbit);
|
---|
537 | break;
|
---|
538 | }
|
---|
539 |
|
---|
540 | s[gcount_++] = traits_type::to_char_type(c);
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (gcount_ == 0)
|
---|
544 | this->setstate(ios_base::failbit);
|
---|
545 | if (n > 0)
|
---|
546 | s[gcount_] = char_type{};
|
---|
547 | }
|
---|
548 |
|
---|
549 | return *this;
|
---|
550 | }
|
---|
551 |
|
---|
552 | basic_istream<Char, Traits>& ignore(streamsize n = 1, int_type delim = traits_type::eof())
|
---|
553 | {
|
---|
554 | sentry sen{*this, true};
|
---|
555 |
|
---|
556 | if (sen)
|
---|
557 | {
|
---|
558 | streamsize i{};
|
---|
559 | while (n == numeric_limits<streamsize>::max() || i < n)
|
---|
560 | {
|
---|
561 | auto c = this->rdbuf()->sbumpc();
|
---|
562 |
|
---|
563 | if (traits_type::eq_int_type(c, traits_type::eof()))
|
---|
564 | {
|
---|
565 | this->setstate(ios_base::eofbit);
|
---|
566 | break;
|
---|
567 | }
|
---|
568 |
|
---|
569 | if (traits_type::eq_int_type(c, delim))
|
---|
570 | break;
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | return *this;
|
---|
575 | }
|
---|
576 |
|
---|
577 | int_type peek()
|
---|
578 | {
|
---|
579 | sentry sen{*this, true};
|
---|
580 |
|
---|
581 | if (!this->good())
|
---|
582 | return traits_type::eof();
|
---|
583 | else
|
---|
584 | return this->rdbuf()->sgetc();
|
---|
585 | }
|
---|
586 |
|
---|
587 | basic_istream<Char, Traits>& read(char_type* s, streamsize n)
|
---|
588 | {
|
---|
589 | gcount_ = 0;
|
---|
590 | sentry sen{*this, true};
|
---|
591 |
|
---|
592 | if (!this->good())
|
---|
593 | {
|
---|
594 | this->setstate(ios_base::failbit);
|
---|
595 | return *this;
|
---|
596 | }
|
---|
597 |
|
---|
598 | while (gcount_ < n)
|
---|
599 | {
|
---|
600 | auto c = this->rdbuf()->sbumpc();
|
---|
601 | if (traits_type::eq_int_type(c, traits_type::eof()))
|
---|
602 | {
|
---|
603 | this->setstate(ios_base::failbit | ios_base::eofbit);
|
---|
604 | break;
|
---|
605 | }
|
---|
606 |
|
---|
607 | s[gcount_++] = traits_type::to_char_type(c);
|
---|
608 | }
|
---|
609 |
|
---|
610 | return *this;
|
---|
611 | }
|
---|
612 |
|
---|
613 | streamsize readsome(char_type* s, streamsize n)
|
---|
614 | {
|
---|
615 | gcount_ = 0;
|
---|
616 | sentry sen{*this, true};
|
---|
617 |
|
---|
618 | if (!this->good())
|
---|
619 | {
|
---|
620 | this->setstate(ios_base::failbit);
|
---|
621 | return streamsize{};
|
---|
622 | }
|
---|
623 |
|
---|
624 | auto avail = this->rdbuf()->in_avail();
|
---|
625 | if (avail == -1)
|
---|
626 | {
|
---|
627 | this->setstate(ios_base::eofbit);
|
---|
628 | return streamsize{};
|
---|
629 | } else if (avail > 0)
|
---|
630 | {
|
---|
631 | auto count = (avail < n ? avail : n);
|
---|
632 | while (gcount_ < count)
|
---|
633 | s[gcount_++] = traits_type::to_char_type(this->rdbuf()->sbumpc());
|
---|
634 | }
|
---|
635 |
|
---|
636 | return gcount_;
|
---|
637 | }
|
---|
638 |
|
---|
639 | basic_istream<Char, Traits>& putback(char_type c)
|
---|
640 | {
|
---|
641 | this->clear(this->rdstate() & (~ios_base::eofbit));
|
---|
642 |
|
---|
643 | gcount_ = 0;
|
---|
644 | sentry sen{*this, true};
|
---|
645 |
|
---|
646 | if (!this->good())
|
---|
647 | {
|
---|
648 | this->setstate(ios_base::failbit);
|
---|
649 | return *this;
|
---|
650 | }
|
---|
651 |
|
---|
652 | if (this->rdbuf())
|
---|
653 | {
|
---|
654 | auto ret = this->rdbuf()->sputbackc(c);
|
---|
655 | if (traits_type::eq_int_type(ret, traits_type::eof()))
|
---|
656 | this->setstate(ios_base::badbit);
|
---|
657 | }
|
---|
658 | else
|
---|
659 | this->setstate(ios_base::badbit);
|
---|
660 |
|
---|
661 | return *this;
|
---|
662 | }
|
---|
663 |
|
---|
664 | basic_istream<Char, Traits>& unget()
|
---|
665 | {
|
---|
666 | this->clear(this->rdstate() & (~ios_base::eofbit));
|
---|
667 |
|
---|
668 | gcount_ = 0;
|
---|
669 | sentry sen{*this, true};
|
---|
670 |
|
---|
671 | if (!this->good())
|
---|
672 | {
|
---|
673 | this->setstate(ios_base::failbit);
|
---|
674 | return *this;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (this->rdbuf())
|
---|
678 | {
|
---|
679 | auto ret = this->rdbuf()->sungetc();
|
---|
680 | if (traits_type::eq_int_type(ret, traits_type::eof()))
|
---|
681 | this->setstate(ios_base::badbit);
|
---|
682 | }
|
---|
683 | else
|
---|
684 | this->setstate(ios_base::badbit);
|
---|
685 |
|
---|
686 | return *this;
|
---|
687 | }
|
---|
688 |
|
---|
689 | int sync()
|
---|
690 | {
|
---|
691 | sentry s{*this, true};
|
---|
692 |
|
---|
693 | if (this->rdbuf())
|
---|
694 | {
|
---|
695 | auto ret = this->rdbuf()->pubsync();
|
---|
696 | if (ret == -1)
|
---|
697 | {
|
---|
698 | this->setstate(ios_base::badbit);
|
---|
699 | return -1;
|
---|
700 | }
|
---|
701 | else
|
---|
702 | return 0;
|
---|
703 | }
|
---|
704 | else
|
---|
705 | return -1;
|
---|
706 | }
|
---|
707 |
|
---|
708 | pos_type tellg()
|
---|
709 | {
|
---|
710 | sentry s{*this, true};
|
---|
711 |
|
---|
712 | if (this->fail())
|
---|
713 | return pos_type(-1);
|
---|
714 | else
|
---|
715 | return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
|
---|
716 | }
|
---|
717 |
|
---|
718 | basic_istream<Char, Traits>& seekg(pos_type pos)
|
---|
719 | {
|
---|
720 | this->clear(this->rdstate() & (~ios_base::eofbit));
|
---|
721 |
|
---|
722 | sentry sen{*this, true};
|
---|
723 |
|
---|
724 | if (!this->fail())
|
---|
725 | this->rdbuf()->pubseekoff(pos, ios_base::in);
|
---|
726 | else
|
---|
727 | this->setstate(ios_base::failbit);
|
---|
728 |
|
---|
729 | return *this;
|
---|
730 | }
|
---|
731 |
|
---|
732 | basic_istream<Char, Traits>& seekg(off_type off, ios_base::seekdir dir)
|
---|
733 | {
|
---|
734 | sentry sen{*this, true};
|
---|
735 |
|
---|
736 | if (!this->fail())
|
---|
737 | this->rdbuf()->pubseekoff(off, dir, ios_base::in);
|
---|
738 | else
|
---|
739 | this->setstate(ios_base::failbit);
|
---|
740 |
|
---|
741 | return *this;
|
---|
742 | }
|
---|
743 |
|
---|
744 | protected:
|
---|
745 | streamsize gcount_;
|
---|
746 |
|
---|
747 | basic_istream(const basic_istream&) = delete;
|
---|
748 |
|
---|
749 | basic_istream(basic_istream&& rhs)
|
---|
750 | {
|
---|
751 | gcount_ = rhs.gcout_;
|
---|
752 |
|
---|
753 | basic_ios<Char, Traits>::move(rhs);
|
---|
754 |
|
---|
755 | rhs.gcount_ = 0;
|
---|
756 | }
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * 27.7.2.1.2, assign/swap:
|
---|
760 | */
|
---|
761 |
|
---|
762 | basic_istream& operator=(const basic_istream& rhs) = delete;
|
---|
763 |
|
---|
764 | basic_istream& operator=(basic_istream&& rhs)
|
---|
765 | {
|
---|
766 | swap(rhs);
|
---|
767 |
|
---|
768 | return *this;
|
---|
769 | }
|
---|
770 |
|
---|
771 | void swap(basic_istream& rhs)
|
---|
772 | {
|
---|
773 | basic_ios<Char, Traits>::swap(rhs);
|
---|
774 | swap(gcount_, rhs.gcount_);
|
---|
775 | }
|
---|
776 | };
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * 27.7.2.2.3, character extraction templates:
|
---|
780 | */
|
---|
781 |
|
---|
782 | template<class Char, class Traits>
|
---|
783 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
784 | Char& c)
|
---|
785 | {
|
---|
786 | typename basic_istream<Char, Traits>::sentry sen{is, false};
|
---|
787 |
|
---|
788 | if (sen)
|
---|
789 | {
|
---|
790 | auto ic = is.rdbuf()->sgetc();
|
---|
791 | if (Traits::eq_int_type(ic, Traits::eof()))
|
---|
792 | {
|
---|
793 | is.setstate(ios_base::failbit | ios_base::eofbit);
|
---|
794 | return is;
|
---|
795 | }
|
---|
796 |
|
---|
797 | c = Traits::to_char_type(is.rdbuf()->sbumpc());
|
---|
798 | }
|
---|
799 |
|
---|
800 | return is;
|
---|
801 | }
|
---|
802 |
|
---|
803 | template<class Char, class Traits>
|
---|
804 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
805 | unsigned char& c)
|
---|
806 | {
|
---|
807 | return is >> static_cast<char&>(c);
|
---|
808 | }
|
---|
809 |
|
---|
810 | template<class Char, class Traits>
|
---|
811 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
812 | signed char& c)
|
---|
813 | {
|
---|
814 | return is >> static_cast<char&>(c);
|
---|
815 | }
|
---|
816 |
|
---|
817 | template<class Char, class Traits>
|
---|
818 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
819 | Char* str)
|
---|
820 | {
|
---|
821 | typename basic_istream<Char, Traits>::sentry sen{is, false};
|
---|
822 |
|
---|
823 | if (sen)
|
---|
824 | {
|
---|
825 | const auto& ct = use_facet<ctype<Char>>(is.getloc());
|
---|
826 |
|
---|
827 | size_t n{};
|
---|
828 | if (is.width() > 0)
|
---|
829 | n = static_cast<size_t>(is.width());
|
---|
830 | else
|
---|
831 | n = (numeric_limits<size_t>::max() / sizeof(Char)) - sizeof(Char);
|
---|
832 |
|
---|
833 | size_t i{};
|
---|
834 | for (; i < n - 1; ++i)
|
---|
835 | {
|
---|
836 | auto ic = is.rdbuf()->sgetc();
|
---|
837 | if (Traits::eq_int_type(ic, Traits::eof()))
|
---|
838 | break;
|
---|
839 |
|
---|
840 | auto c = Traits::to_char_type(ic);
|
---|
841 | if (ct.is(ctype_base::space, c))
|
---|
842 | break;
|
---|
843 |
|
---|
844 | str[i] = c;
|
---|
845 | is.rdbuf()->sbumpc();
|
---|
846 | }
|
---|
847 |
|
---|
848 | str[i] = Char{};
|
---|
849 | if (i == 0)
|
---|
850 | is.setstate(ios_base::failbit);
|
---|
851 | }
|
---|
852 |
|
---|
853 | return is;
|
---|
854 | }
|
---|
855 |
|
---|
856 | template<class Char, class Traits>
|
---|
857 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
858 | unsigned char* str)
|
---|
859 | {
|
---|
860 | return is >> static_cast<char*>(str);
|
---|
861 | }
|
---|
862 |
|
---|
863 | template<class Char, class Traits>
|
---|
864 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is,
|
---|
865 | signed char* str)
|
---|
866 | {
|
---|
867 | return is >> static_cast<char*>(str);
|
---|
868 | }
|
---|
869 |
|
---|
870 | /**
|
---|
871 | * 27.7.2.4, standard basic_istream manipulators:
|
---|
872 | */
|
---|
873 |
|
---|
874 | template<class Char, class Traits = char_traits<Char>>
|
---|
875 | basic_istream<Char, Traits>& ws(basic_istream<Char, Traits>& is)
|
---|
876 | {
|
---|
877 | using sentry = typename basic_istream<Char, Traits>::sentry;
|
---|
878 | sentry sen{is, true};
|
---|
879 |
|
---|
880 | if (sen)
|
---|
881 | {
|
---|
882 | const auto& ct = use_facet<ctype<Char>>(is.getloc());
|
---|
883 | while (true)
|
---|
884 | {
|
---|
885 | auto i = is.rdbuf()->sgetc();
|
---|
886 | if (Traits::eq_int_type(i, Traits::eof()))
|
---|
887 | {
|
---|
888 | is.setstate(ios_base::eofbit);
|
---|
889 | break;
|
---|
890 | }
|
---|
891 |
|
---|
892 | auto c = Traits::to_char_type(i);
|
---|
893 | if (!ct.is(c, ct.space))
|
---|
894 | break;
|
---|
895 | else
|
---|
896 | is.rdbuf()->sbumpc();
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | return is;
|
---|
901 | }
|
---|
902 |
|
---|
903 | using istream = basic_istream<char>;
|
---|
904 | using wistream = basic_istream<wchar_t>;
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * 27.7.2.5, class template basic_iostream:
|
---|
908 | */
|
---|
909 |
|
---|
910 | template<class Char, class Traits>
|
---|
911 | class basic_iostream
|
---|
912 | : public basic_istream<Char, Traits>,
|
---|
913 | public basic_ostream<Char, Traits>
|
---|
914 | {
|
---|
915 | public:
|
---|
916 | using char_type = Char;
|
---|
917 | using traits_type = Traits;
|
---|
918 | using int_type = typename traits_type::int_type;
|
---|
919 | using pos_type = typename traits_type::pos_type;
|
---|
920 | using off_type = typename traits_type::off_type;
|
---|
921 |
|
---|
922 | explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb)
|
---|
923 | : basic_istream<char_type, traits_type>(sb),
|
---|
924 | basic_ostream<char_type, traits_type>(sb)
|
---|
925 | { /* DUMMY BODY */ }
|
---|
926 |
|
---|
927 | virtual ~basic_iostream()
|
---|
928 | { /* DUMMY BODY */ }
|
---|
929 |
|
---|
930 | protected:
|
---|
931 | basic_iostream(const basic_iostream&) = delete;
|
---|
932 | basic_iostream& operator=(const basic_iostream&) = delete;
|
---|
933 |
|
---|
934 | basic_iostream(basic_iostream&& other)
|
---|
935 | : basic_istream<char_type, traits_type>(move(other))
|
---|
936 | { /* DUMMY BODY */ }
|
---|
937 |
|
---|
938 | basic_iostream& operator=(basic_iostream&& other)
|
---|
939 | {
|
---|
940 | swap(other);
|
---|
941 | }
|
---|
942 |
|
---|
943 | void swap(basic_iostream& other)
|
---|
944 | {
|
---|
945 | basic_istream<char_type, traits_type>::swap(other);
|
---|
946 | }
|
---|
947 | };
|
---|
948 |
|
---|
949 | using iostream = basic_iostream<char>;
|
---|
950 | using wiostream = basic_iostream<wchar_t>;
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * 27.7.2.6, rvalue stream extraction:
|
---|
954 | */
|
---|
955 |
|
---|
956 | template<class Char, class Traits, class T>
|
---|
957 | basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>&& is, T& x)
|
---|
958 | {
|
---|
959 | is >> x;
|
---|
960 |
|
---|
961 | return is;
|
---|
962 | }
|
---|
963 | }
|
---|
964 |
|
---|
965 | #endif
|
---|