-/* $Id$
-
-This file is part of libmspio
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#include <cstring>
#include "buffered.h"
#include "except.h"
below.signal_flush_required.connect(sigc::mem_fun(this, &Buffered::flush));
}
-unsigned Buffered::put(char c)
+Buffered::~Buffered()
{
- set_op(M_WRITE);
-
- if(end<buf+buf_size)
+ try
{
- *end++ = c;
- return 1;
+ flush();
}
- else
- return do_write(&c, 1);
+ catch(...)
+ { }
+
+ delete[] buf;
}
void Buffered::flush()
begin=end = buf;
}
-bool Buffered::getline(std::string &line)
-{
- set_op(M_READ);
-
- for(char *i=begin; i!=end; ++i)
- if(*i=='\n')
- {
- line.assign(begin, i-begin);
- begin = i+1;
- return true;
- }
-
- return Base::getline(line);
-}
-
-int Buffered::get()
-{
- set_op(M_READ);
-
- if(begin<end)
- return static_cast<unsigned char>(*begin++);
-
- char c;
- if(do_read(&c, 1)==0)
- return -1;
- return static_cast<unsigned char>(c);
-}
-
-Handle Buffered::get_event_handle()
-{
- throw Exception("Buffered doesn't support events");
-}
-
-unsigned Buffered::get_current_size() const
-{
- return end-begin;
-}
-
-Buffered::~Buffered()
-{
- try
- {
- flush();
- }
- catch(...)
- { }
-
- delete[] buf;
-}
-
-void Buffered::set_op(Mode op)
-{
- if(op!=cur_op)
- flush();
- cur_op = op;
-}
-
unsigned Buffered::do_write(const char *data, unsigned size)
{
set_op(M_WRITE);
}
}
+unsigned Buffered::put(char c)
+{
+ set_op(M_WRITE);
+
+ if(end<buf+buf_size)
+ {
+ *end++ = c;
+ return 1;
+ }
+ else
+ return do_write(&c, 1);
+}
+
+bool Buffered::getline(std::string &line)
+{
+ set_op(M_READ);
+
+ for(char *i=begin; i!=end; ++i)
+ if(*i=='\n')
+ {
+ line.assign(begin, i-begin);
+ begin = i+1;
+ return true;
+ }
+
+ return Base::getline(line);
+}
+
+int Buffered::get()
+{
+ set_op(M_READ);
+
+ if(begin<end)
+ return static_cast<unsigned char>(*begin++);
+
+ char c;
+ if(do_read(&c, 1)==0)
+ return -1;
+ return static_cast<unsigned char>(c);
+}
+
+void Buffered::set_op(Mode op)
+{
+ if(op!=cur_op)
+ flush();
+ cur_op = op;
+}
+
+unsigned Buffered::get_current_size() const
+{
+ return end-begin;
+}
+
+Handle Buffered::get_event_handle()
+{
+ throw Exception("Buffered doesn't support events");
+}
+
} // namespace IO
} // namespace Msp