10 Buffered::Buffered(Base &b, unsigned s):
13 buf(new char[buf_size]),
18 mode = below.get_mode();
19 below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
22 unsigned Buffered::put(char c)
32 return do_write(&c, 1);
35 void Buffered::flush()
39 unsigned used = end-begin;
42 unsigned len = below.write(begin, used);
47 throw Exception("Couldn't flush all data");
50 else if(cur_op==M_READ)
54 bool Buffered::getline(std::string &line)
58 for(char *i=begin; i!=end; ++i)
61 line.assign(begin, i-begin);
66 return Base::getline(line);
74 return static_cast<unsigned char>(*begin++);
79 return static_cast<unsigned char>(c);
82 Handle Buffered::get_event_handle()
84 throw Exception("Buffered doesn't support events");
87 unsigned Buffered::get_current_size() const
104 void Buffered::set_op(Mode op)
111 unsigned Buffered::do_write(const char *data, unsigned size)
115 if(end+size<buf+buf_size)
117 // All data fits in buffer with whatever is already there
118 memcpy(end, data, size);
125 // Clear the buffer to make more room
130 // Put new data in the buffer to wait for more
131 memcpy(end, data, size);
137 // New data still doesn't fit in the buffer, so write it directly
138 return below.write(data, size);
142 unsigned Buffered::do_read(char *data, unsigned size)
148 // The request can be served from the buffer
149 memcpy(data, begin, size);
152 eof_flag = (below.eof() && begin==end);
158 // Give out whatever is in the buffer already
159 memcpy(data, begin, end-begin);
160 unsigned ret = end-begin;
168 // Fill the buffer and serve the rest of the request from it
169 unsigned len = below.read(end, buf+buf_size-end);
172 len = min(static_cast<unsigned>(end-begin), size);
173 memcpy(data, begin, len);
178 // Read the rest directly from the underlying object
179 ret += below.read(data, size);
181 eof_flag = (below.eof() && begin==end);