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 *kFirstQuestionMessage = "? ";
18static 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
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 = g_StdIn.ScanStringUntilNewLine();
35    scannedString.Trim();
36    if (!scannedString.IsEmpty())
37      switch (::MyCharLower_Ascii(scannedString[0]))
38      {
39        case kYes:    return NUserAnswerMode::kYes;
40        case kNo:     return NUserAnswerMode::kNo;
41        case kYesAll: return NUserAnswerMode::kYesAll;
42        case kNoAll:  return NUserAnswerMode::kNoAll;
43        case kAutoRenameAll: return NUserAnswerMode::kAutoRenameAll;
44        case kQuit:   return NUserAnswerMode::kQuit;
45      }
46  }
47}
48
49#ifdef _WIN32
50#ifndef UNDER_CE
51#define MY_DISABLE_ECHO
52#endif
53#endif
54
55UString GetPassword(CStdOutStream *outStream)
56{
57  if (outStream)
58  {
59    *outStream << "\nEnter password"
60      #ifdef MY_DISABLE_ECHO
61      " (will not be echoed)"
62      #endif
63      ":";
64    outStream->Flush();
65  }
66
67  #ifdef MY_DISABLE_ECHO
68
69  HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
70  bool wasChanged = false;
71  DWORD mode = 0;
72  if (console != INVALID_HANDLE_VALUE && console != 0)
73    if (GetConsoleMode(console, &mode))
74      wasChanged = (SetConsoleMode(console, mode & ~ENABLE_ECHO_INPUT) != 0);
75  UString res = g_StdIn.ScanUStringUntilNewLine();
76  if (wasChanged)
77    SetConsoleMode(console, mode);
78  if (outStream)
79  {
80    *outStream << endl;
81    outStream->Flush();
82  }
83  return res;
84
85  #else
86
87  return g_StdIn.ScanUStringUntilNewLine();
88
89  #endif
90}
91