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

@@ -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;
}