4830be6067b21586ed1043f07506e0488c1e3b0f
[polyglot.git] / option.c
1 \r
2 // option.c\r
3 \r
4 // includes\r
5 \r
6 #include <stdlib.h>\r
7 #include <string.h>\r
8 \r
9 #include "option.h"\r
10 #include "util.h"\r
11 \r
12 // constants\r
13 \r
14 static const bool UseDebug = FALSE;\r
15 \r
16 \r
17 // variables\r
18 \r
19 #define NNB { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL }\r
20 option_t Option[] = {\r
21 \r
22     { "OptionFile",       "string","0","0",     "polyglot.ini", NULL,0,NNB,  PG}, \r
23 \r
24    // options\r
25 \r
26     { "EngineName",       "string","0","0",     "<empty>"   , NULL,0,NNB,  PG}, \r
27     { "EngineDir",        "string","0","0",     "."         , NULL,0,NNB,  PG}, \r
28     { "EngineCommand",    "string","0","0",     "<empty>"   , NULL,0,NNB,  PG}, \r
29 \r
30     { "Log",              "check","0","0",     "false"      , NULL,0,NNB,  PG|XBOARD|UCI}, \r
31     { "LogFile",          "string","0","0",     "polyglot.log", NULL,0,NNB,  PG|XBOARD|UCI}, \r
32 \r
33     { "UCI",              "check","0","0",      "false"     , NULL,0,NNB,  PG}, \r
34 \r
35     { "UseNice",          "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD|UCI}, \r
36     { "NiceValue",        "spin", "0","20",     "5"         , NULL,0,NNB,  PG|XBOARD|UCI}, \r
37 \r
38     { "Chess960",         "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
39 \r
40     { "Resign",           "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
41     { "ResignMoves",      "spin","0","10000",    "3"        , NULL,0,NNB,  PG|XBOARD}, \r
42     { "ResignScore",      "spin","0","10000",   "600"       , NULL,0,NNB,  PG|XBOARD}, \r
43 \r
44     { "MateScore",        "spin","0","1000000", "10000"     , NULL,0,NNB,  PG|XBOARD}, \r
45 \r
46     { "Book",             "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD|UCI}, \r
47     { "BookFile",         "string","0","0",     "book.bin"  , NULL,0,NNB,  PG|XBOARD|UCI}, \r
48 \r
49     { "BookRandom",       "check","0","0",      "true"      , NULL,0,NNB,  PG|XBOARD|UCI}, \r
50     { "BookLearn",        "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
51 \r
52     { "KibitzMove",       "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
53     { "KibitzPV",         "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
54 \r
55     { "KibitzCommand",    "string","0","0",     "tellall"   , NULL,0,NNB,  PG|XBOARD}, \r
56     { "KibitzDelay",      "spin","0","10000",  "5"         , NULL,0,NNB,  PG|XBOARD}, \r
57     { "KibitzInterval",   "spin","0","10000",  "0"         , NULL,0,NNB,  PG|XBOARD}, \r
58 \r
59     { "ShowPonder",       "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
60     { "ScoreWhite",       "check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
61 \r
62    // work-arounds\r
63 \r
64     { "UCIVersion",       "spin","1","2",       "2"         , NULL,0,NNB,  PG|XBOARD}, \r
65     { "CanPonder",        "check","1","2",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
66     { "SyncStop",         "check","1","2",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
67     { "Affinity",             "spin","-1","32",     "-1"        , NULL,0,NNB,  PG}, \r
68     { "RepeatPV",             "check","0","0",      "true"     , NULL,0,NNB,  PG|XBOARD},\r
69     { "PromoteWorkAround","check","0","0",      "false"     , NULL,0,NNB,  PG|XBOARD}, \r
70 \r
71     { NULL,               NULL,"0","0",         NULL        , NULL,0,NNB,  0},\r
72 };\r
73 \r
74 // prototypes\r
75 \r
76 static option_t * option_find (const char var[]);\r
77 \r
78 // functions\r
79 \r
80 // option_init()\r
81 \r
82 void option_init() {\r
83 \r
84     option_t *p=Option;\r
85     const char * name;\r
86 \r
87     while((name=(p++)->name)){\r
88         option_set(name,option_get_default(name));\r
89     }\r
90 }\r
91 \r
92 \r
93 // option_set()\r
94 \r
95 bool option_set(const char name[], const char value[]) {\r
96 \r
97    option_t * opt;\r
98    ASSERT(name!=NULL);\r
99    ASSERT(value!=NULL);\r
100 \r
101    opt = option_find(name);\r
102    if (opt == NULL) return FALSE;\r
103 \r
104    my_string_set(&opt->value,value);\r
105 \r
106    if (UseDebug) my_log("POLYGLOT OPTION SET \"%s\" -> \"%s\"\n",opt->name,opt->value);\r
107 \r
108    return TRUE;\r
109 }\r
110 // option_set()\r
111 \r
112 bool option_set_default(const char name[], const char value[]) {\r
113 \r
114    option_t * opt;\r
115    ASSERT(name!=NULL);\r
116    ASSERT(value!=NULL);\r
117 \r
118    opt = option_find(name);\r
119    if (opt == NULL) return FALSE;\r
120 \r
121    opt->default_=my_strdup(value);\r
122 \r
123    if (UseDebug) my_log("POLYGLOT OPTION DEFAULT SET \"%s\" -> \"%s\"\n",opt->name,opt->default_);\r
124 \r
125    return TRUE;\r
126 }\r
127 \r
128 // option_get()\r
129 \r
130 const char * option_get(const char name[]) {\r
131 \r
132    option_t * opt;\r
133 \r
134    ASSERT(name!=NULL);\r
135 \r
136    opt = option_find(name);\r
137    if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",name);\r
138 \r
139    if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->name,opt->value);\r
140 \r
141    return opt->value;\r
142 }\r
143 \r
144 // option_get_default()\r
145 \r
146 const char * option_get_default(const char name[]) {\r
147 \r
148    option_t * opt;\r
149 \r
150    ASSERT(name!=NULL);\r
151 \r
152    opt = option_find(name);\r
153    if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",name);\r
154 \r
155    if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->name,opt->value);\r
156 \r
157    return opt->default_;\r
158 }\r
159 \r
160 // option_get_bool()\r
161 \r
162 bool option_get_bool(const char name[]) {\r
163 \r
164    const char * value;\r
165 \r
166    value = option_get(name);\r
167 \r
168    if (FALSE) {\r
169    } else if (my_string_case_equal(value,"true") || my_string_case_equal(value,"yes") || my_string_equal(value,"1")) {\r
170       return TRUE;\r
171    } else if (my_string_case_equal(value,"false") || my_string_case_equal(value,"no") || my_string_equal(value,"0")) {\r
172       return FALSE;\r
173    }\r
174 \r
175    ASSERT(FALSE);\r
176 \r
177    return FALSE;\r
178 }\r
179 \r
180 // option_get_double()\r
181 \r
182 double option_get_double(const char name[]) {\r
183 \r
184    const char * value;\r
185 \r
186    value = option_get(name);\r
187 \r
188    return atof(value);\r
189 }\r
190 \r
191 // option_get_int()\r
192 \r
193 int option_get_int(const char name[]) {\r
194 \r
195    const char * value;\r
196 \r
197    value = option_get(name);\r
198 \r
199    return atoi(value);\r
200 }\r
201 \r
202 // option_get_string()\r
203 \r
204 const char * option_get_string(const char name[]) {\r
205 \r
206    const char * value;\r
207 \r
208    value = option_get(name);\r
209 \r
210    return value;\r
211 }\r
212 \r
213 // option_find()\r
214 \r
215 static option_t * option_find(const char name[]) {\r
216 \r
217    option_t * opt;\r
218 \r
219    ASSERT(name!=NULL);\r
220 \r
221 \r
222    for (opt = &Option[0]; opt->name != NULL; opt++) {\r
223       if (my_string_case_equal(opt->name,name)) return opt;\r
224    }\r
225 \r
226    return NULL;\r
227 }\r
228 \r
229 // end of option.cpp\r
230 \r