2 * gamelist.c -- Functions to manage a gamelist
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014 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->variantName = NULL;
184 gameInfo->outOfBook = NULL;
185 gameInfo->resultDetails = NULL;
189 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
191 * Note, that the ListGame is *not* added to any list
198 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
199 GameListInitGameInfo(&listGame->gameInfo);
205 /* Creates a new game for the gamelist.
208 GameListNewGame (ListGame **listGamePtr)
210 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
211 GameListFree(&gameList);
214 ListAddTail(&gameList, (ListNode *) *listGamePtr);
219 /* Build the list of games in the open file f.
220 * Returns 0 for success or error number.
223 GameListBuild (FILE *f)
225 ChessMove cm, lastStart;
227 ListGame *currentListGame = NULL;
228 int error, scratch=100, plyNr=0, fromX, fromY, toX, toY;
230 char lastComment[MSG_SIZ], buf[MSG_SIZ];
234 GameListFree(&gameList);
239 lastStart = (ChessMove) 0;
242 yyboardindex = scratch;
244 quickFlag = plyNr + 1;
245 cm = (ChessMove) Myylex();
248 if ((error = GameListNewGame(¤tListGame))) {
253 currentListGame->number = ++gameNumber;
254 currentListGame->offset = offset;
255 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
256 if (currentListGame->gameInfo.event != NULL) {
257 free(currentListGame->gameInfo.event);
259 currentListGame->gameInfo.event = StrSave(yy_text);
271 break; /* Already started */
275 if ((error = GameListNewGame(¤tListGame))) {
280 currentListGame->number = ++gameNumber;
281 currentListGame->offset = offset;
282 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
286 break; /* impossible */
291 if ((error = GameListNewGame(¤tListGame))) {
296 currentListGame->number = ++gameNumber;
297 currentListGame->offset = offset;
298 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
302 cm = (ChessMove) Myylex();
304 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
306 } while (cm == PGNTag || cm == Comment);
309 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen, FALSE);
310 else CopyBoard(boards[scratch], initialPosition);
312 currentListGame->moves = PackGame(boards[scratch]);
314 if(cm != NormalMove) break;
316 if(appData.testLegality) break;
318 /* Allow the first game to start with an unnumbered move */
320 if (lastStart == (ChessMove) 0) {
321 if ((error = GameListNewGame(¤tListGame))) {
326 currentListGame->number = ++gameNumber;
327 currentListGame->offset = offset;
328 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
329 lastStart = MoveNumberOne;
331 case WhiteCapturesEnPassant:
332 case BlackCapturesEnPassant:
335 case WhiteNonPromotion:
336 case BlackNonPromotion:
337 case WhiteKingSideCastle:
338 case WhiteQueenSideCastle:
339 case BlackKingSideCastle:
340 case BlackQueenSideCastle:
341 case WhiteKingSideCastleWild:
342 case WhiteQueenSideCastleWild:
343 case BlackKingSideCastleWild:
344 case BlackQueenSideCastleWild:
345 case WhiteHSideCastleFR:
346 case WhiteASideCastleFR:
347 case BlackHSideCastleFR:
348 case BlackASideCastleFR:
349 fromX = currentMoveString[0] - AAA;
350 fromY = currentMoveString[1] - ONE;
351 toX = currentMoveString[2] - AAA;
352 toY = currentMoveString[3] - ONE;
354 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
355 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
357 case WhiteWins: // [HGM] rescom: save last comment as result details
361 if(!currentListGame) break;
362 if (currentListGame->gameInfo.resultDetails != NULL) {
363 free(currentListGame->gameInfo.resultDetails);
365 if(yy_text[0] == '{') {
367 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
368 if((p = strchr(lastComment, '}'))) *p = 0;
369 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
375 if(gameNumber % 1000 == 0) {
376 snprintf(buf, MSG_SIZ, _("Reading game file (%d)"), gameNumber);
377 DisplayTitle(buf); DoEvents();
380 while (cm != (ChessMove) 0);
382 if(currentListGame) {
383 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
385 if (appData.debugMode) {
386 for (currentListGame = (ListGame *) gameList.head;
387 currentListGame->node.succ;
388 currentListGame = (ListGame *) currentListGame->node.succ) {
390 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
391 currentListGame->number, currentListGame->offset);
392 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
396 if(appData.debugMode) { GetTimeMark(&t2);printf("GameListBuild %ld msec\n", SubtractTimeMarks(&t2,&t)); }
398 PackGame(boards[scratch]); // for appending end-of-game marker.
399 DisplayTitle("WinBoard");
406 /* Clear an existing GameInfo structure.
409 ClearGameInfo (GameInfo *gameInfo)
411 if (gameInfo->event != NULL) {
412 free(gameInfo->event);
414 if (gameInfo->site != NULL) {
415 free(gameInfo->site);
417 if (gameInfo->date != NULL) {
418 free(gameInfo->date);
420 if (gameInfo->round != NULL) {
421 free(gameInfo->round);
423 if (gameInfo->white != NULL) {
424 free(gameInfo->white);
426 if (gameInfo->black != NULL) {
427 free(gameInfo->black);
429 if (gameInfo->resultDetails != NULL) {
430 free(gameInfo->resultDetails);
432 if (gameInfo->fen != NULL) {
435 if (gameInfo->timeControl != NULL) {
436 free(gameInfo->timeControl);
438 if (gameInfo->extraTags != NULL) {
439 free(gameInfo->extraTags);
441 if (gameInfo->variantName != NULL) {
442 free(gameInfo->variantName);
444 if (gameInfo->outOfBook != NULL) {
445 free(gameInfo->outOfBook);
447 GameListInitGameInfo(gameInfo);
450 /* [AS] Replaced by "dynamic" tag selection below */
452 GameListLineOld (int number, GameInfo *gameInfo)
454 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
455 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
456 char *white = gameInfo->white ? gameInfo->white : "?";
457 char *black = gameInfo->black ? gameInfo->black : "?";
458 char *date = gameInfo->date ? gameInfo->date : "?";
459 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
460 strlen(black) + 11 + strlen(date) + 1;
461 char *ret = (char *) malloc(len);
462 sprintf(ret, "%d. %s, %s-%s, %s, %s",
463 number, event, white, black, PGNResult(gameInfo->result), date);
467 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
470 GameListLine (int number, GameInfo * gameInfo)
472 char buffer[2*MSG_SIZ];
474 char * glt = appData.gameListTags;
476 buf += sprintf( buffer, "%d.", number );
478 while( *glt != '\0' ) {
483 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
486 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
489 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
492 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
495 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
496 buf[ MAX_FIELD_LEN-1 ] = '\0';
497 buf += strlen( buf );
499 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
502 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
505 if( gameInfo->whiteRating > 0 )
506 sprintf( buf, "%d", gameInfo->whiteRating );
508 safeStrCpy( buf, "?" , 2*MSG_SIZ);
511 if( gameInfo->blackRating > 0 )
512 sprintf( buf, "%d", gameInfo->blackRating );
514 safeStrCpy( buf, "?" , 2*MSG_SIZ);
516 case GLT_TIME_CONTROL:
517 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
520 strncpy( buf, gameInfo->variantName ? gameInfo->variantName : VariantName(gameInfo->variant), MAX_FIELD_LEN );
521 // strncpy( buf, VariantName(gameInfo->variant), MAX_FIELD_LEN );
523 case GLT_OUT_OF_BOOK:
524 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
526 case GLT_RESULT_COMMENT:
527 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
533 buf[MAX_FIELD_LEN-1] = '\0';
535 buf += strlen( buf );
546 return strdup( buffer );
550 GameListLineFull (int number, GameInfo * gameInfo)
552 char * event = gameInfo->event ? gameInfo->event : "?";
553 char * site = gameInfo->site ? gameInfo->site : "?";
554 char * white = gameInfo->white ? gameInfo->white : "?";
555 char * black = gameInfo->black ? gameInfo->black : "?";
556 char * round = gameInfo->round ? gameInfo->round : "?";
557 char * date = gameInfo->date ? gameInfo->date : "?";
558 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
559 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
561 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
563 char *ret = (char *) malloc(len);
565 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
566 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
570 // --------------------------------------- Game-List options dialog --------------------------------------
578 // back-end: translation table tag id-char <-> full tag name
579 static GLT_Item GLT_ItemInfo[] = {
580 { GLT_EVENT, "Event" },
581 { GLT_SITE, "Site" },
582 { GLT_DATE, "Date" },
583 { GLT_ROUND, "Round" },
584 { GLT_PLAYERS, "Players" },
585 { GLT_RESULT, "Result" },
586 { GLT_WHITE_ELO, "White Rating" },
587 { GLT_BLACK_ELO, "Black Rating" },
588 { GLT_TIME_CONTROL,"Time Control" },
589 { GLT_VARIANT, "Variant" },
590 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
591 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
595 char lpUserGLT[LPUSERGLT_SIZE];
597 // back-end: convert the tag id-char to a full tag name
599 GLT_FindItem (char id)
603 GLT_Item * list = GLT_ItemInfo;
605 while( list->id != 0 ) {
606 if( list->id == id ) {
617 // back-end: build the list of tag names
619 GLT_TagsToList (char *tags)
626 GLT_AddToList( GLT_FindItem(*pc) );
630 GLT_AddToList( " --- Hidden tags --- " );
635 if( strchr( tags, *pc ) == 0 ) {
636 GLT_AddToList( GLT_FindItem(*pc) );
644 // back-end: retrieve item from dialog and translate to id-char
646 GLT_ListItemToTag (int index)
651 GLT_Item * list = GLT_ItemInfo;
653 if( GLT_GetFromList(index, name) ) {
654 while( list->id != 0 ) {
655 if( strcmp( list->name, name ) == 0 ) {
667 // back-end: add items id-chars one-by-one to temp tags string
671 char * pc = lpUserGLT;
676 id = GLT_ListItemToTag( idx );
679 } while( id != '\0' );