-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgib.cpp
More file actions
218 lines (181 loc) · 5.89 KB
/
Copy pathgib.cpp
File metadata and controls
218 lines (181 loc) · 5.89 KB
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
211
212
213
214
215
216
217
218
/*
mugo, sgf editor.
Copyright (C) 2009-2010 nsase.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QRegExp>
#include "appdef.h"
#include "gib.h"
namespace go{
bool gib::readStream(QString::iterator& first, QString::iterator last){
// dataList_.push_back( data() );
while (first != last){
QString s = readLine(first, last);
if (s == "\\HS")
readHeader(first, last);
else if (s == "\\GS")
readGame(first, last);
}
return true;
}
bool gib::saveStream(QTextStream& /*stream*/){
return false;
}
QTextCodec* gib::getCodec(const QByteArray& a) const{
int p = a.indexOf("\\[GIBOKIND=");
if (p == -1)
return NULL;
p += 11;
if (a.mid(p, 7) == "Japan\\]")
return QTextCodec::codecForName("Shift_JIS");
else if (a.mid(p, 8) == "Taiwan\\]")
return QTextCodec::codecForName("Big5");
else if (a.mid(p, 7) == "Korea\\]")
return QTextCodec::codecForName("EUC_KR");
else
return NULL;
}
bool gib::get(go::data& data) const{
data.clear();
int hcapx1[] = {15, 3, 15, 3, 9};
int hcapx2[] = {15, 3, 15, 3, 15, 3, 9};
int hcapx3[] = {15, 3, 15, 3, 15, 3, 9, 9, 9};
int hcapy1[] = {3, 15, 15, 3, 9};
int hcapy2[] = {3, 15, 15, 3, 9, 9, 9};
int hcapy3[] = {3, 15, 15, 3, 9, 9, 15, 3, 9};
data.root->xsize = 19;
data.root->ysize = 19;
data.root->whitePlayer = whitePlayer;
data.root->whiteRank = whiteRank;
data.root->blackPlayer = blackPlayer;
data.root->blackRank = blackRank;
data.root->comment = comment;
data.root->place = place;
data.root->date = gameDate;
data.root->gameName = gameName;
data.root->result = result;
data.root->annotation = condition;
data.root->time = gameTime;
if (handicap)
data.root->komi = 0;
data.root->handicap = handicap;
for (int i=0; i<handicap; ++i){
if (handicap < 6)
data.root->blackStones.push_back( stone(hcapx1[i], hcapy1[i], go::black) );
else if (handicap < 8)
data.root->blackStones.push_back( stone(hcapx2[i], hcapy2[i], go::black) );
else
data.root->blackStones.push_back( stone(hcapx3[i], hcapy3[i], go::black) );
}
get(dataList_.begin(), dataList_.end(), go::nodePtr(data.root));
return true;
}
bool gib::set(const go::data& /*data*/){
return false;
}
bool gib::get(dataList::const_iterator first, dataList::const_iterator last, go::nodePtr parent) const{
if (first == last)
return true;
nodePtr node;
if (first->color == go::black)
node = go::createBlackNode(parent, first->x, first->y);
else if (first->color == go::white)
node = go::createWhiteNode(parent, first->x, first->y);
else
node.reset(new go::node(parent));
parent->childNodes.push_back(node);
get(++first, last, node);
return true;
}
bool gib::readHeader(QString::iterator& first, QString::iterator& last){
QString str;
while (first != last){
str.append( readLine(first, last) );
if (str == "\\HE")
return true;
// \[%1=%2\]
QRegExp rx("\\\\\\[(.+)=(.*)\\\\\\]");
int pos = rx.indexIn(str);
if (pos == -1){
str.push_back(' ');
continue;
}
str.clear();
QStringList list = rx.capturedTexts();
if (list.size() < 3)
continue;
if (list[1] == "GAMEWHITENICK")
whitePlayer = list[2];
else if (list[1] == "GAMEWHITELEVEL")
whiteRank = list[2];
else if (list[1] == "GAMEBLACKNICK")
blackPlayer = list[2];
else if (list[1] == "GAMEBLACKLEVEL")
blackRank = list[2];
else if (list[1] == "GAMECOMMENT")
comment = list[2];
else if (list[1] == "GAMEPLACE")
place = list[2];
else if (list[1] == "GAMEDATE")
gameDate = list[2];
else if (list[1] == "GAMENAME")
gameName = list[2];
else if (list[1] == "GAMERESULT")
result = list[2];
else if (list[1] == "GAMECONDITION")
condition = list[2];
else if (list[1] == "GAMETIME")
gameTime = list[2];
}
return false;
}
bool gib::readGame(QString::iterator& first, QString::iterator& last){
QString str;
while (first != last){
str = readLine(first, last);
if (str == "\\GE")
return true;
QStringList list = str.split(' ');
if (list.size() < 3)
continue;
else if (list[0].compare("INI", Qt::CaseInsensitive) == 0){
readINI(list[3].toInt());
continue;
}
if (list.size() < 6)
continue;
else if (list[0].compare("STO", Qt::CaseInsensitive) == 0)
readSTO(list[4].toInt(), list[5].toInt(), list[3].toInt());
}
return false;
}
bool gib::readINI(int hcap){
if (handicap < 0 || handicap > 9)
return false;
handicap = hcap;
return true;
}
bool gib::readSTO(int x, int y, int color){
data d;
d.x = x;
d.y = y;
if (color == 1)
d.color = go::black;
else if (color == 2)
d.color = go::white;
else
return false;
dataList_.push_back(d);
return true;
}
};