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 char *itoa(int num, char *str, int )
00045 {
00046 int k;
00047 char c, flag, *ostr;
00048
00049 if (num < 0) {
00050 num = -num;
00051 *str++ = '-';
00052 }
00053 k = 10000;
00054 ostr = str;
00055 flag = 0;
00056 while (k) {
00057 c = num / k;
00058 if (c || k == 1 || flag) {
00059 num %= k;
00060 c += '0';
00061 *str++ = c;
00062 flag = 1;
00063 }
00064 k /= 10;
00065 }
00066 *str = '\0';
00067 return ostr;
00068 }
00069
00070 #endif
00071
00072 #if defined(_WIN32) || defined(_WIN64)
00073 static QFile win32_stdin;
00074 static QFile win32_stdout;
00075 static QFile win32_stderr;
00076
00077 static const WORD MAX_CONSOLE_LINES = 500;
00078
00079 void RedirectIOToConsole() {
00080 int hConHandle;
00081 long lStdHandle;
00082 CONSOLE_SCREEN_BUFFER_INFO coninfo;
00083 AllocConsole();
00084 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
00085 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
00086 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
00087
00088 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
00089 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00090 win32_stdin.open(hConHandle,QIODevice::ReadOnly);
00091
00092 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
00093 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00094 win32_stdout.open(hConHandle,QIODevice::WriteOnly);
00095
00096 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
00097 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
00098 win32_stderr.open(hConHandle, QIODevice::WriteOnly);
00099
00100 std::ios::sync_with_stdio();
00101
00102 }
00103
00104
00105
00106 #endif
00107
00108
00109 QTextStream &consoleOut( )
00110 {
00111 return *KJSEmbed::conout();
00112 }
00113
00114 QTextStream &consoleError( )
00115 {
00116 return *KJSEmbed::conerr();
00117 }
00118
00119 QTextStream &consoleIn( )
00120 {
00121 return *KJSEmbed::conin();
00122 }
00123
00124 #ifdef QT_ONLY
00125 QTextStream &kdDebug( int area )
00126 {
00127 #ifndef QT_DEBUG
00128 return consoleError() << "DEBUG: (" << area << ") ";
00129 #else
00130 return consoleOut();
00131 #endif
00132
00133 }
00134
00135 QTextStream &kdWarning( int area )
00136 {
00137 return consoleOut() << "WARNING: (" << area << ") ";
00138 }
00139
00140 QString i18n( const char *string )
00141 {
00142 return QObject::tr( string, "qjsembed string");
00143 }
00144
00145 #endif
00146
00147 QTextStream *KJSEmbed::conin()
00148 {
00149 if ( !kjsembed_in ) {
00150 #ifdef _WIN32
00151 kjsembed_in = new QTextStream( &win32_stdin );
00152 #else
00153 kjsembed_in = new QTextStream( stdin, QIODevice::ReadOnly );
00154 #endif
00155 }
00156 return kjsembed_in;
00157 }
00158
00159 QTextStream *KJSEmbed::conout()
00160 {
00161 if ( !kjsembed_out ) {
00162 #ifdef _WIN32
00163 kjsembed_out = new QTextStream( &win32_stdout );
00164 #else
00165 kjsembed_out = new QTextStream( stdout, QIODevice::WriteOnly );
00166 #endif
00167 }
00168 return kjsembed_out;
00169
00170 }
00171
00172 QTextStream *KJSEmbed::conerr()
00173 {
00174 if ( !kjsembed_err ) {
00175 #ifdef _WIN32
00176 kjsembed_err = new QTextStream( &win32_stderr );
00177 #else
00178 kjsembed_err = new QTextStream( stderr, QIODevice::WriteOnly );
00179 #endif
00180 }
00181 return kjsembed_err;
00182 }
00183
00184