Home | History | Annotate | Download | only in Console
      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 
     10 static const char kYes = 'Y';
     11 static const char kNo = 'N';
     12 static const char kYesAll = 'A';
     13 static const char kNoAll = 'S';
     14 static const char kAutoRenameAll = 'U';
     15 static const char kQuit = 'Q';
     16 
     17 static const char *kFirstQuestionMessage = "?\n";
     18 static const char *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 
     23 NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream)
     24 {
     25   (*outStream) << kFirstQuestionMessage;
     26   for (;;)
     27   {
     28     (*outStream) << kHelpQuestionMessage;
     29     outStream->Flush();
     30     AString scannedString = g_StdIn.ScanStringUntilNewLine();
     31     scannedString.Trim();
     32     if (!scannedString.IsEmpty())
     33       switch(
     34         ::MyCharUpper(
     35         #ifdef UNDER_CE
     36         (wchar_t)
     37         #endif
     38         scannedString[0]))
     39       {
     40         case kYes:
     41           return NUserAnswerMode::kYes;
     42         case kNo:
     43           return NUserAnswerMode::kNo;
     44         case kYesAll:
     45           return NUserAnswerMode::kYesAll;
     46         case kNoAll:
     47           return NUserAnswerMode::kNoAll;
     48         case kAutoRenameAll:
     49           return NUserAnswerMode::kAutoRenameAll;
     50         case kQuit:
     51           return NUserAnswerMode::kQuit;
     52       }
     53   }
     54 }
     55 
     56 #ifdef _WIN32
     57 #ifndef UNDER_CE
     58 #define MY_DISABLE_ECHO
     59 #endif
     60 #endif
     61 
     62 UString GetPassword(CStdOutStream *outStream)
     63 {
     64   (*outStream) << "\nEnter password"
     65       #ifdef MY_DISABLE_ECHO
     66       " (will not be echoed)"
     67       #endif
     68       ":";
     69   outStream->Flush();
     70 
     71   #ifdef MY_DISABLE_ECHO
     72   HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
     73   bool wasChanged = false;
     74   DWORD mode = 0;
     75   if (console != INVALID_HANDLE_VALUE && console != 0)
     76     if (GetConsoleMode(console, &mode))
     77       wasChanged = (SetConsoleMode(console, mode & ~ENABLE_ECHO_INPUT) != 0);
     78   UString res = g_StdIn.ScanUStringUntilNewLine();
     79   if (wasChanged)
     80     SetConsoleMode(console, mode);
     81   (*outStream) << "\n";
     82   outStream->Flush();
     83   return res;
     84   #else
     85   return g_StdIn.ScanUStringUntilNewLine();
     86   #endif
     87 }
     88