1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2011 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define PGN_TIME_FORMAT "%Y.%m.%d"
23 #define MAX_PGN_LINE_LEN 255
24 #define MAX_SAN_MOVE_LEN 7
27 #define VALIDRANK VALIDFILE
28 #define VALIDFILE(f) (f >= 1 && f <= 8)
29 #define RANKTOBOARD(r) (8 - r)
30 #define FILETOBOARD(c) (c - 1)
31 #define RANKTOINT(r) (r - '0')
32 #define FILETOINT(c) (c - ('a' - 1))
33 #define VALIDROW(r) (r >= '1' && r <= '8')
34 #define VALIDCOL(c) (c >= 'a' && c <= 'h')
35 #define INTTORANK(r) (r + '0')
36 #define INTTOFILE(f) (f + ('a' - 1))
38 #define SET_FLAG(var, f) (var |= f)
39 #define CLEAR_FLAG(var, f) (var &= ~(f))
40 #define TOGGLE_FLAG(var, f) (var ^= (f))
41 #define TEST_FLAG(var, f) (var & f)
44 OPEN_SQUARE
, PAWN
, BISHOP
, ROOK
, KNIGHT
, QUEEN
, KING
52 #define GF_PERROR 0x0001 /* Parse error for this game. */
53 #define GF_ENPASSANT 0x0002 /* For En Passant validation. */
54 #define GF_GAMEOVER 0x0004 /* End of game. */
55 #define GF_WK_CASTLE 0x0008
56 #define GF_WQ_CASTLE 0x0010
57 #define GF_BK_CASTLE 0x0020
58 #define GF_BQ_CASTLE 0x0040
59 #define GF_BLACK_OPENING 0x0080
65 unsigned char icon
; // The piece.
66 unsigned char valid
: 1, // != 0 if this square is a valid move for the
75 char *name
; // Tag name.
76 char *value
; // Tag value.
82 * g.hp is the pointer to the current history which may be .rav for
83 * Recursive Annotated Variations. The depth of recursion is kept track of in
86 typedef struct history
{
87 char *move
; // The SAN move text.
88 char *comment
; // Annotation for this move.
89 unsigned char nag
[MAX_PGN_NAG
]; // Numeric Annotation Glyph.
90 char *fen
; // Of the current board.
91 struct history
**rav
; // Variation of the current move.
95 * Keeps game state history for the root move.
98 char *fen
; // Game board state.
100 unsigned short hindex
;
101 HISTORY
**hp
; // Pointer to the root move.
105 * This is an array of 'games' structures. One for each game in a file, or
108 typedef struct game_s
{
109 TAG
**tag
; // Roster tags.
110 HISTORY
**history
; // Move history for this game.
111 HISTORY
**hp
; // History pointer pointing to the location
112 // in *history used mainly for RAV.
113 RAV
*rav
; // Saved game states for the root move of RAV.
114 int ravlevel
; // An index to *rav.
115 unsigned short hindex
; // Current move in *hp.
116 unsigned short flags
; // Game flags.
117 unsigned short oflags
; // Game flags for the previous move.
118 unsigned char side
: 1, // This playing side. BLACK or WHITE.
119 turn
: 1; // BLACK or WHITE.
120 unsigned short ply
; // Move count.
121 void *data
; /* User data associated with this game. Must
122 * be freed by the user. */
123 // internal validation state
139 * Global GAME array. pgn_new_game() appends to this array.
144 * 'gindex' and 'gtotal' are the current and total number of games in 'game'.
149 * Library configuration flags. These will affect all games.
153 * When pgn_write() is called to write a game, write reduced PGN format.
154 * This will only write the seven tag roster and move text skipping any
155 * annotation. The type for this flag is an int.
160 * The number of full moves to write per line. If 0 then pgn_write() will
161 * write as many as possible within 80 columns. The type for this flag
167 * Normally when a parse error occurs in a game the game is flagged with
168 * GF_PERROR and the rest of the game is discarded and processing of the
169 * next game is done. When set and a parse error occurs the rest of the
170 * entire file will be discarded. The type for this flag is an int.
176 * If the following is set to a value > 0 and DEBUG was defined
177 * at compile time then debugging output will be written to
178 * "libchess.debug" in the current directory.
184 * After PGN_PROGRESS amount of bytes have been read from a file, call
185 * PGN_PROGRESS_FUNC. The type for PGN_PROGRESS is a long.
186 * PGN_PROGRESS_FUNC is of type pgn_progress.
192 * If set to 1 and an opponent can attack a castling square the castling
193 * move will not be a valid one.
199 * Errors returned from the following functions.
212 char *tmpfile
; /* For appending. */
217 * The prototype of the PGN_PROGRESS_FUNC function pointer.
219 typedef void (*pgn_progress
)(long size
, long offset
);
222 * Sets config flag 'f' to the next argument. Returns E_PGN_OK on success or
223 * E_PGN_ERR if 'f' is an invalid flag or E_PGN_INVALID if 'val' is an invalid
226 pgn_error_t
pgn_config_set(pgn_config_flag f
, ...);
229 * Gets the value of config flag 'f'. The next argument should be a pointer of
230 * the config type which is set to the value of 'f'. Returns E_PGN_ERR if 'f'
231 * is an invalid flag or E_PGN_OK on success.
233 pgn_error_t
pgn_config_get(pgn_config_flag f
, ...);
236 * Returns E_PGN_OK if 'filename' is a recognized compressed filetype or
239 pgn_error_t
pgn_is_compressed(const char *filename
);
242 * Opens a file 'filename' with the given 'mode'. 'mode' should be "r" for
243 * reading, "w" for writing (will truncate if the file exists) or "a" for
244 * appending to an existing file or creating a new one. Returns E_PGN_OK on
245 * success and sets 'result' to a file handle for use will the other file
246 * functions or E_PGN_ERR if there is an error opening the file in which case
247 * errno will be set to the error or E_PGN_INVALID if 'mode' is an invalid
248 * mode or if 'filename' is not a regular file.
250 pgn_error_t
pgn_open(const char *filename
, const char *mode
, PGN_FILE
**result
);
253 * Closes and free's a PGN file handle. Returns E_PGN_OK on success,
254 * E_PGN_INVALID if 'pgn' is NULL, or E_PGN_ERR if rename() failed.
256 pgn_error_t
pgn_close(PGN_FILE
*pgn
);
259 * Parses a PGN_FILE which was opened with pgn_open(). If 'pgn' is NULL then a
260 * single empty game will be allocated. If there is a parsing error
261 * E_PGN_PARSE is returned, if there was a memory allocation error E_PGN_ERR
262 * is returned, otherwise E_PGN_OK is returned and the global 'gindex' is set
263 * to the last parsed game in the file and the global 'gtotal' is set to the
264 * total number of games in the file. The file should be closed with
265 * pgn_close() after processing.
267 pgn_error_t
pgn_parse(PGN_FILE
*pgn
);
270 * Allocates a new game and increments 'gtotal'. 'gindex' is then set to the
271 * new game. Returns E_PGN_ERR if there was a memory allocation error or
272 * E_PGN_OK on success.
274 pgn_error_t
pgn_new_game();
277 * Writes a PGN formatted game 'g' to a file which was opened with pgn_open().
278 * See 'pgn_config_flag' for output options. Returns E_PGN_ERR if there was a
279 * memory allocation or write error and E_PGN_OK on success.
281 pgn_error_t
pgn_write(PGN_FILE
*pgn
, GAME g
);
284 * Frees all games in the global 'game' array. Returns nothing.
286 void pgn_free_all(void);
289 * Frees a single game 'g'. Returns nothing.
291 void pgn_free(GAME g
);
294 * Adds a tag 'name' with value 'value' to the pointer to array of TAG
295 * pointers 'dst'. If a duplicate tag 'name' was found then the existing tag
296 * is updated to the new 'value'. If 'value' is NULL, the tag is removed.
297 * Returns E_PGN_ERR if there was a memory allocation error or E_PGN_OK on
300 pgn_error_t
pgn_tag_add(TAG
***dst
, char *name
, char *value
);
303 * Returns the total number of tags in 't' or 0 if 't' is NULL.
305 int pgn_tag_total(TAG
**t
);
308 * Finds a tag 'name' in the structure array 't'. Returns the location in the
309 * array of the found tag or E_PGN_ERR if the tag could not be found.
311 pgn_error_t
pgn_tag_find(TAG
**t
, const char *name
);
314 * Sorts a tag array. The first seven tags are in order of the PGN standard so
315 * don't sort'em. Returns nothing.
317 void pgn_tag_sort(TAG
**t
);
320 * Frees a TAG array. Returns nothing.
322 void pgn_tag_free(TAG
**);
325 * It initializes the board (b) to the FEN tag (if found) and sets the
326 * castling and enpassant info for the game 'g'. If 'fen' is set it should be
327 * a fen tag and will be parsed rather than the game 'g'.tag FEN tag. Returns
328 * E_PGN_OK on success or if there was both a FEN and SetUp tag with the SetUp
329 * tag set to 0. Returns E_PGN_PARSE if there was a FEN parse error, E_PGN_ERR
330 * if there was no FEN tag or there was a SetUp tag with a value of 0. Returns
331 * E_PGN_OK on success.
333 pgn_error_t
pgn_board_init_fen(GAME g
, BOARD b
, char *fen
);
336 * Creates a FEN tag from the current game 'g', history move (g.hindex) and
337 * board 'b'. Returns a FEN tag.
339 char *pgn_game_to_fen(GAME g
, BOARD b
);
342 * Resets or initializes a new game board 'b'. Returns nothing.
344 void pgn_board_init(BOARD b
);
347 * Valididate move 'm' against the game state 'g' and game board 'b' and
348 * update board 'b'. 'm' is ensured to be in SAN format and 'frfr' will the
349 * file/rank representation of 'm'. Returns E_PGN_PARSE if there was a move
350 * text parsing error, E_PGN_INVALID if the move is invalid, E_PGN_AMBIGUOUS
351 * if the move is invalid with ambiguities or E_PGN_OK if successful.
353 pgn_error_t
pgn_parse_move(GAME g
, BOARD b
, char **m
, char **frfr
);
356 * Like pgn_parse_move() but don't modify game flags in 'g' or board 'b'.
358 pgn_error_t
pgn_validate_move(GAME g
, BOARD b
, char **m
, char **frfr
);
361 * Returns the total number of moves in 'h' or 0 if there are none.
363 int pgn_history_total(HISTORY
**h
);
366 * Returns the history ply 'n' from 'h'. If 'n' is out of range then NULL is
369 HISTORY
*pgn_history_by_n(HISTORY
**h
, int n
);
372 * Appends move 'm' to game 'g' history pointer and creates a FEN tag for the
373 * current game state using board 'b'. The FEN tag makes things faster than
374 * validating the entire move history by validating only the current move to
375 * the previous moves FEN tag. The history pointer may be a in a RAV so
376 * g->rav.hp is also updated to the new (realloc()'ed) pointer. If not in a
377 * RAV then g->history will be updated. Returns E_PGN_ERR if realloc() failed
378 * or E_PGN_OK on success.
380 pgn_error_t
pgn_history_add(GAME g
, BOARD b
, const char *m
);
383 * Deallocates all of the history data from position 'start' in the array 'h'.
386 void pgn_history_free(HISTORY
**h
, int start
);
389 * Resets the game 'g' using board 'b' up to history move (g.hindex) 'n'.
390 * Returns E_PGN_OK on success or E_PGN_PARSE if there was a FEN tag but the
391 * parsing of it failed. Or returns E_PGN_INVALID if somehow the move failed
392 * validation while resetting.
394 pgn_error_t
pgn_board_update(GAME g
, BOARD b
, int n
);
397 * Updates the game 'g' using board 'b' to the next 'n'th history move.
400 void pgn_history_prev(GAME g
, BOARD b
, int n
);
403 * Updates the game 'g' using board 'b' to the previous 'n'th history move.
406 void pgn_history_next(GAME g
, BOARD b
, int n
);
409 * Converts the character piece 'p' to an integer. Returns the integer
410 * associated with 'p' or E_PGN_ERR if 'p' is invalid.
412 int pgn_piece_to_int(int p
);
415 * Converts the integer piece 'n' to a character whose turn is 'turn'. WHITE
416 * piece are uppercase and BLACK pieces are lowercase. Returns the character
417 * associated with 'n' or E_PGN_ERR if 'n' is an invalid piece.
419 pgn_error_t
pgn_int_to_piece(char turn
, int n
);
422 * Toggles g->turn. Returns nothing.
424 void pgn_switch_turn(GAME
);
427 * Toggles g->side and switches the White and Black roster tags. Returns
430 void pgn_switch_side(GAME g
);
433 * Clears the enpassant flag for all positions on board 'b'. Returns nothing.
435 void pgn_reset_enpassant(BOARD b
);
438 * Clears the valid move flag for all positions on board 'b'. Returns nothing.
440 void pgn_reset_valid_moves(BOARD b
);
443 * Sets valid moves (b.valid) from game 'g' using board 'b'. The valid moves
444 * are for the piece on the board 'b' at 'rank' and 'file'. Returns nothing.
446 void pgn_find_valid_moves(GAME g
, BOARD b
, int rank
, int file
);
449 * Returns the version string of the library.
451 char *pgn_version(void);