Scid  4.6.5
pgnparse.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: pgnparse.h
4 // PgnParser class
5 //
6 // Part of: Scid (Shane's Chess Information Database)
7 // Version: 3.5
8 //
9 // Notice: Copyright (c) 2001-2003 Shane Hudson. All rights reserved.
10 //
11 // Author: Shane Hudson (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 //
16 // PgnParser:
17 // This class implements a PGN-format Parser which can read PGN text
18 // from an open file or from a zero-terminated string, and convert
19 // it into a Scid Game object.
20 //
21 
22 #ifndef SCID_PGNPARSE_H
23 #define SCID_PGNPARSE_H
24 
25 #include "common.h"
26 #include "tokens.h"
27 #include "mfile.h"
28 #include <string>
29 
30 class Game;
31 
32 #define MAX_UNGETCHARS 16
33 static const uint MAX_IGNORED_TAGS = 16;
34 
35 class PgnParser
36 {
37  private:
38 
39  MFile * InFile;
40  const char * InBuffer;
41  const char * InCurrent;
42  uint LineCounter;
43  uint GameCounter;
44  int EndChar;
45  uint BytesSeen;
46  std::string ErrorBuffer;
47  uint NumErrors;
48 
49  enum { PARSE_Searching, PARSE_Header, PARSE_Game } ParseMode;
50 
51  bool StorePreGameText;
52  bool EndOfInputWarnings;
53  bool ResultWarnings;
54 
55  uint NumIgnoredTags;
56  char * IgnoredTags [MAX_IGNORED_TAGS];
57 
58  uint UnGetCount;
59  int UnGetCh [MAX_UNGETCHARS];
60 
61  inline int GetChar();
62  inline void UnGetChar (int ch);
63 
64  void Init (const char * inbuffer);
65  void Reset();
66  void LogError (const char * errMessage, const char * text);
67  void GetLine (char * buffer, uint bufSize);
68  std::string GetComment();
69  void GetRestOfSuffix (char * buffer, char firstChar);
70  void GetRestOfWord_NoDots (char * buffer);
71  void GetRestOfWord_WithDots (char * buffer);
72  void GetRestOfWord_Letters (char * buffer);
73  errorT ExtractPgnTag (const char * buffer, Game * game);
74 
75  bool EndOfInput();
76  tokenT GetRestOfCastling (char * buffer);
77  tokenT GetRestOfMove (char * buffer);
78  tokenT GetRestOfPawnMove (char * buffer);
79  tokenT GetGameToken (char * buffer, uint bufSize);
80 
81  public:
82  // Constructors: PgnParser is initialised with a file pointer or
83  // a pointer to a buffer, or it defaults to an empty buffer.
84  PgnParser (void) { Init ((const char *) ""); }
85  PgnParser (const char * inbuffer) { Init (inbuffer); }
87 
88  void Reset (MFile * infile);
89  void Reset (const char * inbuffer);
90 
91  uint BytesUsed (void) { return BytesSeen; }
92  uint ErrorCount() { return NumErrors; }
93  const char* ErrorMessages() { return ErrorBuffer.c_str(); }
94  void KeepPreGameText() { StorePreGameText = true; }
95  void IgnorePreGameText() { StorePreGameText = false; }
96  void SetPreGameText (bool b) { StorePreGameText = b; }
97  void SetEndOfInputWarnings (bool b) { EndOfInputWarnings = b; }
98  void SetResultWarnings (bool b) { ResultWarnings = b; }
99 
100  void AddIgnoredTag (const char * tag);
101  void ClearIgnoredTags ();
102  bool IsIgnoredTag (const char * tag);
103 
104  tokenT GetNextToken (char * buffer, uint bufSize);
105 
106  errorT ParseGame (Game * game);
107  errorT ParseMoves (Game * game);
108  errorT ParseMoves (Game * game, char * buffer, uint bufSize);
109 
110 };
111 
112 #endif // idndef SCID_PGNPARSE_H
113 
114 //////////////////////////////////////////////////////////////////////
115 // EOF: pgnparse.h
116 //////////////////////////////////////////////////////////////////////
117 
Definition: mfile.h:52
PgnParser(void)
Definition: pgnparse.h:84
void AddIgnoredTag(const char *tag)
Definition: pgnparse.cpp:152
#define MAX_UNGETCHARS
Definition: pgnparse.h:32
void SetEndOfInputWarnings(bool b)
Definition: pgnparse.h:97
tokenT GetNextToken(char *buffer, uint bufSize)
Definition: pgnparse.cpp:967
void KeepPreGameText()
Definition: pgnparse.h:94
bool IsIgnoredTag(const char *tag)
Definition: pgnparse.cpp:170
errorT ParseMoves(Game *game)
Definition: pgnparse.cpp:1028
~PgnParser()
Definition: pgnparse.h:86
PgnParser(const char *inbuffer)
Definition: pgnparse.h:85
uint32_t uint
Definition: common.h:99
uint tokenT
Definition: tokens.h:23
uint BytesUsed(void)
Definition: pgnparse.h:91
void ClearIgnoredTags()
Definition: pgnparse.cpp:161
const char * ErrorMessages()
Definition: pgnparse.h:93
unsigned short errorT
Definition: error.h:20
Definition: game.h:227
void SetResultWarnings(bool b)
Definition: pgnparse.h:98
void IgnorePreGameText()
Definition: pgnparse.h:95
errorT ParseGame(Game *game)
Definition: pgnparse.cpp:1215
void SetPreGameText(bool b)
Definition: pgnparse.h:96
uint ErrorCount()
Definition: pgnparse.h:92