2 * Program: pgn-extract: a Portable Game Notation (PGN) extractor.
3 * Copyright (C) 1994-2014 David Barnes
4 * This program 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 1, or (at your option)
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19 * http://www.cs.kent.ac.uk/people/staff/djb/
23 /* Type definitions required by multiple files. */
25 /* Define a type to represent different output formats.
26 * Currently represented are:
27 * SOURCE: the original source notation.
29 * CM: Chess Master input format.
30 * LALG: Long-algebraic, e.g. e2e4.
31 * HALG: Hyphenated long-algebraic, e.g. e2-e4.
32 * ELALG: Enhanced long-algebraic. Includes piece names, e.g. Ng1f3,
33 * and en-passant notation.
34 * UCI: UCI-compatible format - actually LALG.
36 typedef enum { SOURCE, SAN, EPD, CM, LALG, HALG, ELALG, UCI } OutputFormat;
38 /* Define a type to specify whether a move gives check, checkmate,
40 * checkmate implies check, but check does not imply that a move
43 typedef enum { NOCHECK, CHECK, CHECKMATE } CheckStatus;
45 /* Permit lists of strings, e.g. lists of comments,
48 typedef struct string_list {
50 struct string_list *next;
53 /* The following function is used for linking list items together. */
54 StringList *save_string_list_item(StringList *list,const char *str);
56 typedef struct comment_list{
58 struct comment_list *next;
61 typedef struct variation{
62 CommentList *prefix_comment;
64 CommentList *suffix_comment;
65 struct variation *next;
68 /* Define a maximum length for the text of moves.
71 #define MAX_MOVE_LEN 15
73 /* Retain the text of a move and any associated
77 /* @@@ This array is of type unsigned char,
78 * in order to accommodate full 8-bit letters without
81 unsigned char move[MAX_MOVE_LEN+1];
82 /* Class of move, e.g. PAWN_MOVE, PIECE_MOVE. */
89 /* captured_piece is EMPTY if there is no capture. */
91 /* promoted_piece is EMPTY if class is not PAWN_MOVE_WITH_PROMOTION. */
93 /* Whether this move gives check. */
94 CheckStatus check_status;
95 /* An EPD representation of the board immediately after this move
100 CommentList *Comment;
101 /* terminating_result hold the result of the current list of moves. */
102 char *terminating_result;
104 /* Pointers to the previous and next move.
105 * The extraction program does not need the prev field, but my
106 * intention is to build other interfaces that might need it.
107 * For instance, a game viewer would need to be able to move backwards
108 * and forwards through a game.
110 struct move *prev, *next;
114 /* Tags for this game. */
116 /* The maximum number of strings in tags. */
118 /* Any comment prefixing the game, between
119 * the tags and the moves.
121 CommentList *prefix_comment;
122 /* The hash value of the final position. */
123 HashCode final_hash_value;
124 /* An accumulated hash value, used to disambiguate false clashes
125 * of final_hash_value.
127 HashCode cumulative_hash_value;
128 /* Board hash value at fuzzy_move_depth, if required. */
129 HashCode fuzzy_duplicate_hash;
130 /* The move list of the game. */
132 /* Whether the moves have been checked, or not. */
133 Boolean moves_checked;
134 /* Whether the moves are ok, or not. */
136 /* if !moves_ok, the first ply at which an error was found.
137 * 0 => no error found.
142 /* Define a type to distinguish between CHECK files, NORMAL files,
144 * CHECKFILEs are those whose contents are not output.
145 * Their contents are used to check for duplicates in NORMALFILEs.
146 * An ECOFILE consists of ECO lines for classification.
148 typedef enum { NORMALFILE, CHECKFILE, ECOFILE } SourceFileType;
150 /* 0 = don't divide on ECO code.
151 * 1 = divide by letter.
152 * 2 = divide by letter and single digit.
153 * N > 1 = divide by letter and N-1 digits.
154 * In principle, it should be possible to expand the ECO classification
155 * with an arbitrary number of digits.
158 DONT_DIVIDE = 0, MIN_ECO_LEVEL = 1, MAX_ECO_LEVEL = 10
161 /* Define a type to describe which tags are to be output.
162 * This used to be handled by the Boolean seven_tag_roster field
163 * in GlobalState but there are now different forms of output
167 ALL_TAGS = 0, SEVEN_TAG_ROSTER = 1, NO_TAGS = 2
170 /* This structure holds details of the program state.
171 * Most of these fields are set from the program's arguments.
174 /* Whether we are skipping the current game - typically because
175 * of an error in its text.
177 Boolean skipping_current_game;
178 /* Whether to check, but not write the converted output. */
180 /* Whether to print a running commentary to logfile. */
182 /* Whether to keep NAGs along with moves. */
184 /* Whether to keep comments along with moves. */
185 Boolean keep_comments;
186 /* Whether to keep variations along with moves. */
187 Boolean keep_variations;
188 /* Which tags are to be output. */
189 TagOutputForm tag_output_format;
190 /* Whether to match permutations of textual variations or not. */
191 Boolean match_permutations;
192 /* Whether we are matching positional variations or not. */
193 Boolean positional_variations;
194 /* Whether we are using Soundex matching or not. */
196 /* Whether to suppress duplicate game scores. */
197 Boolean suppress_duplicates;
198 /* Whether to suppress unique game scores. */
199 Boolean suppress_originals;
200 /* Whether to use fuzzy matching for duplicates. */
201 Boolean fuzzy_match_duplicates;
202 /* At what depth to use fuzzy matching. */
203 int fuzzy_match_depth;
204 /* Whether to check the tags for matches. */
206 /* Whether to add ECO codes. */
208 /* Whether an ECO file is currently being parsed. */
209 Boolean parsing_ECO_file;
210 /* Which level to divide the output. */
211 EcoDivision ECO_level;
212 /* What form to write the output in. */
213 OutputFormat output_format;
214 /* Maximum output line length. */
215 unsigned max_line_length;
216 /* Whether to use a virtual hash table or not. */
217 Boolean use_virtual_hash_table;
218 /* Whether to match on the number of moves in a game. */
219 Boolean check_move_bounds;
220 /* Whether to match only games ending in checkmate. */
221 Boolean match_only_checkmate;
222 /* Whether to match only games ending in stalemate. */
223 Boolean match_only_stalemate;
224 /* Whether to output move numbers in the output. */
225 Boolean keep_move_numbers;
226 /* Whether to output results in the output. */
227 Boolean keep_results;
228 /* Whether to keep check and mate characters in the output. */
230 /* Whether to output an evaluation value after each move. */
231 Boolean output_evaluation;
232 /* Whether to keep games which have incorrect moves. */
233 Boolean keep_broken_games;
234 /* Maximum depth to which to search for positional variations.
235 * This is picked up from the length of variations in the positional
238 unsigned depth_of_positional_search;
239 unsigned long num_games_processed;
240 unsigned long num_games_matched;
241 /* How many games to store in each file. */
242 unsigned games_per_file;
243 /* Which is the next file number. */
244 unsigned next_file_number;
245 /* Lower and upper bounds for moves if check_move_bounds. */
246 unsigned lower_move_bound, upper_move_bound;
247 /* Limit to the number of plies to appear in the output. */
248 int output_ply_limit;
249 /* Which single game to output (matching_game_number > 0) */
250 unsigned long matching_game_number;
251 /* Whether to output a FEN string at the end of the game. */
252 Boolean output_FEN_string;
253 /* Whether to add a FEN comment after every move. */
254 Boolean add_FEN_comments;
255 /* Whether to add a 'matching position' comment. */
256 Boolean add_position_match_comments;
257 /* Whether to include a tag with the total ply count of the game. */
258 Boolean output_total_plycount;
259 /* Whether to add a HashCode tag. */
260 Boolean add_hashcode_tag;
261 /* The comment to use for position matches, if required. */
262 const char *position_match_comment;
263 /* Current input file name. */
264 const char *current_input_file;
265 /* Whether this is a CHECKFILE or a NORMALFILE. */
266 SourceFileType current_file_type;
267 /* File of ECO lines. */
268 const char *eco_file;
269 /* Where to write the extracted games. */
271 /* Output file name. */
272 const char *output_filename;
273 /* Where to write errors and running commentary. */
275 /* Where to write duplicate games. */
276 FILE *duplicate_file;
277 /* Where to write games that don't match the criteria. */
278 FILE *non_matching_file;
281 /* Provide access to the global state that has been set
282 * through command line arguments.
284 extern StateInfo GlobalState;
285 FILE *must_open_file(const char *filename,const char *mode);