Advertisement
loloof64

Parsing Pgn File and getting progress (header file) solved

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