/
misc.c
135 lines (70 loc) · 1.56 KB
/
misc.c
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
//misc.c
#include <stdio.h>
#include "header.h"
#include <string.h>
#ifdef WIN32
#include "windows.h"
#else
#include <time.h>
#endif
int gettime()
{
#ifdef WIN32
return GetTickCount();
#else
struct timeval t;
gettimeofday(&t,NULL);
return t.tv_sec*1000 + t.tv_usec/1000;
#endif
}
// http://home.arcor.de/dreamlike/chess/
int InputWaiting()
{
#ifndef WIN32
fd_set readfds;
struct timeval tv;
FD_ZERO (&readfds);
FD_SET (fileno(stdin), &readfds);
tv.tv_sec=0; tv.tv_usec=0;
select(16, &readfds, 0, 0, &tv);
return (FD_ISSET(fileno(stdin), &readfds));
#else
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) return 1;
return dw;
} else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
#endif
}
void ReadInput(S_SEARCHINFO *info) {
int bytes;
char input[256] = "", *endc;
if (InputWaiting()) {
info->stopped = TRUE;
do {
bytes=read(fileno(stdin),input,256);
} while (bytes<0);
endc = strchr(input,'\n');
if (endc) *endc=0;
if (strlen(input) > 0) {
if (!strncmp(input, "quit", 4)) {
info->quit = TRUE;
}
}
return;
}
}