KJsEmbed

kjseglobal.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2004, 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2004, 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2004, 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "kjseglobal.h"
24 
25 #ifdef QT_ONLY
26 # include <QObject>
27 # include <cstdio>
28 #endif
29 
30 #if defined(_WIN32) || defined(_WIN64)
31 # include <windows.h>
32 # include <fcntl.h>
33 # include <io.h>
34 # include <ios>
35 # include <QFile>
36 # include <QTextStream>
37 #endif
38 
39 static QTextStream *kjsembed_err = nullptr;
40 static QTextStream *kjsembed_in = nullptr;
41 static QTextStream *kjsembed_out = nullptr;
42 
43 #if defined(_WIN32) || defined(_WIN64)
44 static QFile win32_stdin;
45 static QFile win32_stdout;
46 static QFile win32_stderr;
47 
48 static const WORD MAX_CONSOLE_LINES = 500;
49 
50 void RedirectIOToConsole()
51 {
52  int hConHandle;
53  intptr_t lStdHandle;
54  CONSOLE_SCREEN_BUFFER_INFO coninfo;
55  AllocConsole();
56  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
57  coninfo.dwSize.Y = MAX_CONSOLE_LINES;
58  SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
59 
60  lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
61  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
62  win32_stdin.open(hConHandle, QIODevice::ReadOnly);
63 
64  lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
65  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
66  win32_stdout.open(hConHandle, QIODevice::WriteOnly);
67 
68  lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
69  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
70  win32_stderr.open(hConHandle, QIODevice::WriteOnly);
71 
72  std::ios::sync_with_stdio();
73 
74 }
75 
76 #endif
77 
78 QTextStream &consoleOut()
79 {
80  return *KJSEmbed::conout();
81 }
82 
83 QTextStream &consoleError()
84 {
85  return *KJSEmbed::conerr();
86 }
87 
88 QTextStream &consoleIn()
89 {
90  return *KJSEmbed::conin();
91 }
92 
93 #ifdef QT_ONLY
94 QTextStream &kdDebug(int area)
95 {
96 #ifndef QT_DEBUG
97  return consoleError() << "DEBUG: (" << area << ") ";
98 #else
99  return consoleOut();
100 #endif
101 
102 }
103 
104 QTextStream &kdWarning(int area)
105 {
106  return consoleOut() << "WARNING: (" << area << ") ";
107 }
108 
109 QString i18n(const char *string)
110 {
111  return QCoreApplication::translate("KJSEmbed", string, "qjsembed string");
112 }
113 
114 #endif
115 
116 QTextStream *KJSEmbed::conin()
117 {
118  if (!kjsembed_in) {
119 #ifdef _WIN32
120  kjsembed_in = new QTextStream(&win32_stdin);
121 #else
122  kjsembed_in = new QTextStream(stdin, QIODevice::ReadOnly);
123 #endif
124  }
125  return kjsembed_in;
126 }
127 
128 QTextStream *KJSEmbed::conout()
129 {
130  if (!kjsembed_out) {
131 #ifdef _WIN32
132  kjsembed_out = new QTextStream(&win32_stdout);
133 #else
134  kjsembed_out = new QTextStream(stdout, QIODevice::WriteOnly);
135 #endif
136  }
137  return kjsembed_out;
138 
139 }
140 
141 QTextStream *KJSEmbed::conerr()
142 {
143  if (!kjsembed_err) {
144 #ifdef _WIN32
145  kjsembed_err = new QTextStream(&win32_stderr);
146 #else
147  kjsembed_err = new QTextStream(stderr, QIODevice::WriteOnly);
148 #endif
149  }
150  return kjsembed_err;
151 }
152 
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
virtual bool open(QIODevice::OpenMode mode) override
QString i18n(const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.