MCPcopy Create free account
hub / github.com/bcndev/bytecoin / getline

Method getline

src/common/ConsoleTools.cpp:94–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92 return 0;
93}
94bool UnicodeConsoleSetup::getline(std::string &line, bool hide_input) {
95// std::string test;
96// std::cout << "Enter test visible: " << std::flush;
97// if (!console_setup.getline(test)) {
98// return 0;
99// }
100// std::cout << "You entered {" << test << "} Enter test invisible: " << std::flush;
101// if (!console_setup.getline(test, true)) {
102// return 0;
103// }
104// std::cout << "You entered {" << test << "}" << std::flush;
105// return 1;
106#ifdef _WIN32
107 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
108 DWORD mode = 0;
109 if (hide_input) {
110 GetConsoleMode(hStdin, &mode);
111 SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
112 }
113 wchar_t wstr[4096]; // TODO - read in chunks?
114 unsigned long read = 0;
115 bool result = ReadConsoleW(hStdin, wstr, sizeof(wstr) / sizeof(*wstr), &read, NULL) != 0;
116 if (result) {
117 line = platform::FileStream::utf16_to_utf8(std::wstring(wstr, read));
118 if (!line.empty() && line.back() == '\n')
119 line.pop_back();
120 if (!line.empty() && line.back() == '\r')
121 line.pop_back();
122 } else {
123 char buf = 0;
124 line.clear();
125 while (ReadFile(hStdin, &buf, 1, &read, nullptr)) {
126 line.push_back(buf);
127 if (buf == '\n') {
128 result = true;
129 break;
130 }
131 }
132 if (!line.empty() && line.back() == '\n')
133 line.pop_back();
134 if (!line.empty() && line.back() == '\r')
135 line.pop_back();
136 }
137 if (hide_input)
138 SetConsoleMode(hStdin, mode);
139#else
140 termios oldt{};
141 if (hide_input) {
142 tcgetattr(STDIN_FILENO, &oldt);
143 termios newt = oldt;
144 newt.c_lflag &= ~ECHO;
145 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
146 }
147 bool result = !!std::getline(std::cin, line);
148 if (hide_input)
149 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
150#endif
151 return result;

Callers 1

getlineFunction · 0.80

Calls 3

getlineFunction · 0.85
emptyMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected