-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-helpers.cpp
160 lines (145 loc) · 3.75 KB
/
console-helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Console Debugger Extension
* Copyright (C) 2018-2021 Peace-Maker All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "console-helpers.h"
#if defined KE_POSIX
#include <unistd.h>
#include <termios.h>
#include <string.h>
#endif
#if defined KE_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
// Convince debugging console to act more like an
// interactive input.
#if defined KE_POSIX
tcflag_t
GetTerminalLocalMode()
{
struct termios term;
tcgetattr(STDIN_FILENO, &term);
return term.c_lflag;
}
void
SetTerminalLocalMode(tcflag_t flag)
{
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag = flag;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
}
unsigned int
EnableTerminalEcho()
{
tcflag_t flags = GetTerminalLocalMode();
tcflag_t old_flags = flags;
flags |= (ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | IEXTEN);
SetTerminalLocalMode(flags);
return old_flags;
}
void
ResetTerminalEcho(unsigned int flag)
{
SetTerminalLocalMode(flag);
}
unsigned int
DisableEngineWatchdog()
{
return alarm(0);
}
void
ResetEngineWatchdog(unsigned int timeout)
{
alarm(timeout);
}
#elif defined KE_WINDOWS
unsigned int
EnableTerminalEcho()
{
DWORD mode, old_mode;
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hConsole, &mode);
old_mode = mode;
mode |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE);
SetConsoleMode(hConsole, mode);
return old_mode;
}
void
ResetTerminalEcho(unsigned int mode)
{
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hConsole, mode);
}
unsigned int
DisableEngineWatchdog()
{
return 0;
}
void
ResetEngineWatchdog(unsigned int timeout)
{
}
#endif
const char *
SkipPath(const char *str)
{
if (str == nullptr)
return nullptr;
const char *p1 = strrchr(str, '\\');
/* DOS/Windows pathnames */
if (p1 != nullptr)
p1++;
else
p1 = str;
if (p1 == str && p1[1] == ':')
p1 = str + 2;
/* Unix pathnames */
const char *p2 = strrchr(str, '/');
if (p2 != nullptr)
p2++;
else
p2 = str;
return p1>p2 ? p1 : p2;
}
std::string& leftTrim(std::string& str, std::string chars)
{
str.erase(0, str.find_first_not_of(chars));
return str;
}
std::string& rightTrim(std::string& str, std::string chars)
{
str.erase(str.find_last_not_of(chars) + 1);
return str;
}
std::string& trimString(std::string& str, std::string chars)
{
return leftTrim(rightTrim(str, chars), chars);
}