1// UserInputUtils.cpp 2 3#include "StdAfx.h" 4 5#include "../../../Common/StdInStream.h" 6#include "../../../Common/StringConvert.h" 7 8#include "UserInputUtils.h" 9 10static const char kYes = 'y'; 11static const char kNo = 'n'; 12static const char kYesAll = 'a'; 13static const char kNoAll = 's'; 14static const char kAutoRenameAll = 'u'; 15static const char kQuit = 'q'; 16 17static const char * const kFirstQuestionMessage = "? "; 18static const char * const kHelpQuestionMessage = 19 "(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? "; 20 21// return true if pressed Quite; 22 23NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream) 24{ 25 if (outStream) 26 *outStream << kFirstQuestionMessage; 27 for (;;) 28 { 29 if (outStream) 30 { 31 *outStream << kHelpQuestionMessage; 32 outStream->Flush(); 33 } 34 AString scannedString; 35 if (!g_StdIn.ScanAStringUntilNewLine(scannedString)) 36 return NUserAnswerMode::kError; 37 if (g_StdIn.Error()) 38 return NUserAnswerMode::kError; 39 scannedString.Trim(); 40 if (scannedString.IsEmpty() && g_StdIn.Eof()) 41 return NUserAnswerMode::kEof; 42 43 if (scannedString.Len() == 1) 44 switch (::MyCharLower_Ascii(scannedString[0])) 45 { 46 case kYes: return NUserAnswerMode::kYes; 47 case kNo: return NUserAnswerMode::kNo; 48 case kYesAll: return NUserAnswerMode::kYesAll; 49 case kNoAll: return NUserAnswerMode::kNoAll; 50 case kAutoRenameAll: return NUserAnswerMode::kAutoRenameAll; 51 case kQuit: return NUserAnswerMode::kQuit; 52 } 53 } 54} 55 56#ifdef _WIN32 57#ifndef UNDER_CE 58#define MY_DISABLE_ECHO 59#endif 60#endif 61 62static bool GetPassword(CStdOutStream *outStream, UString &psw) 63{ 64 if (outStream) 65 { 66 *outStream << "\nEnter password" 67 #ifdef MY_DISABLE_ECHO 68 " (will not be echoed)" 69 #endif 70 ":"; 71 outStream->Flush(); 72 } 73 74 #ifdef MY_DISABLE_ECHO 75 76 const HANDLE console = GetStdHandle(STD_INPUT_HANDLE); 77 78 /* 79 GetStdHandle() returns 80 INVALID_HANDLE_VALUE: If the function fails. 81 NULL : If an application does not have associated standard handles, 82 such as a service running on an interactive desktop, 83 and has not redirected them. */ 84 bool wasChanged = false; 85 DWORD mode = 0; 86 if (console != INVALID_HANDLE_VALUE && console != NULL) 87 if (GetConsoleMode(console, &mode)) 88 wasChanged = (SetConsoleMode(console, mode & ~(DWORD)ENABLE_ECHO_INPUT) != 0); 89 const bool res = g_StdIn.ScanUStringUntilNewLine(psw); 90 if (wasChanged) 91 SetConsoleMode(console, mode); 92 93 #else 94 95 const bool res = g_StdIn.ScanUStringUntilNewLine(psw); 96 97 #endif 98 99 if (outStream) 100 { 101 *outStream << endl; 102 outStream->Flush(); 103 } 104 105 return res; 106} 107 108HRESULT GetPassword_HRESULT(CStdOutStream *outStream, UString &psw) 109{ 110 if (!GetPassword(outStream, psw)) 111 return E_INVALIDARG; 112 if (g_StdIn.Error()) 113 return E_FAIL; 114 if (g_StdIn.Eof() && psw.IsEmpty()) 115 return E_ABORT; 116 return S_OK; 117} 118