| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2004 by Richard Moore ([email protected] * |
| 3 | * Copyright (C) 2007 by Olaf Freyer * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU Library General Public License as * |
| 7 | * published by the Free Software Foundation; either version 2 of the * |
| 8 | * License, or (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU Library General Public * |
| 16 | * License along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "common/setup_before.h" |
| 22 | |
| 23 | #ifdef WIN32 |
| 24 | |
| 25 | #include "console_output.h" |
| 26 | #include <windows.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <io.h> |
| 29 | #include <iostream> |
| 30 | #include <fstream> |
| 31 | #include "common/setup_after.h" |
| 32 | |
| 33 | namespace pvpgn |
| 34 | { |
| 35 | |
| 36 | Console::Console(){ |
| 37 | initialised = false; |
| 38 | } |
| 39 | |
| 40 | Console::~Console() throw () { |
| 41 | if (initialised) |
| 42 | FreeConsole(); |
| 43 | } |
| 44 | |
| 45 | void Console::RedirectIOToConsole() { |
| 46 | const WORD MAX_CONSOLE_LINES = 500; |
| 47 | const WORD CONSOLE_COLUMNS = 140; |
| 48 | int hConHandle; |
| 49 | long lStdHandle; |
| 50 | std::FILE *fp; |
| 51 | CONSOLE_SCREEN_BUFFER_INFO coninfo; |
| 52 | |
| 53 | if (initialised) |
| 54 | return; |
| 55 | |
| 56 | AllocConsole(); |
| 57 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); |
| 58 | coninfo.dwSize.X = CONSOLE_COLUMNS; |
| 59 | coninfo.dwSize.Y = MAX_CONSOLE_LINES; |
| 60 | SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); |
| 61 | |
| 62 | lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); |
| 63 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); |
| 64 | fp = _fdopen(hConHandle, "r"); |
| 65 | *stdin = *fp; |
| 66 | setvbuf(stdin, NULL, _IONBF, 0);; |
| 67 | |
| 68 | lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); |
| 69 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); |
| 70 | fp = _fdopen(hConHandle, "w"); |
| 71 | *stdout = *fp; |
| 72 | setvbuf(stdout, NULL, _IONBF, 0); |
| 73 | |
| 74 | lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); |
| 75 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); |
| 76 | fp = _fdopen(hConHandle, "w"); |
| 77 | *stderr = *fp; |
| 78 | setvbuf(stderr, NULL, _IONBF, 0); |
| 79 | |
| 80 | std::ios::sync_with_stdio(); |
| 81 | initialised = true; |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | #endif |