]> git.sesse.net Git - pgn-extract/blob - typedef.h
Add support for outputting positions in my own bit-packed FEN format.
[pgn-extract] / typedef.h
1 /*
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19  *  http://www.cs.kent.ac.uk/people/staff/djb/
20  *
21  */
22
23         /* Type definitions required by multiple files. */
24
25     /* Define a type to represent different output formats.
26      * Currently represented are:
27      *     SOURCE: the original source notation.
28      *           SAN: SAN.
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.
35      */
36 typedef enum { SOURCE, SAN, EPD, CM, LALG, HALG, ELALG, UCI, SESSE_BIN } OutputFormat;
37
38     /* Define a type to specify whether a move gives check, checkmate,
39      * or nocheck.
40      * checkmate implies check, but check does not imply that a move
41      * is not checkmate.
42      */
43 typedef enum { NOCHECK, CHECK, CHECKMATE } CheckStatus;
44
45         /* Permit lists of strings, e.g. lists of comments,
46          * list of NAGs, etc.
47          */
48 typedef struct string_list {
49     const char *str;
50     struct string_list *next;
51 } StringList;
52
53 /* The following function is used for linking list items together. */
54 StringList *save_string_list_item(StringList *list,const char *str);
55
56 typedef struct comment_list{
57     StringList *Comment;
58     struct comment_list *next;
59 } CommentList;
60
61 typedef struct variation{
62     CommentList *prefix_comment;
63     struct move *moves;
64     CommentList *suffix_comment;
65     struct variation *next;
66 } Variation;
67
68 /* Define a maximum length for the text of moves.
69  * This is generous.
70  */
71 #define MAX_MOVE_LEN 15
72
73         /* Retain the text of a move and any associated 
74          * NAGs and comments.
75          */
76 typedef struct move{
77     /* @@@ This array is of type unsigned char,
78      * in order to accommodate full 8-bit letters without
79      * sign extension.
80      */
81     unsigned char move[MAX_MOVE_LEN+1];
82     /* Class of move, e.g. PAWN_MOVE, PIECE_MOVE. */
83     MoveClass class;
84     Col from_col;
85     Rank from_rank;
86     Col to_col;
87     Rank to_rank;
88     Piece piece_to_move;
89     /* captured_piece is EMPTY if there is no capture. */
90     Piece captured_piece;
91     /* promoted_piece is EMPTY if class is not PAWN_MOVE_WITH_PROMOTION. */
92     Piece promoted_piece;
93     /* Whether this move gives check. */
94     CheckStatus check_status;
95     /* An EPD representation of the board immediately after this move
96      * has been played.
97      */
98     char *epd;
99     /* Same as epd, but in our special binary packed format.
100      * Not zero-terminated, since it is binary.
101      */
102     char *bpfen;
103     int bpfen_len;
104     StringList *Nags;
105     CommentList *Comment;
106     /* terminating_result hold the result of the current list of moves. */
107     char *terminating_result;
108     Variation *Variants;
109     /* Pointers to the previous and next move.
110      * The extraction program does not need the prev field, but my
111      * intention is to build other interfaces that might need it.
112      * For instance, a game viewer would need to be able to move backwards
113      * and forwards through a game.
114      */
115     struct move *prev, *next;
116 } Move;
117
118 typedef struct {
119     /* Tags for this game. */
120     char **tags;
121     /* The maximum number of strings in tags. */
122     int tags_length;
123     /* Any comment prefixing the game, between
124      * the tags and the moves.
125      */
126     CommentList *prefix_comment;
127     /* The hash value of the final position. */
128     HashCode final_hash_value;
129     /* An accumulated hash value, used to disambiguate false clashes
130      * of final_hash_value.
131      */
132     HashCode cumulative_hash_value;
133     /* Board hash value at fuzzy_move_depth, if required. */
134     HashCode fuzzy_duplicate_hash;
135     /* The move list of the game. */
136     Move *moves;
137     /* Whether the moves have been checked, or not. */
138     Boolean moves_checked;
139     /* Whether the moves are ok, or not. */
140     Boolean moves_ok;
141     /* if !moves_ok, the first ply at which an error was found.
142      * 0 => no error found.
143      */
144     int error_ply;
145 } Game;
146
147 /* Define a type to distinguish between CHECK files, NORMAL files,
148  * and ECO files.
149  * CHECKFILEs are those whose contents are not output.
150  * Their contents are used to check for duplicates in NORMALFILEs.
151  * An ECOFILE consists of ECO lines for classification.
152  */
153 typedef enum { NORMALFILE, CHECKFILE, ECOFILE } SourceFileType;
154
155 /*    0 = don't divide on ECO code.
156  *    1 = divide by letter.
157  *    2 = divide by letter and single digit.
158  *    N > 1 = divide by letter and N-1 digits.
159  *    In principle, it should be possible to expand the ECO classification
160  *    with an arbitrary number of digits.
161  */
162 typedef enum {
163     DONT_DIVIDE = 0, MIN_ECO_LEVEL = 1, MAX_ECO_LEVEL = 10
164 } EcoDivision;
165
166 /* Define a type to describe which tags are to be output.
167  * This used to be handled by the Boolean seven_tag_roster field
168  * in GlobalState but there are now different forms of output
169  * available.
170  */
171 typedef enum {
172     ALL_TAGS = 0, SEVEN_TAG_ROSTER = 1, NO_TAGS = 2
173 } TagOutputForm;
174
175 /* This structure holds details of the program state.
176  * Most of these fields are set from the program's arguments.
177  */
178 typedef struct {
179     /* Whether we are skipping the current game - typically because
180      * of an error in its text.
181      */
182     Boolean skipping_current_game;
183     /* Whether to check, but not write the converted output. */
184     Boolean check_only;
185     /* Whether to print a running commentary to logfile. */
186     Boolean verbose;
187     /* Whether to keep NAGs along with moves. */
188     Boolean keep_NAGs;
189     /* Whether to keep comments along with moves. */
190     Boolean keep_comments;
191     /* Whether to keep variations along with moves. */
192     Boolean keep_variations;
193     /* Which tags are to be output. */
194     TagOutputForm tag_output_format;
195     /* Whether to match permutations of textual variations or not. */
196     Boolean match_permutations;
197     /* Whether we are matching positional variations or not. */
198     Boolean positional_variations;
199     /* Whether we are using Soundex matching or not. */
200     Boolean use_soundex;
201     /* Whether to suppress duplicate game scores. */
202     Boolean suppress_duplicates;
203     /* Whether to suppress unique game scores. */
204     Boolean suppress_originals;
205     /* Whether to use fuzzy matching for duplicates. */
206     Boolean fuzzy_match_duplicates;
207     /* At what depth to use fuzzy matching. */
208     int fuzzy_match_depth;
209     /* Whether to check the tags for matches. */
210     Boolean check_tags;
211     /* Whether to add ECO codes. */
212     Boolean add_ECO;
213     /* Whether an ECO file is currently being parsed. */
214     Boolean parsing_ECO_file;
215     /* Which level to divide the output. */
216     EcoDivision ECO_level;
217     /* What form to write the output in. */
218     OutputFormat output_format;
219     /* Maximum output line length. */
220     unsigned max_line_length;
221     /* Whether to use a virtual hash table or not. */
222     Boolean use_virtual_hash_table;
223     /* Whether to match on the number of moves in a game. */
224     Boolean check_move_bounds;
225     /* Whether to match only games ending in checkmate. */
226     Boolean match_only_checkmate;
227     /* Whether to match only games ending in stalemate. */
228     Boolean match_only_stalemate;
229     /* Whether to output move numbers in the output. */
230     Boolean keep_move_numbers;
231     /* Whether to output results in the output. */
232     Boolean keep_results;
233     /* Whether to keep check and mate characters in the output. */
234     Boolean keep_checks;
235     /* Whether to output an evaluation value after each move. */
236     Boolean output_evaluation;
237     /* Whether to keep games which have incorrect moves. */
238     Boolean keep_broken_games;
239     /* Maximum depth to which to search for positional variations.
240      * This is picked up from the length of variations in the positional
241      * variations file.
242      */
243     unsigned depth_of_positional_search;
244     unsigned long num_games_processed;
245     unsigned long num_games_matched;
246     /* How many games to store in each file. */
247     unsigned games_per_file;
248     /* Which is the next file number. */
249     unsigned next_file_number;
250     /* Lower and upper bounds for moves if check_move_bounds. */
251     unsigned lower_move_bound, upper_move_bound;
252     /* Limit to the number of plies to appear in the output. */
253     int output_ply_limit;
254     /* Which single game to output (matching_game_number > 0) */
255     unsigned long matching_game_number;
256     /* Whether to output a FEN string at the end of the game. */
257     Boolean output_FEN_string;
258     /* Whether to add a FEN comment after every move. */
259     Boolean add_FEN_comments;
260     /* Whether to add a 'matching position' comment. */
261     Boolean add_position_match_comments;
262     /* Whether to include a tag with the total ply count of the game. */
263     Boolean output_total_plycount;
264     /* Whether to add a HashCode tag. */
265     Boolean add_hashcode_tag;
266     /* The comment to use for position matches, if required. */
267     const char *position_match_comment;
268     /* Current input file name. */
269     const char *current_input_file;
270     /* Whether this is a CHECKFILE or a NORMALFILE. */
271     SourceFileType current_file_type;
272     /* File of ECO lines. */
273     const char *eco_file;
274     /* Where to write the extracted games. */
275     FILE *outputfile;
276     /* Output file name. */
277     const char *output_filename;
278     /* Where to write errors and running commentary. */
279     FILE *logfile;
280     /* Where to write duplicate games. */
281     FILE *duplicate_file;
282     /* Where to write games that don't match the criteria. */
283     FILE *non_matching_file;
284 } StateInfo;
285
286 /* Provide access to the global state that has been set
287  * through command line arguments.
288  */
289 extern StateInfo GlobalState;
290 FILE *must_open_file(const char *filename,const char *mode);