]> git.tdb.fi Git - libs/core.git/blobdiff - source/buffered.cpp
Add a missing include
[libs/core.git] / source / buffered.cpp
index 4cd5cb4d0eed742c81357cedd634895793dae7f2..32688db83618d874652fd0926a8d556648e765d7 100644 (file)
@@ -4,8 +4,10 @@ This file is part of libmspio
 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
+
+#include <cstring>
 #include "buffered.h"
-#include "error.h"
+#include "except.h"
 
 using namespace std;
 
@@ -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_used<buf_size)
+       set_op(M_WRITE);
+
+       if(end<buf+buf_size)
        {
-               out_buf[out_used++]=c;
+               *end++=c;
                return 1;
        }
        else
@@ -38,27 +41,32 @@ unsigned Buffered::put(char c)
 
 void Buffered::flush()
 {
-       if(out_used==0)
-               return;
-
-       unsigned len=below.write(out_buf, out_used);
-       if(len<out_used)
+       if(cur_op==M_WRITE)
        {
-               memmove(out_buf, out_buf+len, out_used-len);
-               out_used-=len;
-               throw Exception("Couldn't flush all data");
+               unsigned used=end-begin;
+               if(used)
+               {
+                       unsigned len=below.write(begin, used);
+
+                       begin=end=buf;
+
+                       if(len<used)
+                               throw Exception("Couldn't flush all data");
+               }
        }
-       out_used=0;
+       else if(cur_op==M_READ)
+               begin=end=buf;
 }
 
 bool Buffered::getline(std::string &line)
 {
-       for(unsigned i=0; i<in_avail; ++i)
-               if(in_ptr[i]=='\n')
+       set_op(M_READ);
+
+       for(char *i=begin; i!=end; ++i)
+               if(*i=='\n')
                {
-                       line.assign(in_ptr, i);
-                       in_ptr+=i+1;
-                       in_avail-=i+1;
+                       line.assign(begin, i-begin);
+                       begin=i+1;
                        return true;
                }
 
@@ -67,11 +75,10 @@ bool Buffered::getline(std::string &line)
 
 int Buffered::get()
 {
-       if(in_avail>0)
-       {
-               --in_avail;
-               return *in_ptr++;
-       }
+       set_op(M_READ);
+
+       if(begin<end)
+               return static_cast<unsigned char>(*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+size<buf_size)
+       set_op(M_WRITE);
+
+       if(end+size<buf+buf_size)
        {
-               // All data fits in the buffer
-               memcpy(out_buf+out_used, buf, size);
-               out_used+=size;
+               // All data fits in buffer with whatever is already there
+               memcpy(end, data, size);
+               end+=size;
 
                return size;
        }
        else
        {
-               int ret=0;
-               bool ok=true;
+               // Clear the buffer to make more room
+               flush();
 
-               while(size>0)
+               if(size<buf_size)
                {
-                       // XXX sub-obtimal - should try to write directly from input first
-                       // Fill the buffer and pass it on
-                       unsigned head=min(buf_size-out_used, size);
-
-                       memcpy(out_buf+out_used, buf, head);
-                       out_used+=head;
-
-                       buf+=head;
-                       size-=head;
-                       ret+=head;
+                       // Put new data in the buffer to wait for more
+                       memcpy(end, data, size);
+                       end+=size;
 
-                       if(!ok) break;
-
-                       unsigned len=below.write(out_buf, out_used);
-
-                       ok=(len==out_used);
-                       if(ok)
-                               out_used=0;
-                       else
-                       {
-                               memmove(out_buf, out_buf+len, buf_size-len);
-                               out_used=buf_size-len;
-                       }
+                       return size;
                }
-
-               return ret;
+               else
+                       // New data still doesn't fit in the buffer, so write it directly
+                       return below.write(data, size);
        }
 }
 
-unsigned Buffered::do_read(char *buf, unsigned size)
+unsigned Buffered::do_read(char *data, unsigned size)
 {
-       if(size<=in_avail)
+       set_op(M_READ);
+
+       if(begin+size<=end)
        {
                // The request can be served from the buffer
-               memcpy(buf, in_ptr, size);
-               in_ptr+=size;
-               in_avail-=size;
+               memcpy(data, begin, size);
+               begin+=size;
+
+               eof_flag=(below.eof() && begin==end);
 
                return size;
        }
        else
        {
-               // Use whatever is left in the buffer
-               memcpy(buf, in_ptr, in_avail);
-
-               buf+=in_avail;
-               size-=in_avail;
-               int ret=in_avail;
+               // Give out whatever is in the buffer already
+               memcpy(data, begin, end-begin);
+               unsigned ret=end-begin;
+               begin=end=buf;
 
-               in_ptr=in_buf;
-               in_avail=0;
+               data+=ret;
+               size-=ret;
 
-               if(size>=buf_size)
-                       ret+=below.read(buf, size);
-               else
+               if(size<buf_size)
                {
-                       // Read more data into the buffer
-                       while(size>0)
-                       {
-                               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<unsigned>(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;
        }