1 /* option.cpp
2 
3    GNU Chess protocol adapter
4 
5    Copyright (C) 2001-2011 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 // option.cpp
23 
24 // includes
25 
26 #include <cstdlib>
27 #include <cstring>
28 
29 #include "option.h"
30 #include "util.h"
31 
32 namespace adapter {
33 
34 // constants
35 
36 static const bool UseDebug = false;
37 
38 // types
39 
40 struct option_t {
41    const char * var;
42    const char * val;
43 };
44 
45 // variables
46 
47 static option_t Option[] = {
48 
49    { "OptionFile",    NULL, }, // string
50 
51    // options
52 
53    { "EngineName",    NULL, }, // string
54    { "EngineDir",     NULL, }, // string
55    { "EngineCommand", NULL, }, // string
56 
57    { "Log",           NULL, }, // true/false
58    { "LogFile",       NULL, }, // string
59 
60    { "Chess960",      NULL, }, // true/false
61 
62    { "Resign",        NULL, }, // true/false
63    { "ResignMoves",   NULL, }, // move number
64    { "ResignScore",   NULL, }, // centipawns
65 
66    { "MateScore",     NULL, }, // centipawns
67 
68    { "Book",          NULL, }, // true/false
69    { "BookFile",      NULL, }, // string
70 
71    { "BookRandom",    NULL, }, // true/false
72    { "BookWorst",     NULL, }, // true/false
73    { "BookLearn",     NULL, }, // true/false
74 
75    { "KibitzMove",    NULL, }, // true/false
76    { "KibitzPV",      NULL, }, // true/false
77 
78    { "KibitzCommand", NULL, }, // string
79    { "KibitzDelay",   NULL, }, // seconds
80 
81    { "ShowPonder",    NULL, }, // true/false
82 
83    // work-arounds
84 
85    { "UCIVersion",    NULL, }, // 1-
86    { "CanPonder",     NULL, }, // true/false
87    { "SyncStop",      NULL, }, // true/false
88    { "PromoteWorkAround", NULL, }, // true/false
89 
90    // { "",              NULL, },
91 
92    { NULL,            NULL, },
93 };
94 
95 // prototypes
96 
97 static option_t * option_find (const char var[]);
98 
99 // functions
100 
101 // option_init()
102 
option_init()103 void option_init() {
104 
105    // option file
106 
107    const char optionName[]="gnuchess.ini";
108    char optionFile[MaxFileNameSize+1];
109    FILE *of;
110    if ( ( of = fopen(optionName, "r") ) != NULL ) {
111       fclose(of);
112       strcpy(optionFile,"");
113    } else {
114       strcpy(optionFile,compute_pkgdatadir());
115       strcat(optionFile,"/");
116    }
117    strcat(optionFile,optionName);
118 
119    // options
120 
121    option_set("OptionFile",optionFile);
122 
123    option_set("EngineName","GNU Chess");
124    option_set("EngineDir",".");
125    option_set("EngineCommand","<empty>");
126 
127    option_set("Log","false");
128    option_set("LogFile","adapter.log");
129 
130    option_set("Chess960","false");
131 
132    option_set("Resign","false");
133    option_set("ResignMoves","3");
134    option_set("ResignScore","600");
135 
136    option_set("MateScore","10000");
137 
138    option_set("Book","false");
139    option_set("BookFile","book.bin");
140 
141    option_set("BookRandom","true");
142    option_set("BookWorst","false");
143    option_set("BookLearn","false");
144 
145    option_set("KibitzMove","false");
146    option_set("KibitzPV","false");
147 
148    option_set("KibitzCommand","tellall");
149    option_set("KibitzDelay","5");
150 
151    option_set("ShowPonder","true");
152 
153    // work-arounds
154 
155    option_set("UCIVersion","2");
156    option_set("CanPonder","false");
157    option_set("SyncStop","false");
158    option_set("PromoteWorkAround","false");
159 
160    // option_set("","");
161 }
162 
163 // option_set()
164 
option_set(const char var[],const char val[])165 bool option_set(const char var[], const char val[]) {
166 
167    option_t * opt;
168 
169    ASSERT(var!=NULL);
170    ASSERT(val!=NULL);
171 
172    opt = option_find(var);
173    if (opt == NULL) return false;
174 
175    my_string_set(&opt->val,val);
176 
177    if (UseDebug) my_log("POLYGLOT OPTION SET \"%s\" -> \"%s\"\n",opt->var,opt->val);
178 
179    return true;
180 }
181 
182 // option_get()
183 
option_get(const char var[])184 const char * option_get(const char var[]) {
185 
186    option_t * opt;
187 
188    ASSERT(var!=NULL);
189 
190    opt = option_find(var);
191    if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",var);
192 
193    if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->var,opt->val);
194 
195    return opt->val;
196 }
197 
198 // option_get_bool()
199 
option_get_bool(const char var[])200 bool option_get_bool(const char var[]) {
201 
202    const char * val;
203 
204    val = option_get(var);
205 
206    if (false) {
207    } else if (my_string_case_equal(val,"true") || my_string_case_equal(val,"yes") || my_string_equal(val,"1")) {
208       return true;
209    } else if (my_string_case_equal(val,"false") || my_string_case_equal(val,"no") || my_string_equal(val,"0")) {
210       return false;
211    }
212 
213    ASSERT(false);
214 
215    return false;
216 }
217 
218 // option_get_double()
219 
option_get_double(const char var[])220 double option_get_double(const char var[]) {
221 
222    const char * val;
223 
224    val = option_get(var);
225 
226    return atof(val);
227 }
228 
229 // option_get_int()
230 
option_get_int(const char var[])231 int option_get_int(const char var[]) {
232 
233    const char * val;
234 
235    val = option_get(var);
236 
237    return atoi(val);
238 }
239 
240 // option_get_string()
241 
option_get_string(const char var[])242 const char * option_get_string(const char var[]) {
243 
244    const char * val;
245 
246    val = option_get(var);
247 
248    return val;
249 }
250 
251 // option_find()
252 
option_find(const char var[])253 static option_t * option_find(const char var[]) {
254 
255    option_t * opt;
256 
257    ASSERT(var!=NULL);
258 
259    for (opt = &Option[0]; opt->var != NULL; opt++) {
260       if (my_string_case_equal(opt->var,var)) return opt;
261    }
262 
263    return NULL;
264 }
265 
266 }  // namespace adapter
267 
268 // end of option.cpp
269 
270