aboutsummaryrefslogtreecommitdiff
path: root/src/PGN.hpp
blob: d313f9185d03c169841d238341924f60baf8133f (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
#pragma once

#include "HalfMove.hpp"
#include "LargeFileStream.hpp"
#include "Types.hpp"
#include <algorithm>
#include <exception>
#include <fstream>
#include <unordered_map>

namespace pgnp {

class PGN {
private:
  /// @brief Contains tags data
  std::unordered_map<std::string, std::string> tags;
  /// @brief Contains the tags list in parsed order
  std::vector<std::string> tagkeys;
  /// @brief Contains game result (last PGN word)
  std::string result;
  /// @brief Contains the parsed PGN moves
  HalfMove *moves;
  /// @brief Contains the PGN data
  LargeFileStream pgn_content;
  /// @brief Contains the location of the end of the last parsed game (1 PGN
  /// file may have multiple games)
  loctype LastGameEndLoc;

public:
  PGN();
  ~PGN();
  void FromFile(std::string);
  void FromString(std::string);
  /**
   * Parse the next available game. Note that it raises a @a NoGameFound
   * exception if no more game is available. A call to this method flush all the
   * last parsed game data. Be careful.
   */
  void ParseNextGame();
  /// @brief Goto the next game while avoiding to parse entire game moves
  void GotoNextGame();
  /// @brief Check if PGN contains a specific tag
  bool HasTag(std::string);
  /// @brief Perform a Seven Tag Roster compliance check
  void STRCheck();
  /// @brief Dump parsed PGN into a string
  std::string Dump();
  /// @brief Retrieve parsed tag list
  std::vector<std::string> GetTagList();
  /// @brief Access to the value of a tag
  std::string GetTagValue(std::string);
  /// @brief Get game result based on the last PGN word
  std::string GetResult();
  /// @brief Fetch PGN moves as HalfMove structure
  void GetMoves(HalfMove *);

private:
  /// @brief Populate @a tags with by parsing the one starting at location in
  /// argument
  loctype ParseNextTag(loctype);
  /// @brief Parse a HalfMove at a specific location into @a pgn_content
  loctype ParseHalfMove(loctype, HalfMove *);
  /// @brief Parse a consecutive sequence of comment
  loctype ParseComment(loctype, HalfMove *);
  /// @brief Get the next non-blank char location ignoring line comments ('%'
  /// and ';')
  loctype GotoNextToken(loctype);
  /// @brief Goto the end of the current line
  loctype GotoEOL(loctype);
};

struct UnexpectedEOF : public std::exception {
  const char *what() const throw() { return "Unexpected end of pgn file"; }
};

struct InvalidTagName : public std::exception {
  const char *what() const throw() { return "Invalid tag name"; }
};

struct InvalidGameResult : public std::exception {
  const char *what() const throw() { return "Invalid game result"; }
};

struct NoGameFound : public std::exception {
  const char *what() const throw() { return "No game (or more game) found"; }
};

struct UnexpectedCharacter : public std::exception {
  std::string msg;
  UnexpectedCharacter(char actual, char required, loctype loc) {
    std::stringstream ss;
    ss << "Expected \'" << required << "\' at location " << loc
       << " but read \'" << actual << "\'";
    msg = ss.str();
  }
  const char *what() const throw() { return msg.c_str(); }
};

struct STRCheckFailed : public std::exception {
  const char *what() const throw() {
    return "Seven Tag Roster compliance check failed";
  }
};

} // namespace pgnp