]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Finally remove UCIInputParser class altogether
[stockfish] / src / misc.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9   
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14   
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #if !defined(_MSC_VER)
25
26 #  include <sys/time.h>
27 #  include <sys/types.h>
28 #  include <unistd.h>
29
30 #else
31
32 #  include <windows.h>
33 #  include <time.h>
34 #  include "dos.h"
35 int gettimeofday(struct timeval * tp, struct timezone * tzp);
36
37 #endif
38
39 #include <cstdio>
40 #include <iomanip>
41 #include <sstream>
42
43 #include "misc.h"
44
45
46 //// 
47 //// Functions
48 ////
49
50 /// engine_name() returns the full name of the current Glaurung version.
51 /// This will be either "Glaurung YYMMDD" (where YYMMDD is the date when the
52 /// program was compiled) or "Glaurung <version number>", depending on whether
53 /// the constant EngineVersion (defined in misc.h) is empty.
54
55 const std::string engine_name() {
56   if(EngineVersion == "") {
57     static const char monthNames[12][4] = {
58       "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
59     };
60     const char *dateString = __DATE__;
61     std::stringstream s;
62     int month = 0, day = 0;
63     
64     for(int i = 0; i < 12; i++)
65       if(strncmp(dateString, monthNames[i], 3) == 0)
66         month = i + 1;
67     day = atoi(dateString+4);
68     
69     s << "Glaurung " << (dateString+9) << std::setfill('0') << std::setw(2)
70       << month << std::setfill('0') << std::setw(2) << day;
71     
72     return s.str();
73   }
74   else
75     return "Glaurung " + EngineVersion;
76 }
77
78
79 /// get_system_time() returns the current system time, measured in
80 /// milliseconds.
81
82 int get_system_time() {
83   struct timeval t;
84   gettimeofday(&t, NULL);
85   return t.tv_sec*1000 + t.tv_usec/1000; 
86 }
87
88
89 /// cpu_count() tries to detect the number of CPU cores.
90
91 #if !defined(_MSC_VER)
92
93 #  if defined(_SC_NPROCESSORS_ONLN)
94 int cpu_count() {
95   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
96 }
97 #  else
98 int cpu_count() {
99   return 1;
100 }
101 #  endif
102
103 #else
104
105 int cpu_count() {
106   SYSTEM_INFO s;
107   GetSystemInfo(&s);
108   return Min(s.dwNumberOfProcessors, 8);
109 }
110
111 #endif
112
113
114 /*
115   From Beowulf, from Olithink
116 */
117 #ifndef _WIN32
118 /* Non-windows version */
119 int Bioskey()
120 {
121   fd_set          readfds;
122   struct timeval  timeout;
123   
124   FD_ZERO(&readfds);
125   FD_SET(fileno(stdin), &readfds);
126   /* Set to timeout immediately */
127   timeout.tv_sec = 0;
128   timeout.tv_usec = 0;
129   select(16, &readfds, 0, 0, &timeout);
130   
131   return (FD_ISSET(fileno(stdin), &readfds));
132 }
133
134 #else
135 /* Windows-version */
136 #include <windows.h>
137 #include <conio.h>
138 int Bioskey()
139 {
140     static int      init = 0,
141                     pipe;
142     static HANDLE   inh;
143     DWORD           dw;
144     /* If we're running under XBoard then we can't use _kbhit() as the input
145      * commands are sent to us directly over the internal pipe */
146
147 #if defined(FILE_CNT)
148     if (stdin->_cnt > 0)
149         return stdin->_cnt;
150 #endif
151     if (!init) {
152         init = 1;
153         inh = GetStdHandle(STD_INPUT_HANDLE);
154         pipe = !GetConsoleMode(inh, &dw);
155         if (!pipe) {
156             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
157             FlushConsoleInputBuffer(inh);
158         }
159     }
160     if (pipe) {
161         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
162             return 1;
163         return dw;
164     } else {
165         GetNumberOfConsoleInputEvents(inh, &dw);
166         return dw <= 1 ? 0 : dw;
167     }
168 }
169 #endif