changes from H.G. Muller; version 4.3.13
[xboard.git] / uci.c
1 /*\r
2  * UCI support thru Polyglot\r
3  *\r
4  * Author: Alessandro Scotti (Jan 2006)\r
5  *\r
6  * ------------------------------------------------------------------------\r
7  * This program is free software; you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation; either version 2 of the License, or\r
10  * (at your option) any later version.\r
11  *\r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program; if not, write to the Free Software\r
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
20  * ------------------------------------------------------------------------\r
21  */\r
22 #include "config.h"\r
23 \r
24 #include <windows.h> /* required for all Windows applications */\r
25 \r
26 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <malloc.h>\r
29 \r
30 #include "common.h"\r
31 #include "winboard.h"\r
32 #include "frontend.h"\r
33 #include "backend.h"\r
34 \r
35 #define INIFILE_PREFIX      "polyglot_"\r
36 #define INIFILE_SUFFIX_1ST  "1st"\r
37 #define INIFILE_SUFFIX_2ND  "2nd"\r
38 #define INIFILE_EXT         ".ini"\r
39 \r
40 static const char * GetIniFilename( ChessProgramState * cps )\r
41 {\r
42     return cps == &first ? INIFILE_PREFIX INIFILE_SUFFIX_1ST INIFILE_EXT : INIFILE_PREFIX INIFILE_SUFFIX_2ND INIFILE_EXT;\r
43  }\r
44 \r
45 void InitEngineUCI( const char * iniDir, ChessProgramState * cps )\r
46 {\r
47     if( cps->isUCI ) {\r
48         const char * iniFileName = GetIniFilename( cps );\r
49         char polyglotIniFile[ MAX_PATH ];\r
50         FILE * f;\r
51 \r
52         /* Build name of initialization file */\r
53         if( strchr( iniDir, ' ' ) != NULL ) {\r
54             char iniDirShort[ MAX_PATH ];\r
55 \r
56             GetShortPathName( iniDir, iniDirShort, sizeof(iniDirShort) );\r
57 \r
58             strcpy( polyglotIniFile, iniDirShort );\r
59         }\r
60         else {\r
61             strcpy( polyglotIniFile, iniDir );\r
62         }\r
63         \r
64         strcat( polyglotIniFile, "\\" );\r
65         strcat( polyglotIniFile, iniFileName );\r
66 \r
67         /* Create initialization file */\r
68         f = fopen( polyglotIniFile, "w" );\r
69 \r
70         if( f != NULL ) {\r
71             fprintf( f, "[Polyglot]\n" );\r
72 \r
73             if( cps->dir != 0 && strlen(cps->dir) > 0 ) {\r
74                 fprintf( f, "EngineDir = %s\n", cps->dir );\r
75             }\r
76 \r
77             if( cps->program != 0 && strlen(cps->program) > 0 ) {\r
78                 fprintf( f, "EngineCommand = %s\n", cps->program );\r
79             }\r
80 \r
81             fprintf( f, "Book = %s\n", appData.usePolyglotBook ? "true" : "false" );\r
82             fprintf( f, "BookFile = %s\n", appData.polyglotBook );\r
83         \r
84             fprintf( f, "[Engine]\n" );\r
85             fprintf( f, "Hash = %d\n", appData.defaultHashSize );\r
86 \r
87             fprintf( f, "NalimovPath = %s\n", appData.defaultPathEGTB );\r
88             fprintf( f, "NalimovCache = %d\n", appData.defaultCacheSizeEGTB );\r
89 \r
90             fprintf( f, "OwnBook = %s\n", cps->hasOwnBookUCI ? "true" : "false" );\r
91 \r
92             fclose( f );\r
93 \r
94             /* Replace program with properly configured Polyglot */\r
95             cps->dir = appData.polyglotDir;\r
96             cps->program = (char *) malloc( strlen(polyglotIniFile) + 32 );\r
97             strcpy( cps->program, "polyglot " );\r
98             strcat( cps->program, polyglotIniFile );\r
99         }\r
100     }\r
101 }\r