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