3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
15 Buffered::Buffered(Base &b, unsigned s):
18 buf(new char[buf_size]),
23 mode=below.get_mode();
24 below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
27 unsigned Buffered::put(char c)
37 return do_write(&c, 1);
40 void Buffered::flush()
44 unsigned used=end-begin;
47 unsigned len=below.write(begin, used);
52 throw Exception("Couldn't flush all data");
55 else if(cur_op==M_READ)
59 bool Buffered::getline(std::string &line)
63 for(char *i=begin; i!=end; ++i)
66 line.assign(begin, i-begin);
71 return Base::getline(line);
79 return static_cast<unsigned char>(*begin++);
84 return static_cast<unsigned char>(c);
87 Handle Buffered::get_event_handle()
89 throw Exception("Buffered doesn't support events");
92 unsigned Buffered::get_current_size() const
109 void Buffered::set_op(Mode op)
116 unsigned Buffered::do_write(const char *data, unsigned size)
120 if(end+size<buf+buf_size)
122 // All data fits in buffer with whatever is already there
123 memcpy(end, data, size);
130 // Clear the buffer to make more room
135 // Put new data in the buffer to wait for more
136 memcpy(end, data, size);
142 // New data still doesn't fit in the buffer, so write it directly
143 return below.write(data, size);
147 unsigned Buffered::do_read(char *data, unsigned size)
153 // The request can be served from the buffer
154 memcpy(data, begin, size);
157 eof_flag=(below.eof() && begin==end);
163 // Give out whatever is in the buffer already
164 memcpy(data, begin, end-begin);
165 unsigned ret=end-begin;
173 // Fill the buffer and serve the rest of the request from it
174 unsigned len=below.read(end, buf+buf_size-end);
177 len=min(static_cast<unsigned>(end-begin), size);
178 memcpy(data, begin, len);
183 // Read the rest directly from the underlying object
184 ret+=below.read(data, size);
186 eof_flag=(below.eof() && begin==end);