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];
221 long sec; /* Assuming this is >= 32 bits */
222 int ms; /* Assuming this is >= 16 bits */
223 } t,t2; GetTimeMark(&t);
224 GameListFree(&gameList);
229 lastStart = (ChessMove) 0;
232 yyboardindex = scratch + (plyNr & 1);
234 cm = (ChessMove) Myylex();
237 if ((error = GameListNewGame(¤tListGame))) {
242 currentListGame->number = ++gameNumber;
243 currentListGame->offset = offset;
244 if (currentListGame->gameInfo.event != NULL) {
245 free(currentListGame->gameInfo.event);
247 currentListGame->gameInfo.event = StrSave(yy_text);
259 break; /* Already started */
263 if ((error = GameListNewGame(¤tListGame))) {
268 currentListGame->number = ++gameNumber;
269 currentListGame->offset = offset;
273 break; /* impossible */
278 if ((error = GameListNewGame(¤tListGame))) {
283 currentListGame->number = ++gameNumber;
284 currentListGame->offset = offset;
285 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
289 cm = (ChessMove) Myylex();
291 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
293 } while (cm == PGNTag || cm == Comment);
294 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
295 if(cm != NormalMove) break;
297 if(appData.testLegality) break;
299 /* Allow the first game to start with an unnumbered move */
301 if (lastStart == (ChessMove) 0) {
302 if ((error = GameListNewGame(¤tListGame))) {
307 currentListGame->number = ++gameNumber;
308 currentListGame->offset = offset;
309 lastStart = MoveNumberOne;
311 case WhiteCapturesEnPassant:
312 case BlackCapturesEnPassant:
315 case WhiteNonPromotion:
316 case BlackNonPromotion:
317 case WhiteKingSideCastle:
318 case WhiteQueenSideCastle:
319 case BlackKingSideCastle:
320 case BlackQueenSideCastle:
321 case WhiteKingSideCastleWild:
322 case WhiteQueenSideCastleWild:
323 case BlackKingSideCastleWild:
324 case BlackQueenSideCastleWild:
325 case WhiteHSideCastleFR:
326 case WhiteASideCastleFR:
327 case BlackHSideCastleFR:
328 case BlackASideCastleFR:
329 fromX = currentMoveString[0] - AAA;
330 fromY = currentMoveString[1] - ONE;
331 toX = currentMoveString[2] - AAA;
332 toY = currentMoveString[3] - ONE;
333 CopyBoard(boards[scratch + (plyNr+1&1)], boards[scratch + (plyNr&1)]);
335 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch + (plyNr&1)]);
336 PackMove(fromX, fromY, toX, toY, currentMoveString[4]);
338 case WhiteWins: // [HGM] rescom: save last comment as result details
342 if(!currentListGame) break;
343 if (currentListGame->gameInfo.resultDetails != NULL) {
344 free(currentListGame->gameInfo.resultDetails);
346 if(yy_text[0] == '{') { char *p;
347 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
348 if(p = strchr(lastComment, '}')) *p = 0;
349 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
355 if(gameNumber % 1000 == 0) {
356 snprintf(buf, MSG_SIZ,"Reading game file (%d)", gameNumber);
360 while (cm != (ChessMove) 0);
362 if (appData.debugMode) {
363 for (currentListGame = (ListGame *) gameList.head;
364 currentListGame->node.succ;
365 currentListGame = (ListGame *) currentListGame->node.succ) {
367 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
368 currentListGame->number, currentListGame->offset);
369 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
372 GetTimeMark(&t2);printf("GameListBuild %d msec\n", SubtractTimeMarks(&t2,&t));
374 DisplayTitle("WinBoard");
381 /* Clear an existing GameInfo structure.
383 void ClearGameInfo(gameInfo)
386 if (gameInfo->event != NULL) {
387 free(gameInfo->event);
389 if (gameInfo->site != NULL) {
390 free(gameInfo->site);
392 if (gameInfo->date != NULL) {
393 free(gameInfo->date);
395 if (gameInfo->round != NULL) {
396 free(gameInfo->round);
398 if (gameInfo->white != NULL) {
399 free(gameInfo->white);
401 if (gameInfo->black != NULL) {
402 free(gameInfo->black);
404 if (gameInfo->resultDetails != NULL) {
405 free(gameInfo->resultDetails);
407 if (gameInfo->fen != NULL) {
410 if (gameInfo->timeControl != NULL) {
411 free(gameInfo->timeControl);
413 if (gameInfo->extraTags != NULL) {
414 free(gameInfo->extraTags);
416 if (gameInfo->outOfBook != NULL) {
417 free(gameInfo->outOfBook);
419 GameListInitGameInfo(gameInfo);
422 /* [AS] Replaced by "dynamic" tag selection below */
424 GameListLineOld(number, gameInfo)
428 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
429 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
430 char *white = gameInfo->white ? gameInfo->white : "?";
431 char *black = gameInfo->black ? gameInfo->black : "?";
432 char *date = gameInfo->date ? gameInfo->date : "?";
433 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
434 strlen(black) + 11 + strlen(date) + 1;
435 char *ret = (char *) malloc(len);
436 sprintf(ret, "%d. %s, %s-%s, %s, %s",
437 number, event, white, black, PGNResult(gameInfo->result), date);
441 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
443 char * GameListLine( int number, GameInfo * gameInfo )
445 char buffer[2*MSG_SIZ];
447 char * glt = appData.gameListTags;
449 buf += sprintf( buffer, "%d.", number );
451 while( *glt != '\0' ) {
456 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
459 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
462 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
465 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
468 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
469 buf[ MAX_FIELD_LEN-1 ] = '\0';
470 buf += strlen( buf );
472 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
475 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
478 if( gameInfo->whiteRating > 0 )
479 sprintf( buf, "%d", gameInfo->whiteRating );
481 safeStrCpy( buf, "?" , 2*MSG_SIZ);
484 if( gameInfo->blackRating > 0 )
485 sprintf( buf, "%d", gameInfo->blackRating );
487 safeStrCpy( buf, "?" , 2*MSG_SIZ);
489 case GLT_TIME_CONTROL:
490 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
494 case GLT_OUT_OF_BOOK:
495 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
497 case GLT_RESULT_COMMENT:
498 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
504 buf[MAX_FIELD_LEN-1] = '\0';
506 buf += strlen( buf );
517 return strdup( buffer );
520 char * GameListLineFull( int number, GameInfo * gameInfo )
522 char * event = gameInfo->event ? gameInfo->event : "?";
523 char * site = gameInfo->site ? gameInfo->site : "?";
524 char * white = gameInfo->white ? gameInfo->white : "?";
525 char * black = gameInfo->black ? gameInfo->black : "?";
526 char * round = gameInfo->round ? gameInfo->round : "?";
527 char * date = gameInfo->date ? gameInfo->date : "?";
528 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
529 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
531 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
533 char *ret = (char *) malloc(len);
535 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
536 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
540 // --------------------------------------- Game-List options dialog --------------------------------------
548 // back-end: translation table tag id-char <-> full tag name
549 static GLT_Item GLT_ItemInfo[] = {
550 { GLT_EVENT, "Event" },
551 { GLT_SITE, "Site" },
552 { GLT_DATE, "Date" },
553 { GLT_ROUND, "Round" },
554 { GLT_PLAYERS, "Players" },
555 { GLT_RESULT, "Result" },
556 { GLT_WHITE_ELO, "White Rating" },
557 { GLT_BLACK_ELO, "Black Rating" },
558 { GLT_TIME_CONTROL,"Time Control" },
559 { GLT_VARIANT, "Variant" },
560 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
561 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
565 char lpUserGLT[LPUSERGLT_SIZE];
567 // back-end: convert the tag id-char to a full tag name
568 char * GLT_FindItem( char id )
572 GLT_Item * list = GLT_ItemInfo;
574 while( list->id != 0 ) {
575 if( list->id == id ) {
586 // back-end: build the list of tag names
588 GLT_TagsToList( char * tags )
595 GLT_AddToList( GLT_FindItem(*pc) );
599 GLT_AddToList( " --- Hidden tags --- " );
604 if( strchr( tags, *pc ) == 0 ) {
605 GLT_AddToList( GLT_FindItem(*pc) );
613 // back-end: retrieve item from dialog and translate to id-char
615 GLT_ListItemToTag( int index )
620 GLT_Item * list = GLT_ItemInfo;
622 if( GLT_GetFromList(index, name) ) {
623 while( list->id != 0 ) {
624 if( strcmp( list->name, name ) == 0 ) {
636 // back-end: add items id-chars one-by-one to temp tags string
640 char * pc = lpUserGLT;
645 id = GLT_ListItemToTag( idx );
648 } while( id != '\0' );