Advertisement
loloof64

Parsing Pgn File and getting progress (header file)

Dec 12th, 2015
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #ifndef PGNGAMESEXTRACTOR_HPP
  2. #define PGNGAMESEXTRACTOR_HPP
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <fstream>
  7. #include <stdexcept>
  8.  
  9. #include <boost/fusion/adapted/std_tuple.hpp>
  10. #include <boost/spirit/include/phoenix.hpp>
  11.  
  12. #include <boost/spirit/include/qi.hpp>
  13. #include <boost/spirit/repository/include/qi_iter_pos.hpp>
  14.  
  15. namespace loloof64 {
  16.  
  17.     namespace phx = boost::phoenix;
  18.     namespace qi = boost::spirit::qi;
  19.  
  20.  
  21.     /*
  22.      * This class has been taken from http://marko-editor.com/articles/position_tracking/
  23.     */
  24.     template<typename Iterator>
  25.     struct CurrentPos {
  26.       CurrentPos() {
  27.         save_start_pos = qi::omit[boost::spirit::repository::qi::iter_pos[
  28.                 phx::bind(&CurrentPos::setStartPos, this, qi::_1)]];
  29.         current_pos = boost::spirit::repository::qi::iter_pos[
  30.                 qi::_val = phx::bind(&CurrentPos::getCurrentPos, this, qi::_1)];
  31.       }
  32.  
  33.       qi::rule<Iterator> save_start_pos;
  34.       qi::rule<Iterator, std::size_t()> current_pos;
  35.  
  36.     private:
  37.       void setStartPos(const Iterator &iterator) {
  38.         start_pos_ = iterator;
  39.       }
  40.  
  41.       std::size_t getCurrentPos(const Iterator &iterator) {
  42.         return std::distance(start_pos_, iterator);
  43.       }
  44.  
  45.       Iterator start_pos_;
  46.     };
  47.  
  48.     enum result_t { white_won, black_won, draw, undecided };
  49.  
  50.     struct pgn_tag {
  51.         std::string key;
  52.         std::string value;
  53.     };
  54.  
  55.     struct game_move {
  56.         unsigned move_number;
  57.         std::string white_move;
  58.         std::string black_move;
  59.         result_t result;
  60.     };
  61.  
  62.     struct pgn_game {
  63.         std::vector<pgn_tag> header;
  64.         std::vector<game_move> moves;
  65.     };
  66.  
  67.     class PgnGamesExtractor {
  68.       public:
  69.         PgnGamesExtractor(std::string inputFilePath);
  70.         PgnGamesExtractor(std::istream &inputFile);
  71.         /*
  72.         Both constructos may throw PgnParsingException (if bad pgn format) and InputFileException (if missing file)
  73.         */
  74.         std::vector<pgn_game> getGames() const { return games; }
  75.         virtual ~PgnGamesExtractor();
  76.  
  77.       protected:
  78.       private:
  79.         std::vector<pgn_game> games;
  80.         void parseInput(std::istream &inputFile);
  81.     };
  82.  
  83.     class PgnParsingException : public virtual std::runtime_error {
  84.       public:
  85.         PgnParsingException(std::string message) : std::runtime_error(message) {}
  86.     };
  87.  
  88.     class InputFileException : public virtual std::runtime_error {
  89.       public:
  90.         InputFileException(std::string message) : std::runtime_error(message) {}
  91.     };
  92. }
  93.  
  94. #endif // PGNGAMESEXTRACTOR_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement