2022-01-24 15:29:22 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <exception>
|
2022-01-23 20:57:28 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2022-01-24 15:29:22 +01:00
|
|
|
#include <streambuf>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2022-01-23 20:57:28 +01:00
|
|
|
|
|
|
|
namespace pgnp {
|
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
class HalfMove {
|
|
|
|
private:
|
|
|
|
/// @brief Recursive dump
|
|
|
|
void NestedDump(HalfMove *, int);
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
public:
|
|
|
|
int count;
|
|
|
|
bool isBlack;
|
|
|
|
std::string move;
|
|
|
|
std::string comment;
|
|
|
|
HalfMove *MainLine;
|
|
|
|
std::vector<HalfMove *> variations;
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
HalfMove();
|
|
|
|
~HalfMove();
|
|
|
|
int GetLength();
|
|
|
|
/// @brief Dump move and all its variations
|
|
|
|
void Dump();
|
|
|
|
};
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
class PGN {
|
|
|
|
private:
|
|
|
|
std::unordered_map<std::string, std::string> tags;
|
|
|
|
std::vector<std::string> tagkeys;
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
HalfMove *moves;
|
|
|
|
std::string pgn_content;
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
public:
|
|
|
|
~PGN();
|
|
|
|
void FromFile(std::string);
|
|
|
|
void FromString(std::string);
|
|
|
|
bool HasTag(std::string);
|
|
|
|
/// @brief Perform a Seven Tag Roster compliance check
|
|
|
|
void STRCheck();
|
|
|
|
/// @brief Dump parsed PGN
|
|
|
|
void Dump();
|
|
|
|
std::vector<std::string> GetTagList();
|
|
|
|
std::string GetTagValue(std::string);
|
|
|
|
HalfMove *GetMoves();
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
private:
|
|
|
|
/// @brief Populate @a tags with by parsing the one starting at location in
|
|
|
|
/// argument
|
|
|
|
int ParseNextTag(int);
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
/// @brief Get the next non-blank char location starting from location in
|
|
|
|
/// argument
|
|
|
|
int NextNonBlank(int);
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
int ParseLine(int, HalfMove *);
|
|
|
|
};
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
struct UnexpectedEOF : public std::exception {
|
|
|
|
const char *what() const throw() { return "Unexpected end of pgn file"; }
|
|
|
|
};
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
struct STRCheckFailed : public std::exception {
|
|
|
|
const char *what() const throw() {
|
|
|
|
return "Seven Tag Roster compliance check failed";
|
|
|
|
}
|
|
|
|
};
|
2022-01-23 20:57:28 +01:00
|
|
|
|
2022-01-24 15:29:22 +01:00
|
|
|
} // namespace pgnp
|