Since window is singleton anyway, get rid of window* parameters
[lsnes.git] / window.cpp
blob9f76a28f0119552d8c3bac4e2bcdde80dfcaf56c
1 #include "window.hpp"
2 #include <boost/iostreams/categories.hpp>
3 #include <boost/iostreams/copy.hpp>
4 #include <boost/iostreams/stream.hpp>
5 #include <boost/iostreams/stream_buffer.hpp>
6 #include <boost/iostreams/filter/symmetric.hpp>
7 #include <boost/iostreams/filter/zlib.hpp>
8 #include <boost/iostreams/filtering_stream.hpp>
9 #include <boost/iostreams/device/back_inserter.hpp>
11 namespace
13 class window_output
15 public:
16 typedef char char_type;
17 typedef boost::iostreams::sink_tag category;
18 window_output(window* win)
22 void close()
26 std::streamsize write(const char* s, std::streamsize n)
28 size_t oldsize = stream.size();
29 stream.resize(oldsize + n);
30 memcpy(&stream[oldsize], s, n);
31 while(true) {
32 size_t lf = stream.size();
33 for(size_t i = 0; i < stream.size(); i++)
34 if(stream[i] == '\n') {
35 lf = i;
36 break;
38 if(lf == stream.size())
39 break;
40 std::string foo(stream.begin(), stream.begin() + lf);
41 window::message(foo);
42 if(lf + 1 < stream.size())
43 memmove(&stream[0], &stream[lf + 1], stream.size() - lf - 1);
44 stream.resize(stream.size() - lf - 1);
46 return n;
48 protected:
49 std::vector<char> stream;
53 std::ostream& window::out() throw(std::bad_alloc)
55 static std::ostream* cached = NULL;
56 window* win = NULL;
57 if(!cached)
58 cached = new boost::iostreams::stream<window_output>(win);
59 return *cached;