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;
234 quickFlag = plyNr + 1;
235 cm = (ChessMove) Myylex();
238 if ((error = GameListNewGame(¤tListGame))) {
243 currentListGame->number = ++gameNumber;
244 currentListGame->offset = offset;
245 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
246 if (currentListGame->gameInfo.event != NULL) {
247 free(currentListGame->gameInfo.event);
249 currentListGame->gameInfo.event = StrSave(yy_text);
261 break; /* Already started */
265 if ((error = GameListNewGame(¤tListGame))) {
270 currentListGame->number = ++gameNumber;
271 currentListGame->offset = offset;
272 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
276 break; /* impossible */
281 if ((error = GameListNewGame(¤tListGame))) {
286 currentListGame->number = ++gameNumber;
287 currentListGame->offset = offset;
288 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
292 cm = (ChessMove) Myylex();
294 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
296 } while (cm == PGNTag || cm == Comment);
299 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen);
300 else CopyBoard(boards[scratch], initialPosition);
302 currentListGame->moves = PackGame(boards[scratch]);
304 if(cm != NormalMove) break;
306 if(appData.testLegality) break;
308 /* Allow the first game to start with an unnumbered move */
310 if (lastStart == (ChessMove) 0) {
311 if ((error = GameListNewGame(¤tListGame))) {
316 currentListGame->number = ++gameNumber;
317 currentListGame->offset = offset;
318 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
319 lastStart = MoveNumberOne;
321 case WhiteCapturesEnPassant:
322 case BlackCapturesEnPassant:
325 case WhiteNonPromotion:
326 case BlackNonPromotion:
327 case WhiteKingSideCastle:
328 case WhiteQueenSideCastle:
329 case BlackKingSideCastle:
330 case BlackQueenSideCastle:
331 case WhiteKingSideCastleWild:
332 case WhiteQueenSideCastleWild:
333 case BlackKingSideCastleWild:
334 case BlackQueenSideCastleWild:
335 case WhiteHSideCastleFR:
336 case WhiteASideCastleFR:
337 case BlackHSideCastleFR:
338 case BlackASideCastleFR:
339 fromX = currentMoveString[0] - AAA;
340 fromY = currentMoveString[1] - ONE;
341 toX = currentMoveString[2] - AAA;
342 toY = currentMoveString[3] - ONE;
344 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
345 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
347 case WhiteWins: // [HGM] rescom: save last comment as result details
351 if(!currentListGame) break;
352 if (currentListGame->gameInfo.resultDetails != NULL) {
353 free(currentListGame->gameInfo.resultDetails);
355 if(yy_text[0] == '{') { char *p;
356 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
357 if(p = strchr(lastComment, '}')) *p = 0;
358 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
364 if(gameNumber % 1000 == 0) {
365 snprintf(buf, MSG_SIZ,"Reading game file (%d)", gameNumber);
369 while (cm != (ChessMove) 0);
371 if(currentListGame) {
372 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
374 if (appData.debugMode) {
375 for (currentListGame = (ListGame *) gameList.head;
376 currentListGame->node.succ;
377 currentListGame = (ListGame *) currentListGame->node.succ) {
379 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
380 currentListGame->number, currentListGame->offset);
381 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
385 GetTimeMark(&t2);printf("GameListBuild %d msec\n", SubtractTimeMarks(&t2,&t));
387 PackGame(boards[scratch]); // for appending end-of-game marker.
388 DisplayTitle("WinBoard");
395 /* Clear an existing GameInfo structure.
397 void ClearGameInfo(gameInfo)
400 if (gameInfo->event != NULL) {
401 free(gameInfo->event);
403 if (gameInfo->site != NULL) {
404 free(gameInfo->site);
406 if (gameInfo->date != NULL) {
407 free(gameInfo->date);
409 if (gameInfo->round != NULL) {
410 free(gameInfo->round);
412 if (gameInfo->white != NULL) {
413 free(gameInfo->white);
415 if (gameInfo->black != NULL) {
416 free(gameInfo->black);
418 if (gameInfo->resultDetails != NULL) {
419 free(gameInfo->resultDetails);
421 if (gameInfo->fen != NULL) {
424 if (gameInfo->timeControl != NULL) {
425 free(gameInfo->timeControl);
427 if (gameInfo->extraTags != NULL) {
428 free(gameInfo->extraTags);
430 if (gameInfo->outOfBook != NULL) {
431 free(gameInfo->outOfBook);
433 GameListInitGameInfo(gameInfo);
436 /* [AS] Replaced by "dynamic" tag selection below */
438 GameListLineOld(number, gameInfo)
442 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
443 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
444 char *white = gameInfo->white ? gameInfo->white : "?";
445 char *black = gameInfo->black ? gameInfo->black : "?";
446 char *date = gameInfo->date ? gameInfo->date : "?";
447 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
448 strlen(black) + 11 + strlen(date) + 1;
449 char *ret = (char *) malloc(len);
450 sprintf(ret, "%d. %s, %s-%s, %s, %s",
451 number, event, white, black, PGNResult(gameInfo->result), date);
455 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
457 char * GameListLine( int number, GameInfo * gameInfo )
459 char buffer[2*MSG_SIZ];
461 char * glt = appData.gameListTags;
463 buf += sprintf( buffer, "%d.", number );
465 while( *glt != '\0' ) {
470 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
473 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
476 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
479 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
482 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
483 buf[ MAX_FIELD_LEN-1 ] = '\0';
484 buf += strlen( buf );
486 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
489 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
492 if( gameInfo->whiteRating > 0 )
493 sprintf( buf, "%d", gameInfo->whiteRating );
495 safeStrCpy( buf, "?" , 2*MSG_SIZ);
498 if( gameInfo->blackRating > 0 )
499 sprintf( buf, "%d", gameInfo->blackRating );
501 safeStrCpy( buf, "?" , 2*MSG_SIZ);
503 case GLT_TIME_CONTROL:
504 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
508 case GLT_OUT_OF_BOOK:
509 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
511 case GLT_RESULT_COMMENT:
512 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
518 buf[MAX_FIELD_LEN-1] = '\0';
520 buf += strlen( buf );
531 return strdup( buffer );
534 char * GameListLineFull( int number, GameInfo * gameInfo )
536 char * event = gameInfo->event ? gameInfo->event : "?";
537 char * site = gameInfo->site ? gameInfo->site : "?";
538 char * white = gameInfo->white ? gameInfo->white : "?";
539 char * black = gameInfo->black ? gameInfo->black : "?";
540 char * round = gameInfo->round ? gameInfo->round : "?";
541 char * date = gameInfo->date ? gameInfo->date : "?";
542 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
543 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
545 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
547 char *ret = (char *) malloc(len);
549 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
550 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
554 // --------------------------------------- Game-List options dialog --------------------------------------
562 // back-end: translation table tag id-char <-> full tag name
563 static GLT_Item GLT_ItemInfo[] = {
564 { GLT_EVENT, "Event" },
565 { GLT_SITE, "Site" },
566 { GLT_DATE, "Date" },
567 { GLT_ROUND, "Round" },
568 { GLT_PLAYERS, "Players" },
569 { GLT_RESULT, "Result" },
570 { GLT_WHITE_ELO, "White Rating" },
571 { GLT_BLACK_ELO, "Black Rating" },
572 { GLT_TIME_CONTROL,"Time Control" },
573 { GLT_VARIANT, "Variant" },
574 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
575 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
579 char lpUserGLT[LPUSERGLT_SIZE];
581 // back-end: convert the tag id-char to a full tag name
582 char * GLT_FindItem( char id )
586 GLT_Item * list = GLT_ItemInfo;
588 while( list->id != 0 ) {
589 if( list->id == id ) {
600 // back-end: build the list of tag names
602 GLT_TagsToList( char * tags )
609 GLT_AddToList( GLT_FindItem(*pc) );
613 GLT_AddToList( " --- Hidden tags --- " );
618 if( strchr( tags, *pc ) == 0 ) {
619 GLT_AddToList( GLT_FindItem(*pc) );
627 // back-end: retrieve item from dialog and translate to id-char
629 GLT_ListItemToTag( int index )
634 GLT_Item * list = GLT_ItemInfo;
636 if( GLT_GetFromList(index, name) ) {
637 while( list->id != 0 ) {
638 if( strcmp( list->name, name ) == 0 ) {
650 // back-end: add items id-chars one-by-one to temp tags string
654 char * pc = lpUserGLT;
659 id = GLT_ListItemToTag( idx );
662 } while( id != '\0' );