3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
17 Buffered::Buffered(Base &b, unsigned s):
20 buf(new char[buf_size]),
25 mode=below.get_mode();
26 below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
29 unsigned Buffered::put(char c)
39 return do_write(&c, 1);
42 void Buffered::flush()
46 unsigned used=end-begin;
49 unsigned len=below.write(begin, used);
54 throw Exception("Couldn't flush all data");
57 else if(cur_op==M_READ)
61 bool Buffered::getline(std::string &line)
65 for(char *i=begin; i!=end; ++i)
68 line.assign(begin, i-begin);
73 return Base::getline(line);
81 return static_cast<unsigned char>(*begin++);
86 return static_cast<unsigned char>(c);
89 Handle Buffered::get_event_handle()
91 throw Exception("Buffered doesn't support events");
94 unsigned Buffered::get_current_size() const
111 void Buffered::set_op(Mode op)
118 unsigned Buffered::do_write(const char *data, unsigned size)
122 if(end+size<buf+buf_size)
124 // All data fits in buffer with whatever is already there
125 memcpy(end, data, size);
132 // Clear the buffer to make more room
137 // Put new data in the buffer to wait for more
138 memcpy(end, data, size);
144 // New data still doesn't fit in the buffer, so write it directly
145 return below.write(data, size);
149 unsigned Buffered::do_read(char *data, unsigned size)
155 // The request can be served from the buffer
156 memcpy(data, begin, size);
159 eof_flag=(below.eof() && begin==end);
165 // Give out whatever is in the buffer already
166 memcpy(data, begin, end-begin);
167 unsigned ret=end-begin;
175 // Fill the buffer and serve the rest of the request from it
176 unsigned len=below.read(end, buf+buf_size-end);
179 len=min(static_cast<unsigned>(end-begin), size);
180 memcpy(data, begin, len);
185 // Read the rest directly from the underlying object
186 ret+=below.read(data, size);
188 eof_flag=(below.eof() && begin==end);