2 * gamelist.c -- Functions to manage a gamelist
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
5 * Software Foundation, Inc.
7 * Enhancements Copyright 2005 Alessandro Scotti
9 * ------------------------------------------------------------------------
11 * GNU XBoard is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or (at
14 * your option) any later version.
16 * GNU XBoard is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
24 *------------------------------------------------------------------------
25 ** See the file ChangeLog for a revision history. */
34 #else /* not STDC_HEADERS */
37 # else /* not HAVE_STRING_H */
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
50 # define _(s) gettext (s)
51 # define N_(s) gettext_noop (s)
61 extern Board initialPosition;
65 /* Local function prototypes
67 static void GameListDeleteGame P((ListGame *));
68 static ListGame *GameListCreate P((void));
69 static void GameListFree P((List *));
70 static int GameListNewGame P((ListGame **));
72 /* [AS] Wildcard pattern matching */
74 HasPattern (const char * text, const char * pattern)
76 while( *pattern != '\0' ) {
77 if( *pattern == '*' ) {
78 while( *pattern == '*' ) {
82 if( *pattern == '\0' ) {
86 while( *text != '\0' ) {
87 if( HasPattern( text, pattern ) ) {
93 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
106 SearchPattern (const char * text, const char * pattern)
108 Boolean result = TRUE;
110 if( pattern != NULL && *pattern != '\0' ) {
111 if( *pattern == '*' ) {
112 result = HasPattern( text, pattern );
117 while( *text != '\0' ) {
118 if( HasPattern( text, pattern ) ) {
130 /* Delete a ListGame; implies removint it from a list.
133 GameListDeleteGame (ListGame *listGame)
136 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
137 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
138 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
139 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
140 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
141 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
142 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
143 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
144 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
145 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
146 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
147 ListNodeFree((ListNode *) listGame);
152 /* Free the previous list of games.
155 GameListFree (List *gameList)
157 while (!ListEmpty(gameList))
159 GameListDeleteGame((ListGame *) gameList->head);
165 /* Initialize a new GameInfo structure.
168 GameListInitGameInfo (GameInfo *gameInfo)
170 gameInfo->event = NULL;
171 gameInfo->site = NULL;
172 gameInfo->date = NULL;
173 gameInfo->round = NULL;
174 gameInfo->white = NULL;
175 gameInfo->black = NULL;
176 gameInfo->result = GameUnfinished;
177 gameInfo->fen = NULL;
178 gameInfo->resultDetails = NULL;
179 gameInfo->timeControl = NULL;
180 gameInfo->extraTags = NULL;
181 gameInfo->whiteRating = -1; /* unknown */
182 gameInfo->blackRating = -1; /* unknown */
183 gameInfo->variant = VariantNormal;
184 gameInfo->variantName = NULL;
185 gameInfo->outOfBook = NULL;
186 gameInfo->resultDetails = NULL;
190 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
192 * Note, that the ListGame is *not* added to any list
199 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
200 GameListInitGameInfo(&listGame->gameInfo);
206 /* Creates a new game for the gamelist.
209 GameListNewGame (ListGame **listGamePtr)
211 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
212 GameListFree(&gameList);
215 ListAddTail(&gameList, (ListNode *) *listGamePtr);
220 /* Build the list of games in the open file f.
221 * Returns 0 for success or error number.
224 GameListBuild (FILE *f)
226 ChessMove cm, lastStart;
228 ListGame *currentListGame = NULL;
229 int error, scratch=100, plyNr=0, fromX, fromY, toX, toY;
231 char lastComment[MSG_SIZ], buf[MSG_SIZ];
235 GameListFree(&gameList);
240 lastStart = (ChessMove) 0;
243 yyboardindex = scratch;
245 quickFlag = plyNr + 1;
246 cm = (ChessMove) Myylex();
249 if ((error = GameListNewGame(¤tListGame))) {
254 currentListGame->number = ++gameNumber;
255 currentListGame->offset = offset;
256 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
257 if (currentListGame->gameInfo.event != NULL) {
258 free(currentListGame->gameInfo.event);
260 currentListGame->gameInfo.event = StrSave(yy_text);
272 break; /* Already started */
276 if ((error = GameListNewGame(¤tListGame))) {
281 currentListGame->number = ++gameNumber;
282 currentListGame->offset = offset;
283 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
287 break; /* impossible */
292 if ((error = GameListNewGame(¤tListGame))) {
297 currentListGame->number = ++gameNumber;
298 currentListGame->offset = offset;
299 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
303 cm = (ChessMove) Myylex();
305 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
307 } while (cm == PGNTag || cm == Comment);
310 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen, FALSE);
311 else CopyBoard(boards[scratch], initialPosition);
313 currentListGame->moves = PackGame(boards[scratch]);
315 if(cm != NormalMove) break;
317 if(appData.testLegality) break;
319 /* Allow the first game to start with an unnumbered move */
321 if (lastStart == (ChessMove) 0) {
322 if ((error = GameListNewGame(¤tListGame))) {
327 currentListGame->number = ++gameNumber;
328 currentListGame->offset = offset;
329 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
330 lastStart = MoveNumberOne;
332 case WhiteCapturesEnPassant:
333 case BlackCapturesEnPassant:
336 case WhiteNonPromotion:
337 case BlackNonPromotion:
338 case WhiteKingSideCastle:
339 case WhiteQueenSideCastle:
340 case BlackKingSideCastle:
341 case BlackQueenSideCastle:
342 case WhiteKingSideCastleWild:
343 case WhiteQueenSideCastleWild:
344 case BlackKingSideCastleWild:
345 case BlackQueenSideCastleWild:
346 case WhiteHSideCastleFR:
347 case WhiteASideCastleFR:
348 case BlackHSideCastleFR:
349 case BlackASideCastleFR:
350 fromX = currentMoveString[0] - AAA;
351 fromY = currentMoveString[1] - ONE;
352 toX = currentMoveString[2] - AAA;
353 toY = currentMoveString[3] - ONE;
355 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
356 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
358 case WhiteWins: // [HGM] rescom: save last comment as result details
362 if(!currentListGame) break;
363 if(currentListGame->gameInfo.result == GameUnfinished)
364 currentListGame->gameInfo.result = cm; // correct result tag with actual result
365 if (currentListGame->gameInfo.resultDetails != NULL) {
366 free(currentListGame->gameInfo.resultDetails);
368 if(yy_text[0] == '{') {
370 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
371 if((p = strchr(lastComment, '}'))) *p = 0;
372 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
378 if(gameNumber % 1000 == 0) {
379 snprintf(buf, MSG_SIZ, _("Reading game file (%d)"), gameNumber);
380 DisplayTitle(buf); DoEvents();
383 while (cm != (ChessMove) 0);
385 if(currentListGame) {
386 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
388 if (appData.debugMode) {
389 for (currentListGame = (ListGame *) gameList.head;
390 currentListGame->node.succ;
391 currentListGame = (ListGame *) currentListGame->node.succ) {
393 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
394 currentListGame->number, currentListGame->offset);
395 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
399 if(appData.debugMode) { GetTimeMark(&t2);printf("GameListBuild %ld msec\n", SubtractTimeMarks(&t2,&t)); }
401 PackGame(boards[scratch]); // for appending end-of-game marker.
402 DisplayTitle("WinBoard");
409 /* Clear an existing GameInfo structure.
412 ClearGameInfo (GameInfo *gameInfo)
414 if (gameInfo->event != NULL) {
415 free(gameInfo->event);
417 if (gameInfo->site != NULL) {
418 free(gameInfo->site);
420 if (gameInfo->date != NULL) {
421 free(gameInfo->date);
423 if (gameInfo->round != NULL) {
424 free(gameInfo->round);
426 if (gameInfo->white != NULL) {
427 free(gameInfo->white);
429 if (gameInfo->black != NULL) {
430 free(gameInfo->black);
432 if (gameInfo->resultDetails != NULL) {
433 free(gameInfo->resultDetails);
435 if (gameInfo->fen != NULL) {
438 if (gameInfo->timeControl != NULL) {
439 free(gameInfo->timeControl);
441 if (gameInfo->extraTags != NULL) {
442 free(gameInfo->extraTags);
444 if (gameInfo->variantName != NULL) {
445 free(gameInfo->variantName);
447 if (gameInfo->outOfBook != NULL) {
448 free(gameInfo->outOfBook);
450 GameListInitGameInfo(gameInfo);
453 /* [AS] Replaced by "dynamic" tag selection below */
455 GameListLineOld (int number, GameInfo *gameInfo)
457 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
458 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
459 char *white = gameInfo->white ? gameInfo->white : "?";
460 char *black = gameInfo->black ? gameInfo->black : "?";
461 char *date = gameInfo->date ? gameInfo->date : "?";
462 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
463 strlen(black) + 11 + strlen(date) + 1;
464 char *ret = (char *) malloc(len);
465 sprintf(ret, "%d. %s, %s-%s, %s, %s",
466 number, event, white, black, PGNResult(gameInfo->result), date);
470 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
473 GameListLine (int number, GameInfo * gameInfo)
475 char buffer[2*MSG_SIZ];
477 char * glt = appData.gameListTags;
479 buf += sprintf( buffer, "%d.", number );
481 while( *glt != '\0' ) {
486 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
489 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
492 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
495 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
498 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
499 buf[ MAX_FIELD_LEN-1 ] = '\0';
500 buf += strlen( buf );
502 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
505 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
508 if( gameInfo->whiteRating > 0 )
509 sprintf( buf, "%d", gameInfo->whiteRating );
511 safeStrCpy( buf, "?" , 2*MSG_SIZ);
514 if( gameInfo->blackRating > 0 )
515 sprintf( buf, "%d", gameInfo->blackRating );
517 safeStrCpy( buf, "?" , 2*MSG_SIZ);
519 case GLT_TIME_CONTROL:
520 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
523 strncpy( buf, gameInfo->variantName ? gameInfo->variantName : VariantName(gameInfo->variant), MAX_FIELD_LEN );
524 // strncpy( buf, VariantName(gameInfo->variant), MAX_FIELD_LEN );
526 case GLT_OUT_OF_BOOK:
527 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
529 case GLT_RESULT_COMMENT:
530 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
536 buf[MAX_FIELD_LEN-1] = '\0';
538 buf += strlen( buf );
549 return strdup( buffer );
553 GameListLineFull (int number, GameInfo * gameInfo)
555 char * event = gameInfo->event ? gameInfo->event : "?";
556 char * site = gameInfo->site ? gameInfo->site : "?";
557 char * white = gameInfo->white ? gameInfo->white : "?";
558 char * black = gameInfo->black ? gameInfo->black : "?";
559 char * round = gameInfo->round ? gameInfo->round : "?";
560 char * date = gameInfo->date ? gameInfo->date : "?";
561 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
562 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
564 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
566 char *ret = (char *) malloc(len);
568 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
569 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
573 // --------------------------------------- Game-List options dialog --------------------------------------
581 // back-end: translation table tag id-char <-> full tag name
582 static GLT_Item GLT_ItemInfo[] = {
583 { GLT_EVENT, "Event" },
584 { GLT_SITE, "Site" },
585 { GLT_DATE, "Date" },
586 { GLT_ROUND, "Round" },
587 { GLT_PLAYERS, "Players" },
588 { GLT_RESULT, "Result" },
589 { GLT_WHITE_ELO, "White Rating" },
590 { GLT_BLACK_ELO, "Black Rating" },
591 { GLT_TIME_CONTROL,"Time Control" },
592 { GLT_VARIANT, "Variant" },
593 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
594 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
598 char lpUserGLT[LPUSERGLT_SIZE];
600 // back-end: convert the tag id-char to a full tag name
602 GLT_FindItem (char id)
606 GLT_Item * list = GLT_ItemInfo;
608 while( list->id != 0 ) {
609 if( list->id == id ) {
620 // back-end: build the list of tag names
622 GLT_TagsToList (char *tags)
629 GLT_AddToList( GLT_FindItem(*pc) );
633 GLT_AddToList( " --- Hidden tags --- " );
638 if( strchr( tags, *pc ) == 0 ) {
639 GLT_AddToList( GLT_FindItem(*pc) );
647 // back-end: retrieve item from dialog and translate to id-char
649 GLT_ListItemToTag (int index)
654 GLT_Item * list = GLT_ItemInfo;
656 if( GLT_GetFromList(index, name) ) {
657 while( list->id != 0 ) {
658 if( strcmp( list->name, name ) == 0 ) {
670 // back-end: add items id-chars one-by-one to temp tags string
674 char * pc = lpUserGLT;
679 id = GLT_ListItemToTag( idx );
682 } while( id != '\0' );