kjsembed
kjseglobal.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kjseglobal.h"
00024
00025 #ifdef QT_ONLY
00026 # include <QObject>
00027 # include <cstdio>
00028 #endif
00029
00030 #if defined(_WIN32) || defined(_WIN64)
00031 # include <windows.h>
00032 # include <fcntl.h>
00033 # include <io.h>
00034 # include <ios>
00035 # include <QFile>
00036 # include <QTextStream>
00037 #endif
00038
00039 static QTextStream *kjsembed_err = 0L;
00040 static QTextStream *kjsembed_in = 0L;
00041 static QTextStream *kjsembed_out = 0L;
00042
00043 #if defined(_WIN32) || defined(_WIN64)
00044 static QFile win32_stdin;
00045 static QFile win32_stdout;
00046 static QFile win32_stderr;
00047
00048 static const WORD MAX_CONSOLE_LINES = 500;
00049
00050 void RedirectIOToConsole() {
00051 int hConHandle;
00052 long lStdHandle;
00053 CONSOLE_SCREEN_BUFFER_INFO coninfo;
00054 AllocConsole();
00055 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
00056 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
00057 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
00058
00059 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
00060 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00061 win32_stdin.open(hConHandle,QIODevice::ReadOnly);
00062
00063 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
00064 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00065 win32_stdout.open(hConHandle,QIODevice::WriteOnly);
00066
00067 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
00068 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00069 win32_stderr.open(hConHandle, QIODevice::WriteOnly);
00070
00071 std::ios::sync_with_stdio();
00072
00073 }
00074
00075
00076
00077 #endif
00078
00079
00080 QTextStream &consoleOut( )
00081 {
00082 return *KJSEmbed::conout();
00083 }
00084
00085 QTextStream &consoleError( )
00086 {
00087 return *KJSEmbed::conerr();
00088 }
00089
00090 QTextStream &consoleIn( )
00091 {
00092 return *KJSEmbed::conin();
00093 }
00094
00095 #ifdef QT_ONLY
00096 QTextStream &kdDebug( int area )
00097 {
00098 #ifndef QT_DEBUG
00099 return consoleError() << "DEBUG: (" << area << ") ";
00100 #else
00101 return consoleOut();
00102 #endif
00103
00104 }
00105
00106 QTextStream &kdWarning( int area )
00107 {
00108 return consoleOut() << "WARNING: (" << area << ") ";
00109 }
00110
00111 QString i18n( const char *string )
00112 {
00113 return QObject::tr( string, "qjsembed string");
00114 }
00115
00116 #endif
00117
00118 QTextStream *KJSEmbed::conin()
00119 {
00120 if ( !kjsembed_in ) {
00121 #ifdef _WIN32
00122 kjsembed_in = new QTextStream( &win32_stdin );
00123 #else
00124 kjsembed_in = new QTextStream( stdin, QIODevice::ReadOnly );
00125 #endif
00126 }
00127 return kjsembed_in;
00128 }
00129
00130 QTextStream *KJSEmbed::conout()
00131 {
00132 if ( !kjsembed_out ) {
00133 #ifdef _WIN32
00134 kjsembed_out = new QTextStream( &win32_stdout );
00135 #else
00136 kjsembed_out = new QTextStream( stdout, QIODevice::WriteOnly );
00137 #endif
00138 }
00139 return kjsembed_out;
00140
00141 }
00142
00143 QTextStream *KJSEmbed::conerr()
00144 {
00145 if ( !kjsembed_err ) {
00146 #ifdef _WIN32
00147 kjsembed_err = new QTextStream( &win32_stderr );
00148 #else
00149 kjsembed_err = new QTextStream( stderr, QIODevice::WriteOnly );
00150 #endif
00151 }
00152 return kjsembed_err;
00153 }
00154
00155