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