1
/* Copyright (C) 2004 Garrett A. Kajmowicz
3
This file is part of the uClibc C++ Library. This library is free
4
software; you can redistribute it and/or modify it under the
5
terms of the GNU General Public License as published by the
6
Free Software Foundation; either version 2, or (at your option)
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with this library; see the file COPYING. If not, write to the Free
16
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23
#include <istream_helpers>
26
#ifndef __STD_HEADER_ISTREAM
27
#define __STD_HEADER_ISTREAM 1
29
#pragma GCC visibility push(default)
33
typedef basic_istream<char> istream;
35
#ifdef __UCLIBCXX_HAS_WCHAR__
36
typedef basic_istream<wchar_t> wistream;
39
template <class charT, class traits> basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
41
template <class charT, class traits> class _UCXXEXPORT basic_istream :
42
virtual public basic_ios<charT,traits>
46
typedef charT char_type;
47
typedef typename traits::int_type int_type;
48
typedef typename traits::pos_type pos_type;
49
typedef typename traits::off_type off_type;
50
typedef basic_streambuf<charT,traits> streambuf_type;
51
typedef traits traits_type;
53
explicit basic_istream(basic_streambuf<charT,traits>* sb)
54
: basic_ios<charT, traits>(sb), count_last_ufmt_input(0)
56
basic_ios<charT, traits>::init(sb);
58
virtual ~basic_istream() { }
62
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&));
63
basic_istream<charT,traits>& operator>>(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
64
basic_istream<charT,traits>& operator>>(ios_base& (*pf)(ios_base&));
65
basic_istream<charT,traits>& operator>>(bool& n);
66
basic_istream<charT,traits>& operator>>(short& n);
67
basic_istream<charT,traits>& operator>>(unsigned short& n);
68
basic_istream<charT,traits>& operator>>(int& n);
69
basic_istream<charT,traits>& operator>>(unsigned int& n);
70
basic_istream<charT,traits>& operator>>(long& n);
71
basic_istream<charT,traits>& operator>>(unsigned long& n);
72
basic_istream<charT,traits>& operator>>(void*& p);
73
basic_istream<charT,traits>& operator>>(basic_streambuf<char_type,traits>* sb);
75
#ifdef __UCLIBCXX_HAS_FLOATS__
76
basic_istream<charT,traits>& operator>>(float& f);
77
basic_istream<charT,traits>& operator>>(double& f);
78
basic_istream<charT,traits>& operator>>(long double& f);
81
_UCXXEXPORT streamsize gcount() const{
82
return count_last_ufmt_input;
85
_UCXXEXPORT int_type get(); //below
86
_UCXXEXPORT basic_istream<charT,traits>& get(char_type& c); //Below
88
_UCXXEXPORT basic_istream<charT,traits>& get(char_type* s, streamsize n){
89
return get(s, n, basic_ios<charT,traits>::widen('\n'));
92
_UCXXEXPORT basic_istream<charT,traits>& get(char_type* s, streamsize n, char_type delim){
97
c = basic_ios<charT, traits>::mstreambuf->sgetc();
98
basic_ios<charT, traits>::mstreambuf->sbumpc();
99
if(c == traits::eof() ){
101
basic_ios<charT,traits>::setstate(ios_base::failbit);
103
basic_ios<charT,traits>::setstate(ios_base::eofbit);
109
basic_ios<charT,traits>::setstate(ios_base::failbit);
111
basic_ios<charT, traits>::mstreambuf->sputbackc(c);
116
s[i] = traits::eos();
117
count_last_ufmt_input = i;
121
_UCXXEXPORT basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb){
122
return get(sb, basic_ios<charT,traits>::widen('\n'));
125
_UCXXEXPORT basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb, char_type delim){
129
while(1){ //We will exit internally based upon error conditions
130
c = basic_ios<charT, traits>::mstreambuf->sgetc();
131
if(c == traits::eof()){
133
basic_ios<charT,traits>::setstate(ios_base::failbit);
135
basic_ios<charT,traits>::setstate(ios_base::eofbit);
137
count_last_ufmt_input = i;
142
basic_ios<charT,traits>::setstate(ios_base::failbit);
144
count_last_ufmt_input = i;
147
if(sb.sputc(c) != c){ //Error doing output
148
count_last_ufmt_input = i;
152
basic_ios<charT, traits>::mstreambuf->sbumpc();
156
_UCXXEXPORT basic_istream<charT,traits>& getline(char_type* s, streamsize n){
157
return getline(s, n, basic_ios<charT,traits>::widen('\n'));
160
_UCXXEXPORT basic_istream<charT,traits>& getline(char_type* s, streamsize n, char_type delim){
165
c = basic_ios<charT, traits>::mstreambuf->sgetc();
166
if(c == traits::eof() ){
167
if( basic_ios<charT,traits>::eof() ){
168
basic_ios<charT,traits>::setstate(ios_base::failbit);
170
basic_ios<charT,traits>::setstate(ios_base::eofbit);
172
count_last_ufmt_input = i;
173
s[i] = traits::eos();
176
if(basic_ios<charT, traits>::mstreambuf->sbumpc()==traits::eof() ){
177
basic_ios<charT,traits>::setstate(ios_base::eofbit);
180
count_last_ufmt_input = i+1;
181
s[i] = traits::eos();
186
s[n-1] = traits::eos();
190
_UCXXEXPORT basic_istream<charT,traits>& ignore (streamsize n = 1, int_type delim = traits::eof()){
195
c = basic_ios<charT, traits>::mstreambuf->sgetc();
196
if(c == traits::eof()){
197
basic_ios<charT,traits>::setstate(ios_base::eofbit);
200
basic_ios<charT, traits>::mstreambuf->sbumpc();
208
_UCXXEXPORT int_type peek(){
209
if(basic_ios<charT,traits>::good() == false){
210
return traits::eof();
212
int_type c = basic_ios<charT, traits>::mstreambuf->sgetc();
213
if(c == traits::eof()){
214
basic_ios<charT,traits>::setstate(ios_base::eofbit);
216
return basic_ios<charT, traits>::mstreambuf->sgetc();
220
_UCXXEXPORT basic_istream<charT,traits>& read (char_type* s, streamsize n){
225
c = basic_ios<charT, traits>::mstreambuf->sgetc();
227
if(c == traits::eof()){
228
basic_ios<charT,traits>::setstate(ios_base::failbit);
229
basic_ios<charT,traits>::setstate(ios_base::eofbit);
230
count_last_ufmt_input = i;
233
basic_ios<charT, traits>::mstreambuf->sbumpc();
236
count_last_ufmt_input = n;
240
_UCXXEXPORT streamsize readsome(char_type* s, streamsize n){
242
if(!basic_ios<charT,traits>::good()){
243
count_last_ufmt_input = 0;
244
basic_ios<charT,traits>::setstate(ios_base::failbit);
248
if( basic_ios<charT, traits>::mstreambuf->in_avail() == -1){
249
count_last_ufmt_input=0;
250
basic_ios<charT,traits>::setstate(ios_base::eofbit);
254
if(n > basic_ios<charT, traits>::mstreambuf->in_avail() ){
255
n = basic_ios<charT, traits>::mstreambuf->in_avail();
262
c = basic_ios<charT, traits>::mstreambuf->sgetc();
263
basic_ios<charT, traits>::mstreambuf->sbumpc();
266
count_last_ufmt_input = n;
270
_UCXXEXPORT basic_istream<charT,traits>& putback(char_type c){
272
if(!basic_ios<charT,traits>::good()){
273
basic_ios<charT,traits>::setstate(ios_base::failbit);
276
if(basic_ios<charT, traits>::mstreambuf == 0){
277
basic_ios<charT,traits>::setstate(ios_base::badbit);
280
if(basic_ios<charT, traits>::mstreambuf->sputbackc(c) == traits::eof()){
281
basic_ios<charT,traits>::setstate(ios_base::badbit);
287
_UCXXEXPORT basic_istream<charT,traits>& unget(){
289
if(!basic_ios<charT,traits>::good()){
290
basic_ios<charT,traits>::setstate(ios_base::failbit);
293
if(basic_ios<charT, traits>::mstreambuf == 0){
294
basic_ios<charT,traits>::setstate(ios_base::failbit);
297
if(basic_ios<charT, traits>::mstreambuf->sungetc() == traits::eof()){
298
basic_ios<charT,traits>::setstate(ios_base::failbit);
303
_UCXXEXPORT int sync(){
305
if(basic_ios<charT, traits>::mstreambuf == 0){
308
if(basic_ios<charT, traits>::mstreambuf->pubsync() == -1){
309
basic_ios<charT,traits>::setstate(ios_base::badbit);
310
return traits::eof();
315
_UCXXEXPORT pos_type tellg(){
316
if(basic_ios<charT,traits>::fail() !=false){
319
return basic_ios<charT, traits>::mstreambuf->pubseekoff(0, ios_base::cur, ios_base::in);
322
_UCXXEXPORT basic_istream<charT,traits>& seekg(pos_type pos){
323
if(basic_ios<charT,traits>::fail() !=true){
324
basic_ios<charT, traits>::mstreambuf->pubseekpos(pos);
329
_UCXXEXPORT basic_istream<charT,traits>& seekg(off_type off, ios_base::seekdir dir){
330
if(basic_ios<charT,traits>::fail() !=true){
331
basic_ios<charT, traits>::mstreambuf->pubseekoff(off, dir);
337
_UCXXEXPORT basic_istream(const basic_istream<charT,traits> &): basic_ios<charT, traits>() { }
338
_UCXXEXPORT basic_istream<charT,traits> & operator=(const basic_istream<charT,traits> &){ return *this; }
339
streamsize count_last_ufmt_input;
343
template <class charT,class traits = char_traits<charT> > class _UCXXEXPORT basic_istream<charT,traits>::sentry {
346
explicit _UCXXEXPORT sentry(basic_istream<charT,traits>& os, bool noskipws = false){
347
if(os.good() !=0){ //Prepare for output
350
//Flush any tied buffer
360
_UCXXEXPORT ~sentry() { }
361
_UCXXEXPORT operator bool() {
366
//Template implementations of basic_istream functions which may be partially specialized
369
template <class charT, class traits>
370
_UCXXEXPORT typename basic_istream<charT,traits>::int_type basic_istream<charT,traits>::get(){
372
int_type retval = basic_ios<charT, traits>::mstreambuf->sgetc();
373
if(retval == traits::eof()){
374
count_last_ufmt_input = 0;
375
basic_ios<charT,traits>::setstate(ios_base::eofbit);
377
count_last_ufmt_input = 1;
378
basic_ios<charT, traits>::mstreambuf->sbumpc();
383
template <class charT, class traits>
384
_UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::get(char_type& c){
386
int_type retval = basic_ios<charT, traits>::mstreambuf->sgetc();
387
if(retval == traits::eof()){
388
count_last_ufmt_input = 0;
389
basic_ios<charT,traits>::setstate(ios_base::eofbit);
390
basic_ios<charT,traits>::setstate(ios_base::failbit);
392
count_last_ufmt_input = 1;
393
c = traits::to_char_type(retval);
394
basic_ios<charT, traits>::mstreambuf->sbumpc();
400
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
401
basic_istream<charT,traits>::operator>>(bool& n)
404
__istream_readin<traits, charT, bool>::readin(*this, n);
408
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
409
basic_istream<charT,traits>::operator>>(short& n)
412
__istream_readin<traits, charT, short>::readin(*this, n);
416
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
417
basic_istream<charT,traits>::operator>>(unsigned short& n)
420
__istream_readin<traits, charT, unsigned short>::readin(*this, n);
424
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(int& n){
426
__istream_readin<traits, charT, int>::readin(*this, n);
430
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(unsigned int& n){
432
__istream_readin<traits, charT, unsigned int>::readin(*this, n);
436
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(long int& n){
438
__istream_readin<traits, charT, long int>::readin(*this, n);
442
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
443
basic_istream<charT,traits>::operator>>(unsigned long int& n)
446
__istream_readin<traits, charT, unsigned long int>::readin(*this, n);
450
#ifdef __UCLIBCXX_HAS_FLOATS__
451
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
452
basic_istream<charT,traits>::operator>>(float& n)
455
__istream_readin<traits, charT, float>::readin(*this, n);
458
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
459
basic_istream<charT,traits>::operator>>(double& n)
462
__istream_readin<traits, charT, double>::readin(*this, n);
465
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
466
basic_istream<charT,traits>::operator>>(long double& n)
469
__istream_readin<traits, charT, long double>::readin(*this, n);
473
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
474
basic_istream<charT,traits>::operator>>(void *& n)
477
__istream_readin<traits, charT, void*>::readin(*this, n);
481
template<class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
482
operator>>(basic_istream<charT,traits>& is, charT& c)
484
typename basic_istream<charT,traits>::sentry s(is);
489
template<class traits> _UCXXEXPORT basic_istream<char,traits>&
490
operator>>(basic_istream<char,traits>& is, unsigned char& c)
492
typename basic_istream<char,traits>::sentry s(is);
498
template<class traits> _UCXXEXPORT basic_istream<char,traits>&
499
operator>>(basic_istream<char,traits>& is, signed char& c)
501
typename basic_istream<char,traits>::sentry s(is);
506
template<class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
507
operator>>(basic_istream<charT,traits>& is, charT* c)
509
typename basic_istream<charT,traits>::sentry s(is);
512
n = __STRING_MAX_UNITS;
518
template<class traits> _UCXXEXPORT basic_istream<char,traits>&
519
operator>>(basic_istream<char,traits>& is, unsigned char* c)
521
typename basic_istream<char,traits>::sentry s(is);
524
n = __STRING_MAX_UNITS;
529
template<class traits> _UCXXEXPORT basic_istream<char,traits>&
530
operator>>(basic_istream<char,traits>& is, signed char* c)
532
typename basic_istream<char,traits>::sentry s(is);
535
n = __STRING_MAX_UNITS;
541
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
542
basic_istream<charT,traits>::operator>>(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
549
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
550
basic_istream<charT,traits>::operator>>(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
557
template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
558
ws(basic_istream<charT,traits>& is)
565
#ifdef __UCLIBCXX_EXPAND_ISTREAM_CHAR__
566
#ifndef __UCLIBCXX_COMPILE_ISTREAM__
569
template <> _UCXXEXPORT istream & basic_istream<char, char_traits<char> >::get(char & c);
570
template <> _UCXXEXPORT istream::int_type basic_istream<char, char_traits<char> >::get();
572
template <> _UCXXEXPORT istream & istream::operator>>(bool &n);
573
template <> _UCXXEXPORT istream & istream::operator>>(short &n);
574
template <> _UCXXEXPORT istream & istream::operator>>(unsigned short &n);
575
template <> _UCXXEXPORT istream & istream::operator>>(int &n);
576
template <> _UCXXEXPORT istream & istream::operator>>(unsigned int &n);
577
template <> _UCXXEXPORT istream & istream::operator>>(long unsigned &n);
578
template <> _UCXXEXPORT istream & istream::operator>>(long int &n);
579
template <> _UCXXEXPORT istream & istream::operator>>(void *& p);
581
#ifdef __UCLIBCXX_HAS_FLOATS__
582
template <> _UCXXEXPORT istream & istream::operator>>(float &f);
583
template <> _UCXXEXPORT istream & istream::operator>>(double &f);
584
template <> _UCXXEXPORT istream & istream::operator>>(long double &f);
587
template <> _UCXXEXPORT istream & operator>>(istream & is, char & c);
589
template <> _UCXXEXPORT void __skipws(basic_istream<char,char_traits<char> >& is);
598
#pragma GCC visibility pop