Scid  4.6.5
codec_pgn.h
Go to the documentation of this file.
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 class CodecPgn : public CodecProxy<CodecPgn> {
38  PgnParser parser_;
39  std::string filename_;
40  size_t fileSize_;
41  MFile file_;
42 
43 public:
44  Codec getType() override { return ICodecDatabase::PGN; }
45 
46  std::vector<std::string> getFilenames() override {
47  return std::vector<std::string>(1, filename_);
48  };
49 
50  errorT flush() override {
51  file_.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  errorT open(const char* filename, fileModeT fmode) {
64  filename_ = filename;
65  if (filename_.empty()) return ERROR_FileOpen;
66 
67  errorT res = (fmode != FMODE_Create)
68  ? file_.Open(filename, fmode)
69  : file_.Create(filename, FMODE_Both);
70 
71  if (res == OK) {
72  if (fmode == FMODE_Create) {
73  fileSize_ = 0;
74  } else {
75  fileSize_ = fileSize(filename, "");
76  if (fileSize_ == 0 && !file_.EndOfFile())
77  return ERROR_FileOpen;
78  }
79  parser_.Reset(&file_);
80  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  */
95  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  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  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  */
125  std::pair<const char*, unsigned> pgn = game->WriteToPGN(75, true);
126 
127  file_.Seek(fileSize_);
128  errorT err = file_.WriteNBytes(pgn.first, pgn.second);
129  if (err == OK) fileSize_ += pgn.second;
130  return err;
131  }
132 };
133 
134 #if !CPP11_SUPPORT
135 #undef override
136 #endif
137 
138 #endif
#define PGN_STYLE_COMMENTS
Definition: game.h:202
Definition: mfile.h:52
const errorT OK
Definition: error.h:23
std::vector< std::string > getFilenames() override
Returns the full path of the files used by the database.
Definition: codec_pgn.h:46
errorT flush() override
Writes all pending output to the files.
Definition: codec_pgn.h:50
errorT dyn_addGame(Game *game)
Add a game into the database.
Definition: codec_pgn.h:121
errorT Flush()
Definition: mfile.cpp:88
uint fileSize(const char *name, const char *suffix)
Definition: misc.cpp:764
Codec getType() override
Returns the Codec type.
Definition: codec_pgn.h:44
errorT Seek(uint position)
Definition: mfile.cpp:57
#define PGN_STYLE_VARS
Definition: game.h:203
errorT Open(const char *name, fileModeT fmode)
Definition: mfile.cpp:95
std::pair< size_t, size_t > parseProgress()
Returns info about the parsing progress.
Definition: codec_pgn.h:103
std::pair< const char *, unsigned > WriteToPGN(uint lineWidth=0, bool NewLineAtEnd=false, bool newLineToSpaces=true)
Definition: game.cpp:2874
#define PGN_STYLE_SCIDFLAGS
Definition: game.h:210
uint BytesUsed(void)
Definition: pgnparse.h:91
Implements the CodecProxy class, which serves as base class for non-native databases.
void SetPgnFormat(gameFormatT gf)
Definition: game.h:513
const char * ErrorMessages()
Definition: pgnparse.h:93
unsigned short errorT
Definition: error.h:20
Base class for non-native databases.
Definition: codec_proxy.h:48
Definition: game.h:227
void IgnorePreGameText()
Definition: pgnparse.h:95
errorT flush() override
Writes all pending output to the files.
Definition: codec_memory.h:100
errorT Create(const char *name, fileModeT fmode)
Definition: mfile.cpp:137
errorT ParseGame(Game *game)
Definition: pgnparse.cpp:1215
bool EndOfFile()
Definition: mfile.h:108
errorT open(const char *filename, fileModeT fmode)
Opens/creates a PGN database.
Definition: codec_pgn.h:63
fileModeT
Definition: common.h:144
Definition: pgn.tcl:4
const errorT ERROR_FileOpen
Definition: error.h:31
const char * parseErrors()
Returns the list of errors produced by parseNext() calls.
Definition: codec_pgn.h:110
#define PGN_STYLE_TAGS
Definition: game.h:201
errorT parseNext(Game *g)
Reads the next game.
Definition: codec_pgn.h:94
errorT WriteNBytes(const char *str, uint length)
Definition: mfile.cpp:155
void ResetPgnStyle(void)
Definition: game.h:503