Home | History | Annotate | Download | only in istream.unformatted
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <istream>
     11 
     12 // basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
     13 //                                  char_type delim);
     14 
     15 #include <istream>
     16 #include <cassert>
     17 
     18 template <class CharT>
     19 class testbuf
     20     : public std::basic_streambuf<CharT>
     21 {
     22     typedef std::basic_streambuf<CharT> base;
     23     std::basic_string<CharT> str_;
     24 public:
     25     testbuf()
     26     {
     27     }
     28     testbuf(const std::basic_string<CharT>& str)
     29         : str_(str)
     30     {
     31         base::setg(const_cast<CharT*>(str_.data()),
     32                    const_cast<CharT*>(str_.data()),
     33                    const_cast<CharT*>(str_.data() + str_.size()));
     34     }
     35 
     36     std::basic_string<CharT> str() const
     37         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
     38 
     39 protected:
     40 
     41     virtual typename base::int_type
     42         overflow(typename base::int_type __c = base::traits_type::eof())
     43         {
     44             if (__c != base::traits_type::eof())
     45             {
     46                 int n = str_.size();
     47                 str_.push_back(__c);
     48                 str_.resize(str_.capacity());
     49                 base::setp(const_cast<CharT*>(str_.data()),
     50                            const_cast<CharT*>(str_.data() + str_.size()));
     51                 base::pbump(n+1);
     52             }
     53             return __c;
     54         }
     55 };
     56 
     57 int main()
     58 {
     59     {
     60         testbuf<char> sb("testing*...");
     61         std::istream is(&sb);
     62         testbuf<char> sb2;
     63         is.get(sb2, '*');
     64         assert(sb2.str() == "testing");
     65         assert(is.good());
     66         assert(is.gcount() == 7);
     67         assert(is.get() == '*');
     68         is.get(sb2, '*');
     69         assert(sb2.str() == "testing...");
     70         assert(is.eof());
     71         assert(!is.fail());
     72         assert(is.gcount() == 3);
     73     }
     74     {
     75         testbuf<wchar_t> sb(L"testing*...");
     76         std::wistream is(&sb);
     77         testbuf<wchar_t> sb2;
     78         is.get(sb2, L'*');
     79         assert(sb2.str() == L"testing");
     80         assert(is.good());
     81         assert(is.gcount() == 7);
     82         assert(is.get() == L'*');
     83         is.get(sb2, L'*');
     84         assert(sb2.str() == L"testing...");
     85         assert(is.eof());
     86         assert(!is.fail());
     87         assert(is.gcount() == 3);
     88     }
     89 }
     90