Reputation: 8100
I'm making a class that buffers some binary data read from std::cin
; I need some limited
ability to back-track through the stream.
In more detail, when I read
n
bytes from the stream, I want to be able to rewind up to
n
bytes in the stream.
Currently, I append to the buffer by appending individual characters
to a std::deque
, but I expect that there is a better way to copy
chunks of data from an std::istream
into a std::deque
.
Basically is there a way to improve (remove?) the while loop in
the load
function below?
Currently I have:
#include<algorithm>
#include<deque>
class BufferedStdIn{
public:
BufferedStdIn(): m_buffer(), m_cursor(0){
}
// copy data out from my internal buffer to the client's array
// format dictated by external interface requirement
void read(char* dest, size_t n){
load(n)
std::copy( m_buffer.begin(), m_buffer.begin()+n, dest );
m_cursor+=n;
}
// Go backwards in the stream; return the number of bytes moved.
// @retval < input arg n indicates that we can't rewind as far as asked for
size_t rewind(size_t n){
int final_pos=m_cursor-n;
if( final_pos < 0 ){
// can't go as far as the client wants, just go back to start of my buffer
size_t moved=m_cursor;
m_cursor=0;
return moved;
}
else{ // can do what client wants
m_cursor=final_pos;
return n;
}
}
void load(n){
m_buffer.erase( m_buffer.begin(), m_buffer.begin()+m_cursor);
m_cursor=0;
while( m_buffer.size()<n){
char c;
std::cin.get(c);
m_buffer.push_back(c);
}
}
// SNIP: other functions that let clients look at m_buffer
std::deque<char> m_buffer;
size_t m_cursor;
}
Upvotes: 0
Views: 728
Reputation: 16761
Maybe not more efficient, but shorter:
void load(size_t n)
{
m_buffer.erase( m_buffer.begin(), m_buffer.begin()+m_cursor);
m_cursor=0;
if(m_buffer.size() < n)
std::copy_n(std::istream_iterator<char>(std::cin), n - m_buffer.size(), std::back_inserter(m_buffer));
}
Upvotes: 1