From 9eea07fb66e0b64033fa814edab78c433b1e217a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 16 Jul 2011 00:15:20 +0200 Subject: [PATCH] The console is now awesome when you use C++ streams too. --- library/Console-linux.cpp | 19 ++++++++++++++----- library/Console-windows.cpp | 26 ++++++++++++++++++-------- plugins/kittens.cpp | 2 +- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp index 81ebf7242..acbf5b119 100644 --- a/library/Console-linux.cpp +++ b/library/Console-linux.cpp @@ -122,17 +122,28 @@ const char * getANSIColor(const int c) namespace DFHack { - class Private + class Private : public std::stringbuf { public: Private() { dfout_C = NULL; - stream_o = NULL; rawmode = false; supported_terminal = false; state = con_unclaimed; }; + virtual ~Private() + { + //sync(); + } + protected: + int sync() + { + print(str().c_str()); + str(std::string()); // Clear the string buffer + return 0; + } + public: /// Print a formatted string, like printf int print(const char * format, ...) { @@ -563,7 +574,6 @@ namespace DFHack return raw_buffer.size(); } FILE * dfout_C; - duthomhas::stdiobuf * stream_o; std::deque history; bool supported_terminal; // state variables @@ -598,9 +608,8 @@ bool Console::init(void) d = new Private(); // make our own weird streams so our IO isn't redirected d->dfout_C = fopen("/dev/tty", "w"); - d->stream_o = new duthomhas::stdiobuf(d->dfout_C); d->wlock = SDL_CreateMutex(); - rdbuf(d->stream_o); + rdbuf(d); std::cin.tie(this); clear(); d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO); diff --git a/library/Console-windows.cpp b/library/Console-windows.cpp index af6af6b25..e0e5310f3 100644 --- a/library/Console-windows.cpp +++ b/library/Console-windows.cpp @@ -64,13 +64,12 @@ using namespace DFHack; namespace DFHack { - class Private + class Private : public std::stringbuf { public: - Private() + Private() : basic_stringbuf::basic_stringbuf() { dfout_C = 0; - stream_o = 0; rawmode = 0; console_in = 0; console_out = 0; @@ -79,7 +78,20 @@ namespace DFHack state = con_unclaimed; raw_cursor = 0; }; - /// Print a formatted string, like printf + virtual ~Private() + { + //sync(); + } + protected: + int sync() + { + print (str().c_str()); + // Clear the string buffer + str(std::string()); + return 0; + } + public: + /// Print a formatted string, like printf int print(const char * format, ...) { va_list args; @@ -371,7 +383,6 @@ namespace DFHack } FILE * dfout_C; - duthomhas::stdiobuf * stream_o; int rawmode; /* for atexit() function to check if restore is needed*/ std::deque history; @@ -415,6 +426,7 @@ bool Console::init(void) // Allocate a console! AllocConsole(); d->ConsoleWindow = GetConsoleWindow(); + d->wlock = SDL_CreateMutex(); HMENU hm = GetSystemMenu(d->ConsoleWindow,false); DeleteMenu(hm, SC_CLOSE, MF_BYCOMMAND); @@ -444,10 +456,8 @@ bool Console::init(void) std::ios::sync_with_stdio(); // make our own weird streams so our IO isn't redirected - d->stream_o = new duthomhas::stdiobuf(d->dfout_C); - rdbuf(d->stream_o); + rdbuf(d); std::cin.tie(this); - d->wlock = SDL_CreateMutex(); clear(); return true; } diff --git a/plugins/kittens.cpp b/plugins/kittens.cpp index c6c4728a7..1b096065e 100644 --- a/plugins/kittens.cpp +++ b/plugins/kittens.cpp @@ -129,7 +129,7 @@ DFhackCExport command_result kittens (Core * c, vector & parameters) while (kit != 0) { con.gotoxy(1,1+index); - con << kit; + con << kit << std::endl; index++; kit = nyan[index]; }