Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PGNGAMESEXTRACTOR_HPP
- #define PGNGAMESEXTRACTOR_HPP
- #include <string>
- #include <vector>
- #include <fstream>
- #include <stdexcept>
- #include <boost/spirit/include/phoenix.hpp>
- #include <boost/spirit/include/qi.hpp>
- #include <boost/spirit/repository/include/qi_iter_pos.hpp>
- namespace loloof64 {
- namespace phx = boost::phoenix;
- namespace qi = boost::spirit::qi;
- namespace qr = boost::spirit::repository::qi;
- enum result_t { white_won, black_won, draw, undecided };
- struct pgn_tag {
- std::string key;
- std::string value;
- };
- struct game_move {
- unsigned move_number;
- std::string white_move;
- std::string black_move;
- result_t result;
- };
- struct pgn_game {
- std::vector<pgn_tag> header;
- std::vector<game_move> moves;
- };
- /**
- * Got this class there : http://marko-editor.com/articles/position_tracking/
- */
- template<typename Iterator>
- struct CurrentPos {
- CurrentPos() {
- save_start_pos = qi::omit[qr::iter_pos[
- phx::bind(&CurrentPos::setStartPos, this, qi::_1)]];
- current_pos = qr::iter_pos[
- qi::_val = phx::bind(&CurrentPos::getCurrentPos, this, qi::_1)];
- }
- qi::rule<Iterator> save_start_pos;
- qi::rule<Iterator, std::size_t()> current_pos;
- private:
- void setStartPos(const Iterator &iterator) {
- start_pos_ = iterator;
- }
- std::size_t getCurrentPos(const Iterator &iterator) {
- return std::distance(start_pos_, iterator);
- }
- Iterator start_pos_;
- };
- class PgnGamesExtractor {
- public:
- PgnGamesExtractor(std::string const &inputFilePath);
- /*
- Constructor may throw PgnParsingException (if bad pgn format) and InputFileException (if missing file)
- */
- std::vector<pgn_game> getGames() const { return games; }
- virtual ~PgnGamesExtractor();
- protected:
- private:
- std::vector<pgn_game> games;
- void parseInput(std::string const &inputFilePath);
- };
- class PgnParsingException : public virtual std::runtime_error {
- public:
- PgnParsingException(std::string message) : std::runtime_error(message) {}
- };
- class InputFileException : public virtual std::runtime_error {
- public:
- InputFileException(std::string message) : std::runtime_error(message) {}
- };
- }
- #endif // PGNGAMESEXTRACTOR_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement