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;
114 gameInfo->resultDetails = NULL;
118 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
120 * Note, that the ListGame is *not* added to any list
122 static ListGame *GameListCreate()
127 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
128 GameListInitGameInfo(&listGame->gameInfo);
134 /* Creates a new game for the gamelist.
136 static int GameListNewGame(listGamePtr)
137 ListGame **listGamePtr;
139 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
140 GameListFree(&gameList);
143 ListAddTail(&gameList, (ListNode *) *listGamePtr);
148 /* Build the list of games in the open file f.
149 * Returns 0 for success or error number.
154 ChessMove cm, lastStart;
156 ListGame *currentListGame = NULL;
159 char lastComment[MSG_SIZ];
161 GameListFree(&gameList);
165 lastStart = (ChessMove) 0;
170 cm = (ChessMove) yylex();
173 if ((error = GameListNewGame(¤tListGame))) {
178 currentListGame->number = ++gameNumber;
179 currentListGame->offset = offset;
180 if (currentListGame->gameInfo.event != NULL) {
181 free(currentListGame->gameInfo.event);
183 currentListGame->gameInfo.event = StrSave(yy_text);
195 break; /* Already started */
199 if ((error = GameListNewGame(¤tListGame))) {
204 currentListGame->number = ++gameNumber;
205 currentListGame->offset = offset;
209 break; /* impossible */
214 if ((error = GameListNewGame(¤tListGame))) {
219 currentListGame->number = ++gameNumber;
220 currentListGame->offset = offset;
221 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
225 cm = (ChessMove) yylex();
227 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
229 } while (cm == PGNTag || cm == Comment);
232 /* Allow the first game to start with an unnumbered move */
234 if (lastStart == (ChessMove) 0) {
235 if ((error = GameListNewGame(¤tListGame))) {
240 currentListGame->number = ++gameNumber;
241 currentListGame->offset = offset;
242 lastStart = MoveNumberOne;
245 case WhiteWins: // [HGM] rescom: save last comment as result details
249 if (currentListGame->gameInfo.resultDetails != NULL) {
250 free(currentListGame->gameInfo.resultDetails);
252 if(yy_text[0] == '{') { char *p;
253 strcpy(lastComment, yy_text+1);
254 if(p = strchr(lastComment, '}')) *p = 0;
255 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
262 while (cm != (ChessMove) 0);
265 if (appData.debugMode) {
266 for (currentListGame = (ListGame *) gameList.head;
267 currentListGame->node.succ;
268 currentListGame = (ListGame *) currentListGame->node.succ) {
270 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
271 currentListGame->number, currentListGame->offset);
272 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
282 /* Clear an existing GameInfo structure.
284 void ClearGameInfo(gameInfo)
287 if (gameInfo->event != NULL) {
288 free(gameInfo->event);
290 if (gameInfo->site != NULL) {
291 free(gameInfo->site);
293 if (gameInfo->date != NULL) {
294 free(gameInfo->date);
296 if (gameInfo->round != NULL) {
297 free(gameInfo->round);
299 if (gameInfo->white != NULL) {
300 free(gameInfo->white);
302 if (gameInfo->black != NULL) {
303 free(gameInfo->black);
305 if (gameInfo->resultDetails != NULL) {
306 free(gameInfo->resultDetails);
308 if (gameInfo->fen != NULL) {
311 if (gameInfo->timeControl != NULL) {
312 free(gameInfo->timeControl);
314 if (gameInfo->extraTags != NULL) {
315 free(gameInfo->extraTags);
317 if (gameInfo->outOfBook != NULL) {
318 free(gameInfo->outOfBook);
320 GameListInitGameInfo(gameInfo);
323 /* [AS] Replaced by "dynamic" tag selection below */
325 GameListLineOld(number, gameInfo)
329 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
330 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
331 char *white = gameInfo->white ? gameInfo->white : "?";
332 char *black = gameInfo->black ? gameInfo->black : "?";
333 char *date = gameInfo->date ? gameInfo->date : "?";
334 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
335 strlen(black) + 11 + strlen(date) + 1;
336 char *ret = (char *) malloc(len);
337 sprintf(ret, "%d. %s, %s-%s, %s, %s",
338 number, event, white, black, PGNResult(gameInfo->result), date);
342 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
344 char * GameListLine( int number, GameInfo * gameInfo )
348 char * glt = appData.gameListTags;
350 buf += sprintf( buffer, "%d.", number );
352 while( *glt != '\0' ) {
357 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
360 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
363 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
366 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
369 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
370 buf[ MAX_FIELD_LEN-1 ] = '\0';
371 buf += strlen( buf );
373 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
376 strcpy( buf, PGNResult(gameInfo->result) );
379 if( gameInfo->whiteRating > 0 )
380 sprintf( buf, "%d", gameInfo->whiteRating );
385 if( gameInfo->blackRating > 0 )
386 sprintf( buf, "%d", gameInfo->blackRating );
390 case GLT_TIME_CONTROL:
391 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
395 case GLT_OUT_OF_BOOK:
396 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
398 case GLT_RESULT_COMMENT:
399 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
405 buf[MAX_FIELD_LEN-1] = '\0';
407 buf += strlen( buf );
418 return strdup( buffer );
421 char * GameListLineFull( int number, GameInfo * gameInfo )
423 char * event = gameInfo->event ? gameInfo->event : "?";
424 char * site = gameInfo->site ? gameInfo->site : "?";
425 char * white = gameInfo->white ? gameInfo->white : "?";
426 char * black = gameInfo->black ? gameInfo->black : "?";
427 char * round = gameInfo->round ? gameInfo->round : "?";
428 char * date = gameInfo->date ? gameInfo->date : "?";
429 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
430 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
432 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
434 char *ret = (char *) malloc(len);
436 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
437 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );