/usr/include/boost/iostreams/detail/streambuf/indirect_streambuf.hpp is in libboost1.55-dev 1.55.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2003-2007 Jonathan Turkanis
// Distributed under 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/iostreams for documentation.
// This material is heavily indebted to the discussion and code samples in
// A. Langer and K. Kreft, "Standard C++ IOStreams and Locales",
// Addison-Wesley, 2000, pp. 228-43.
// User "GMSB" provided an optimization for small seeks.
#ifndef BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
#define BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
#include <algorithm> // min, max.
#include <cassert>
#include <exception>
#include <typeinfo>
#include <boost/config.hpp> // Member template friends.
#include <boost/detail/workaround.hpp>
#include <boost/iostreams/constants.hpp>
#include <boost/iostreams/detail/adapter/concept_adapter.hpp>
#include <boost/iostreams/detail/buffer.hpp>
#include <boost/iostreams/detail/config/wide_streams.hpp>
#include <boost/iostreams/detail/double_object.hpp>
#include <boost/iostreams/detail/execute.hpp>
#include <boost/iostreams/detail/functional.hpp>
#include <boost/iostreams/detail/ios.hpp>
#include <boost/iostreams/detail/optional.hpp>
#include <boost/iostreams/detail/push.hpp>
#include <boost/iostreams/detail/streambuf/linked_streambuf.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/positioning.hpp>
#include <boost/iostreams/traits.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/mpl/if.hpp>
#include <boost/throw_exception.hpp>
#include <boost/type_traits/is_convertible.hpp>
// Must come last.
#include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC, BCC 5.x
namespace boost { namespace iostreams { namespace detail {
//
// Description: The implementation of basic_streambuf used by chains.
//
template<typename T, typename Tr, typename Alloc, typename Mode>
class indirect_streambuf
: public linked_streambuf<BOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
{
public:
typedef typename char_type_of<T>::type char_type;
BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
private:
typedef typename category_of<T>::type category;
typedef concept_adapter<T> wrapper;
typedef detail::basic_buffer<char_type, Alloc> buffer_type;
typedef indirect_streambuf<T, Tr, Alloc, Mode> my_type;
typedef detail::linked_streambuf<char_type, traits_type> base_type;
typedef linked_streambuf<char_type, Tr> streambuf_type;
public:
indirect_streambuf();
void open(const T& t BOOST_IOSTREAMS_PUSH_PARAMS());
bool is_open() const;
void close();
bool auto_close() const;
void set_auto_close(bool close);
bool strict_sync();
// Declared in linked_streambuf.
T* component() { return &*obj(); }
protected:
#if !BOOST_WORKAROUND(__GNUC__, == 2)
BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
#endif
//----------virtual functions---------------------------------------------//
#ifndef BOOST_IOSTREAMS_NO_LOCALE
void imbue(const std::locale& loc);
#endif
#ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
public:
#endif
int_type underflow();
int_type pbackfail(int_type c);
int_type overflow(int_type c);
int sync();
pos_type seekoff( off_type off, BOOST_IOS::seekdir way,
BOOST_IOS::openmode which );
pos_type seekpos(pos_type sp, BOOST_IOS::openmode which);
// Declared in linked_streambuf.
void set_next(streambuf_type* next);
void close_impl(BOOST_IOS::openmode m);
const std::type_info& component_type() const { return typeid(T); }
void* component_impl() { return component(); }
private:
//----------Accessor functions--------------------------------------------//
wrapper& obj() { return *storage_; }
streambuf_type* next() const { return next_; }
buffer_type& in() { return buffer_.first(); }
buffer_type& out() { return buffer_.second(); }
bool can_read() const { return is_convertible<Mode, input>::value; }
bool can_write() const { return is_convertible<Mode, output>::value; }
bool output_buffered() const { return (flags_ & f_output_buffered) != 0; }
bool shared_buffer() const { return is_convertible<Mode, seekable>::value; }
void set_flags(int f) { flags_ = f; }
//----------State changing functions--------------------------------------//
virtual void init_get_area();
virtual void init_put_area();
//----------Utility function----------------------------------------------//
pos_type seek_impl( stream_offset off, BOOST_IOS::seekdir way,
BOOST_IOS::openmode which );
void sync_impl();
enum flag_type {
f_open = 1,
f_output_buffered = f_open << 1,
f_auto_close = f_output_buffered << 1
};
optional<wrapper> storage_;
streambuf_type* next_;
double_object<
buffer_type,
is_convertible<
Mode,
two_sequence
>
> buffer_;
std::streamsize pback_size_;
int flags_;
};
//--------------Implementation of indirect_streambuf--------------------------//
template<typename T, typename Tr, typename Alloc, typename Mode>
indirect_streambuf<T, Tr, Alloc, Mode>::indirect_streambuf()
: next_(0), pback_size_(0), flags_(f_auto_close) { }
//--------------Implementation of open, is_open and close---------------------//
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::open
(const T& t, std::streamsize buffer_size, std::streamsize pback_size)
{
using namespace std;
// Normalize buffer sizes.
buffer_size =
(buffer_size != -1) ?
buffer_size :
iostreams::optimal_buffer_size(t);
pback_size =
(pback_size != -1) ?
pback_size :
default_pback_buffer_size;
// Construct input buffer.
if (can_read()) {
pback_size_ = (std::max)(std::streamsize(2), pback_size); // STLPort needs 2.
std::streamsize size =
pback_size_ +
( buffer_size ? buffer_size: 1 );
in().resize(size);
if (!shared_buffer())
init_get_area();
}
// Construct output buffer.
if (can_write() && !shared_buffer()) {
if (buffer_size != 0)
out().resize(buffer_size);
init_put_area();
}
storage_.reset(wrapper(t));
flags_ |= f_open;
if (can_write() && buffer_size > 1)
flags_ |= f_output_buffered;
this->set_true_eof(false);
this->set_needs_close();
}
template<typename T, typename Tr, typename Alloc, typename Mode>
inline bool indirect_streambuf<T, Tr, Alloc, Mode>::is_open() const
{ return (flags_ & f_open) != 0; }
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::close()
{
using namespace std;
base_type* self = this;
detail::execute_all(
detail::call_member_close(*self, BOOST_IOS::in),
detail::call_member_close(*self, BOOST_IOS::out),
detail::call_reset(storage_),
detail::clear_flags(flags_)
);
}
template<typename T, typename Tr, typename Alloc, typename Mode>
bool indirect_streambuf<T, Tr, Alloc, Mode>::auto_close() const
{ return (flags_ & f_auto_close) != 0; }
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::set_auto_close(bool close)
{ flags_ = (flags_ & ~f_auto_close) | (close ? f_auto_close : 0); }
//--------------Implementation virtual functions------------------------------//
#ifndef BOOST_IOSTREAMS_NO_LOCALE
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::imbue(const std::locale& loc)
{
if (is_open()) {
obj().imbue(loc);
if (next_)
next_->pubimbue(loc);
}
}
#endif
template<typename T, typename Tr, typename Alloc, typename Mode>
typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
indirect_streambuf<T, Tr, Alloc, Mode>::underflow()
{
using namespace std;
if (!gptr()) init_get_area();
buffer_type& buf = in();
if (gptr() < egptr()) return traits_type::to_int_type(*gptr());
// Fill putback buffer.
std::streamsize keep =
(std::min)( static_cast<std::streamsize>(gptr() - eback()),
pback_size_ );
if (keep)
traits_type::move( buf.data() + (pback_size_ - keep),
gptr() - keep, keep );
// Set pointers to reasonable values in case read throws.
setg( buf.data() + pback_size_ - keep,
buf.data() + pback_size_,
buf.data() + pback_size_ );
// Read from source.
std::streamsize chars =
obj().read(buf.data() + pback_size_, buf.size() - pback_size_, next_);
if (chars == -1) {
this->set_true_eof(true);
chars = 0;
}
setg(eback(), gptr(), buf.data() + pback_size_ + chars);
return chars != 0 ?
traits_type::to_int_type(*gptr()) :
traits_type::eof();
}
template<typename T, typename Tr, typename Alloc, typename Mode>
typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
indirect_streambuf<T, Tr, Alloc, Mode>::pbackfail(int_type c)
{
if (gptr() != eback()) {
gbump(-1);
if (!traits_type::eq_int_type(c, traits_type::eof()))
*gptr() = traits_type::to_char_type(c);
return traits_type::not_eof(c);
} else {
boost::throw_exception(bad_putback());
}
}
template<typename T, typename Tr, typename Alloc, typename Mode>
typename indirect_streambuf<T, Tr, Alloc, Mode>::int_type
indirect_streambuf<T, Tr, Alloc, Mode>::overflow(int_type c)
{
if ( (output_buffered() && pptr() == 0) ||
(shared_buffer() && gptr() != 0) )
{
init_put_area();
}
if (!traits_type::eq_int_type(c, traits_type::eof())) {
if (output_buffered()) {
if (pptr() == epptr()) {
sync_impl();
if (pptr() == epptr())
return traits_type::eof();
}
*pptr() = traits_type::to_char_type(c);
pbump(1);
} else {
char_type d = traits_type::to_char_type(c);
if (obj().write(&d, 1, next_) != 1)
return traits_type::eof();
}
}
return traits_type::not_eof(c);
}
template<typename T, typename Tr, typename Alloc, typename Mode>
int indirect_streambuf<T, Tr, Alloc, Mode>::sync()
{
try { // sync() is no-throw.
sync_impl();
obj().flush(next_);
return 0;
} catch (...) { return -1; }
}
template<typename T, typename Tr, typename Alloc, typename Mode>
bool indirect_streambuf<T, Tr, Alloc, Mode>::strict_sync()
{
try { // sync() is no-throw.
sync_impl();
return obj().flush(next_);
} catch (...) { return false; }
}
template<typename T, typename Tr, typename Alloc, typename Mode>
inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
indirect_streambuf<T, Tr, Alloc, Mode>::seekoff
(off_type off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
{ return seek_impl(off, way, which); }
template<typename T, typename Tr, typename Alloc, typename Mode>
inline typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
indirect_streambuf<T, Tr, Alloc, Mode>::seekpos
(pos_type sp, BOOST_IOS::openmode which)
{
return seek_impl(position_to_offset(sp), BOOST_IOS::beg, which);
}
template<typename T, typename Tr, typename Alloc, typename Mode>
typename indirect_streambuf<T, Tr, Alloc, Mode>::pos_type
indirect_streambuf<T, Tr, Alloc, Mode>::seek_impl
(stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
{
if ( gptr() != 0 && way == BOOST_IOS::cur && which == BOOST_IOS::in &&
eback() - gptr() <= off && off <= egptr() - gptr() )
{ // Small seek optimization
gbump(off);
return obj().seek(0, BOOST_IOS::cur, BOOST_IOS::in, next_) -
static_cast<off_type>(egptr() - gptr());
}
if (pptr() != 0)
this->BOOST_IOSTREAMS_PUBSYNC(); // sync() confuses VisualAge 6.
if (way == BOOST_IOS::cur && gptr())
off -= static_cast<off_type>(egptr() - gptr());
setg(0, 0, 0);
setp(0, 0);
return obj().seek(off, way, which, next_);
}
template<typename T, typename Tr, typename Alloc, typename Mode>
inline void indirect_streambuf<T, Tr, Alloc, Mode>::set_next
(streambuf_type* next)
{ next_ = next; }
template<typename T, typename Tr, typename Alloc, typename Mode>
inline void indirect_streambuf<T, Tr, Alloc, Mode>::close_impl
(BOOST_IOS::openmode which)
{
if (which == BOOST_IOS::in && is_convertible<Mode, input>::value) {
setg(0, 0, 0);
}
if (which == BOOST_IOS::out && is_convertible<Mode, output>::value) {
sync();
setp(0, 0);
}
if ( !is_convertible<category, dual_use>::value ||
is_convertible<Mode, input>::value == (which == BOOST_IOS::in) )
{
obj().close(which, next_);
}
}
//----------State changing functions------------------------------------------//
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::sync_impl()
{
std::streamsize avail, amt;
if ((avail = static_cast<std::streamsize>(pptr() - pbase())) > 0) {
if ((amt = obj().write(pbase(), avail, next())) == avail)
setp(out().begin(), out().end());
else {
const char_type* ptr = pptr();
setp(out().begin() + amt, out().end());
pbump(ptr - pptr());
}
}
}
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::init_get_area()
{
if (shared_buffer() && pptr() != 0) {
sync_impl();
setp(0, 0);
}
setg(in().begin(), in().begin(), in().begin());
}
template<typename T, typename Tr, typename Alloc, typename Mode>
void indirect_streambuf<T, Tr, Alloc, Mode>::init_put_area()
{
using namespace std;
if (shared_buffer() && gptr() != 0)
setg(0, 0, 0);
if (output_buffered())
setp(out().begin(), out().end());
else
setp(0, 0);
}
//----------------------------------------------------------------------------//
} } } // End namespaces detail, iostreams, boost.
#include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC, BCC 5.x
#endif // #ifndef BOOST_IOSTREAMS_DETAIL_INDIRECT_STREAMBUF_HPP_INCLUDED
|