您可以创建一个过滤流缓冲区,即从std::streambuf
派生的类。为了支持缓冲读取,一旦输入字符被消耗,您将覆盖underflow()
以填充字符的下一个缓冲区。为了支持有限的寻求,以前的缓冲区不会被丢弃,而是被部分保留。另外,你会覆盖seekoff()
。
像这样的东西应该做的伎俩:
#include <iostream>
#include <streambuf>
#include <string>
#include <cstdlib>
#include <cstring>
class bufferbuf
: public std::streambuf {
enum { size = 2000, half = size/2 };
char buffer[size];
std::streambuf* sbuf;
std::streamoff base;
public:
bufferbuf(std::streambuf* sbuf): sbuf(sbuf), base() {
auto read = sbuf->sgetn(this->buffer, size);
this->setg(this->buffer, this->buffer, this->buffer + read);
}
int underflow() {
if (this->gptr() == this->buffer + size) {
std::memmove(this->eback(), this->eback() + half, half);
base += half;
auto read = sbuf->sgetn(this->eback() + half, half);
this->setg(this->eback(), this->eback() + half, this->eback() + half + read);
}
return this->gptr() != this->egptr()
? traits_type::to_int_type(*this->gptr())
: traits_type::eof();
}
std::streampos seekoff(off_type offset,
std::ios_base::seekdir whence,
std::ios_base::openmode which) override {
if (this->gptr() - this->eback() < -offset
|| this->egptr() - this->gptr() < offset
|| whence != std::ios_base::cur
|| !(which & std::ios_base::in)) {
return pos_type(off_type(-1));
}
this->gbump(offset);
return pos_type(this->base + (this->gptr() - this->eback()));
}
std::streampos seekpos(pos_type pos, std::ios_base::openmode which) override {
if (off_type(pos) < this->base
|| this->base + (this->egptr() - this->eback()) < off_type(pos)
|| !(which & std::ios_base::in)) {
return pos_type(off_type(-1));
}
this->setg(this->eback(), this->eback() + (off_type(pos) - this->base), this->egptr());
return pos_type(base + (this->gptr() - this->eback()));
}
};
int main() {
bufferbuf buf(std::cin.rdbuf());
std::istream in(&buf);
// ...
std::string s0, s1;
bool relative(false);
if (relative) {
while (in >> s0
&& (in.seekg(-int(s0.size()), std::ios_base::cur), in >> s1)) {
std::cout << "read "
<< "s0='" << s0 << "' " << "s1='" << s1 << "'\n";
}
}
else {
for (std::streampos pos = in.tellg();
in >> s0 && (in.seekg(pos), in >> s1); pos = in.tellg()) {
std::cout << "read "
<< "s0='" << s0 << "' " << "s1='" << s1 << "'\n";
}
}
}
上面的代码与几个简单的测试情况下工作。它演示了相对和绝对定位的用法。一般来说,我发现在流中寻找是没有用的,因为通常每个有趣的选择都可以通过一个字符来完成。结果,我可能在位置上错过了一些东西。不过,我期望上面的代码正常工作。
我推荐Nicolai Josuttis的* C++标准库*。虽然已经写了关于IOStreams的章节的相关部分,但我可能会有偏见。我唯一知道的其他选择是Angelika Langer&Klaus Kreft的* IOStreams和Locales Library *。 Steve Teale的IOStreams *已经过时了(我也在那里学习了基础知识)。 –