Cute Chess  0.1
pgnstream.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef PGNSTREAM_H
19 #define PGNSTREAM_H
20 
21 #include <QtGlobal>
22 #include <QString>
23 class QIODevice;
24 namespace Chess { class Board; }
25 
26 
41 class LIB_EXPORT PgnStream
42 {
43  public:
45  enum Status
46  {
47  Ok,
48  ReadPastEnd
49  };
51  enum TokenType
52  {
78  Unknown
79  };
80 
87  explicit PgnStream(const QString& variant = "standard");
89  explicit PgnStream(QIODevice* device,
90  const QString& variant = "standard");
92  explicit PgnStream(const QByteArray* string,
93  const QString& variant = "standard");
94 
96  ~PgnStream();
97 
102  Chess::Board* board();
103 
105  QIODevice* device() const;
107  void setDevice(QIODevice* device);
108 
110  const QByteArray* string() const;
112  void setString(const QByteArray* string);
113 
115  QString variant() const;
120  bool setVariant(const QString& variant);
121 
123  bool isOpen() const;
124 
126  qint64 pos() const;
127 
129  qint64 lineNumber() const;
130 
132  void reset();
133 
135  char readChar();
145  void rewindChar();
150  void rewind();
156  bool seek(qint64 pos, qint64 lineNumber = 1);
157 
159  Status status() const;
160 
170  bool nextGame();
177  TokenType readNext();
178 
183  QByteArray tokenString() const;
188  TokenType tokenType() const;
189 
191  QByteArray tagName() const;
193  QByteArray tagValue() const;
194  private:
195  enum Phase
196  {
197  OutOfGame,
198  InTags,
199  InGame
200  };
201 
202  void parseUntil(const char* chars);
203  void parseTag();
204  void parseComment(char opBracket);
205 
206  Chess::Board* m_board;
207  qint64 m_pos;
208  qint64 m_lineNumber;
209  char m_lastChar;
210  QByteArray m_tokenString;
211  QByteArray m_tagName;
212  QByteArray m_tagValue;
213  TokenType m_tokenType;
214  QIODevice* m_device;
215  const QByteArray* m_string;
216  Status m_status;
217  Phase m_phase;
218 };
219 
220 #endif // PGNSTREAM_H
A class for reading games in PGN format from a text stream.
Definition: pgnstream.h:41
Definition: pgnstream.h:56
Definition: pgnstream.h:54
An internal chessboard class.
Definition: board.h:56
Definition: pgnstream.h:58
Definition: boardscene.h:28
Definition: pgnstream.h:76
TokenType
Definition: pgnstream.h:51
Status
Definition: pgnstream.h:45
Definition: pgnstream.h:70
The stream is operating normally.
Definition: pgnstream.h:47
Definition: pgnstream.h:64
Definition: pgnstream.h:72
Definition: pgnstream.h:74