mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::getline to read strings from stdin
parent
f333372dd1
commit
3f18a8863a
|
@ -23,6 +23,7 @@
|
||||||
#include "../saveload/saveload.h"
|
#include "../saveload/saveload.h"
|
||||||
#include "../thread.h"
|
#include "../thread.h"
|
||||||
#include "../window_func.h"
|
#include "../window_func.h"
|
||||||
|
#include <iostream>
|
||||||
#include "dedicated_v.h"
|
#include "dedicated_v.h"
|
||||||
|
|
||||||
#if defined(UNIX)
|
#if defined(UNIX)
|
||||||
|
@ -50,19 +51,15 @@ static void DedicatedSignalHandler(int sig)
|
||||||
|
|
||||||
static HANDLE _hInputReady, _hWaitForInputHandling;
|
static HANDLE _hInputReady, _hWaitForInputHandling;
|
||||||
static HANDLE _hThread; // Thread to close
|
static HANDLE _hThread; // Thread to close
|
||||||
static char _win_console_thread_buffer[200];
|
static std::string _win_console_thread_buffer;
|
||||||
|
|
||||||
/* Windows Console thread. Just loop and signal when input has been received */
|
/* Windows Console thread. Just loop and signal when input has been received */
|
||||||
static void WINAPI CheckForConsoleInput()
|
static void WINAPI CheckForConsoleInput()
|
||||||
{
|
{
|
||||||
SetCurrentThreadName("ottd:win-console");
|
SetCurrentThreadName("ottd:win-console");
|
||||||
|
|
||||||
DWORD nb;
|
|
||||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, nullptr);
|
std::getline(std::cin, _win_console_thread_buffer);
|
||||||
if (nb >= lengthof(_win_console_thread_buffer)) nb = lengthof(_win_console_thread_buffer) - 1;
|
|
||||||
_win_console_thread_buffer[nb] = '\0';
|
|
||||||
|
|
||||||
/* Signal input waiting that input is read and wait for it being handled. */
|
/* Signal input waiting that input is read and wait for it being handled. */
|
||||||
SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
|
SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
|
||||||
|
@ -174,31 +171,23 @@ static bool InputWaiting()
|
||||||
|
|
||||||
static void DedicatedHandleKeyInput()
|
static void DedicatedHandleKeyInput()
|
||||||
{
|
{
|
||||||
static char input_line[1024] = "";
|
|
||||||
|
|
||||||
if (!InputWaiting()) return;
|
if (!InputWaiting()) return;
|
||||||
|
|
||||||
if (_exit_game) return;
|
if (_exit_game) return;
|
||||||
|
|
||||||
|
std::string input_line;
|
||||||
#if defined(UNIX)
|
#if defined(UNIX)
|
||||||
if (fgets(input_line, lengthof(input_line), stdin) == nullptr) return;
|
if (!std::getline(std::cin, input_line)) return;
|
||||||
#else
|
#else
|
||||||
/* Handle console input, and signal console thread, it can accept input again */
|
/* Handle console input, and signal console thread, it can accept input again */
|
||||||
static_assert(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
|
std::swap(input_line, _win_console_thread_buffer);
|
||||||
strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
|
|
||||||
SetEvent(_hWaitForInputHandling);
|
SetEvent(_hWaitForInputHandling);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Remove trailing \r or \n */
|
/* Remove any trailing \r or \n, and ensure the string is valid. */
|
||||||
for (char *c = input_line; *c != '\0'; c++) {
|
auto p = input_line.find_last_not_of("\r\n");
|
||||||
if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
|
if (p != std::string::npos) p++;
|
||||||
*c = '\0';
|
IConsoleCmdExec(StrMakeValid(input_line.substr(0, p)));
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StrMakeValidInPlace(input_line, lastof(input_line));
|
|
||||||
|
|
||||||
IConsoleCmdExec(input_line); // execute command
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoDriver_Dedicated::MainLoop()
|
void VideoDriver_Dedicated::MainLoop()
|
||||||
|
|
Loading…
Reference in New Issue