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 14 #include <istream> 15 #include <cassert> 16 17 template <class CharT> 18 class testbuf 19 : public std::basic_streambuf<CharT> 20 { 21 typedef std::basic_streambuf<CharT> base; 22 std::basic_string<CharT> str_; 23 public: 24 testbuf() 25 { 26 } 27 testbuf(const std::basic_string<CharT>& str) 28 : str_(str) 29 { 30 base::setg(const_cast<CharT*>(str_.data()), 31 const_cast<CharT*>(str_.data()), 32 const_cast<CharT*>(str_.data() + str_.size())); 33 } 34 35 std::basic_string<CharT> str() const 36 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 37 38 protected: 39 40 virtual typename base::int_type 41 overflow(typename base::int_type __c = base::traits_type::eof()) 42 { 43 if (__c != base::traits_type::eof()) 44 { 45 int n = str_.size(); 46 str_.push_back(__c); 47 str_.resize(str_.capacity()); 48 base::setp(const_cast<CharT*>(str_.data()), 49 const_cast<CharT*>(str_.data() + str_.size())); 50 base::pbump(n+1); 51 } 52 return __c; 53 } 54 }; 55 56 int main() 57 { 58 { 59 testbuf<char> sb("testing\n..."); 60 std::istream is(&sb); 61 testbuf<char> sb2; 62 is.get(sb2); 63 assert(sb2.str() == "testing"); 64 assert(is.good()); 65 assert(is.gcount() == 7); 66 assert(is.get() == '\n'); 67 is.get(sb2); 68 assert(sb2.str() == "testing..."); 69 assert(is.eof()); 70 assert(!is.fail()); 71 assert(is.gcount() == 3); 72 } 73 { 74 testbuf<wchar_t> sb(L"testing\n..."); 75 std::wistream is(&sb); 76 testbuf<wchar_t> sb2; 77 is.get(sb2); 78 assert(sb2.str() == L"testing"); 79 assert(is.good()); 80 assert(is.gcount() == 7); 81 assert(is.get() == L'\n'); 82 is.get(sb2); 83 assert(sb2.str() == L"testing..."); 84 assert(is.eof()); 85 assert(!is.fail()); 86 assert(is.gcount() == 3); 87 } 88 } 89