11 Buffered::Buffered(Base &b, unsigned s):
14 buf(new char[buf_size]),
19 mode = below.get_mode();
20 below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
35 void Buffered::flush()
39 unsigned used = end-begin;
42 unsigned len = below.write(begin, used);
47 throw runtime_error("Couldn't flush all data");
50 else if(cur_op==M_READ)
54 unsigned Buffered::do_write(const char *data, unsigned size)
58 if(end+size<buf+buf_size)
60 // All data fits in buffer with whatever is already there
61 memcpy(end, data, size);
68 // Clear the buffer to make more room
73 // Put new data in the buffer to wait for more
74 memcpy(end, data, size);
80 // New data still doesn't fit in the buffer, so write it directly
81 return below.write(data, size);
85 unsigned Buffered::do_read(char *data, unsigned size)
91 // The request can be served from the buffer
92 memcpy(data, begin, size);
95 eof_flag = (below.eof() && begin==end);
101 // Give out whatever is in the buffer already
102 memcpy(data, begin, end-begin);
103 unsigned ret = end-begin;
111 // Fill the buffer and serve the rest of the request from it
112 unsigned len = below.read(end, buf+buf_size-end);
115 len = min(static_cast<unsigned>(end-begin), size);
116 memcpy(data, begin, len);
121 // Read the rest directly from the underlying object
122 ret += below.read(data, size);
124 eof_flag = (below.eof() && begin==end);
130 unsigned Buffered::put(char c)
140 return do_write(&c, 1);
143 bool Buffered::getline(std::string &line)
147 for(char *i=begin; i!=end; ++i)
150 line.assign(begin, i-begin);
155 return Base::getline(line);
163 return static_cast<unsigned char>(*begin++);
166 if(do_read(&c, 1)==0)
168 return static_cast<unsigned char>(c);
171 void Buffered::set_op(Mode op)
178 unsigned Buffered::get_current_size() const