2 * gamelist.c -- Functions to manage a gamelist
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013 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 */
49 # define _(s) gettext (s)
50 # define N_(s) gettext_noop (s)
60 extern Board initialPosition;
64 /* Local function prototypes
66 static void GameListDeleteGame P((ListGame *));
67 static ListGame *GameListCreate P((void));
68 static void GameListFree P((List *));
69 static int GameListNewGame P((ListGame **));
71 /* [AS] Wildcard pattern matching */
73 HasPattern (const char * text, const char * pattern)
75 while( *pattern != '\0' ) {
76 if( *pattern == '*' ) {
77 while( *pattern == '*' ) {
81 if( *pattern == '\0' ) {
85 while( *text != '\0' ) {
86 if( HasPattern( text, pattern ) ) {
92 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
105 SearchPattern (const char * text, const char * pattern)
107 Boolean result = TRUE;
109 if( pattern != NULL && *pattern != '\0' ) {
110 if( *pattern == '*' ) {
111 result = HasPattern( text, pattern );
116 while( *text != '\0' ) {
117 if( HasPattern( text, pattern ) ) {
129 /* Delete a ListGame; implies removint it from a list.
132 GameListDeleteGame (ListGame *listGame)
135 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
136 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
137 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
138 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
139 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
140 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
141 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
142 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
143 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
144 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
145 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
146 ListNodeFree((ListNode *) listGame);
151 /* Free the previous list of games.
154 GameListFree (List *gameList)
156 while (!ListEmpty(gameList))
158 GameListDeleteGame((ListGame *) gameList->head);
164 /* Initialize a new GameInfo structure.
167 GameListInitGameInfo (GameInfo *gameInfo)
169 gameInfo->event = NULL;
170 gameInfo->site = NULL;
171 gameInfo->date = NULL;
172 gameInfo->round = NULL;
173 gameInfo->white = NULL;
174 gameInfo->black = NULL;
175 gameInfo->result = GameUnfinished;
176 gameInfo->fen = NULL;
177 gameInfo->resultDetails = NULL;
178 gameInfo->timeControl = NULL;
179 gameInfo->extraTags = NULL;
180 gameInfo->whiteRating = -1; /* unknown */
181 gameInfo->blackRating = -1; /* unknown */
182 gameInfo->variant = VariantNormal;
183 gameInfo->outOfBook = NULL;
184 gameInfo->resultDetails = NULL;
188 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
190 * Note, that the ListGame is *not* added to any list
197 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
198 GameListInitGameInfo(&listGame->gameInfo);
204 /* Creates a new game for the gamelist.
207 GameListNewGame (ListGame **listGamePtr)
209 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
210 GameListFree(&gameList);
213 ListAddTail(&gameList, (ListNode *) *listGamePtr);
218 /* Build the list of games in the open file f.
219 * Returns 0 for success or error number.
222 GameListBuild (FILE *f)
224 ChessMove cm, lastStart;
226 ListGame *currentListGame = NULL;
227 int error, scratch=100, plyNr=0, fromX, fromY, toX, toY;
229 char lastComment[MSG_SIZ], buf[MSG_SIZ];
233 GameListFree(&gameList);
238 lastStart = (ChessMove) 0;
241 yyboardindex = scratch;
243 quickFlag = plyNr + 1;
244 cm = (ChessMove) Myylex();
247 if ((error = GameListNewGame(¤tListGame))) {
252 currentListGame->number = ++gameNumber;
253 currentListGame->offset = offset;
254 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
255 if (currentListGame->gameInfo.event != NULL) {
256 free(currentListGame->gameInfo.event);
258 currentListGame->gameInfo.event = StrSave(yy_text);
270 break; /* Already started */
274 if ((error = GameListNewGame(¤tListGame))) {
279 currentListGame->number = ++gameNumber;
280 currentListGame->offset = offset;
281 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
285 break; /* impossible */
290 if ((error = GameListNewGame(¤tListGame))) {
295 currentListGame->number = ++gameNumber;
296 currentListGame->offset = offset;
297 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
301 cm = (ChessMove) Myylex();
303 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
305 } while (cm == PGNTag || cm == Comment);
308 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen);
309 else CopyBoard(boards[scratch], initialPosition);
311 currentListGame->moves = PackGame(boards[scratch]);
313 if(cm != NormalMove) break;
315 if(appData.testLegality) break;
317 /* Allow the first game to start with an unnumbered move */
319 if (lastStart == (ChessMove) 0) {
320 if ((error = GameListNewGame(¤tListGame))) {
325 currentListGame->number = ++gameNumber;
326 currentListGame->offset = offset;
327 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
328 lastStart = MoveNumberOne;
330 case WhiteCapturesEnPassant:
331 case BlackCapturesEnPassant:
334 case WhiteNonPromotion:
335 case BlackNonPromotion:
336 case WhiteKingSideCastle:
337 case WhiteQueenSideCastle:
338 case BlackKingSideCastle:
339 case BlackQueenSideCastle:
340 case WhiteKingSideCastleWild:
341 case WhiteQueenSideCastleWild:
342 case BlackKingSideCastleWild:
343 case BlackQueenSideCastleWild:
344 case WhiteHSideCastleFR:
345 case WhiteASideCastleFR:
346 case BlackHSideCastleFR:
347 case BlackASideCastleFR:
348 fromX = currentMoveString[0] - AAA;
349 fromY = currentMoveString[1] - ONE;
350 toX = currentMoveString[2] - AAA;
351 toY = currentMoveString[3] - ONE;
353 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
354 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
356 case WhiteWins: // [HGM] rescom: save last comment as result details
360 if(!currentListGame) break;
361 if (currentListGame->gameInfo.resultDetails != NULL) {
362 free(currentListGame->gameInfo.resultDetails);
364 if(yy_text[0] == '{') {
366 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
367 if((p = strchr(lastComment, '}'))) *p = 0;
368 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
374 if(gameNumber % 1000 == 0) {
375 snprintf(buf, MSG_SIZ, _("Reading game file (%d)"), gameNumber);
379 while (cm != (ChessMove) 0);
381 if(currentListGame) {
382 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
384 if (appData.debugMode) {
385 for (currentListGame = (ListGame *) gameList.head;
386 currentListGame->node.succ;
387 currentListGame = (ListGame *) currentListGame->node.succ) {
389 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
390 currentListGame->number, currentListGame->offset);
391 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
395 if(appData.debugMode) { GetTimeMark(&t2);printf("GameListBuild %ld msec\n", SubtractTimeMarks(&t2,&t)); }
397 PackGame(boards[scratch]); // for appending end-of-game marker.
398 DisplayTitle("WinBoard");
405 /* Clear an existing GameInfo structure.
408 ClearGameInfo (GameInfo *gameInfo)
410 if (gameInfo->event != NULL) {
411 free(gameInfo->event);
413 if (gameInfo->site != NULL) {
414 free(gameInfo->site);
416 if (gameInfo->date != NULL) {
417 free(gameInfo->date);
419 if (gameInfo->round != NULL) {
420 free(gameInfo->round);
422 if (gameInfo->white != NULL) {
423 free(gameInfo->white);
425 if (gameInfo->black != NULL) {
426 free(gameInfo->black);
428 if (gameInfo->resultDetails != NULL) {
429 free(gameInfo->resultDetails);
431 if (gameInfo->fen != NULL) {
434 if (gameInfo->timeControl != NULL) {
435 free(gameInfo->timeControl);
437 if (gameInfo->extraTags != NULL) {
438 free(gameInfo->extraTags);
440 if (gameInfo->outOfBook != NULL) {
441 free(gameInfo->outOfBook);
443 GameListInitGameInfo(gameInfo);
446 /* [AS] Replaced by "dynamic" tag selection below */
448 GameListLineOld (int number, GameInfo *gameInfo)
450 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
451 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
452 char *white = gameInfo->white ? gameInfo->white : "?";
453 char *black = gameInfo->black ? gameInfo->black : "?";
454 char *date = gameInfo->date ? gameInfo->date : "?";
455 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
456 strlen(black) + 11 + strlen(date) + 1;
457 char *ret = (char *) malloc(len);
458 sprintf(ret, "%d. %s, %s-%s, %s, %s",
459 number, event, white, black, PGNResult(gameInfo->result), date);
463 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
466 GameListLine (int number, GameInfo * gameInfo)
468 char buffer[2*MSG_SIZ];
470 char * glt = appData.gameListTags;
472 buf += sprintf( buffer, "%d.", number );
474 while( *glt != '\0' ) {
479 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
482 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
485 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
488 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
491 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
492 buf[ MAX_FIELD_LEN-1 ] = '\0';
493 buf += strlen( buf );
495 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
498 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
501 if( gameInfo->whiteRating > 0 )
502 sprintf( buf, "%d", gameInfo->whiteRating );
504 safeStrCpy( buf, "?" , 2*MSG_SIZ);
507 if( gameInfo->blackRating > 0 )
508 sprintf( buf, "%d", gameInfo->blackRating );
510 safeStrCpy( buf, "?" , 2*MSG_SIZ);
512 case GLT_TIME_CONTROL:
513 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
517 case GLT_OUT_OF_BOOK:
518 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
520 case GLT_RESULT_COMMENT:
521 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
527 buf[MAX_FIELD_LEN-1] = '\0';
529 buf += strlen( buf );
540 return strdup( buffer );
544 GameListLineFull (int number, GameInfo * gameInfo)
546 char * event = gameInfo->event ? gameInfo->event : "?";
547 char * site = gameInfo->site ? gameInfo->site : "?";
548 char * white = gameInfo->white ? gameInfo->white : "?";
549 char * black = gameInfo->black ? gameInfo->black : "?";
550 char * round = gameInfo->round ? gameInfo->round : "?";
551 char * date = gameInfo->date ? gameInfo->date : "?";
552 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
553 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
555 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
557 char *ret = (char *) malloc(len);
559 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
560 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
564 // --------------------------------------- Game-List options dialog --------------------------------------
572 // back-end: translation table tag id-char <-> full tag name
573 static GLT_Item GLT_ItemInfo[] = {
574 { GLT_EVENT, "Event" },
575 { GLT_SITE, "Site" },
576 { GLT_DATE, "Date" },
577 { GLT_ROUND, "Round" },
578 { GLT_PLAYERS, "Players" },
579 { GLT_RESULT, "Result" },
580 { GLT_WHITE_ELO, "White Rating" },
581 { GLT_BLACK_ELO, "Black Rating" },
582 { GLT_TIME_CONTROL,"Time Control" },
583 { GLT_VARIANT, "Variant" },
584 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
585 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
589 char lpUserGLT[LPUSERGLT_SIZE];
591 // back-end: convert the tag id-char to a full tag name
593 GLT_FindItem (char id)
597 GLT_Item * list = GLT_ItemInfo;
599 while( list->id != 0 ) {
600 if( list->id == id ) {
611 // back-end: build the list of tag names
613 GLT_TagsToList (char *tags)
620 GLT_AddToList( GLT_FindItem(*pc) );
624 GLT_AddToList( " --- Hidden tags --- " );
629 if( strchr( tags, *pc ) == 0 ) {
630 GLT_AddToList( GLT_FindItem(*pc) );
638 // back-end: retrieve item from dialog and translate to id-char
640 GLT_ListItemToTag (int index)
645 GLT_Item * list = GLT_ItemInfo;
647 if( GLT_GetFromList(index, name) ) {
648 while( list->id != 0 ) {
649 if( strcmp( list->name, name ) == 0 ) {
661 // back-end: add items id-chars one-by-one to temp tags string
665 char * pc = lpUserGLT;
670 id = GLT_ListItemToTag( idx );
673 } while( id != '\0' );