-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp.h
211 lines (163 loc) · 4.49 KB
/
gtp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#pragma once
#include "string.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <functional>
using namespace std;
using namespace placeholders; //for bind
struct GTPResponse {
bool success;
string id;
string response;
GTPResponse() { }
GTPResponse(bool s, string r = ""){
success = s;
response = r;
rtrim(response);
}
GTPResponse(string r){
GTPResponse(true, r);
}
string to_s(){
return (success ? '=' : '?') + id + ' ' + response + "\n\n";
}
};
typedef function<GTPResponse(vecstr)> gtp_callback_fn;
struct GTPCallback {
string name;
string desc;
gtp_callback_fn func;
GTPCallback() { }
GTPCallback(string n, string d, gtp_callback_fn fn) : name(n), desc(d), func(fn) { }
};
class GTPclient {
FILE * in, * out, * logfile;
vector<GTPCallback> callbacks;
unsigned int longest_cmd;
bool running;
public:
GTPclient(FILE * i = stdin, FILE * o = stdout, FILE * l = NULL){
in = i;
out = o;
logfile = l;
longest_cmd = 0;
running = false;
newcallback("list_commands", bind(>Pclient::gtp_list_commands, this, _1, false), "List the commands");
newcallback("help", bind(>Pclient::gtp_list_commands, this, _1, true), "List the commands, with descriptions");
newcallback("quit", bind(>Pclient::gtp_quit, this, _1), "Quit the program");
newcallback("exit", bind(>Pclient::gtp_quit, this, _1), "Alias for quit");
newcallback("protocol_version", bind(>Pclient::gtp_protocol_version, this, _1), "Show the gtp protocol version");
newcallback("logfile", bind(>Pclient::gtp_logfile, this, _1), "Start logging the gtp commands to this file, disabled in server mode");
newcallback("lognote", bind(>Pclient::gtp_lognote, this, _1), "Add a comment to the logfile");
newcallback("logend", bind(>Pclient::gtp_logend, this, _1), "Stop logging");
}
~GTPclient(){
if(logfile)
fclose(logfile);
}
void setinfile(FILE * i){
in = i;
}
void setoutfile(FILE * o){
out = o;
}
void setlogfile(FILE * l){
if(logfile)
fclose(logfile);
logfile = l;
}
void newcallback(const string name, const gtp_callback_fn & fn, const string desc = ""){
newcallback(GTPCallback(name, desc, fn));
if(longest_cmd < name.length())
longest_cmd = name.length();
}
void newcallback(const GTPCallback & a){
callbacks.push_back(a);
}
int find_callback(const string & name){
for(unsigned int i = 0; i < callbacks.size(); i++)
if(callbacks[i].name == name)
return i;
return -1;
}
void log(const string & str){
if(logfile){
fprintf(logfile, "%s\n", str.c_str());
fflush(logfile);
}
}
GTPResponse cmd(string line){
vecstr parts = explode(line, " ");
string id;
if(parts.size() > 1 && atoi(parts[0].c_str())){
id = parts[0];
parts.erase(parts.begin());
}
string name = parts[0];
parts.erase(parts.begin());
int cb = find_callback(name);
GTPResponse response;
if(cb < 0){
response = GTPResponse(false, "Unknown command");
}else{
response = callbacks[cb].func(parts);
}
response.id = id;
return response;
}
bool run(){
running = true;
char buf[1001];
while(running && fgets(buf, 1000, in)){
string line(buf);
trim(line);
if(line.length() == 0 || line[0] == '#')
continue;
GTPResponse response = cmd(line);
if(out){
string output = response.to_s();
fwrite(output.c_str(), 1, output.length(), out);
fflush(out);
}
}
return running;
}
GTPResponse gtp_logfile(vecstr args){
if(args.size() != 1)
return GTPResponse(false, "Wrong number of arguments");
FILE * fd = fopen(args[0].c_str(), "w");
if(!fd)
return GTPResponse(false, "Couldn't open file " + args[0]);
setlogfile(fd);
return GTPResponse(true);
}
GTPResponse gtp_lognote(vecstr args){
log(implode(args, " "));
return GTPResponse(true);
}
GTPResponse gtp_logend(vecstr args){
setlogfile(NULL);
return GTPResponse(true);
}
GTPResponse gtp_protocol_version(vecstr args){
return GTPResponse(true, "2");
}
GTPResponse gtp_quit(vecstr args){
running = false;
return true;
}
GTPResponse gtp_list_commands(vecstr args, bool showdesc){
string ret = "\n";
for(unsigned int i = 0; i < callbacks.size(); i++){
ret += callbacks[i].name;
if(showdesc && callbacks[i].desc.length() > 0){
ret += string(longest_cmd + 2 - callbacks[i].name.length(), ' ');
ret += callbacks[i].desc;
}
ret += "\n";
}
return GTPResponse(true, ret);
}
};