diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-17 09:10:14 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-22 07:33:38 -0400 |
| commit | ad77e315f5c53994d75056608df0f9ff0a390530 (patch) | |
| tree | 486ec4cc333301b62fbe89e72fcdbf2671f60178 /src/shared | |
| parent | bca6283311cf1dea4c96f8ee5bf192bdb1640cb3 (diff) | |
moved Console to util
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/util.cpp | 43 | ||||
| -rw-r--r-- | src/shared/util.h | 14 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/shared/util.cpp b/src/shared/util.cpp index 17df3b92..29e52f40 100644 --- a/src/shared/util.cpp +++ b/src/shared/util.cpp @@ -441,6 +441,49 @@ private: };
+Console::Console()
+ : m_hasConsole(false), m_in(nullptr), m_out(nullptr), m_err(nullptr)
+{
+ // open a console
+ if (!AllocConsole()) {
+ // failed, ignore
+ }
+
+ m_hasConsole = true;
+
+ // redirect stdin, stdout and stderr to it
+ freopen_s(&m_in, "CONIN$", "r", stdin);
+ freopen_s(&m_out, "CONOUT$", "w", stdout);
+ freopen_s(&m_err, "CONOUT$", "w", stderr);
+}
+
+Console::~Console()
+{
+ // close redirected handles and redirect standard stream to NUL in case
+ // they're used after this
+
+ if (m_err) {
+ std::fclose(m_err);
+ freopen_s(&m_err, "NUL", "w", stderr);
+ }
+
+ if (m_out) {
+ std::fclose(m_out);
+ freopen_s(&m_out, "NUL", "w", stdout);
+ }
+
+ if (m_in) {
+ std::fclose(m_in);
+ freopen_s(&m_in, "NUL", "r", stdin);
+ }
+
+ // close console
+ if (m_hasConsole) {
+ FreeConsole();
+ }
+}
+
+
Shortcut::Shortcut()
: m_iconIndex(0)
{
diff --git a/src/shared/util.h b/src/shared/util.h index c4a2ed7d..a5d096ac 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -54,6 +54,20 @@ bool CaseInsensitiveEqual(const std::wstring &lhs, const std::wstring &rhs); namespace env
{
+class Console
+{
+public:
+ Console();
+ ~Console();
+
+private:
+ bool m_hasConsole;
+ FILE* m_in;
+ FILE* m_out;
+ FILE* m_err;
+};
+
+
// an application shortcut that can be either on the desktop or the start menu
//
class Shortcut
|
