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));
34 void Buffered::flush()
38 unsigned used = end-begin;
41 unsigned len = below.write(begin, used);
46 throw runtime_error("Couldn't flush all data");
49 else if(cur_op==M_READ)
53 unsigned Buffered::do_write(const char *data, unsigned size)
57 if(end+size<buf+buf_size)
59 // All data fits in buffer with whatever is already there
60 memcpy(end, data, size);
67 // Clear the buffer to make more room
72 // Put new data in the buffer to wait for more
73 memcpy(end, data, size);
79 // New data still doesn't fit in the buffer, so write it directly
80 return below.write(data, size);
84 unsigned Buffered::do_read(char *data, unsigned size)
90 // The request can be served from the buffer
91 memcpy(data, begin, size);
94 eof_flag = (below.eof() && begin==end);
100 // Give out whatever is in the buffer already
101 memcpy(data, begin, end-begin);
102 unsigned ret = end-begin;
110 // Fill the buffer and serve the rest of the request from it
111 unsigned len = below.read(end, buf+buf_size-end);
114 len = min(static_cast<unsigned>(end-begin), size);
115 memcpy(data, begin, len);
120 // Read the rest directly from the underlying object
121 ret += below.read(data, size);
123 eof_flag = (below.eof() && begin==end);
129 unsigned Buffered::put(char c)
139 return do_write(&c, 1);
142 bool Buffered::getline(std::string &line)
146 for(char *i=begin; i!=end; ++i)
149 line.assign(begin, i-begin);
154 return Base::getline(line);
162 return static_cast<unsigned char>(*begin++);
165 if(do_read(&c, 1)==0)
167 return static_cast<unsigned char>(c);
170 void Buffered::set_op(Mode op)
177 unsigned Buffered::get_current_size() const
182 Handle Buffered::get_event_handle()
184 throw logic_error("Buffered doesn't support events");