X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fbuffered.cpp;h=32688db83618d874652fd0926a8d556648e765d7;hp=4327413d2c3e136f34f952f70bf52ecd32cf6fe3;hb=ed9f0b874e7e09fc3b3b223585572a18854765f6;hpb=c8a3f4ae89bc415bfbd877b3f4c3605ac3cf4010 diff --git a/source/buffered.cpp b/source/buffered.cpp index 4327413..32688db 100644 --- a/source/buffered.cpp +++ b/source/buffered.cpp @@ -4,6 +4,8 @@ This file is part of libmspio Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + +#include #include "buffered.h" #include "except.h" @@ -15,21 +17,22 @@ namespace IO { Buffered::Buffered(Base &b, unsigned s): below(b), buf_size(s), - in_buf(new char[buf_size]), - in_ptr(in_buf), - in_avail(0), - out_buf(new char[buf_size]), - out_used(0) + buf(new char[buf_size]), + begin(buf), + end(buf), + cur_op(M_NONE) { mode=below.get_mode(); - below.signal_closing.connect(sigc::mem_fun(this, &Buffered::below_closing)); + below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush)); } unsigned Buffered::put(char c) { - if(out_used0) - { - --in_avail; - return *in_ptr++; - } + set_op(M_READ); + + if(begin(*begin++); char c; if(do_read(&c, 1)==0) @@ -84,6 +91,11 @@ Handle Buffered::get_event_handle() throw Exception("Buffered doesn't support events"); } +unsigned Buffered::get_current_size() const +{ + return end-begin; +} + Buffered::~Buffered() { try @@ -93,108 +105,87 @@ Buffered::~Buffered() catch(...) { } - delete[] in_buf; - delete[] out_buf; + delete[] buf; } -void Buffered::below_closing() +void Buffered::set_op(Mode op) { - flush(); + if(op!=cur_op) + flush(); + cur_op=op; } -unsigned Buffered::do_write(const char *buf, unsigned size) +unsigned Buffered::do_write(const char *data, unsigned size) { - if(out_used+size0) + if(size=buf_size) - ret+=below.read(buf, size); - else + if(size0) - { - in_avail=below.read(in_buf, buf_size); - if(in_avail==0) - { - eof_flag=true; - break; - } - - unsigned head=min(size, in_avail); - memcpy(buf, in_buf, head); - buf+=head; - size-=head; - - in_ptr=in_buf+head; - in_avail-=head; - ret+=head; - } + // Fill the buffer and serve the rest of the request from it + unsigned len=below.read(end, buf+buf_size-end); + end+=len; + + len=min(static_cast(end-begin), size); + memcpy(data, begin, len); + begin+=len; + ret+=len; } + else + // Read the rest directly from the underlying object + ret+=below.read(data, size); + + eof_flag=(below.eof() && begin==end); return ret; }