Issue #297 - Implemented BasicMemoryStreamBuf::seekoff

This change adds support for `tellp`, `tellg` and seeking.

**Additional notes:**

+ `tellg` calls `rdbuf->pubseekoff(0, cur, out)` [lib.ostream.seeks] so need to
  provide an override of `seekoff`.
+ Requirements for `seekoff` are in 27.7.1.3.
+ 27.7.1.3 - Standard has a roundabout way of saying `cur` is valid if only one
  of `in` or `out` is specified (Condition 3).
+ `xend` is `egptr`/`epptr` (get area/put area).
+ `xbeg` is `eback`/`pbase` (get area/put area).
+ `xnext` is `gptr`/`pptr` (get area/put area).
+ `newoff` is a bit of a misnomer, the new offset into the stream is given by
  `newoff + off`.  `newoff` is really the start point.
+ You can set `gnext` with `setg`, but you can't do that with `setp` you have
  to `pbump` by the new offset minus the current offset.
This commit is contained in:
martin-osborne 2015-01-24 18:39:06 +00:00
parent f79ab5b77a
commit 5e104addd3
3 changed files with 202 additions and 12 deletions

View File

@ -32,8 +32,8 @@
namespace Poco {
template <typename ch, typename tr>
class BasicMemoryStreamBuf: public std::basic_streambuf<ch, tr>
template <typename charT, typename traits>
class BasicMemoryStreamBuf: public std::basic_streambuf<charT, traits>
/// BasicMemoryStreamBuf is a simple implementation of a
/// stream buffer for reading and writing from a memory area.
///
@ -43,13 +43,11 @@ class BasicMemoryStreamBuf: public std::basic_streambuf<ch, tr>
/// ostream, but not for an iostream.
{
protected:
typedef std::basic_streambuf<ch, tr> Base;
typedef std::basic_ios<ch, tr> IOS;
typedef ch char_type;
typedef tr char_traits;
typedef typename Base::int_type int_type;
typedef typename Base::pos_type pos_type;
typedef typename Base::off_type off_type;
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
public:
BasicMemoryStreamBuf(char_type* pBuffer, std::streamsize bufferSize):
@ -66,12 +64,74 @@ public:
virtual int_type overflow(int_type /*c*/)
{
return char_traits::eof();
return traits_type::eof();
}
virtual int_type underflow()
{
return char_traits::eof();
return traits_type::eof();
}
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
{
const pos_type fail = off_type(-1);
off_type newoff = off_type(-1);
if ((which & std::ios_base::in) != 0)
{
// Position the input sequence
if (gptr() == 0)
return fail;
switch (way)
{
case std::ios_base::beg:
newoff = 0;
break;
case std::ios_base::cur:
// cur is not valid if both in and out are specified (Condition 3)
if ((which & std::ios_base::out) != 0)
return fail;
newoff = gptr() - eback();
break;
case std::ios_base::end:
newoff = egptr() - eback();
break;
}
if ((newoff + off) < 0 || (egptr() - eback()) < (newoff + off))
return fail;
this->setg(eback(), eback() + newoff + off, egptr());
}
if ((which & std::ios_base::out) != 0)
{
if (pptr() == 0)
return fail;
switch (way)
{
case std::ios_base::beg:
newoff = 0;
break;
case std::ios_base::cur:
// cur is not valid if both in and out are specified (Condition 3)
if ((which & std::ios_base::in) != 0)
return fail;
newoff = pptr() - pbase();
break;
case std::ios_base::end:
newoff = epptr() - pbase();
break;
}
if (newoff + off < 0 || (epptr() - pbase()) < newoff + off)
return fail;
this->pbump((int)(newoff + off - (pptr() - pbase())));
}
return newoff;
}
virtual int sync()

View File

@ -16,6 +16,7 @@
#include "Poco/Buffer.h"
#include "Poco/MemoryStream.h"
#include <sstream>
using Poco::MemoryInputStream;
using Poco::MemoryOutputStream;
@ -87,7 +88,6 @@ void MemoryStreamTest::testOutput()
assert (ostr2.fail());
}
void MemoryStreamTest::testTell()
{
Poco::Buffer<char> buffer(1024);
@ -113,6 +113,128 @@ void MemoryStreamTest::testTell()
}
void MemoryStreamTest::testInputSeek()
{
Poco::Buffer<char> buffer(9);
Poco::MemoryOutputStream ostr(buffer.begin(), buffer.size());
ostr << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
Poco::MemoryInputStream istr(buffer.begin(), buffer.size());
char c;
istr >> c;
assert (c == '1');
istr.seekg(3, std::ios_base::beg); // 3 from beginning
istr >> c; // now that makes 4
assert (c == '4');
istr.seekg(2, std::ios_base::cur); // now that makes 6
istr >> c; // now that makes 7
assert (c == '7');
istr.seekg(-7, std::ios_base::end); // so that puts us at 2
istr >> c; // now 3
assert (c == '3');
}
void MemoryStreamTest::testInputSeekVsStringStream()
{
Poco::Buffer<char> buffer(9);
Poco::MemoryOutputStream ostr(buffer.begin(), buffer.size());
ostr << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
std::stringstream sss;
sss << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
Poco::MemoryInputStream mis(buffer.begin(), buffer.size());
char x, y;
sss >> x;
mis >> y;
assert (x == y);
sss.seekg(3, std::ios_base::beg);
mis.seekg(3, std::ios_base::beg);
sss >> x;
mis >> y;
assert (x == y);
sss.seekg(2, std::ios_base::cur);
mis.seekg(2, std::ios_base::cur);
sss >> x;
mis >> y;
assert (x == y);
sss.seekg(-7, std::ios_base::end);
mis.seekg(-7, std::ios_base::end);
sss >> x;
mis >> y;
assert (x == y);
}
void MemoryStreamTest::testOutputSeek()
{
Poco::Buffer<char> buffer(9);
Poco::MemoryOutputStream ostr(buffer.begin(), buffer.size());
ostr << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
ostr.seekp(4, std::ios_base::beg); // 4 from beginning
ostr << 'a'; // and that makes 5 (zero index 4)
assert (buffer[4] == 'a');
ostr.seekp(2, std::ios_base::cur); // and this makes 7
ostr << 'b'; // and this makes 8 (zero index 7)
assert (buffer[7] == 'b');
ostr.seekp(-3, std::ios_base::end); // 10-7 from the beginning
ostr << 'c'; // and this makes 4 (zero index 3)
assert (buffer[6] == 'c');
}
void MemoryStreamTest::testOutputSeekVsStringStream()
{
Poco::Buffer<char> buffer(9);
Poco::MemoryOutputStream mos(buffer.begin(), buffer.size());
mos << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
std::ostringstream oss;
oss << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9';
mos.seekp(4, std::ios_base::beg);
oss.seekp(4, std::ios_base::beg);
mos << 'a';
oss << 'a';
assert (oss.str()[4] == 'a');
assert (buffer[4] == oss.str()[4]);
mos.seekp(2, std::ios_base::cur);
oss.seekp(2, std::ios_base::cur);
mos << 'b';
oss << 'b';
assert (oss.str()[7] == 'b');
assert (buffer[7] == oss.str()[7]);
mos.seekp(-3, std::ios_base::end);
oss.seekp(-3, std::ios_base::end);
mos << 'c';
oss << 'c';
assert (oss.str()[6] == 'c');
assert (buffer[6] == oss.str()[6]);
mos.seekp(-2, std::ios_base::cur);
oss.seekp(-2, std::ios_base::cur);
mos << 'd';
oss << 'd';
assert (oss.str()[5] == 'd');
assert (buffer[5] == oss.str()[5]);
}
void MemoryStreamTest::setUp()
{
}
@ -130,6 +252,10 @@ CppUnit::Test* MemoryStreamTest::suite()
CppUnit_addTest(pSuite, MemoryStreamTest, testInput);
CppUnit_addTest(pSuite, MemoryStreamTest, testOutput);
CppUnit_addTest(pSuite, MemoryStreamTest, testTell);
CppUnit_addTest(pSuite, MemoryStreamTest, testInputSeek);
CppUnit_addTest(pSuite, MemoryStreamTest, testInputSeekVsStringStream);
CppUnit_addTest(pSuite, MemoryStreamTest, testOutputSeek);
CppUnit_addTest(pSuite, MemoryStreamTest, testOutputSeekVsStringStream);
return pSuite;
}

View File

@ -29,6 +29,10 @@ public:
void testInput();
void testOutput();
void testTell();
void testInputSeek();
void testInputSeekVsStringStream();
void testOutputSeek();
void testOutputSeekVsStringStream();
void setUp();
void tearDown();