📄 alt_sstream_impl.hpp
字号:
// ----------------------------------------------------------------------------// alt_sstream_impl.hpp : alternative stringstream, templates implementation // ----------------------------------------------------------------------------// Copyright Samuel Krempp 2003. Use, modification, and distribution are// subject to the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)// See http://www.boost.org/libs/format for library home page// ----------------------------------------------------------------------------#ifndef BOOST_SK_ALT_SSTREAM_IMPL_HPP#define BOOST_SK_ALT_SSTREAM_IMPL_HPPnamespace boost { namespace io {// --- Implementation ------------------------------------------------------// template<class Ch, class Tr, class Alloc> void basic_altstringbuf<Ch, Tr, Alloc>:: clear_buffer () { const Ch * p = pptr(); const Ch * b = pbase(); if(p != NULL && p != b) { seekpos(0, ::std::ios_base::out); } p = gptr(); b = eback(); if(p != NULL && p != b) { seekpos(0, ::std::ios_base::in); } } template<class Ch, class Tr, class Alloc> void basic_altstringbuf<Ch, Tr, Alloc>:: str (const string_type& s) { std::size_t sz=s.size(); if(sz != 0 && mode_ & (::std::ios_base::in | ::std::ios_base::out) ) { Ch *new_ptr = alloc_.allocate(sz, is_allocated_? eback() : 0); // if this didnt throw, we're safe, update the buffer dealloc(); sz = s.copy(new_ptr); putend_ = new_ptr + sz; if(mode_ & ::std::ios_base::in) streambuf_t::setg(new_ptr, new_ptr, new_ptr + sz); if(mode_ & ::std::ios_base::out) { streambuf_t::setp(new_ptr, new_ptr + sz); if(mode_ & (::std::ios_base::app | ::std::ios_base::ate)) streambuf_t::pbump(sz); if(gptr() == NULL) streambuf_t::setg(new_ptr, NULL, new_ptr); } is_allocated_ = true; } else dealloc(); } template<class Ch, class Tr, class Alloc> Ch* basic_altstringbuf<Ch, Tr, Alloc>:: begin () const { if(mode_ & ::std::ios_base::out && pptr() != NULL) return pbase(); else if(mode_ & ::std::ios_base::in && gptr() != NULL) return eback(); return NULL; } template<class Ch, class Tr, class Alloc> std::streamsize basic_altstringbuf<Ch, Tr, Alloc>:: size () const { if(mode_ & ::std::ios_base::out && pptr()) return static_cast<streamsize>( pend() - pbase()); else if(mode_ & ::std::ios_base::in && gptr()) return static_cast<streamsize>( egptr() - eback()); else return 0; } template<class Ch, class Tr, class Alloc> std::streamsize basic_altstringbuf<Ch, Tr, Alloc>:: cur_size () const { if(mode_ & ::std::ios_base::out && pptr()) return static_cast<streamsize>( pptr() - pbase()); else if(mode_ & ::std::ios_base::in && gptr()) return static_cast<streamsize>( gptr() - eback()); else return 0; } template<class Ch, class Tr, class Alloc> typename basic_altstringbuf<Ch, Tr, Alloc>::pos_type basic_altstringbuf<Ch, Tr, Alloc>:: seekoff (off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) { if(pptr() != NULL && putend_ < pptr()) putend_ = pptr(); if(which & ::std::ios_base::in && gptr() != NULL) { // get area if(way == ::std::ios_base::end) off += putend_ - eback(); else if(way == ::std::ios_base::cur && (which & ::std::ios_base::out) == 0) off += gptr() - eback(); else if(way != ::std::ios_base::beg) off = off_type(-1); if(0 <= off && off <= putend_ - eback()) { // set gptr streambuf_t::gbump(off + (eback() - gptr())); if(which & ::std::ios_base::out && pptr() != NULL) // update pptr to match gptr streambuf_t::pbump(gptr()-pptr()); } else off = off_type(-1); } else if(which & ::std::ios_base::out && pptr() != NULL) { // put area if(way == ::std::ios_base::end) off += putend_ - eback(); else if(way == ::std::ios_base::cur) off += pptr() - eback(); else if(way != ::std::ios_base::beg) off = off_type(-1); if(0 <= off && off <= putend_ - eback()) // set pptr streambuf_t::pbump((int)(eback() - pptr() + off)); else off = off_type(-1); } else // neither in nor out off = off_type(-1); return (pos_type(off)); } //- end seekoff(..) template<class Ch, class Tr, class Alloc> typename basic_altstringbuf<Ch, Tr, Alloc>::pos_type basic_altstringbuf<Ch, Tr, Alloc>:: seekpos (pos_type pos, ::std::ios_base::openmode which) { off_type off = off_type(pos); // operation guaranteed by
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -