updated copyright to reflect A. Scotte as copyright holder
[xboard.git] / uci.c
1 /*
2  * UCI support thru Polyglot
3  *
4  * Author: Alessandro Scotti (Jan 2006)
5  *
6  * Copyright 2006 Alessandro Scotti
7  *
8  * ------------------------------------------------------------------------
9  *
10  * GNU XBoard is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or (at
13  * your option) any later version.
14  *
15  * GNU XBoard is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see http://www.gnu.org/licenses/. 
22  *
23  * ------------------------------------------------------------------------
24  */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28
29 #ifdef WIN32
30 // [HGM] this was probably a Windows-specific constant. Needs to be defined here now I
31 //       threw out the Windows-specific includes (winboard.h etc.). 100 seems enough.
32 #include <windows.h>
33 #define SLASH_CHAR "\\"
34 #else
35 #define MAX_PATH 100
36 #define SLASH_CHAR "/"
37 #endif
38
39 #include "common.h"
40 #include "backend.h"
41
42 #define INIFILE_PREFIX      "polyglot_"
43 #define INIFILE_SUFFIX_1ST  "1st"
44 #define INIFILE_SUFFIX_2ND  "2nd"
45 #define INIFILE_EXT         ".ini"
46
47
48 static const char * GetIniFilename( ChessProgramState * cps )
49 {
50     return cps == &first ? INIFILE_PREFIX INIFILE_SUFFIX_1ST INIFILE_EXT : INIFILE_PREFIX INIFILE_SUFFIX_2ND INIFILE_EXT;
51  }
52
53 void InitEngineUCI( const char * iniDir, ChessProgramState * cps )
54 {
55     if( cps->isUCI ) {
56         const char * iniFileName = GetIniFilename( cps );
57         char polyglotIniFile[ MAX_PATH ];
58         FILE * f;
59
60         /* Build name of initialization file */
61         if( strchr( iniDir, ' ' ) != NULL ) {
62             char iniDirShort[ MAX_PATH ];
63 #ifdef WIN32
64             GetShortPathName( iniDir, iniDirShort, sizeof(iniDirShort) );
65
66             strcpy( polyglotIniFile, iniDirShort );
67 #else
68             // [HGM] UCI: not sure if this works, but GetShortPathName seems Windows pecific
69             // and names with spaces in it do not work in xboard in many places, so ignore
70             strcpy( polyglotIniFile, iniDir );
71 #endif
72         }
73         else {
74             strcpy( polyglotIniFile, iniDir );
75         }
76         
77         strcat( polyglotIniFile, SLASH_CHAR );
78         strcat( polyglotIniFile, iniFileName );
79
80         /* Create initialization file */
81         f = fopen( polyglotIniFile, "w" );
82
83         if( f != NULL ) {
84             fprintf( f, "[Polyglot]\n" );
85
86             if( cps->dir != 0 && strlen(cps->dir) > 0 ) {
87                 fprintf( f, "EngineDir = %s\n", cps->dir );
88             }
89
90             if( cps->program != 0 && strlen(cps->program) > 0 ) {
91                 fprintf( f, "EngineCommand = %s\n", cps->program );
92             }
93
94             fprintf( f, "Book = %s\n", appData.usePolyglotBook ? "true" : "false" );
95             fprintf( f, "BookFile = %s\n", appData.polyglotBook );
96         
97             fprintf( f, "[Engine]\n" );
98             fprintf( f, "Hash = %d\n", appData.defaultHashSize );
99
100             fprintf( f, "NalimovPath = %s\n", appData.defaultPathEGTB );
101             fprintf( f, "NalimovCache = %d\n", appData.defaultCacheSizeEGTB );
102
103             fprintf( f, "OwnBook = %s\n", cps->hasOwnBookUCI ? "true" : "false" );
104
105             fclose( f );
106
107             /* Replace program with properly configured Polyglot */
108             cps->dir = appData.polyglotDir;
109             cps->program = (char *) malloc( strlen(polyglotIniFile) + 32 );
110             strcpy( cps->program, "polyglot " );
111             strcat( cps->program, polyglotIniFile );
112         }
113     }
114 }