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::set_block(bool)
37 throw logic_error("Buffered::set_block");
40 void Buffered::set_inherit(bool)
42 throw logic_error("Buffered::set_block");
45 void Buffered::flush()
49 unsigned used = end-begin;
52 unsigned len = below.write(begin, used);
57 throw runtime_error("Couldn't flush all data");
60 else if(cur_op==M_READ)
64 unsigned Buffered::do_write(const char *data, unsigned size)
68 if(end+size<buf+buf_size)
70 // All data fits in buffer with whatever is already there
71 memcpy(end, data, size);
78 // Clear the buffer to make more room
83 // Put new data in the buffer to wait for more
84 memcpy(end, data, size);
90 // New data still doesn't fit in the buffer, so write it directly
91 return below.write(data, size);
95 unsigned Buffered::do_read(char *data, unsigned size)
101 // The request can be served from the buffer
102 memcpy(data, begin, size);
105 eof_flag = (below.eof() && begin==end);
111 // Give out whatever is in the buffer already
112 memcpy(data, begin, end-begin);
113 unsigned ret = end-begin;
121 // Fill the buffer and serve the rest of the request from it
122 unsigned len = below.read(end, buf+buf_size-end);
125 len = min(static_cast<unsigned>(end-begin), size);
126 memcpy(data, begin, len);
131 // Read the rest directly from the underlying object
132 ret += below.read(data, size);
134 eof_flag = (below.eof() && begin==end);
140 unsigned Buffered::put(char c)
150 return do_write(&c, 1);
153 bool Buffered::getline(std::string &line)
157 for(char *i=begin; i!=end; ++i)
160 line.assign(begin, i-begin);
165 return Base::getline(line);
173 return static_cast<unsigned char>(*begin++);
176 if(do_read(&c, 1)==0)
178 return static_cast<unsigned char>(c);
181 const Handle &Buffered::get_handle(Mode)
183 throw logic_error("Buffered::get_handle");
186 void Buffered::set_op(Mode op)
193 unsigned Buffered::get_current_size() const