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,2009 Free Software Foundation, Inc.
7 * ------------------------------------------------------------------------
9 * GNU XBoard is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
14 * GNU XBoard is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see http://www.gnu.org/licenses/.
22 *------------------------------------------------------------------------
23 ** See the file ChangeLog for a revision history. */
32 #else /* not STDC_HEADERS */
35 # else /* not HAVE_STRING_H */
37 # endif /* not HAVE_STRING_H */
38 #endif /* not STDC_HEADERS */
51 /* Local function prototypes
53 static void GameListDeleteGame P((ListGame *));
54 static ListGame *GameListCreate P((void));
55 static void GameListFree P((List *));
56 static int GameListNewGame P((ListGame **));
58 /* Delete a ListGame; implies removint it from a list.
60 static void GameListDeleteGame(listGame)
64 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
65 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
66 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
67 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
68 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
69 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
70 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
71 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
72 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
73 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
74 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
75 ListNodeFree((ListNode *) listGame);
80 /* Free the previous list of games.
82 static void GameListFree(gameList)
85 while (!ListEmpty(gameList))
87 GameListDeleteGame((ListGame *) gameList->head);
93 /* Initialize a new GameInfo structure.
95 void GameListInitGameInfo(gameInfo)
98 gameInfo->event = NULL;
99 gameInfo->site = NULL;
100 gameInfo->date = NULL;
101 gameInfo->round = NULL;
102 gameInfo->white = NULL;
103 gameInfo->black = NULL;
104 gameInfo->result = GameUnfinished;
105 gameInfo->fen = NULL;
106 gameInfo->resultDetails = NULL;
107 gameInfo->timeControl = NULL;
108 gameInfo->extraTags = NULL;
109 gameInfo->whiteRating = -1; /* unknown */
110 gameInfo->blackRating = -1; /* unknown */
111 gameInfo->variant = VariantNormal;
112 gameInfo->outOfBook = NULL;
116 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
118 * Note, that the ListGame is *not* added to any list
120 static ListGame *GameListCreate()
125 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
126 GameListInitGameInfo(&listGame->gameInfo);
132 /* Creates a new game for the gamelist.
134 static int GameListNewGame(listGamePtr)
135 ListGame **listGamePtr;
137 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
138 GameListFree(&gameList);
141 ListAddTail(&gameList, (ListNode *) *listGamePtr);
146 /* Build the list of games in the open file f.
147 * Returns 0 for success or error number.
152 ChessMove cm, lastStart;
154 ListGame *currentListGame = NULL;
158 GameListFree(&gameList);
162 lastStart = (ChessMove) 0;
167 cm = (ChessMove) yylex();
170 if ((error = GameListNewGame(¤tListGame))) {
175 currentListGame->number = ++gameNumber;
176 currentListGame->offset = offset;
177 if (currentListGame->gameInfo.event != NULL) {
178 free(currentListGame->gameInfo.event);
180 currentListGame->gameInfo.event = StrSave(yy_text);
192 break; /* Already started */
196 if ((error = GameListNewGame(¤tListGame))) {
201 currentListGame->number = ++gameNumber;
202 currentListGame->offset = offset;
206 break; /* impossible */
211 if ((error = GameListNewGame(¤tListGame))) {
216 currentListGame->number = ++gameNumber;
217 currentListGame->offset = offset;
218 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
222 cm = (ChessMove) yylex();
224 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
226 } while (cm == PGNTag || cm == Comment);
229 /* Allow the first game to start with an unnumbered move */
231 if (lastStart == (ChessMove) 0) {
232 if ((error = GameListNewGame(¤tListGame))) {
237 currentListGame->number = ++gameNumber;
238 currentListGame->offset = offset;
239 lastStart = MoveNumberOne;
246 while (cm != (ChessMove) 0);
249 if (appData.debugMode) {
250 for (currentListGame = (ListGame *) gameList.head;
251 currentListGame->node.succ;
252 currentListGame = (ListGame *) currentListGame->node.succ) {
254 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
255 currentListGame->number, currentListGame->offset);
256 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
266 /* Clear an existing GameInfo structure.
268 void ClearGameInfo(gameInfo)
271 if (gameInfo->event != NULL) {
272 free(gameInfo->event);
274 if (gameInfo->site != NULL) {
275 free(gameInfo->site);
277 if (gameInfo->date != NULL) {
278 free(gameInfo->date);
280 if (gameInfo->round != NULL) {
281 free(gameInfo->round);
283 if (gameInfo->white != NULL) {
284 free(gameInfo->white);
286 if (gameInfo->black != NULL) {
287 free(gameInfo->black);
289 if (gameInfo->resultDetails != NULL) {
290 free(gameInfo->resultDetails);
292 if (gameInfo->fen != NULL) {
295 if (gameInfo->timeControl != NULL) {
296 free(gameInfo->timeControl);
298 if (gameInfo->extraTags != NULL) {
299 free(gameInfo->extraTags);
301 if (gameInfo->outOfBook != NULL) {
302 free(gameInfo->outOfBook);
305 GameListInitGameInfo(gameInfo);
308 /* [AS] Replaced by "dynamic" tag selection below */
310 GameListLineOld(number, gameInfo)
314 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
315 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
316 char *white = gameInfo->white ? gameInfo->white : "?";
317 char *black = gameInfo->black ? gameInfo->black : "?";
318 char *date = gameInfo->date ? gameInfo->date : "?";
319 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
320 strlen(black) + 11 + strlen(date) + 1;
321 char *ret = (char *) malloc(len);
322 sprintf(ret, "%d. %s, %s-%s, %s, %s",
323 number, event, white, black, PGNResult(gameInfo->result), date);
327 #define MAX_FIELD_LEN 64 /* To avoid overflowing the buffer */
329 char * GameListLine( int number, GameInfo * gameInfo )
333 char * glt = appData.gameListTags;
335 buf += sprintf( buffer, "%d.", number );
337 while( *glt != '\0' ) {
342 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
345 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
348 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
351 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
354 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
355 buf[ MAX_FIELD_LEN-1 ] = '\0';
356 buf += strlen( buf );
358 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
361 strcpy( buf, PGNResult(gameInfo->result) );
364 if( gameInfo->whiteRating > 0 )
365 sprintf( buf, "%d", gameInfo->whiteRating );
370 if( gameInfo->blackRating > 0 )
371 sprintf( buf, "%d", gameInfo->blackRating );
375 case GLT_TIME_CONTROL:
376 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
380 case GLT_OUT_OF_BOOK:
381 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
387 buf[MAX_FIELD_LEN-1] = '\0';
389 buf += strlen( buf );
400 return strdup( buffer );
403 char * GameListLineFull( int number, GameInfo * gameInfo )
405 char * event = gameInfo->event ? gameInfo->event : "?";
406 char * site = gameInfo->site ? gameInfo->site : "?";
407 char * white = gameInfo->white ? gameInfo->white : "?";
408 char * black = gameInfo->black ? gameInfo->black : "?";
409 char * round = gameInfo->round ? gameInfo->round : "?";
410 char * date = gameInfo->date ? gameInfo->date : "?";
411 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
413 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob);
415 char *ret = (char *) malloc(len);
417 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"", number, event, site, round, white, black, PGNResult(gameInfo->result), date, oob );