2 * gamelist.c -- Functions to manage a gamelist
\r
3 * XBoard $Id: gamelist.c,v 2.1 2003/10/27 19:21:00 mann Exp $
\r
5 * Copyright 1995 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program; if not, write to the Free Software
\r
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
\r
21 * ------------------------------------------------------------------------
\r
29 # include <stdlib.h>
\r
30 # include <string.h>
\r
31 #else /* not STDC_HEADERS */
\r
33 # include <string.h>
\r
34 # else /* not HAVE_STRING_H */
\r
35 # include <strings.h>
\r
36 # endif /* not HAVE_STRING_H */
\r
37 #endif /* not STDC_HEADERS */
\r
40 #include "frontend.h"
\r
41 #include "backend.h"
\r
50 /* Local function prototypes
\r
52 static void GameListDeleteGame P((ListGame *));
\r
53 static ListGame *GameListCreate P((void));
\r
54 static void GameListFree P((List *));
\r
55 static int GameListNewGame P((ListGame **));
\r
57 /* Delete a ListGame; implies removint it from a list.
\r
59 static void GameListDeleteGame(listGame)
\r
63 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
\r
64 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
\r
65 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
\r
66 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
\r
67 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
\r
68 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
\r
69 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
\r
70 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
\r
71 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
\r
72 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
\r
73 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
\r
74 ListNodeFree((ListNode *) listGame);
\r
79 /* Free the previous list of games.
\r
81 static void GameListFree(gameList)
\r
84 while (!ListEmpty(gameList))
\r
86 GameListDeleteGame((ListGame *) gameList->head);
\r
92 /* Initialize a new GameInfo structure.
\r
94 void GameListInitGameInfo(gameInfo)
\r
97 gameInfo->event = NULL;
\r
98 gameInfo->site = NULL;
\r
99 gameInfo->date = NULL;
\r
100 gameInfo->round = NULL;
\r
101 gameInfo->white = NULL;
\r
102 gameInfo->black = NULL;
\r
103 gameInfo->result = GameUnfinished;
\r
104 gameInfo->fen = NULL;
\r
105 gameInfo->resultDetails = NULL;
\r
106 gameInfo->timeControl = NULL;
\r
107 gameInfo->extraTags = NULL;
\r
108 gameInfo->whiteRating = -1; /* unknown */
\r
109 gameInfo->blackRating = -1; /* unknown */
\r
110 gameInfo->variant = VariantNormal;
\r
111 gameInfo->outOfBook = NULL;
\r
115 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
\r
117 * Note, that the ListGame is *not* added to any list
\r
119 static ListGame *GameListCreate()
\r
122 ListGame *listGame;
\r
124 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
\r
125 GameListInitGameInfo(&listGame->gameInfo);
\r
131 /* Creates a new game for the gamelist.
\r
133 static int GameListNewGame(listGamePtr)
\r
134 ListGame **listGamePtr;
\r
136 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
\r
137 GameListFree(&gameList);
\r
140 ListAddTail(&gameList, (ListNode *) *listGamePtr);
\r
145 /* Build the list of games in the open file f.
\r
146 * Returns 0 for success or error number.
\r
148 int GameListBuild(f)
\r
151 ChessMove cm, lastStart;
\r
153 ListGame *currentListGame = NULL;
\r
157 GameListFree(&gameList);
\r
161 lastStart = (ChessMove) 0;
\r
162 yyskipmoves = FALSE;
\r
165 offset = yyoffset();
\r
166 cm = (ChessMove) yylex();
\r
169 if ((error = GameListNewGame(¤tListGame))) {
\r
171 yyskipmoves = FALSE;
\r
174 currentListGame->number = ++gameNumber;
\r
175 currentListGame->offset = offset;
\r
176 if (currentListGame->gameInfo.event != NULL) {
\r
177 free(currentListGame->gameInfo.event);
\r
179 currentListGame->gameInfo.event = StrSave(yy_text);
\r
185 case MoveNumberOne:
\r
186 switch (lastStart) {
\r
188 break; /* ignore */
\r
191 break; /* Already started */
\r
192 case (ChessMove) 0:
\r
193 case MoveNumberOne:
\r
195 if ((error = GameListNewGame(¤tListGame))) {
\r
197 yyskipmoves = FALSE;
\r
200 currentListGame->number = ++gameNumber;
\r
201 currentListGame->offset = offset;
\r
205 break; /* impossible */
\r
210 if ((error = GameListNewGame(¤tListGame))) {
\r
212 yyskipmoves = FALSE;
\r
215 currentListGame->number = ++gameNumber;
\r
216 currentListGame->offset = offset;
\r
217 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
\r
220 offset = yyoffset();
\r
221 cm = (ChessMove) yylex();
\r
222 if (cm == PGNTag) {
\r
223 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
\r
225 } while (cm == PGNTag || cm == Comment);
\r
228 /* Allow the first game to start with an unnumbered move */
\r
229 yyskipmoves = TRUE;
\r
230 if (lastStart == (ChessMove) 0) {
\r
231 if ((error = GameListNewGame(¤tListGame))) {
\r
233 yyskipmoves = FALSE;
\r
236 currentListGame->number = ++gameNumber;
\r
237 currentListGame->offset = offset;
\r
238 lastStart = MoveNumberOne;
\r
245 while (cm != (ChessMove) 0);
\r
248 if (appData.debugMode) {
\r
249 for (currentListGame = (ListGame *) gameList.head;
\r
250 currentListGame->node.succ;
\r
251 currentListGame = (ListGame *) currentListGame->node.succ) {
\r
253 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
\r
254 currentListGame->number, currentListGame->offset);
\r
255 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
\r
260 yyskipmoves = FALSE;
\r
265 /* Clear an existing GameInfo structure.
\r
267 void ClearGameInfo(gameInfo)
\r
268 GameInfo *gameInfo;
\r
270 if (gameInfo->event != NULL) {
\r
271 free(gameInfo->event);
\r
273 if (gameInfo->site != NULL) {
\r
274 free(gameInfo->site);
\r
276 if (gameInfo->date != NULL) {
\r
277 free(gameInfo->date);
\r
279 if (gameInfo->round != NULL) {
\r
280 free(gameInfo->round);
\r
282 if (gameInfo->white != NULL) {
\r
283 free(gameInfo->white);
\r
285 if (gameInfo->black != NULL) {
\r
286 free(gameInfo->black);
\r
288 if (gameInfo->resultDetails != NULL) {
\r
289 free(gameInfo->resultDetails);
\r
291 if (gameInfo->fen != NULL) {
\r
292 free(gameInfo->fen);
\r
294 if (gameInfo->timeControl != NULL) {
\r
295 free(gameInfo->timeControl);
\r
297 if (gameInfo->extraTags != NULL) {
\r
298 free(gameInfo->extraTags);
\r
300 if (gameInfo->outOfBook != NULL) {
\r
301 free(gameInfo->outOfBook);
\r
304 GameListInitGameInfo(gameInfo);
\r
307 /* [AS] Replaced by "dynamic" tag selection below */
\r
309 GameListLineOld(number, gameInfo)
\r
311 GameInfo *gameInfo;
\r
313 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
\r
314 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
\r
315 char *white = gameInfo->white ? gameInfo->white : "?";
\r
316 char *black = gameInfo->black ? gameInfo->black : "?";
\r
317 char *date = gameInfo->date ? gameInfo->date : "?";
\r
318 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
\r
319 strlen(black) + 11 + strlen(date) + 1;
\r
320 char *ret = (char *) malloc(len);
\r
321 sprintf(ret, "%d. %s, %s-%s, %s, %s",
\r
322 number, event, white, black, PGNResult(gameInfo->result), date);
\r
326 #define MAX_FIELD_LEN 64 /* To avoid overflowing the buffer */
\r
328 char * GameListLine( int number, GameInfo * gameInfo )
\r
331 char * buf = buffer;
\r
332 char * glt = appData.gameListTags;
\r
334 buf += sprintf( buffer, "%d.", number );
\r
336 while( *glt != '\0' ) {
\r
341 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
\r
344 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
\r
347 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
\r
350 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
\r
353 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
\r
354 buf[ MAX_FIELD_LEN-1 ] = '\0';
\r
355 buf += strlen( buf );
\r
357 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
\r
360 strcpy( buf, PGNResult(gameInfo->result) );
\r
362 case GLT_WHITE_ELO:
\r
363 if( gameInfo->whiteRating > 0 )
\r
364 sprintf( buf, "%d", gameInfo->whiteRating );
\r
366 strcpy( buf, "?" );
\r
368 case GLT_BLACK_ELO:
\r
369 if( gameInfo->blackRating > 0 )
\r
370 sprintf( buf, "%d", gameInfo->blackRating );
\r
372 strcpy( buf, "?" );
\r
374 case GLT_TIME_CONTROL:
\r
375 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
\r
379 case GLT_OUT_OF_BOOK:
\r
380 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
\r
386 buf[MAX_FIELD_LEN-1] = '\0';
\r
388 buf += strlen( buf );
\r
392 if( *glt != '\0' ) {
\r
399 return strdup( buffer );
\r
402 char * GameListLineFull( int number, GameInfo * gameInfo )
\r
404 char * event = gameInfo->event ? gameInfo->event : "?";
\r
405 char * site = gameInfo->site ? gameInfo->site : "?";
\r
406 char * white = gameInfo->white ? gameInfo->white : "?";
\r
407 char * black = gameInfo->black ? gameInfo->black : "?";
\r
408 char * round = gameInfo->round ? gameInfo->round : "?";
\r
409 char * date = gameInfo->date ? gameInfo->date : "?";
\r
410 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
\r
412 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob);
\r
414 char *ret = (char *) malloc(len);
\r
416 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"", number, event, site, round, white, black, PGNResult(gameInfo->result), date, oob );
\r