home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / CBUFF.H < prev    next >
C/C++ Source or Header  |  1993-11-16  |  27KB  |  943 lines

  1. /*  $Id: cbuff.h,v 1.1.1.1 1993/06/21 11:11:59 anjo Exp $
  2.  *  
  3.  *  File    cbuff.h
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Horst Aurisch, aurisch@informatik.uni-bonn.de
  6.  *              Anjo Anjewierden, anjo@swi.psy.uva.nl
  7.  *  Purpose    Definitions
  8.  *  Works with    GNU CC 2.4.5
  9.  *  
  10.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  11.  *  
  12.  *  History    08/06/93  (Created)
  13.  *          10/11/93  (Last modified)
  14.  */ 
  15.  
  16.  
  17. /*------------------------------------------------------------
  18.  *  Directives
  19.  *------------------------------------------------------------*/
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <setjmp.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <stdarg.h>
  28.  
  29. #include "machine.h"
  30.  
  31.  
  32. /*------------------------------------------------------------
  33.  *  Compatibility issues
  34.  *------------------------------------------------------------*/
  35.  
  36. /*  ``Move'' is already defined by THINK_C on the Macintosh.
  37.  *  (Reported by Rolf Exner).
  38.  */
  39.  
  40. #ifdef THINK_C
  41. #    define Move    MMove
  42. #endif
  43.  
  44.  
  45. /*------------------------------------------------------------
  46.  *  Constants
  47.  *------------------------------------------------------------*/
  48.  
  49. #ifndef TRUE
  50. #    define TRUE    1
  51. #    define FALSE    0
  52. #endif
  53.  
  54. #define NO_RESULT    0
  55. #define NO_ROUND    0
  56.  
  57. #define REQUEST_DIAGRAM    '\004'
  58.  
  59. #define LONG_ALGEBRAIC    1
  60. #define SHORT_ALGEBRAIC    2
  61.  
  62. extern char *    NullString;        /* Equals "" */
  63.  
  64.  
  65. /*------------------------------------------------------------
  66.  *  Global data
  67.  *------------------------------------------------------------*/
  68.  
  69. extern unsigned short UserId;        /* ChessBase uid; option.c */
  70.  
  71.  
  72. /*------------------------------------------------------------
  73.  *  Type definitions
  74.  *------------------------------------------------------------*/
  75.  
  76. typedef int            bool;        /* TRUE or FALSE */
  77. typedef struct cbase *        CBase;        /* ChessBase database */
  78. typedef struct game *        Game;        /* Chess game */
  79. typedef struct move *        Move;        /* Chess move */
  80. typedef struct cbheader *    CbHeader;    /* ChessBase game header */
  81. typedef struct cbgame *        CbGame;        /* ChessBase game */
  82. typedef struct option *        Option;        /* Command line options */
  83. typedef struct position *    Position;    /* Chess position */
  84. typedef unsigned char        Square;        /* Square (0..63) */
  85. typedef unsigned char        Piece;        /* Piece in position */
  86. typedef int            Colour;        /* WHITE or BLACK */
  87. typedef struct textbuffer *    TextBuffer;     /* To print formatted text */
  88. typedef int            Result;        /* Result of a game */
  89.  
  90.  
  91. /*------------------------------------------------------------
  92.  *  Chess pieces
  93.  *------------------------------------------------------------*/
  94.  
  95. #define NO_PIECE    0
  96.  
  97. #define WHITE_MASK    0
  98. #define BLACK_MASK    8
  99. #define PIECE_MASK    7
  100.  
  101. #define KING        1
  102. #define QUEEN        2
  103. #define KNIGHT        3
  104. #define BISHOP        4
  105. #define ROOK        5
  106. #define PAWN        6
  107.  
  108.  
  109. #define WHITE_KING    (KING | WHITE_MASK)
  110. #define WHITE_QUEEN    (QUEEN | WHITE_MASK)
  111. #define WHITE_KNIGHT    (KNIGHT | WHITE_MASK)
  112. #define WHITE_BISHOP    (BISHOP | WHITE_MASK)
  113. #define WHITE_ROOK    (ROOK | WHITE_MASK)
  114. #define WHITE_PAWN    (PAWN | WHITE_MASK)
  115.  
  116. #define BLACK_KING    (KING | BLACK_MASK)
  117. #define BLACK_QUEEN    (QUEEN | BLACK_MASK)
  118. #define BLACK_KNIGHT    (KNIGHT | BLACK_MASK)
  119. #define BLACK_BISHOP    (BISHOP | BLACK_MASK)
  120. #define BLACK_ROOK    (ROOK | BLACK_MASK)
  121. #define BLACK_PAWN    (PAWN | BLACK_MASK)
  122.  
  123. #define isPieceP(x)        ((x) & PIECE_MASK)
  124. #define isWhitePieceP(x)    ((x) >= WHITE_KING && (x) <= WHITE_PAWN)
  125. #define isBlackPieceP(x)    ((x) >= BLACK_KING && (x) <= BLACK_PAWN)
  126. #define isKingP(x)        (((x) & PIECE_MASK) == KING)
  127. #define isQueenP(x)        (((x) & PIECE_MASK) == QUEEN)
  128. #define isKnightP(x)        (((x) & PIECE_MASK) == KNIGHT)
  129. #define isBishopP(x)        (((x) & PIECE_MASK) == BISHOP)
  130. #define isRookP(x)        (((x) & PIECE_MASK) == ROOK)
  131. #define isPawnP(x)        (((x) & PIECE_MASK) == PAWN)
  132.  
  133.  
  134. /*----------------------------------------------
  135.  *  Chess squares
  136.  *----------------------------------------------
  137.  *
  138.  *  Below are the definitions related to squares on the chess board.
  139.  *  There are not many design considerations about squares, the most
  140.  *  obvious representation is as a number.  Given that an ordered
  141.  *  collection of squares is a board, squares are numbered from 0 (a1)
  142.  *  to 63 (h8).
  143.  *
  144.  *  Representation.  A square is represented as a small integer, where
  145.  *  the three lowest bits represented the file (a..h) and the next
  146.  *  three bits the rank (1..8).
  147.  *
  148.  *  Notes.
  149.  *  (1) Always refer to a number representing a square as of type
  150.  *      Square for reasons of clarity.
  151.  *  (2) Symbolic names for all the squares are defined below.  They
  152.  *      are of the form A1 (where A is the character for the column
  153.  *      and 1 the number for the row).
  154.  */
  155.  
  156.  
  157. /*----------------------------------------------
  158.  *  Square macro's
  159.  *----------------------------------------------*/
  160.  
  161. #define newSquare(file, rank)    (((rank) - '1') | (((file) - 'a') << 3))
  162. #define makeSquare(file, rank)    (((file)<<3) | (rank))
  163.  
  164. #define rankSquare(sq)        ((sq) & 0x7)
  165. #define fileSquare(sq)        (((sq)>>3) & 0x7)
  166. #define isFileSquareP(sq, file)    (fileSquare(sq) == (file))
  167. #define isRankSquareP(sq, rank)    (rankSquare(sq) == (rank))
  168.  
  169. #define for_squares(sq)        for (sq=H8+1; --sq;)
  170.  
  171.  
  172. /*----------------------------------------------
  173.  *  Ranks and files
  174.  *----------------------------------------------*/
  175.  
  176. #define RANK_1        0
  177. #define RANK_2        1
  178. #define RANK_3        2
  179. #define RANK_4        3
  180. #define RANK_5        4
  181. #define RANK_6        5
  182. #define RANK_7        6
  183. #define RANK_8        7
  184.  
  185. #define A_FILE        0
  186. #define B_FILE        1
  187. #define C_FILE        2
  188. #define D_FILE        3
  189. #define E_FILE        4
  190. #define F_FILE        5
  191. #define G_FILE        6
  192. #define H_FILE        7
  193.  
  194.  
  195. /*----------------------------------------------
  196.  *  Square names (rather boring)
  197.  *----------------------------------------------*/
  198.  
  199. #define A1    newSquare('a','1')
  200. #define A2    newSquare('a','2')
  201. #define A3    newSquare('a','3')
  202. #define A4    newSquare('a','4')
  203. #define A5    newSquare('a','5')
  204. #define A6    newSquare('a','6')
  205. #define A7    newSquare('a','7')
  206. #define A8    newSquare('a','8')
  207.  
  208. #define B1    newSquare('b','1')
  209. #define B2    newSquare('b','2')
  210. #define B3    newSquare('b','3')
  211. #define B4    newSquare('b','4')
  212. #define B5    newSquare('b','5')
  213. #define B6    newSquare('b','6')
  214. #define B7    newSquare('b','7')
  215. #define B8    newSquare('b','8')
  216.  
  217. #define C1    newSquare('c','1')
  218. #define C2    newSquare('c','2')
  219. #define C3    newSquare('c','3')
  220. #define C4    newSquare('c','4')
  221. #define C5    newSquare('c','5')
  222. #define C6    newSquare('c','6')
  223. #define C7    newSquare('c','7')
  224. #define C8    newSquare('c','8')
  225.  
  226. #define D1    newSquare('d','1')
  227. #define D2    newSquare('d','2')
  228. #define D3    newSquare('d','3')
  229. #define D4    newSquare('d','4')
  230. #define D5    newSquare('d','5')
  231. #define D6    newSquare('d','6')
  232. #define D7    newSquare('d','7')
  233. #define D8    newSquare('d','8')
  234.  
  235. #define E1    newSquare('e','1')
  236. #define E2    newSquare('e','2')
  237. #define E3    newSquare('e','3')
  238. #define E4    newSquare('e','4')
  239. #define E5    newSquare('e','5')
  240. #define E6    newSquare('e','6')
  241. #define E7    newSquare('e','7')
  242. #define E8    newSquare('e','8')
  243.  
  244. #define F1    newSquare('f','1')
  245. #define F2    newSquare('f','2')
  246. #define F3    newSquare('f','3')
  247. #define F4    newSquare('f','4')
  248. #define F5    newSquare('f','5')
  249. #define F6    newSquare('f','6')
  250. #define F7    newSquare('f','7')
  251. #define F8    newSquare('f','8')
  252.  
  253. #define G1    newSquare('g','1')
  254. #define G2    newSquare('g','2')
  255. #define G3    newSquare('g','3')
  256. #define G4    newSquare('g','4')
  257. #define G5    newSquare('g','5')
  258. #define G6    newSquare('g','6')
  259. #define G7    newSquare('g','7')
  260. #define G8    newSquare('g','8')
  261.  
  262. #define H1    newSquare('h','1')
  263. #define H2    newSquare('h','2')
  264. #define H3    newSquare('h','3')
  265. #define H4    newSquare('h','4')
  266. #define H5    newSquare('h','5')
  267. #define H6    newSquare('h','6')
  268. #define H7    newSquare('h','7')
  269. #define H8    newSquare('h','8')
  270.  
  271.  
  272. /*------------------------------------------------------------
  273.  *  File alloc.c
  274.  *------------------------------------------------------------*/
  275.  
  276. void *        alloc(unsigned long);
  277. void        unalloc(void *);
  278. char *        allocCharp(char *);
  279. void        unallocCharp(char *);
  280. void        printMemoryAvailable(FILE *);
  281.  
  282.  
  283. /*------------------------------------------------------------
  284.  *  File cbase.c
  285.  *------------------------------------------------------------*/
  286.  
  287. #define    START_VARIATION        255    /* Indicates start of a variation */
  288. #define END_VARIATION        128    /* Indicates end of a variation */
  289.  
  290. #define PHYSICALLY_DELETED    (unsigned long) 0x80000000/* Value for index */
  291.  
  292. #define READ_MODE    1        /* Base opened for reading */
  293. #define WRITE_MODE    2        /* Base opened for writing */
  294.  
  295.  
  296. struct cbase
  297. { char *    name;            /* Name of base */
  298.   FILE *    cbf;            /* ChessBase .cbf file */
  299.   FILE *    cbi;            /* ChessBase .cbi file */
  300.   long        noGames;        /* Number of games */
  301.   unsigned long *index;            /* Start position of each game */
  302.   unsigned long    position;        /* Next index position for write */
  303.   int        mode;            /* READ_MODE, WRITE_MODE */
  304. };
  305.  
  306.  
  307. CBase        newCBase(char *, char *);
  308. void        freeCBase(CBase);
  309. long        getNoGamesCBase(CBase);
  310. void        reportCBase(CBase, FILE *);
  311. void        deleteGameCBase(CBase, long);
  312. bool        deletedGameCBaseP(CBase, long);
  313. void        exportGameCBase(CBase, CBase, long);
  314. void        exportManyCBase(CBase, CBase, long, long);
  315. unsigned long    getIndexGameCBase(CBase, long);
  316.  
  317.  
  318. /*------------------------------------------------------------
  319.  *  File cbgame.c
  320.  *------------------------------------------------------------*/
  321.  
  322. struct cbgame
  323. { CbHeader    header;            /* Game header */
  324.   char *    text;            /* Player and source fields */
  325.   unsigned char *moves;            /* Moves */
  326.   unsigned char *comments;        /* Comments */
  327.   char *    position;        /* Position */
  328.   int        textLength;        /* Bytes in text field */
  329.   int        movesLength;        /* Bytes in moves field */
  330.   int        commentLength;        /* Bytes in comments field */
  331.                     /* Remaining fields: partial games */
  332.   Piece        board[64];        /* Starting position */
  333.   short        nextMove;        /* Next move number */
  334. };
  335.  
  336.  
  337. CbGame        newCbGame(void);
  338. void        freeCbGame(CbGame);
  339. void        resetCbGame(CbGame);
  340. long        readCbGame(CbGame, CBase, long);
  341. bool        extractHeaderCbGame(CbGame, Game);
  342. bool        extractMovesCbGame(CbGame, Game);
  343. void        dumpCommentsCbGame(CbGame, FILE *);
  344.  
  345.  
  346. /*------------------------------------------------------------
  347.  *  File cbheader.c
  348.  *------------------------------------------------------------*/
  349.  
  350. /*  Field: year
  351.  *
  352.  *  The year the game was played.  The real year is the value found
  353.  *  in the field + 1900.
  354.  */
  355.  
  356. #define YEAR_UNKNOWN        127
  357.  
  358.  
  359. /*  Field: result
  360.  *
  361.  *  Encodes the result of the game.
  362.  */
  363.  
  364. #define RESULT_MASK        (3<<0)    /* 0: 0-1; 1: 1/2; 2: 1-0; 3: below */
  365. #define EVALUATION_MASK        (0xf<<2)/* If RESULT==3 */
  366. #define RESULT_BIT6_MASK    (1<<6)    /* UNUSED (CBM 34) */
  367. #define RESULT_BIT7_MASK    (1<<7)    /* UNUSED (CBM 34) */
  368.  
  369.  
  370. /*  Field: playersLength
  371.  *
  372.  *  The number of characters used for the players names.  The unused
  373.  *  bits are for the ECO code.
  374.  */
  375.  
  376. #define PLAYERS_MASK        (0x3f<<0)
  377. #define ECO_PART1_MASK        (3<<6)    /* Part 1 of ECO */
  378.  
  379.  
  380. /*  Field: sourceLength
  381.  *
  382.  *  The number of characters used for the source.  The unused
  383.  *  bits are for the ECO code.
  384.  */
  385.  
  386. #define SOURCE_MASK        (0x3f<<0)
  387. #define ECO_PART2_MASK        (3<<6)    /* Part 2 of ECO */
  388.  
  389.  
  390. /*  Field: flags1
  391.  *
  392.  *  This field either encodes the castling rights (if the game does
  393.  *  not start from the initial position) or part of the ECO code.
  394.  */
  395.  
  396. #define FULL_GAME_MASK        (1<<0)    /* 0=game, 1=position */
  397. #define COLOUR_TO_MOVE_MASK    (1<<1)    /* 0=white, 1=black */
  398. #define WHITE_CASTLE_LONG_MASK    (1<<2)    /* For positions */
  399. #define WHITE_CASTLE_SHORT_MASK    (1<<3)    /* For positions */
  400. #define BLACK_CASTLE_LONG_MASK    (1<<4)    /* For positions */
  401. #define BLACK_CASTLE_SHORT_MASK    (1<<5)    /* For positions */
  402. #define GAME_MARKED_MASK    (1<<6)    /* 0=normal, 1=marked */
  403. #define GAME_DELETED_MASK    (1<<7)    /* 0=not deleted, 1=deleted */
  404. #define ECO_PART3_MASK        (0x1f<<1) /* Part 3 of ECO */
  405.  
  406.  
  407. /*  Field: flags2
  408.  *
  409.  *  This field contains the ECO-2 (full games) or information
  410.  *  about en passant capability (partial games).
  411.  */
  412.  
  413. #define FLAGS2_BIT6_MASK    (1<<6)    /* UNKNOWN; VERSION ??? */
  414. #define ECO2_PART1_MASK        (0x1f<<0) /* Part 1 of ECO2 */
  415. #define ECO2_PART2_MASK        (1<<7)    /* Part 2 of ECO2 */
  416. #define EN_PASSANT_MASK        (0xf<<0)/* 1...8: A-H */
  417.  
  418.  
  419. /*  Field: moves
  420.  *
  421.  *  Contains the number of moves in the game.
  422.  */
  423.  
  424. #define NO_MOVES_MASK        (0xff<0)
  425.  
  426. /*  Field: checksum
  427.  *
  428.  *  This field is a checksum.
  429.  */
  430.  
  431. #define CORRECT_CHECKSUM    0x01
  432. #define INCORRECT_CHECKSUM    0x00
  433.  
  434. struct cbheader
  435. { char        year;
  436.   unsigned char    result;
  437.   short        movesLength;
  438.   unsigned char    playersLength;
  439.   unsigned char    sourceLength;
  440.   short        commentLength;
  441.   unsigned char    whiteElo;
  442.   unsigned char blackElo;
  443.   unsigned char flags1;
  444.   unsigned char    flags2;
  445.   unsigned char    moves;
  446.   unsigned char checksum;
  447. };
  448.  
  449.  
  450. CbHeader    newCbHeader(void);
  451. void        freeCbHeader(CbHeader);
  452. void        resetCbHeader(CbHeader);
  453. void        initCbHeader(CbHeader, unsigned char *);
  454. void        dumpCbHeader(CbHeader, FILE *);
  455.  
  456. bool        fullGameCbHeaderP(CbHeader);
  457. bool        deletedCbHeaderP(CbHeader);
  458. bool        markedCbHeaderP(CbHeader);
  459. int        yearCbHeader(CbHeader);
  460. int        whiteEloCbHeader(CbHeader);
  461. int        blackEloCbHeader(CbHeader);
  462. int        movesLengthCbHeader(CbHeader);
  463. int        playersLengthCbHeader(CbHeader);
  464. int        sourceLengthCbHeader(CbHeader);
  465. int        commentLengthCbHeader(CbHeader);
  466. int        movesCbHeader(CbHeader);
  467. char *        ecoCbHeader(CbHeader);
  468. bool        flags2bit6CbHeader(CbHeader);
  469.  
  470. void        setYearCbHeader(CbHeader, int);
  471. void        setWhiteEloCbHeader(CbHeader, int);
  472. void        setBlackEloCbHeader(CbHeader, int);
  473.  
  474. void        decodeCbHeader(CbHeader);
  475.  
  476.  
  477. /*------------------------------------------------------------
  478.  *  File cbuff.c
  479.  *------------------------------------------------------------*/
  480.  
  481. extern char *    UTILITY_NAME;        /* Different for each utility */
  482. extern char *    UTILITY_VERSION;    /* Different for each utility */
  483.  
  484. void        helpUtility(FILE *);    /* Different for each utility */
  485.  
  486. extern CBase    CurrentBase;
  487.  
  488. void        versionCBUFF(FILE *);
  489. void        helpCBUFF(FILE *);
  490.  
  491. void        setCurrentCBase(char *, char *, int, int);
  492. void        checkCurrentBase(CBase cb, char *);
  493. long        longArgument(char *, char *, int, int);
  494. int        intArgument(char *, char *, int, int);
  495. CBase        databaseArgument(char *, char *, int, int);
  496. CBase        createBaseArgument(char *, char *, int, int);
  497. CBase        appendBaseArgument(char *, char *, int, int);
  498. char *        stringArgument(char *, char *, int, int);
  499. FILE *        fileArgument(char *, char *, int, int, char *);
  500.  
  501. bool        strhead(char *, char *);
  502.  
  503.  
  504. /*------------------------------------------------------------
  505.  *  File file.c
  506.  *------------------------------------------------------------*/
  507.  
  508. FILE *        fopenExtension(char *, char *, char *, bool);
  509. FILE *        fopenCbuffFile(char *, char *);
  510. long        readLong(FILE *);
  511. void        writeLong(long, FILE *);
  512. char *        getCbuffDirectory(void);
  513.  
  514.  
  515. /*------------------------------------------------------------
  516.  *  File game.c
  517.  *------------------------------------------------------------*/
  518.  
  519. #define    GAME_DELETED        (1 << 0)
  520. #define    GAME_MARKED        (1 << 1)
  521. #define    GAME_INIT_OK        (1 << 2)
  522. #define    GAME_HEADER_OK        (1 << 3)
  523. #define    GAME_MOVES_OK        (1 << 4)
  524. #define    GAME_FLAGS2_BIT6    (1 << 5)
  525. #define GAME_INITIAL_POSITION    (1 << 6)
  526.  
  527. struct game
  528. { long        number;            /* Game number relative to base: 1.. */
  529.   CBase        database;        /* ChessBase data-base */
  530.   CbGame    cbGame;            /* ChessBase game representation */
  531.   short        flags;            /* Flags and status information */
  532.   char        players[65];        /* Names of the players */
  533.   char        source[65];        /* Source for game */
  534.   char *    place;            /* Place where game was played */
  535.   char *    event;            /* Event of which game is part */
  536.   char *    annotator;        /* Person who wrote the comments */
  537.   short        year;            /* Year game was played */
  538.   Result    result;            /* Result */
  539.   short        eloWhite;        /* ELO of White */
  540.   short        eloBlack;        /* ELO of Black */
  541.   char        eco[7];            /* Format A00/00 */
  542.   short        moves;            /* Number of full moves */
  543.   short        plies;            /* Number of plies */
  544.   Position    position;        /* Initial position */
  545.   Move        firstMove;        /* Pointer to first move */
  546. };
  547.  
  548.  
  549. Game        newGame(void);
  550. void        freeGame(Game);
  551. void        resetGame(Game);
  552. bool        initialiseGame(Game, long, CBase);
  553. unsigned long    bytesGame(Game);
  554.  
  555. bool        flags2Bit6GameP(Game);
  556. bool        fullGameP(Game);
  557. bool        deletedGameP(Game);
  558. bool        markedGameP(Game);
  559. bool        containsCommentsGameP(Game);
  560. bool        containsVariationsGameP(Game);
  561. bool        containsPlayerNamesGameP(Game);
  562. char *        getPlayersGame(Game);
  563. char *        getSourceGame(Game);
  564. char *        getPlaceGame(Game);
  565. char *        getEventGame(Game);
  566. char *        getAnnotatorGame(Game);
  567. char *        getWhiteGame(Game);
  568. char *        getBlackGame(Game);
  569. long        getNumberGame(Game);
  570. short        getYearGame(Game);
  571. char *        getEcoGame(Game);
  572. char *        getEcoMajorGame(Game);
  573. Move        getFirstMoveGame(Game);
  574. Result        getResultGame(Game);
  575. short        getRoundGame(Game);
  576. short        getMovesGame(Game);
  577. short        getEloWhiteGame(Game);
  578. short        getEloBlackGame(Game);
  579.  
  580. bool        checkHeaderGame(Game);
  581.  
  582. void        printGame(Game, FILE *);
  583. void        printMovesGame(Game, Move, Position, int, int, int, TextBuffer);
  584. void        pgnGame(Game, FILE *, int);
  585. void        pgnMovesGame(Game, Move, Position, int, int, TextBuffer);
  586.  
  587.  
  588. /*------------------------------------------------------------
  589.  *  File error.c
  590.  *------------------------------------------------------------*/
  591.  
  592. #define ERR_INCORRECT_CHECKSUM        1
  593. #define ERR_COULD_NOT_CREATE_FILE    2
  594. #define ERR_COULD_NOT_OVERWRITE_FILE    3
  595. #define ERR_COULD_NOT_OPEN_WRITE    4
  596. #define ERR_COULD_NOT_OPEN_READ        5
  597. #define ERR_GAME_NUMBER_OUT_OF_RANGE    6
  598. #define ERR_CANNOT_SEEK                  7
  599. #define ERR_CANNOT_READ_HEADER           8
  600. #define ERR_CANNOT_READ_TEXT             9
  601. #define ERR_CANNOT_READ_MOVES            10
  602. #define ERR_CANNOT_READ_COMMENTS         11
  603. #define ERR_CANNOT_READ_POSITION           12
  604. #define ERR_CANNOT_READ_MOVE_NUMBER     13
  605. #define ERR_UNEXPECTED_EOF              14
  606. #define ERR_ILLEGAL_RESULT              15
  607. #define ERR_VARIATION_SEEN        16
  608. #define ERR_CANNOT_INTERPRET_MOVE    17
  609. #define ERR_MOVE_COUNT_INCORRECT    18
  610. #define ERR_COMMENT_NOT_TERMINATED    19
  611. #define ERR_FORMAT_ERROR_ANNOTATIONS    20
  612. #define ERR_UNKNOWN_MOVE_EVALUATION    21
  613. #define ERR_UNKNOWN_POSITION_EVALUATION    22
  614. #define ERR_UNKNOWN_EXTRA_EVALUATION    23
  615. #define ERR_UNKNOWN_CHARACTER        24
  616. #define ERR_UNKNOWN_PIECE        25
  617. #define ERR_UNKNOWN_SYMBOL        26
  618.  
  619.  
  620. void        clearError(void);
  621. void        environmentError(CBase, Game, long);
  622. void        setNameError(char *);
  623. void        setIntError(int);
  624. void        setError(int);
  625. bool        foundError(void);
  626. void          reportError(FILE *);
  627.  
  628.  
  629. /*------------------------------------------------------------
  630.  *  File language.c
  631.  *------------------------------------------------------------*/
  632.  
  633. void        setLanguage(char *);
  634.  
  635.  
  636. /*------------------------------------------------------------
  637.  *  File list.c
  638.  *------------------------------------------------------------*/
  639.  
  640. void        listUtility(Option);
  641. void        listGame(Game g, FILE *fd);
  642.  
  643.  
  644. /*------------------------------------------------------------
  645.  *  File move.c
  646.  *------------------------------------------------------------*/
  647.  
  648. struct move
  649. { Square           from;            /* From square */
  650.   Square        to;            /* To square */
  651.   unsigned char piece;            /* Piece promoted to */
  652.   unsigned char moveEvaluation;        /* Evaluation of the move */
  653.   unsigned char    positionEvaluation;    /* Evaluation of the position */
  654.   unsigned char    extraEvaluation;    /* Additional evaluation */
  655.   int        commentLength;        /* Length of comments */
  656.   unsigned char *comments;        /* Comments (annotations) */
  657.   Move        next;            /* Next move */
  658.   Move        alternative;        /* Alternative move */
  659. };
  660.  
  661.  
  662. Move        newMove(void);
  663. void        freeMove(Move);
  664. bool        diagramMoveP(Move);
  665. bool        containsCommentsMoveP(Move);
  666. bool        containsVariationsMoveP(Move);
  667. void        outputMove(Move, TextBuffer, Position, int);
  668. void        outputCommentsMove(Move, TextBuffer, Position);
  669. unsigned char *    commentMove(Move, unsigned char *);
  670.  
  671.  
  672. /*------------------------------------------------------------
  673.  *  File option.c
  674.  *------------------------------------------------------------*/
  675.  
  676. struct option
  677. { long        from;            /* From this game number */
  678.   long        to;            /* To this game number */
  679.   CBase        database;        /* Database to operate on */
  680.   CBase        dump;            /* Output database */
  681.   FILE *    output;            /* Output file */
  682.   bool        verbose;        /* Additional printing */
  683.   int        notation;        /* SHORT_ALGEBRAIC, LONG_ALGEBRAIC */
  684. };
  685.  
  686.  
  687. Option        newOption(void);
  688. void        freeOption(Option);
  689. int        genericOption(Option, char **, int, int);
  690.  
  691.  
  692. /*------------------------------------------------------------
  693.  *  File position.c
  694.  *------------------------------------------------------------*/
  695.  
  696. #define WHITE        0
  697. #define BLACK        1
  698.  
  699. #define WHITE_SHORT    (1 << 0)
  700. #define WHITE_LONG    (1 << 1)
  701. #define BLACK_SHORT    (1 << 2)
  702. #define BLACK_LONG    (1 << 3)
  703.  
  704. struct position
  705. { Piece         board[64];        /* Pieces on the board */
  706.   int        enPassant;        /* File for en passant, a=1 */
  707.   Colour    toMove;            /* WHITE or BLACK */
  708.   short        castling;        /* Castling rights */
  709.   short        nextMove;        /* Next move to play */
  710. };
  711.  
  712.  
  713. Position    newPosition(void);
  714. void        freePosition(Position);
  715. void        clearPosition(Position);
  716. void        copyPosition(Position dst, Position src);
  717. void        doMovePosition(Position, Move);
  718. bool        ambiguousMovePositionP(Position, Move, Square *);
  719. bool        checkMovePositionP(Position, Move);
  720. bool        enPassantMovePositionP(Position, Move);
  721. bool        castlingMovePositionP(Position, Move);
  722. bool        attacksSquarePositionP(Position, Piece, Square, Square);
  723. void        diagramPosition(Position, TextBuffer);
  724. bool        getMovePosition(Position, int, Move);
  725. char *        getStringMovePosition(Position, Move, int);
  726.  
  727.  
  728. /*------------------------------------------------------------
  729.  *  File string.c
  730.  *------------------------------------------------------------*/
  731.  
  732. char *        normalisePlayer(char *);
  733. char *        normalisePlace(char *);
  734.  
  735. bool        headStringP(char *s, char *head);
  736. bool        tailStringP(char *s, char *tail);
  737. void        replaceString(char *s, char *in, char *out);
  738. void        chopString(char *s, char *specials);
  739. void        stripString(char *s);
  740.  
  741.  
  742. /*------------------------------------------------------------
  743.  *  File strtable.c
  744.  *------------------------------------------------------------*/
  745.  
  746. typedef struct string_table *    StringTable;
  747.  
  748. struct string_table
  749. { long        count;            /* Number of entries present */
  750.   long        allocated;        /* Number of entries allocated */
  751.   char **    entries;        /* Entries themselves */
  752.   void **    values;            /* Values associated with entries */
  753. };
  754.  
  755.  
  756. StringTable    newStringTable(long entries);
  757. void        freeStringTable(StringTable);
  758. bool        containsStringTableP(StringTable, char *);
  759. void *        findStringTable(StringTable, char *);
  760. void        editStringTable(StringTable, char *, void *);
  761. char *        addStringTable(StringTable, char *, void *);
  762. void        printStringTable(StringTable, char *, FILE *);
  763. void        incStringTable(StringTable, char *);
  764.  
  765.  
  766. /*------------------------------------------------------------
  767.  *  File symbols.c
  768.  *------------------------------------------------------------*/
  769.  
  770. #define ETC            1
  771. #define KING_SYMBOL        2
  772. #define QUEEN_SYMBOL        3
  773. #define ROOK_SYMBOL        4
  774. #define BISHOP_SYMBOL        5
  775. #define KNIGHT_SYMBOL        6
  776. #define PAWN_SYMBOL        7
  777. #define QUARTER            8
  778. #define HALF            9
  779. #define THREE_QUARTERS        10
  780. #define WHITE_BETTER        11
  781. #define WHITE_ADVANTAGE        12
  782. #define WHITE_WINNING        13
  783. #define BLACK_BETTER        14
  784. #define BLACK_ADVANTAGE        15
  785. #define BLACK_WINNING        16
  786. #define WHITE_COMPENSATION    17
  787. #define BLACK_COMPENSATION    18
  788. #define UNCLEAR            19
  789. #define BISHOP_PAIR        20
  790. #define OPPOSITE_BISHOPS    21
  791. #define SAME_BISHOPS        22
  792. #define DOUBLE_PAWN        23
  793. #define PASSED_PAWN        24
  794. #define ISOLATED_PAWN        25
  795. #define CONNECTED_PAWN        26
  796. #define WORSE_IS        27
  797. #define BETTER_IS        28
  798. #define SPACE_ADVANTAGE        29
  799. #define ONLY_MOVE        30
  800. #define WITH_IDEA        31
  801. #define QUEENS_SIDE        32
  802. #define KINGS_SIDE        33
  803. #define WITH            34
  804. #define WITHOUT            35
  805. #define WITH_INITIATIVE        36
  806. #define WITH_ATTACK        37
  807. #define WITH_COUNTERPLAY    38
  808. #define DIAGONAL        39
  809. #define CENTRE            40
  810. #define LINE_FILE        41
  811. #define ENDGAME            42
  812. #define ZUGZWANG        43
  813. #define TIME_TROUBLE        44
  814. #define DEVELOPMENT_ADVANTAGE    45
  815. #define WHITE_WINS        46
  816. #define BLACK_WINS        47
  817. #define DRAW            48
  818. #define EQUALITY        49
  819. #define NOVELTY            50
  820. #define COMPENSATION        51
  821. #define WEAK_POINT        52
  822. #define PAWN_EVALUATION        53
  823. #define PAWN_CONNECTION        54
  824. #define UMLAUT_u        55
  825. #define UMLAUT_a        56
  826. #define UMLAUT_U        57
  827. #define UMLAUT_o        58
  828. #define GERMAN_S        58
  829. #define NEXT_LINE        59
  830. #define EDITORIAL_COMMENT    60
  831. #define GOOD_MOVE        61
  832. #define BAD_MOVE        62
  833. #define INTERESTING_MOVE    63
  834. #define DUBIOUS_MOVE        64
  835. #define BRILLIANT_MOVE        65
  836. #define BLUNDER            66
  837. #define MATE            67
  838. #define CEDILLE_C        68
  839. #define ACUTE_e            69
  840. #define CIRCUMFLEX_a        70
  841. #define GRAVE_a            71
  842. #define DOT_a            72
  843. #define CEDILLE_c        73
  844. #define CIRCUMFLEX_e        74
  845. #define UMLAUT_e        75
  846. #define GRAVE_e            76
  847. #define UMLAUT_i        77
  848. #define CIRCUMFLEX_i        78
  849. #define GRAVE_i            79
  850. #define UMLAUT_A        80
  851. #define DOT_A            81
  852. #define ACUTE_E            82
  853. #define SCANDI_ae        83
  854. #define SCANDI_AE        84
  855. #define CIRCUMFLEX_o        85
  856. #define GRAVE_o            86
  857. #define CIRCUMFLEX_u        87
  858. #define GRAVE_u            88
  859. #define UMLAUT_y        89
  860. #define UMLAUT_O        90
  861. #define ACUTE_a            91
  862. #define ACUTE_i            92
  863. #define ACUTE_o            93
  864. #define ACUTE_u            94
  865. #define TILDE_n            95
  866. #define TILDE_N            96
  867. #define CASTLING_SHORT        97
  868. #define CASTLING_LONG        98
  869. #define CAPTURE_SYMBOL        99
  870. #define INCHECK_SYMBOL        100
  871. #define MOVE_HYPHEN        101
  872. #define PROMOTION_SYMBOL    102
  873. #define WHITE_MOVE_SYMBOL    103
  874. #define BLACK_MOVE_SYMBOL    104
  875. #define START_MAJOR_ALTERNATIVE    105
  876. #define END_MAJOR_ALTERNATIVE    106
  877. #define START_ALTERNATIVE    107
  878. #define END_ALTERNATIVE        108
  879. #define START_COMMENT        109
  880. #define END_COMMENT        110
  881. #define START_SUB_ALTERNATIVE    111
  882. #define START_DIAGRAM        112
  883. #define END_DIAGRAM        113
  884. #define START_DIAGRAM_RANK    114
  885. #define END_DIAGRAM_RANK    115
  886. #define DIAGRAM_WK        116
  887. #define DIAGRAM_WQ        117
  888. #define DIAGRAM_WR        118
  889. #define DIAGRAM_WB        119
  890. #define DIAGRAM_WN        120
  891. #define DIAGRAM_WP        121
  892. #define DIAGRAM_BK        122
  893. #define DIAGRAM_BQ        123
  894. #define DIAGRAM_BR        124
  895. #define DIAGRAM_BB        125
  896. #define DIAGRAM_BN        126
  897. #define DIAGRAM_BP        127
  898. #define DIAGRAM_WHITE_SQUARE    128
  899. #define DIAGRAM_BLACK_SQUARE    129
  900.  
  901. #define LAST_SYMBOL        130
  902.  
  903.  
  904. extern char *    SymbolNames[LAST_SYMBOL];
  905. extern char *    AsciiSymbols[LAST_SYMBOL];
  906.  
  907.  
  908. void        initChessSymbols(void);
  909. void        loadChessSymbols(FILE *);
  910. void        printChessSymbols(FILE *);
  911. char *        chessSymbol(int);
  912. char *        mapChessSymbol(unsigned char);
  913. char *        stringChessSymbol(char *, char *);
  914.  
  915.  
  916. /*------------------------------------------------------------
  917.  *  File text.c
  918.  *------------------------------------------------------------*/
  919.  
  920. struct textbuffer
  921. { FILE *    file;            /* File to which text is written */
  922.   int        columns;        /* Columns per line */
  923.   int        maxColumns;        /* For extremely long lines */
  924.   char *    line;            /* Current line of text */
  925.   int        count;            /* Count of characters in line */
  926.   char *    current;        /* Current position in buffer */
  927.   int        lastSpace;        /* Index of last space */
  928. };
  929.  
  930.  
  931. TextBuffer    newTextBuffer(FILE *, int);
  932. void        freeTextBuffer(TextBuffer);
  933. void        flushTextBuffer(TextBuffer);
  934.  
  935. void        stringTextBuffer(TextBuffer, char *);
  936. void        stringSpaceTextBuffer(TextBuffer, char *);
  937. void        stringNewlineTextBuffer(TextBuffer, char *);
  938. void        stringRawTextBuffer(TextBuffer, char *);
  939. void        formatTextBuffer(TextBuffer, char *, ...);
  940. void        formatSpaceTextBuffer(TextBuffer, char *, ...);
  941. void        formatNewlineTextBuffer(TextBuffer, char *, ...);
  942. void        formatRawTextBuffer(TextBuffer, char *, ...);
  943.