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