Fixed bug in the search, now no more near-mate values should randomly appear.
[rattatechess.git] / platform.cpp
blobd5d9a2c952501cbb2dd152dfc554f8fe58494014
1 /***************************************************************************
2 platform.cpp - Platoform dependent utilities, implementation
3 -------------------
4 begin : Sun Sep 28 2007
5 copyright : (C) 2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #if defined(_WIN32) || defined(_WIN64)
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <windows.h>
23 #include "platform.h"
25 static bool pipe;
26 static HANDLE handle;
28 void init_stdin()
30 DWORD val;
32 handle = GetStdHandle(STD_INPUT_HANDLE);
33 pipe = !GetConsoleMode(handle,&val);
35 if (!pipe)
37 SetConsoleMode(handle, val & ~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
38 FlushConsoleInputBuffer(handle);
42 bool input_available() {
43 DWORD val;
45 if (stdin->_cnt > 0)
46 return true;
48 if(pipe)
50 if(!PeekNamedPipe(handle, NULL, 0, NULL, &val, NULL) )
51 return true;
52 return val > 0;
54 else
56 GetNumberOfConsoleInputEvents(handle, &val);
57 return val > 1;
60 return false;
63 int current_time()
65 return GetTickCount() / 10;
68 bool start_engine(const char *prog, FILE **in, FILE **out)
70 printf("Error, start_engine unimplemented on windows!\n");
71 return false;
74 void *map_file(const char *file_name, uint32_t *size)
76 HANDLE file, fmap;
77 uint64_t offset = 0;
78 DWORD hiword, loword;
79 void *retv;
81 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
82 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
83 if(file==INVALID_HANDLE_VALUE)
84 return NULL;
86 loword = GetFileSize(file, &hiword);
87 if(loword == INVALID_FILE_SIZE)
89 CloseHandle(file);
90 return(NULL);
92 *size = loword | ((uint64_t)hiword<<32);
94 fmap = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
95 if(fmap == INVALID_HANDLE_VALUE)
97 CloseHandle(file);
98 return NULL;
101 retv = MapViewOfFile(fmap, FILE_MAP_READ, (DWORD)(offset >> 32), (DWORD)(offset % 0xffffffff), 0);
103 CloseHandle(fmap);
104 CloseHandle(file);
105 return retv;
108 void unmap_file(void *addr, uint32_t size)
110 UnmapViewOfFile(addr);
113 #else //defined(_WIN32) || defined(_WIN64)
115 #include <unistd.h>
116 #include <stdlib.h>
117 #include <sys/time.h>
118 #include <sys/types.h>
119 #include <sys/stat.h>
120 #include <sys/mman.h>
121 #include <fcntl.h>
122 #include <unistd.h>
123 #include <string.h>
124 #include <errno.h>
125 #include "platform.h"
127 void init_stdin()
131 bool input_available()
133 int n = fileno(stdin);
134 fd_set ifds;
135 fd_set efds;
136 struct timeval tv = { 0, 0 };
138 FD_ZERO(&ifds);
139 FD_SET(n, &ifds);
140 FD_ZERO(&efds);
141 FD_SET(n, &efds);
143 while(select(n+1, &ifds, NULL, &efds, &tv)==-1)
144 if(errno != EINTR)
145 printf("select: %s\n", strerror(errno));
147 return FD_ISSET(0, &ifds) || FD_ISSET(0, &efds);
150 int current_time()
152 timeval tv;
153 gettimeofday(&tv,NULL);
154 return tv.tv_sec*100 + tv.tv_usec/10000;
157 void *map_file(const char *file_name, uint32_t *size)
159 struct stat st;
160 void *addr;
161 int fd;
163 fd = open( file_name, O_RDONLY);
164 if(fd == -1)
165 return NULL;
167 fstat(fd, &st);
168 if(size)
169 *size = st.st_size;
171 addr = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
172 if(addr == (void*)-1)
174 close(fd);
175 return NULL;
178 close(fd);
179 return addr;
182 void unmap_file(void*addr, uint32_t size)
184 munmap(addr, size);
187 bool start_engine(const char *prog, FILE **in, FILE **out)
189 int pin[2];
190 int pout[2];
191 pipe(pin);
192 pipe(pout);
193 if(!fork())
195 dup2(pout[0], 0);
196 dup2(pin[1], 1);
197 dup2(pin[1], 2);
198 execlp(prog, prog, NULL);
199 fprintf(stderr, "Error, could not run %s!\n", prog);
200 exit(0);
202 *in = fdopen(pin[0], "r");
203 *out = fdopen(pout[1], "w");
204 setbuf(*in, NULL);
205 setbuf(*out, NULL);
206 fprintf(*out, "xboard\n");
207 fprintf(*out, "protover 2\n");
208 //fprintf(*out, "post\n");
209 fprintf(*out, "easy\n");
210 fprintf(*out, "new\n");
211 fprintf(*out, "force\n");
213 return true;
215 #endif //defined(_WIN32) || defined(_WIN64)