Merge branch 'feature/no_win_header' into develop

This commit is contained in:
Sebastian Messmer 2018-09-26 21:00:52 -07:00
commit c4b56e0d99
2 changed files with 78 additions and 45 deletions

View File

@ -1 +1,73 @@
#include "DontEchoStdinToStdoutRAII.h" #include "DontEchoStdinToStdoutRAII.h"
#if !defined(_MSC_VER)
#include <termios.h>
#include <unistd.h>
namespace cpputils {
namespace details {
class _DontEchoStdinToStdoutRAII final {
public:
_DontEchoStdinToStdoutRAII() : _old_state() {
tcgetattr(STDIN_FILENO, &_old_state);
termios new_state = _old_state;
new_state.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_state);
}
~_DontEchoStdinToStdoutRAII() {
tcsetattr(STDIN_FILENO, TCSANOW, &_old_state);
}
private:
termios _old_state;
DISALLOW_COPY_AND_ASSIGN(_DontEchoStdinToStdoutRAII);
};
}
}
#else
#include <windows.h>
namespace cpputils {
namespace details {
class _DontEchoStdinToStdoutRAII final {
public:
_DontEchoStdinToStdoutRAII() : _old_state() {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdin, &_old_state);
SetConsoleMode(hStdin, _old_state & (~ENABLE_ECHO_INPUT));
}
~_DontEchoStdinToStdoutRAII() {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hStdin, _old_state);
}
private:
DWORD _old_state;
DISALLOW_COPY_AND_ASSIGN(_DontEchoStdinToStdoutRAII);
};
}
}
#endif
using cpputils::make_unique_ref;
namespace cpputils {
DontEchoStdinToStdoutRAII::DontEchoStdinToStdoutRAII()
: raii(make_unique_ref<details::_DontEchoStdinToStdoutRAII>()) {}
DontEchoStdinToStdoutRAII::~DontEchoStdinToStdoutRAII() {}
}

View File

@ -2,7 +2,7 @@
#ifndef MESSMER_CPPUTILS_IO_DONTECHOSTDINTOSTDOUTRAII_H #ifndef MESSMER_CPPUTILS_IO_DONTECHOSTDINTOSTDOUTRAII_H
#define MESSMER_CPPUTILS_IO_DONTECHOSTDINTOSTDOUTRAII_H #define MESSMER_CPPUTILS_IO_DONTECHOSTDINTOSTDOUTRAII_H
#include <memory> #include <cpp-utils/pointer/unique_ref.h>
#include "../macros.h" #include "../macros.h"
/** /**
@ -11,55 +11,18 @@
* This can be very handy for password inputs where you don't want the password to be visible on screen. * This can be very handy for password inputs where you don't want the password to be visible on screen.
*/ */
#if !defined(_MSC_VER)
#include <termios.h>
#include <unistd.h>
namespace cpputils { namespace cpputils {
class DontEchoStdinToStdoutRAII final { namespace details {
public: class _DontEchoStdinToStdoutRAII;
DontEchoStdinToStdoutRAII() : _old_state() {
tcgetattr(STDIN_FILENO, &_old_state);
termios new_state = _old_state;
new_state.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_state);
}
~DontEchoStdinToStdoutRAII() {
tcsetattr(STDIN_FILENO, TCSANOW, &_old_state);
}
private:
termios _old_state;
DISALLOW_COPY_AND_ASSIGN(DontEchoStdinToStdoutRAII);
};
} }
#else
#include <windows.h>
namespace cpputils {
class DontEchoStdinToStdoutRAII final { class DontEchoStdinToStdoutRAII final {
public: public:
DontEchoStdinToStdoutRAII() : _old_state() { DontEchoStdinToStdoutRAII();
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); ~DontEchoStdinToStdoutRAII();
GetConsoleMode(hStdin, &_old_state);
SetConsoleMode(hStdin, _old_state & (~ENABLE_ECHO_INPUT));
}
~DontEchoStdinToStdoutRAII() {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hStdin, _old_state);
}
private: private:
DWORD _old_state; cpputils::unique_ref<details::_DontEchoStdinToStdoutRAII> raii;
DISALLOW_COPY_AND_ASSIGN(DontEchoStdinToStdoutRAII); DISALLOW_COPY_AND_ASSIGN(DontEchoStdinToStdoutRAII);
}; };
@ -67,5 +30,3 @@ private:
} }
#endif #endif
#endif