Make DontEchoStdinToStdoutRAII work on windows
This commit is contained in:
parent
22f729bcab
commit
e5d8bf82c3
@ -2,19 +2,22 @@
|
|||||||
#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 <iostream>
|
#include <memory>
|
||||||
#include <string>
|
|
||||||
#include <termios.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "../macros.h"
|
#include "../macros.h"
|
||||||
|
|
||||||
namespace cpputils {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If you create an instance of this class in your scope, then any user input from stdin
|
* If you create an instance of this class in your scope, then any user input from stdin
|
||||||
* won't be echoed back to stdout until the instance leaves the scope.
|
* won't be echoed back to stdout until the instance leaves the scope.
|
||||||
* 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 {
|
||||||
|
|
||||||
class DontEchoStdinToStdoutRAII final {
|
class DontEchoStdinToStdoutRAII final {
|
||||||
public:
|
public:
|
||||||
DontEchoStdinToStdoutRAII() : _old_state() {
|
DontEchoStdinToStdoutRAII() : _old_state() {
|
||||||
@ -36,4 +39,33 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
namespace cpputils {
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user