LCOV - code coverage report
Current view: top level - src - codec_pgn.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 29 30 96.7 %
Date: 2017-06-21 14:32:49 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2016-2017  Fulvio Benini
       3             : 
       4             :  * This file is part of Scid (Shane's Chess Information Database).
       5             :  *
       6             :  * Scid is free software: you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation.
       9             :  *
      10             :  * Scid is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :  * GNU General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU General Public License
      16             :  * along with Scid.  If not, see <http://www.gnu.org/licenses/>.
      17             :  *
      18             :  */
      19             : 
      20             : /** @file
      21             :  * Implements the CodecPgn class, which manages the databases encoded in PGN
      22             :  * format.
      23             :  */
      24             : 
      25             : #ifndef CODEC_PGN_H
      26             : #define CODEC_PGN_H
      27             : 
      28             : #include "codec_proxy.h"
      29             : #include "common.h"
      30             : #include "mfile.h"
      31             : #include "pgnparse.h"
      32             : 
      33             : #if !CPP11_SUPPORT
      34             : #define override
      35             : #endif
      36             : 
      37         104 : class CodecPgn : public CodecProxy<CodecPgn> {
      38             :         PgnParser parser_;
      39             :         std::string filename_;
      40             :         size_t fileSize_;
      41             :         MFile file_;
      42             : 
      43             : public:
      44           4 :         Codec getType() override { return ICodecDatabase::PGN; }
      45             : 
      46           9 :         std::vector<std::string> getFilenames() override {
      47          18 :                 return std::vector<std::string>(1, filename_);
      48             :         };
      49             : 
      50           4 :         errorT flush() override {
      51           4 :                 file_.Flush();
      52           4 :                 return CodecProxy<CodecPgn>::flush();
      53             :         }
      54             : 
      55             :         /**
      56             :          * Opens/creates a PGN database.
      57             :          * After successfully opening/creating the file, the PgnParser object @e
      58             :          * parser_ is initialized to be ready for parseNext() calls.
      59             :          * @param filename: full path of the pgn file to be opened.
      60             :          * @param fMode:    valid file access mode.
      61             :          * @returns OK in case of success, an @p errorT code otherwise.
      62             :          */
      63          13 :         errorT open(const char* filename, fileModeT fmode) {
      64          26 :                 filename_ = filename;
      65          26 :                 if (filename_.empty()) return ERROR_FileOpen;
      66             : 
      67             :                 errorT res = (fmode != FMODE_Create)
      68          17 :                                  ? file_.Open(filename, fmode)
      69          17 :                                  : file_.Create(filename, FMODE_Both);
      70             : 
      71          12 :                 if (res == OK) {
      72          11 :                         if (fmode == FMODE_Create) {
      73           5 :                                 fileSize_ = 0;
      74             :                         } else {
      75           6 :                                 fileSize_ = fileSize(filename, "");
      76           6 :                                 if (fileSize_ == 0 && !file_.EndOfFile())
      77             :                                         return ERROR_FileOpen;
      78             :                         }
      79          11 :                         parser_.Reset(&file_);
      80          11 :                         parser_.IgnorePreGameText();
      81             :                 }
      82             : 
      83             :                 return res;
      84             :         }
      85             : 
      86             :         /**
      87             :          * Reads the next game.
      88             :          * @param g: valid pointer to the Game object where the data will be stored.
      89             :          * @returns
      90             :          * - OK on success.
      91             :          * - ERROR_NotFound if there are no more games to be read.
      92             :          * - ERROR code if the game cannot be read and was skipped.
      93             :          */
      94             :         errorT parseNext(Game* g) {
      95        2011 :                 return parser_.ParseGame(g);
      96             :         }
      97             : 
      98             :         /**
      99             :          * Returns info about the parsing progress.
     100             :          * @returns a pair<size_t, size_t> where first element is the quantity of
     101             :          * data parsed and second one is the total amount of data of the database.
     102             :          */
     103             :         std::pair<size_t, size_t> parseProgress() {
     104           0 :                 return std::make_pair(size_t(parser_.BytesUsed()) / 1024, fileSize_ / 1024);
     105             :         }
     106             : 
     107             :         /**
     108             :          * Returns the list of errors produced by parseNext() calls.
     109             :          */
     110             :         const char* parseErrors() {
     111          22 :                 return parser_.ErrorMessages();
     112             :         }
     113             : 
     114             : public:
     115             :         /**
     116             :          * Add a game into the database.
     117             :          * The @e game is encoded in pgn format and appended at the end of @e file_.
     118             :          * @param game: valid pointer to a Game object with the new data.
     119             :          * @returns OK in case of success, an @e errorT code otherwise.
     120             :          */
     121        2000 :         errorT dyn_addGame(Game* game) {
     122        4000 :                 game->SetPgnFormat(PGN_FORMAT_Plain);
     123             :                 game->ResetPgnStyle(PGN_STYLE_TAGS | PGN_STYLE_VARS |
     124        4000 :                                     PGN_STYLE_COMMENTS | PGN_STYLE_SCIDFLAGS);
     125        2000 :                 std::pair<const char*, unsigned> pgn = game->WriteToPGN(75, true);
     126             : 
     127        2000 :                 file_.Seek(fileSize_);
     128        2000 :                 errorT err = file_.WriteNBytes(pgn.first, pgn.second);
     129        2000 :                 if (err == OK) fileSize_ += pgn.second;
     130        2000 :                 return err;
     131             :         }
     132             : };
     133             : 
     134             : #if !CPP11_SUPPORT
     135             : #undef override
     136             : #endif
     137             : 
     138             : #endif

Generated by: LCOV version 1.12