chess-move-interface/src/PGN.cpp

356 lines
7.8 KiB
C++
Raw Normal View History

2022-01-23 20:57:28 +01:00
#include "pgnp.hpp"
#include <iostream>
2022-01-24 18:32:05 +01:00
#include <string>
2022-01-23 20:57:28 +01:00
#define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
2022-01-24 15:29:22 +01:00
#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())
2022-01-24 15:29:22 +01:00
#define EOF_CHECK(loc) \
{ \
2022-01-26 16:24:48 +01:00
if (IS_EOF) \
2022-01-24 15:29:22 +01:00
throw UnexpectedEOF(); \
}
2022-01-23 20:57:28 +01:00
namespace pgnp {
2022-01-25 16:51:37 +01:00
PGN::PGN() : LastGameEndLoc(0), moves(NULL) {}
2022-01-24 15:29:22 +01:00
PGN::~PGN() {
if (moves != NULL)
delete moves;
}
2022-01-24 18:32:05 +01:00
std::string PGN::GetResult() { return (result); }
void PGN::FromFile(std::string filepath) { pgn_content.FromFile(filepath); }
2022-01-24 15:29:22 +01:00
void PGN::FromString(std::string pgn_content) {
this->pgn_content.FromString(pgn_content);
2022-01-25 16:51:37 +01:00
}
2022-01-25 17:46:29 +01:00
void PGN::ParseNextGame() {
2022-01-25 16:51:37 +01:00
// Clean previous parse
2022-01-25 17:46:29 +01:00
if (moves != NULL) {
2022-01-25 16:51:37 +01:00
delete moves;
}
2022-01-25 17:46:29 +01:00
result = "";
2022-01-25 16:51:37 +01:00
tagkeys.clear();
tags.clear();
2022-01-25 14:53:34 +01:00
moves = new HalfMove();
// Search for new game
if (IS_EOF) {
throw NoGameFound();
}
loctype loc = GotoNextToken(LastGameEndLoc);
if (IS_EOF) {
2022-01-25 16:51:37 +01:00
throw NoGameFound();
}
// Parse game
while (!IS_EOF) {
2022-01-24 15:29:22 +01:00
char c = pgn_content[loc];
if (!IS_BLANK(c)) {
if (c == '[') {
loc = ParseNextTag(loc);
} else if (IS_DIGIT(c)) {
LastGameEndLoc = ParseHalfMove(loc, moves);
2022-01-24 15:29:22 +01:00
break;
2022-01-25 16:51:37 +01:00
} else if (c == '{') {
loc = ParseComment(loc, moves);
2022-01-25 14:53:34 +01:00
continue; // No need loc++
2022-01-26 16:24:48 +01:00
} else if (c == '%' || c == ';') {
loc = GotoEOL(loc);
2022-01-23 20:57:28 +01:00
}
}
2022-01-24 15:29:22 +01:00
loc++;
}
2022-01-24 18:32:05 +01:00
if (result.size() <= 0) {
throw InvalidGameResult();
}
2022-01-24 15:29:22 +01:00
}
2022-01-23 20:57:28 +01:00
2022-01-24 15:29:22 +01:00
void PGN::STRCheck() {
int i = 0;
// Locate Event tag
while (i < tagkeys.size()) {
if (tagkeys[i] == "Event") {
break;
}
i++;
2022-01-23 20:57:28 +01:00
}
2022-01-24 15:29:22 +01:00
// 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();
2022-01-23 20:57:28 +01:00
}
2022-01-24 15:29:22 +01:00
} else {
throw STRCheckFailed();
}
}
2022-01-23 20:57:28 +01:00
2022-01-24 15:29:22 +01:00
bool PGN::HasTag(std::string key) {
auto tags = GetTagList();
return (std::find(tags.begin(), tags.end(), key) != tags.end());
}
loctype PGN::ParseComment(loctype loc, HalfMove *hm) {
2022-01-25 14:53:34 +01:00
// Goto next char
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
2022-01-25 14:53:34 +01:00
EOF_CHECK(loc);
char c = pgn_content[loc];
2022-01-25 16:51:37 +01:00
// Parse a sequence of comment
while (!IS_EOF && c == '{') {
2022-01-25 14:53:34 +01:00
loc++;
EOF_CHECK(loc);
c = pgn_content[loc];
2022-01-25 16:51:37 +01:00
while (c != '}') {
hm->comment += c;
2022-01-25 14:53:34 +01:00
loc++;
EOF_CHECK(loc);
c = pgn_content[loc];
}
loc++; // Skip '}'
// Goto next non blank to look for another comment token
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
if (!IS_EOF) {
c = pgn_content[loc];
}
2022-01-25 14:53:34 +01:00
}
2022-01-25 16:51:37 +01:00
return (loc);
2022-01-25 14:53:34 +01:00
}
loctype PGN::ParseHalfMove(loctype loc, HalfMove *hm) {
2022-01-24 15:29:22 +01:00
// Goto next char
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
2022-01-24 15:29:22 +01:00
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);
2022-01-24 15:29:22 +01:00
}
2022-01-23 20:57:28 +01:00
// Parse move number and check if end of game
2022-01-24 15:29:22 +01:00
if (IS_DIGIT(c)) {
std::string move_nb;
char first_digit = c;
2022-01-24 15:29:22 +01:00
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);
}
}
2022-01-24 15:29:22 +01:00
}
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
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
2022-01-24 15:29:22 +01:00
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;
2022-01-23 20:57:28 +01:00
2022-01-27 08:20:32 +01:00
// Parse Comments, Variations and NAG
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
EOF_CHECK(loc);
c = pgn_content[loc];
2022-01-27 08:20:32 +01:00
while (c == '{' || c == '$' || c == '(') {
2022-01-27 08:04:32 +01:00
if (c == '{') {
// Parse comment
loc = ParseComment(loc, hm);
} else if (c == '$') {
// Check for NAG
loc = GotoNextToken(loc);
EOF_CHECK(loc);
c = pgn_content[loc];
2022-01-27 08:04:32 +01:00
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];
}
}
} else if (c == '(') {
// 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];
}
}
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
EOF_CHECK(loc);
c = pgn_content[loc];
2022-01-24 15:29:22 +01:00
}
// Skip end of variation
if (c == ')') {
return (loc);
}
2022-01-25 15:23:54 +01:00
2022-01-24 15:29:22 +01:00
// Parse next HalfMove
2022-01-26 16:24:48 +01:00
loc = GotoNextToken(loc);
if (!IS_EOF) {
2022-01-24 15:29:22 +01:00
HalfMove *next_hm = new HalfMove;
next_hm->count = hm->count;
2022-01-25 10:53:10 +01:00
loc = ParseHalfMove(loc, next_hm);
2022-01-24 15:29:22 +01:00
// Check if move parsed successfuly
if (next_hm->move.size() > 0) {
hm->MainLine = next_hm;
} else {
delete next_hm;
}
}
return (loc);
}
loctype PGN::ParseNextTag(loctype start_loc) {
2022-01-24 15:29:22 +01:00
// Parse key
std::string key;
loctype keyloc = start_loc + 1;
2022-01-24 15:29:22 +01:00
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;
loctype valueloc = GotoNextToken(keyloc) + 1;
2022-01-24 15:29:22 +01:00
EOF_CHECK(keyloc);
c = pgn_content[valueloc];
while (c != '"' or IS_EOF) {
2022-01-24 15:29:22 +01:00
value += c;
valueloc++;
EOF_CHECK(keyloc);
c = pgn_content[valueloc];
}
// Add tag
tags[key] = value;
tagkeys.push_back(key);
2022-01-24 16:41:02 +01:00
EOF_CHECK(valueloc + 1);
c = pgn_content[valueloc + 1];
if (c != ']') {
throw UnexpectedCharacter(c, ']', valueloc + 1);
}
2022-01-24 15:29:22 +01:00
return (valueloc + 1); // +1 For the last char of the tag which is ']'
}
2022-01-25 10:53:10 +01:00
void PGN::GetMoves(HalfMove *copy) { moves->Copy(copy); }
2022-01-24 15:29:22 +01:00
std::vector<std::string> PGN::GetTagList() { return tagkeys; }
2022-01-24 16:41:02 +01:00
std::string PGN::GetTagValue(std::string key) {
if (tags.find(key) == tags.end()) {
throw InvalidTagName();
}
return tags[key];
}
2022-01-24 15:29:22 +01:00
2022-01-25 10:53:10 +01:00
std::string PGN::Dump() {
std::stringstream ss;
ss << "---------- PGN DUMP ----------" << std::endl;
ss << "Tags:" << std::endl;
2022-01-24 15:29:22 +01:00
for (auto &tag : GetTagList()) {
2022-01-25 10:53:10 +01:00
ss << " " << tag << "=" << GetTagValue(tag) << std::endl;
2022-01-24 15:29:22 +01:00
}
2022-01-25 10:53:10 +01:00
ss << "Moves:" << std::endl;
2022-01-24 15:29:22 +01:00
if (moves != NULL)
2022-01-25 10:53:10 +01:00
ss << moves->Dump();
return (ss.str());
2022-01-24 15:29:22 +01:00
}
loctype PGN::GotoNextToken(loctype loc) {
2022-01-24 15:29:22 +01:00
char c = pgn_content[loc];
while (IS_BLANK(c)) {
loc++;
if (IS_EOF) {
2022-01-24 15:29:22 +01:00
return (loc);
}
2022-01-24 15:29:22 +01:00
c = pgn_content[loc];
2022-01-26 16:24:48 +01:00
if (c == '%' || c == ';') {
2022-01-27 08:20:32 +01:00
loc = GotoEOL(loc)+1;
2022-01-26 16:24:48 +01:00
if (IS_EOF) {
return (loc);
}
}
2022-01-23 20:57:28 +01:00
}
2022-01-24 15:29:22 +01:00
return (loc);
}
2022-01-23 20:57:28 +01:00
loctype PGN::GotoEOL(loctype loc) {
2022-01-26 16:24:48 +01:00
char c = pgn_content[loc];
while (true) {
loc++;
if (IS_EOF) {
return (loc);
}
c = pgn_content[loc];
if (c == '\n') {
return (loc);
}
}
}
2022-01-24 15:29:22 +01:00
} // namespace pgnp