| 1 | #include <sstream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/iostreams/slice.hpp>
|
|---|
| 4 | #include <boost/iostreams/stream.hpp>
|
|---|
| 5 |
|
|---|
| 6 | void runTest(bool doSeek)
|
|---|
| 7 | {
|
|---|
| 8 | namespace io = boost::iostreams;
|
|---|
| 9 | typedef io::restriction<std::istream> SlicedStreamDev;
|
|---|
| 10 |
|
|---|
| 11 | std::istringstream data;
|
|---|
| 12 | data.str("foo bar");
|
|---|
| 13 | // Seek to start of "bar", and restrict to a single character
|
|---|
| 14 | int offset = 4;
|
|---|
| 15 | int length = 1;
|
|---|
| 16 | data.seekg(offset);
|
|---|
| 17 | io::stream<SlicedStreamDev> restrictedStream(SlicedStreamDev(data, offset, length));
|
|---|
| 18 | if (doSeek)
|
|---|
| 19 | restrictedStream.seekg(0); // Seek to beginning of restricted stream - should do nothing.
|
|---|
| 20 |
|
|---|
| 21 | // Should dump the entire contents of the restricted stream (ie, "b") to
|
|---|
| 22 | // the screen. This doesn't work if seekg(0) has been called above
|
|---|
| 23 | std::cout << restrictedStream.rdbuf() << "\n";
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | int main()
|
|---|
| 28 | {
|
|---|
| 29 | runTest(false);
|
|---|
| 30 | runTest(true);
|
|---|
| 31 |
|
|---|
| 32 | // Here's part of what's happening behind the scenes:
|
|---|
| 33 | std::istringstream data;
|
|---|
| 34 | data.str("foo bar");
|
|---|
| 35 | {
|
|---|
| 36 | // boost::iostreams::seek with std::streambuf calls the following, which
|
|---|
| 37 | // fails according to C++11 standard N3242, section 27.8.2.4 (table 130)
|
|---|
| 38 | std::streambuf::pos_type result = data.rdbuf()->pubseekoff(4, std::ios::cur, std::ios::in | std::ios::out);
|
|---|
| 39 | std::cout << "istringstream pubseekoff() result: " << result << "\n";
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | {
|
|---|
| 43 | // The following also does not work in libstdc++, since istringstream
|
|---|
| 44 | // has no output pointer set in the streambuf, so passing std::ios::out
|
|---|
| 45 | // doesn't make sense (note implications for boost::iostreams::seek,
|
|---|
| 46 | // which passes (std::ios::in | std::ios::out) by default!)
|
|---|
| 47 | std::streambuf::pos_type result = data.rdbuf()->pubseekoff(4, std::ios::beg, std::ios::in | std::ios::out);
|
|---|
| 48 | std::cout << "pubseekoff() result: " << result << "\n";
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return 0;
|
|---|
| 52 | }
|
|---|