📄 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_HPP
namespace 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) {
size_type 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, sz);
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(static_cast<int>(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>
typename std::basic_string<Ch,Tr,Alloc>::size_type
basic_altstringbuf<Ch, Tr, Alloc>::
size () const {
if(mode_ & ::std::ios_base::out && pptr())
return static_cast<size_type>(pend() - pbase());
else if(mode_ & ::std::ios_base::in && gptr())
return static_cast<size_type>(egptr() - eback());
else
return 0;
}
template<class Ch, class Tr, class Alloc>
typename std::basic_string<Ch,Tr,Alloc>::size_type
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 += static_cast<off_type>(putend_ - gptr());
else if(way == ::std::ios_base::beg)
off += static_cast<off_type>(eback() - gptr());
else if(way != ::std::ios_base::cur || (which & ::std::ios_base::out) )
// (altering in&out is only supported if way is beg or end, not cur)
return pos_type(off_type(-1));
if(eback() <= off+gptr() && off+gptr() <= putend_ ) {
// set gptr
streambuf_t::gbump(off);
if(which & ::std::ios_base::out && pptr() != NULL)
// update pptr to match gptr
streambuf_t::pbump(static_cast<int>(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 += static_cast<off_type>(putend_ - pptr());
else if(way == ::std::ios_base::beg)
off += static_cast<off_type>(pbase() - pptr());
else if(way != ::std::ios_base::beg)
return pos_type(off_type(-1));
if(pbase() <= off+pptr() && off+pptr() <= putend_)
// set pptr
streambuf_t::pbump(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 + -