2 * gamelist.c -- Functions to manage a gamelist
4 * Copyright 1995, 2009, 2010, 2011 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 */
50 extern Board initialPosition;
54 /* Local function prototypes
56 static void GameListDeleteGame P((ListGame *));
57 static ListGame *GameListCreate P((void));
58 static void GameListFree P((List *));
59 static int GameListNewGame P((ListGame **));
61 /* [AS] Wildcard pattern matching */
63 HasPattern( const char * text, const char * pattern )
65 while( *pattern != '\0' ) {
66 if( *pattern == '*' ) {
67 while( *pattern == '*' ) {
71 if( *pattern == '\0' ) {
75 while( *text != '\0' ) {
76 if( HasPattern( text, pattern ) ) {
82 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
95 SearchPattern( const char * text, const char * pattern )
97 Boolean result = TRUE;
99 if( pattern != NULL && *pattern != '\0' ) {
100 if( *pattern == '*' ) {
101 result = HasPattern( text, pattern );
106 while( *text != '\0' ) {
107 if( HasPattern( text, pattern ) ) {
119 /* Delete a ListGame; implies removint it from a list.
121 static void GameListDeleteGame(listGame)
125 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
126 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
127 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
128 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
129 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
130 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
131 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
132 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
133 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
134 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
135 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
136 ListNodeFree((ListNode *) listGame);
141 /* Free the previous list of games.
143 static void GameListFree(gameList)
146 while (!ListEmpty(gameList))
148 GameListDeleteGame((ListGame *) gameList->head);
154 /* Initialize a new GameInfo structure.
156 void GameListInitGameInfo(gameInfo)
159 gameInfo->event = NULL;
160 gameInfo->site = NULL;
161 gameInfo->date = NULL;
162 gameInfo->round = NULL;
163 gameInfo->white = NULL;
164 gameInfo->black = NULL;
165 gameInfo->result = GameUnfinished;
166 gameInfo->fen = NULL;
167 gameInfo->resultDetails = NULL;
168 gameInfo->timeControl = NULL;
169 gameInfo->extraTags = NULL;
170 gameInfo->whiteRating = -1; /* unknown */
171 gameInfo->blackRating = -1; /* unknown */
172 gameInfo->variant = VariantNormal;
173 gameInfo->outOfBook = NULL;
174 gameInfo->resultDetails = NULL;
178 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
180 * Note, that the ListGame is *not* added to any list
182 static ListGame *GameListCreate()
187 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
188 GameListInitGameInfo(&listGame->gameInfo);
194 /* Creates a new game for the gamelist.
196 static int GameListNewGame(listGamePtr)
197 ListGame **listGamePtr;
199 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
200 GameListFree(&gameList);
203 ListAddTail(&gameList, (ListNode *) *listGamePtr);
208 /* Build the list of games in the open file f.
209 * Returns 0 for success or error number.
214 ChessMove cm, lastStart;
216 ListGame *currentListGame = NULL;
217 int error, scratch=100, plyNr=0, fromX, fromY, toX, toY;
219 char lastComment[MSG_SIZ], buf[MSG_SIZ];
223 GameListFree(&gameList);
228 lastStart = (ChessMove) 0;
231 yyboardindex = scratch;
233 quickFlag = plyNr + 1;
234 cm = (ChessMove) Myylex();
237 if ((error = GameListNewGame(¤tListGame))) {
242 currentListGame->number = ++gameNumber;
243 currentListGame->offset = offset;
244 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
245 if (currentListGame->gameInfo.event != NULL) {
246 free(currentListGame->gameInfo.event);
248 currentListGame->gameInfo.event = StrSave(yy_text);
260 break; /* Already started */
264 if ((error = GameListNewGame(¤tListGame))) {
269 currentListGame->number = ++gameNumber;
270 currentListGame->offset = offset;
271 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
275 break; /* impossible */
280 if ((error = GameListNewGame(¤tListGame))) {
285 currentListGame->number = ++gameNumber;
286 currentListGame->offset = offset;
287 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
291 cm = (ChessMove) Myylex();
293 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
295 } while (cm == PGNTag || cm == Comment);
298 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen);
299 else CopyBoard(boards[scratch], initialPosition);
301 currentListGame->moves = PackGame(boards[scratch]);
303 if(cm != NormalMove) break;
305 if(appData.testLegality) break;
307 /* Allow the first game to start with an unnumbered move */
309 if (lastStart == (ChessMove) 0) {
310 if ((error = GameListNewGame(¤tListGame))) {
315 currentListGame->number = ++gameNumber;
316 currentListGame->offset = offset;
317 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
318 lastStart = MoveNumberOne;
320 case WhiteCapturesEnPassant:
321 case BlackCapturesEnPassant:
324 case WhiteNonPromotion:
325 case BlackNonPromotion:
326 case WhiteKingSideCastle:
327 case WhiteQueenSideCastle:
328 case BlackKingSideCastle:
329 case BlackQueenSideCastle:
330 case WhiteKingSideCastleWild:
331 case WhiteQueenSideCastleWild:
332 case BlackKingSideCastleWild:
333 case BlackQueenSideCastleWild:
334 case WhiteHSideCastleFR:
335 case WhiteASideCastleFR:
336 case BlackHSideCastleFR:
337 case BlackASideCastleFR:
338 fromX = currentMoveString[0] - AAA;
339 fromY = currentMoveString[1] - ONE;
340 toX = currentMoveString[2] - AAA;
341 toY = currentMoveString[3] - ONE;
343 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
344 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
346 case WhiteWins: // [HGM] rescom: save last comment as result details
350 if(!currentListGame) break;
351 if (currentListGame->gameInfo.resultDetails != NULL) {
352 free(currentListGame->gameInfo.resultDetails);
354 if(yy_text[0] == '{') { char *p;
355 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
356 if(p = strchr(lastComment, '}')) *p = 0;
357 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
363 if(gameNumber % 1000 == 0) {
364 snprintf(buf, MSG_SIZ,"Reading game file (%d)", gameNumber);
368 while (cm != (ChessMove) 0);
370 if(currentListGame) {
371 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
373 if (appData.debugMode) {
374 for (currentListGame = (ListGame *) gameList.head;
375 currentListGame->node.succ;
376 currentListGame = (ListGame *) currentListGame->node.succ) {
378 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
379 currentListGame->number, currentListGame->offset);
380 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
384 GetTimeMark(&t2);printf("GameListBuild %d msec\n", SubtractTimeMarks(&t2,&t));
386 PackGame(boards[scratch]); // for appending end-of-game marker.
387 DisplayTitle("WinBoard");
394 /* Clear an existing GameInfo structure.
396 void ClearGameInfo(gameInfo)
399 if (gameInfo->event != NULL) {
400 free(gameInfo->event);
402 if (gameInfo->site != NULL) {
403 free(gameInfo->site);
405 if (gameInfo->date != NULL) {
406 free(gameInfo->date);
408 if (gameInfo->round != NULL) {
409 free(gameInfo->round);
411 if (gameInfo->white != NULL) {
412 free(gameInfo->white);
414 if (gameInfo->black != NULL) {
415 free(gameInfo->black);
417 if (gameInfo->resultDetails != NULL) {
418 free(gameInfo->resultDetails);
420 if (gameInfo->fen != NULL) {
423 if (gameInfo->timeControl != NULL) {
424 free(gameInfo->timeControl);
426 if (gameInfo->extraTags != NULL) {
427 free(gameInfo->extraTags);
429 if (gameInfo->outOfBook != NULL) {
430 free(gameInfo->outOfBook);
432 GameListInitGameInfo(gameInfo);
435 /* [AS] Replaced by "dynamic" tag selection below */
437 GameListLineOld(number, gameInfo)
441 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
442 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
443 char *white = gameInfo->white ? gameInfo->white : "?";
444 char *black = gameInfo->black ? gameInfo->black : "?";
445 char *date = gameInfo->date ? gameInfo->date : "?";
446 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
447 strlen(black) + 11 + strlen(date) + 1;
448 char *ret = (char *) malloc(len);
449 sprintf(ret, "%d. %s, %s-%s, %s, %s",
450 number, event, white, black, PGNResult(gameInfo->result), date);
454 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
456 char * GameListLine( int number, GameInfo * gameInfo )
458 char buffer[2*MSG_SIZ];
460 char * glt = appData.gameListTags;
462 buf += sprintf( buffer, "%d.", number );
464 while( *glt != '\0' ) {
469 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
472 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
475 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
478 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
481 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
482 buf[ MAX_FIELD_LEN-1 ] = '\0';
483 buf += strlen( buf );
485 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
488 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
491 if( gameInfo->whiteRating > 0 )
492 sprintf( buf, "%d", gameInfo->whiteRating );
494 safeStrCpy( buf, "?" , 2*MSG_SIZ);
497 if( gameInfo->blackRating > 0 )
498 sprintf( buf, "%d", gameInfo->blackRating );
500 safeStrCpy( buf, "?" , 2*MSG_SIZ);
502 case GLT_TIME_CONTROL:
503 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
507 case GLT_OUT_OF_BOOK:
508 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
510 case GLT_RESULT_COMMENT:
511 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
517 buf[MAX_FIELD_LEN-1] = '\0';
519 buf += strlen( buf );
530 return strdup( buffer );
533 char * GameListLineFull( int number, GameInfo * gameInfo )
535 char * event = gameInfo->event ? gameInfo->event : "?";
536 char * site = gameInfo->site ? gameInfo->site : "?";
537 char * white = gameInfo->white ? gameInfo->white : "?";
538 char * black = gameInfo->black ? gameInfo->black : "?";
539 char * round = gameInfo->round ? gameInfo->round : "?";
540 char * date = gameInfo->date ? gameInfo->date : "?";
541 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
542 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
544 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
546 char *ret = (char *) malloc(len);
548 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
549 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
553 // --------------------------------------- Game-List options dialog --------------------------------------
561 // back-end: translation table tag id-char <-> full tag name
562 static GLT_Item GLT_ItemInfo[] = {
563 { GLT_EVENT, "Event" },
564 { GLT_SITE, "Site" },
565 { GLT_DATE, "Date" },
566 { GLT_ROUND, "Round" },
567 { GLT_PLAYERS, "Players" },
568 { GLT_RESULT, "Result" },
569 { GLT_WHITE_ELO, "White Rating" },
570 { GLT_BLACK_ELO, "Black Rating" },
571 { GLT_TIME_CONTROL,"Time Control" },
572 { GLT_VARIANT, "Variant" },
573 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
574 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
578 char lpUserGLT[LPUSERGLT_SIZE];
580 // back-end: convert the tag id-char to a full tag name
581 char * GLT_FindItem( char id )
585 GLT_Item * list = GLT_ItemInfo;
587 while( list->id != 0 ) {
588 if( list->id == id ) {
599 // back-end: build the list of tag names
601 GLT_TagsToList( char * tags )
608 GLT_AddToList( GLT_FindItem(*pc) );
612 GLT_AddToList( " --- Hidden tags --- " );
617 if( strchr( tags, *pc ) == 0 ) {
618 GLT_AddToList( GLT_FindItem(*pc) );
626 // back-end: retrieve item from dialog and translate to id-char
628 GLT_ListItemToTag( int index )
633 GLT_Item * list = GLT_ItemInfo;
635 if( GLT_GetFromList(index, name) ) {
636 while( list->id != 0 ) {
637 if( strcmp( list->name, name ) == 0 ) {
649 // back-end: add items id-chars one-by-one to temp tags string
653 char * pc = lpUserGLT;
658 id = GLT_ListItemToTag( idx );
661 } while( id != '\0' );