aboutsummaryrefslogtreecommitdiff
path: root/src/PGN.cpp
blob: fe83cce8516f5d43fd1c29c53cb35b572a0e559a (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

#include "pgnp.hpp"
#include <iostream>
#include <string>

#define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
#define IS_DIGIT(c)                                                            \
  (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' ||     \
   c == '6' || c == '7' || c == '8' || c == '9')
#define IS_EOF (pgn_content.IsEOF())
#define EOF_CHECK(loc)                                                         \
  {                                                                            \
    if (IS_EOF)                                                                \
      throw UnexpectedEOF();                                                   \
  }

namespace pgnp {

PGN::PGN() : LastGameEndLoc(0), moves(NULL) {}

PGN::~PGN() {
  if (moves != NULL)
    delete moves;
}

std::string PGN::GetResult() { return (result); }

void PGN::FromFile(std::string filepath) { pgn_content.FromFile(filepath); }

void PGN::FromString(std::string pgn_content) {
  this->pgn_content.FromString(pgn_content);
}

void PGN::ParseNextGame() {
  // Clean previous parse
  if (moves != NULL) {
    delete moves;
  }
  result = "";
  tagkeys.clear();
  tags.clear();
  moves = new HalfMove();

  // Search for new game
  if (IS_EOF) {
    throw NoGameFound();
  }
  long loc = GotoNextToken(LastGameEndLoc);
  if (IS_EOF) {
    throw NoGameFound();
  }

  // Parse game
  while (!IS_EOF) {
    char c = pgn_content[loc];
    if (!IS_BLANK(c)) {
      if (c == '[') {
        loc = ParseNextTag(loc);
      } else if (IS_DIGIT(c)) {
        LastGameEndLoc = ParseHalfMove(loc, moves);
        break;
      } else if (c == '{') {
        loc = ParseComment(loc, moves);
        continue; // No need loc++
      } else if (c == '%' || c == ';') {
        loc = GotoEOL(loc);
      }
    }
    loc++;
  }

  if (result.size() <= 0) {
    throw InvalidGameResult();
  }
}

void PGN::STRCheck() {
  int i = 0;
  // Locate Event tag
  while (i < tagkeys.size()) {
    if (tagkeys[i] == "Event") {
      break;
    }
    i++;
  }

  // Check tags
  if (i + 6 < tagkeys.size()) {
    bool valid = (tagkeys[i] == "Event") && (tagkeys[i + 1] == "Site") &&
                 (tagkeys[i + 2] == "Date") && (tagkeys[i + 3] == "Round") &&
                 (tagkeys[i + 4] == "White") && (tagkeys[i + 5] == "Black") &&
                 (tagkeys[i + 6] == "Result");
    if (!valid) {
      throw STRCheckFailed();
    }
  } else {
    throw STRCheckFailed();
  }
}

bool PGN::HasTag(std::string key) {
  auto tags = GetTagList();
  return (std::find(tags.begin(), tags.end(), key) != tags.end());
}

long PGN::ParseComment(long loc, HalfMove *hm) {
  // Goto next char
  loc = GotoNextToken(loc);
  EOF_CHECK(loc);
  char c = pgn_content[loc];

  // Parse a sequence of comment
  while (!IS_EOF && c == '{') {
    loc++;
    EOF_CHECK(loc);
    c = pgn_content[loc];
    while (c != '}') {
      hm->comment += c;
      loc++;
      EOF_CHECK(loc);
      c = pgn_content[loc];
    }
    loc++; // Skip '}'

    // Goto next non blank to look for another comment token
    loc = GotoNextToken(loc);
    if (!IS_EOF) {
      c = pgn_content[loc];
    }
  }
  return (loc);
}

long PGN::ParseHalfMove(long loc, HalfMove *hm) {
  // Goto next char
  loc = GotoNextToken(loc);
  EOF_CHECK(loc);
  char c = pgn_content[loc];

  // Check if we reach score entry (* or 1-0 or 0-1 or 1/2-1/2)
  if (c == '*') {
    result = "*";
    return (loc + 1);
  }

  // Parse move number and check if end of game
  if (IS_DIGIT(c)) {
    std::string move_nb;
    char first_digit = c;
    while (IS_DIGIT(c)) {
      move_nb += c;
      loc++;
      c = pgn_content[loc];
      EOF_CHECK(loc);
      if (c == '/' || c == '-') {
        if (c == '/') {
          result = "1/2-1/2";
          return (loc + 6);
        } else if (first_digit == '1') {
          result = "1-0";
          return (loc + 2);
        } else {
          result = "0-1";
          return (loc + 2);
        }
      }
    }
    hm->count = std::stoi(move_nb);
    loc++;
    EOF_CHECK(loc);
    if (pgn_content[loc] == '.') {
      hm->isBlack = true;
      loc += 2; // Skip two dots
      EOF_CHECK(loc);
    }
  } else {
    hm->isBlack = true;
  }

  // Parse the HalfMove
  loc = GotoNextToken(loc);
  EOF_CHECK(loc);
  c = pgn_content[loc];
  std::string move;
  while (!IS_BLANK(c) && c != ')') {
    move += c;
    loc++;
    c = pgn_content[loc];
    EOF_CHECK(loc);
  }
  hm->move = move;

  // Check for NAG
  loc = GotoNextToken(loc);
  EOF_CHECK(loc);
  c = pgn_content[loc];
  if (c == '$') {
    hm->NAG += c;
    loc++;
    EOF_CHECK(loc);
    c = pgn_content[loc];
    while (IS_DIGIT(c)) {
      hm->NAG += c;
      loc++;
      EOF_CHECK(loc);
      c = pgn_content[loc];
    }
  }

  // Parse comment
  loc = ParseComment(loc, hm);

  // Check for variations
  loc = GotoNextToken(loc);
  while (!IS_EOF && pgn_content[loc] == '(') {
    loc++; // Skip '('
    HalfMove *var = new HalfMove;
    loc = ParseHalfMove(loc, var);
    hm->variations.push_back(var);
    loc++; // Skip ')'
    // Goto next var
    loc = GotoNextToken(loc);
    EOF_CHECK(loc);
    c = pgn_content[loc];
  }

  // Skip end of variation
  loc = GotoNextToken(loc);
  EOF_CHECK(loc);
  c = pgn_content[loc];
  if (c == ')') {
    return (loc);
  }

  // Parse next HalfMove
  loc = GotoNextToken(loc);
  if (!IS_EOF) {
    HalfMove *next_hm = new HalfMove;
    next_hm->count = hm->count;
    loc = ParseHalfMove(loc, next_hm);
    // Check if move parsed successfuly
    if (next_hm->move.size() > 0) {
      hm->MainLine = next_hm;
    } else {
      delete next_hm;
    }
  }

  return (loc);
}

long PGN::ParseNextTag(long start_loc) {
  // Parse key
  std::string key;
  long keyloc = start_loc + 1;
  EOF_CHECK(keyloc);
  char c = pgn_content[keyloc];
  while (!IS_BLANK(c)) {
    key += c;
    keyloc++;
    EOF_CHECK(keyloc);
    c = pgn_content[keyloc];
  }

  // Parse value
  std::string value;
  long valueloc = GotoNextToken(keyloc) + 1;
  EOF_CHECK(keyloc);
  c = pgn_content[valueloc];
  while (c != '"' or IS_EOF) {
    value += c;
    valueloc++;
    EOF_CHECK(keyloc);
    c = pgn_content[valueloc];
  }

  // Add tag
  tags[key] = value;
  tagkeys.push_back(key);

  EOF_CHECK(valueloc + 1);
  c = pgn_content[valueloc + 1];
  if (c != ']') {
    throw UnexpectedCharacter(c, ']', valueloc + 1);
  }

  return (valueloc + 1); // +1 For the last char of the tag which is ']'
}

void PGN::GetMoves(HalfMove *copy) { moves->Copy(copy); }

std::vector<std::string> PGN::GetTagList() { return tagkeys; }

std::string PGN::GetTagValue(std::string key) {
  if (tags.find(key) == tags.end()) {
    throw InvalidTagName();
  }
  return tags[key];
}

std::string PGN::Dump() {
  std::stringstream ss;
  ss << "---------- PGN DUMP ----------" << std::endl;
  ss << "Tags:" << std::endl;
  for (auto &tag : GetTagList()) {
    ss << "  " << tag << "=" << GetTagValue(tag) << std::endl;
  }
  ss << "Moves:" << std::endl;

  if (moves != NULL)
    ss << moves->Dump();
  return (ss.str());
}

long PGN::GotoNextToken(long loc) {
  char c = pgn_content[loc];
  while (IS_BLANK(c)) {
    loc++;
    if (IS_EOF) {
      return (loc);
    }
    c = pgn_content[loc];
    if (c == '%' || c == ';') {
      loc = GotoEOL(loc);
      if (IS_EOF) {
        return (loc);
      }
    }
  }

  return (loc);
}

long PGN::GotoEOL(long loc) {
  char c = pgn_content[loc];
  while (true) {
    loc++;
    if (IS_EOF) {
      return (loc);
    }
    c = pgn_content[loc];
    if (c == '\n') {
      return (loc);
    }
  }
}

} // namespace pgnp