Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / src / vfs / console.cpp
blobf178866c575ee223c06c697264e2cbc9493eb838
1 //
2 // Copyright (C) 2008 by Martin Moracek
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /**
20 * @file console.cpp
22 * Ble.
25 #include <string.h>
26 #include <stdarg.h>
28 #include <string>
29 #include <istream>
30 #include <streambuf>
31 #include <deque>
33 #include "vfs/console.h"
34 #include "vfs/vario.h"
36 namespace tre {
38 bool ConsoleStream::ready = false;
40 // write one character
41 int ConsoleBuffer::overflow(int c)
43 if(c != EOF) {
44 if(c != '\n') {
45 sq_.front().push_back(c);
46 } else {
47 sq_.push_front(std::string());
48 sq_.pop_back();
51 return c;
54 // write multiple characters
55 std::streamsize ConsoleBuffer::xsputn(const char * s, std::streamsize num)
57 const char * sptr = s, * eptr;
59 // split on newlines
60 while((eptr = strchr(sptr, '\n'))) {
61 sq_.push_front(std::string(sptr, eptr - sptr));
62 sq_.pop_back();
63 sptr = eptr + 1;
65 sq_.push_front(std::string(sptr));
66 sq_.pop_back();
67 return strlen(s);
70 ConsoleStream::ConsoleStream(uint lines/*=defConsoleLines*/)
71 : std::iostream(&buf_), buf_(lines)
73 ready = true;
76 ConsoleStream::~ConsoleStream()
78 ready = false;
81 int ConsoleStream::format(const char * fmt, ...)
83 if(!rdbuf())
84 return -1;
86 va_list l_va;
87 va_start(l_va, fmt);
89 int t = StreamPrintf(*this, fmt, l_va);
91 va_end(l_va);
92 return t;