2 * gamelist.c -- Functions to manage a gamelist
4 * Copyright 1995,2009 Free Software Foundation, Inc.
6 * Enhancements Copyright 2005 Alessandro Scotti
8 * ------------------------------------------------------------------------
10 * GNU XBoard is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at
13 * your option) any later version.
15 * GNU XBoard is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see http://www.gnu.org/licenses/.
23 *------------------------------------------------------------------------
24 ** See the file ChangeLog for a revision history. */
33 #else /* not STDC_HEADERS */
36 # else /* not HAVE_STRING_H */
38 # endif /* not HAVE_STRING_H */
39 #endif /* not STDC_HEADERS */
52 /* Local function prototypes
54 static void GameListDeleteGame P((ListGame *));
55 static ListGame *GameListCreate P((void));
56 static void GameListFree P((List *));
57 static int GameListNewGame P((ListGame **));
59 /* Delete a ListGame; implies removint it from a list.
61 static void GameListDeleteGame(listGame)
65 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
66 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
67 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
68 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
69 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
70 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
71 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
72 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
73 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
74 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
75 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
76 ListNodeFree((ListNode *) listGame);
81 /* Free the previous list of games.
83 static void GameListFree(gameList)
86 while (!ListEmpty(gameList))
88 GameListDeleteGame((ListGame *) gameList->head);
94 /* Initialize a new GameInfo structure.
96 void GameListInitGameInfo(gameInfo)
99 gameInfo->event = NULL;
100 gameInfo->site = NULL;
101 gameInfo->date = NULL;
102 gameInfo->round = NULL;
103 gameInfo->white = NULL;
104 gameInfo->black = NULL;
105 gameInfo->result = GameUnfinished;
106 gameInfo->fen = NULL;
107 gameInfo->resultDetails = NULL;
108 gameInfo->timeControl = NULL;
109 gameInfo->extraTags = NULL;
110 gameInfo->whiteRating = -1; /* unknown */
111 gameInfo->blackRating = -1; /* unknown */
112 gameInfo->variant = VariantNormal;
113 gameInfo->outOfBook = NULL;
117 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
119 * Note, that the ListGame is *not* added to any list
121 static ListGame *GameListCreate()
126 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
127 GameListInitGameInfo(&listGame->gameInfo);
133 /* Creates a new game for the gamelist.
135 static int GameListNewGame(listGamePtr)
136 ListGame **listGamePtr;
138 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
139 GameListFree(&gameList);
142 ListAddTail(&gameList, (ListNode *) *listGamePtr);
147 /* Build the list of games in the open file f.
148 * Returns 0 for success or error number.
153 ChessMove cm, lastStart;
155 ListGame *currentListGame = NULL;
159 GameListFree(&gameList);
163 lastStart = (ChessMove) 0;
168 cm = (ChessMove) yylex();
171 if ((error = GameListNewGame(¤tListGame))) {
176 currentListGame->number = ++gameNumber;
177 currentListGame->offset = offset;
178 if (currentListGame->gameInfo.event != NULL) {
179 free(currentListGame->gameInfo.event);
181 currentListGame->gameInfo.event = StrSave(yy_text);
193 break; /* Already started */
197 if ((error = GameListNewGame(¤tListGame))) {
202 currentListGame->number = ++gameNumber;
203 currentListGame->offset = offset;
207 break; /* impossible */
212 if ((error = GameListNewGame(¤tListGame))) {
217 currentListGame->number = ++gameNumber;
218 currentListGame->offset = offset;
219 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
223 cm = (ChessMove) yylex();
225 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
227 } while (cm == PGNTag || cm == Comment);
230 /* Allow the first game to start with an unnumbered move */
232 if (lastStart == (ChessMove) 0) {
233 if ((error = GameListNewGame(¤tListGame))) {
238 currentListGame->number = ++gameNumber;
239 currentListGame->offset = offset;
240 lastStart = MoveNumberOne;
247 while (cm != (ChessMove) 0);
250 if (appData.debugMode) {
251 for (currentListGame = (ListGame *) gameList.head;
252 currentListGame->node.succ;
253 currentListGame = (ListGame *) currentListGame->node.succ) {
255 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
256 currentListGame->number, currentListGame->offset);
257 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
267 /* Clear an existing GameInfo structure.
269 void ClearGameInfo(gameInfo)
272 if (gameInfo->event != NULL) {
273 free(gameInfo->event);
275 if (gameInfo->site != NULL) {
276 free(gameInfo->site);
278 if (gameInfo->date != NULL) {
279 free(gameInfo->date);
281 if (gameInfo->round != NULL) {
282 free(gameInfo->round);
284 if (gameInfo->white != NULL) {
285 free(gameInfo->white);
287 if (gameInfo->black != NULL) {
288 free(gameInfo->black);
290 if (gameInfo->resultDetails != NULL) {
291 free(gameInfo->resultDetails);
293 if (gameInfo->fen != NULL) {
296 if (gameInfo->timeControl != NULL) {
297 free(gameInfo->timeControl);
299 if (gameInfo->extraTags != NULL) {
300 free(gameInfo->extraTags);
302 if (gameInfo->outOfBook != NULL) {
303 free(gameInfo->outOfBook);
306 GameListInitGameInfo(gameInfo);
309 /* [AS] Replaced by "dynamic" tag selection below */
311 GameListLineOld(number, gameInfo)
315 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
316 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
317 char *white = gameInfo->white ? gameInfo->white : "?";
318 char *black = gameInfo->black ? gameInfo->black : "?";
319 char *date = gameInfo->date ? gameInfo->date : "?";
320 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
321 strlen(black) + 11 + strlen(date) + 1;
322 char *ret = (char *) malloc(len);
323 sprintf(ret, "%d. %s, %s-%s, %s, %s",
324 number, event, white, black, PGNResult(gameInfo->result), date);
328 #define MAX_FIELD_LEN 64 /* To avoid overflowing the buffer */
330 char * GameListLine( int number, GameInfo * gameInfo )
334 char * glt = appData.gameListTags;
336 buf += sprintf( buffer, "%d.", number );
338 while( *glt != '\0' ) {
343 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
346 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
349 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
352 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
355 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
356 buf[ MAX_FIELD_LEN-1 ] = '\0';
357 buf += strlen( buf );
359 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
362 strcpy( buf, PGNResult(gameInfo->result) );
365 if( gameInfo->whiteRating > 0 )
366 sprintf( buf, "%d", gameInfo->whiteRating );
371 if( gameInfo->blackRating > 0 )
372 sprintf( buf, "%d", gameInfo->blackRating );
376 case GLT_TIME_CONTROL:
377 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
381 case GLT_OUT_OF_BOOK:
382 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
388 buf[MAX_FIELD_LEN-1] = '\0';
390 buf += strlen( buf );
401 return strdup( buffer );
404 char * GameListLineFull( int number, GameInfo * gameInfo )
406 char * event = gameInfo->event ? gameInfo->event : "?";
407 char * site = gameInfo->site ? gameInfo->site : "?";
408 char * white = gameInfo->white ? gameInfo->white : "?";
409 char * black = gameInfo->black ? gameInfo->black : "?";
410 char * round = gameInfo->round ? gameInfo->round : "?";
411 char * date = gameInfo->date ? gameInfo->date : "?";
412 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
414 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob);
416 char *ret = (char *) malloc(len);
418 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"", number, event, site, round, white, black, PGNResult(gameInfo->result), date, oob );