1// (C) Copyright John Maddock 2001. 2// Use, modification and distribution are subject to the 3// Boost Software License, Version 1.0. (See accompanying file 4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6// See http://www.boost.org/libs/config for most recent version. 7 8// MACRO: BOOST_NO_STD_WSTREAMBUF 9// TITLE: std::basic_streambuf<wchar_t> 10// DESCRIPTION: The standard library lacks std::basic_streambuf<wchar_t>. 11 12#include <iostream> 13#include <streambuf> 14#include <string> 15 16namespace boost_no_std_wstreambuf{ 17 18template <class charT, 19 class traits = ::std::char_traits<charT> > 20class parser_buf : public ::std::basic_streambuf<charT, traits> 21{ 22 typedef ::std::basic_streambuf<charT, traits> base_type; 23 typedef typename base_type::int_type int_type; 24 typedef typename base_type::char_type char_type; 25 typedef typename base_type::pos_type pos_type; 26 typedef ::std::streamsize streamsize; 27 typedef typename base_type::off_type off_type; 28public: 29 parser_buf() : base_type() { setbuf(0, 0); } 30 const charT* getnext() { return this->gptr(); } 31protected: 32 std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n); 33 typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which); 34 typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which); 35private: 36 parser_buf& operator=(const parser_buf&) 37 { return *this; }; 38 parser_buf(const parser_buf&); 39}; 40 41template<class charT, class traits> 42std::basic_streambuf<charT, traits>* 43parser_buf<charT, traits>::setbuf(char_type* s, streamsize n) 44{ 45 this->setg(s, s, s + n); 46 return this; 47} 48 49template<class charT, class traits> 50typename parser_buf<charT, traits>::pos_type 51parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) 52{ 53 typedef typename parser_buf<charT, traits>::pos_type pos_type; 54 if(which & ::std::ios_base::out) 55 return pos_type(off_type(-1)); 56 int size = this->egptr() - this->eback(); 57 int pos = this->gptr() - this->eback(); 58 charT* g = this->eback(); 59 switch((int)way) 60 { 61 case ::std::ios_base::beg: 62 if((off < 0) || (off > size)) 63 return pos_type(off_type(-1)); 64 else 65 this->setg(g, g + off, g + size); 66 case ::std::ios_base::end: 67 if((off < 0) || (off > size)) 68 return pos_type(off_type(-1)); 69 else 70 this->setg(g, g + size - off, g + size); 71 case ::std::ios_base::cur: 72 { 73 int newpos = pos + off; 74 if((newpos < 0) || (newpos > size)) 75 return pos_type(off_type(-1)); 76 else 77 this->setg(g, g + newpos, g + size); 78 } 79 } 80 return static_cast<pos_type>(this->gptr() - this->eback()); 81} 82 83template<class charT, class traits> 84typename parser_buf<charT, traits>::pos_type 85parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which) 86{ 87 if(which & ::std::ios_base::out) 88 return pos_type(off_type(-1)); 89 int size = this->egptr() - this->eback(); 90 charT* g = this->eback(); 91 if(off_type(sp) <= size) 92 { 93 this->setg(g, g + off_type(sp), g + size); 94 } 95 return pos_type(off_type(-1)); 96} 97 98 99int test() 100{ 101 return 0; 102} 103 104template class parser_buf<char>; 105template class parser_buf<wchar_t>; 106 107} 108 109