I would like to copy data efficiently between std::streambuf
instances. That is, I would like to shovel blocks of data between them, as opposed to perform character-by-character copying. For example, this is not what I am looking for:
stringbuf in{ios_base::in};
stringbuf out{ios_base::out};
copy(istreambuf_iterator<char>{in},
istreambuf_iterator<char>{},
ostreambuf_iterator<char>{out});
There exists syntactic sugar for this, with a bit more error checking:
ostream os{&out};
os << ∈
Here's a snippet of the implementation of operator<<(basic_streambuf<..>*)
in my standard library (Mac OS X, XCode 7):
typedef istreambuf_iterator<_CharT, _Traits> _Ip;
typedef ostreambuf_iterator<_CharT, _Traits> _Op;
_Ip __i(__sb);
_Ip __eof;
_Op __o(*this);
size_t __c = 0;
for (; __i != __eof; ++__i, ++__o, ++__c)
{
*__o = *__i;
if (__o.failed())
break;
}
The bottom line is: this is still per-character copying. I was hoping the standard library uses an algorithm that relies on the block-level member functions of streambuffers, sputn
and sgetn
, as opposed to per-character transport. Does the standard library provide such an algorithm or do I have to roll my own?
*__o = *__i
will fail to output, so you cannot read ahead and risk losing those characters. – Katleen