-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
153 lines (132 loc) · 3.26 KB
/
utils.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
#include "utils.h"
#include <algorithm>
static int run_as_daemon = 0;
#ifdef HAVE_TERMIOS_H
/* init terminal so that we can grab keys */
static struct termios oldtty;
static int restore_tty;
#endif
void term_exit(void)
{
//printf("term_exit");
#ifdef HAVE_TERMIOS_H
if(restore_tty)
tcsetattr (0, TCSANOW, &oldtty);
#endif
}
static void sigterm_handler(int sig)
{
term_exit();
}
void term_init(void)
{
setvbuf(stderr,NULL,_IONBF,0);
#ifdef HAVE_TERMIOS_H
if(!run_as_daemon){
struct termios tty;
int istty = 1;
#if HAVE_ISATTY
istty = isatty(0) && isatty(2);
#endif
if (istty && tcgetattr (0, &tty) == 0) {
oldtty = tty;
restore_tty = 1;
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag |= OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
tcsetattr (0, TCSANOW, &tty);
}
signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
}
signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
#endif
}
int read_key()
{
unsigned char ch;
#if HAVE_TERMIOS_H
int n = 1;
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
n = select(1, &rfds, NULL, NULL, &tv);
if (n > 0) {
n = read(0, &ch, 1);
if (n == 1)
return ch;
return n;
}
#elif HAVE_KBHIT
# if HAVE_PEEKNAMEDPIPE
static int is_pipe;
static HANDLE input_handle;
DWORD dw, nchars;
if(!input_handle){
input_handle = GetStdHandle(STD_INPUT_HANDLE);
is_pipe = !GetConsoleMode(input_handle, &dw);
}
if (is_pipe) {
/* When running under a GUI, you will end here. */
if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
// input pipe may have been closed by the program that ran ffmpeg
return -1;
}
//Read it
if(nchars != 0) {
read(0, &ch, 1);
return ch;
}else{
return -1;
}
}
# endif
if(kbhit())
return(getch());
#endif
return -1;
}
bool set_debug_privilege()
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken ))
return 0;
if(!LookupPrivilegeValueW(L"", L"SeDebugPrivilege", &luid))
return 0;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
return AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL );
}
std::string get_system_winodws_path()
{
std::string ret = "C:\\";
char str[MAX_PATH];
if (GetWindowsDirectoryA(str, MAX_PATH) > 0) {
ret = str;
ret += "\\";
}
return ret;
}
std::wstring lower_case(const std::wstring& text)
{
std::wstring ret = text;
std::transform(ret.begin(), ret.end(), ret.begin(), tolower);
return ret;
}
std::string lower_case(const std::string& text)
{
std::string ret = text;
std::transform(ret.begin(), ret.end(), ret.begin(), tolower);
return ret;
}