1 /*
2 * gamelist.c -- Functions to manage a gamelist
3 *
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
5 * Software Foundation, Inc.
6 *
7 * Enhancements Copyright 2005 Alessandro Scotti
8 *
9 * ------------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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/.
23 *
24 *------------------------------------------------------------------------
25 ** See the file ChangeLog for a revision history. */
26
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <errno.h>
31 #if STDC_HEADERS
32 # include <stdlib.h>
33 # include <string.h>
34 #else /* not STDC_HEADERS */
35 # if HAVE_STRING_H
36 # include <string.h>
37 # else /* not HAVE_STRING_H */
38 # include <strings.h>
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
41
42 #include "common.h"
43 #include "frontend.h"
44 #include "backend.h"
45 #include "parser.h"
46 #include "moves.h"
47 #include "gettext.h"
48
49 #ifdef ENABLE_NLS
50 # define _(s) gettext (s)
51 # define N_(s) gettext_noop (s)
52 #else
53 # define _(s) (s)
54 # define N_(s) s
55 #endif
56
57
58 /* Variables
59 */
60 List gameList;
61 extern Board initialPosition;
62 extern int quickFlag;
63 extern int movePtr;
64
65 /* Local function prototypes
66 */
67 static void GameListDeleteGame P((ListGame *));
68 static ListGame *GameListCreate P((void));
69 static void GameListFree P((List *));
70 static int GameListNewGame P((ListGame **));
71
72 /* [AS] Wildcard pattern matching */
73 Boolean
HasPattern(const char * text,const char * pattern)74 HasPattern (const char * text, const char * pattern)
75 {
76 while( *pattern != '\0' ) {
77 if( *pattern == '*' ) {
78 while( *pattern == '*' ) {
79 pattern++;
80 }
81
82 if( *pattern == '\0' ) {
83 return TRUE;
84 }
85
86 while( *text != '\0' ) {
87 if( HasPattern( text, pattern ) ) {
88 return TRUE;
89 }
90 text++;
91 }
92 }
93 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
94 pattern++;
95 text++;
96 continue;
97 }
98
99 return FALSE;
100 }
101
102 return TRUE;
103 }
104
105 Boolean
SearchPattern(const char * text,const char * pattern)106 SearchPattern (const char * text, const char * pattern)
107 {
108 Boolean result = TRUE;
109
110 if( pattern != NULL && *pattern != '\0' ) {
111 if( *pattern == '*' ) {
112 result = HasPattern( text, pattern );
113 }
114 else {
115 result = FALSE;
116
117 while( *text != '\0' ) {
118 if( HasPattern( text, pattern ) ) {
119 result = TRUE;
120 break;
121 }
122 text++;
123 }
124 }
125 }
126
127 return result;
128 }
129
130 /* Delete a ListGame; implies removint it from a list.
131 */
132 static void
GameListDeleteGame(ListGame * listGame)133 GameListDeleteGame (ListGame *listGame)
134 {
135 if (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);
148 }
149 }
150
151
152 /* Free the previous list of games.
153 */
154 static void
GameListFree(List * gameList)155 GameListFree (List *gameList)
156 {
157 while (!ListEmpty(gameList))
158 {
159 GameListDeleteGame((ListGame *) gameList->head);
160 }
161 }
162
163
164
165 /* Initialize a new GameInfo structure.
166 */
167 void
GameListInitGameInfo(GameInfo * gameInfo)168 GameListInitGameInfo (GameInfo *gameInfo)
169 {
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;
187 }
188
189
190 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
191 *
192 * Note, that the ListGame is *not* added to any list
193 */
194 static ListGame *
GameListCreate()195 GameListCreate ()
196 {
197 ListGame *listGame;
198
199 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
200 GameListInitGameInfo(&listGame->gameInfo);
201 }
202 return(listGame);
203 }
204
205
206 /* Creates a new game for the gamelist.
207 */
208 static int
GameListNewGame(ListGame ** listGamePtr)209 GameListNewGame (ListGame **listGamePtr)
210 {
211 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
212 GameListFree(&gameList);
213 return(ENOMEM);
214 }
215 ListAddTail(&gameList, (ListNode *) *listGamePtr);
216 return(0);
217 }
218
219
220 /* Build the list of games in the open file f.
221 * Returns 0 for success or error number.
222 */
223 int
GameListBuild(FILE * f)224 GameListBuild (FILE *f)
225 {
226 ChessMove cm, lastStart;
227 int gameNumber;
228 ListGame *currentListGame = NULL;
229 int error, scratch=100, plyNr=0, fromX, fromY, toX, toY;
230 int offset;
231 char lastComment[MSG_SIZ], buf[MSG_SIZ];
232 TimeMark t, t2;
233
234 GetTimeMark(&t);
235 GameListFree(&gameList);
236 yynewfile(f);
237 gameNumber = 0;
238 movePtr = 0;
239
240 lastStart = (ChessMove) 0;
241 yyskipmoves = FALSE;
242 do {
243 yyboardindex = scratch;
244 offset = yyoffset();
245 quickFlag = plyNr + 1;
246 cm = (ChessMove) Myylex();
247 switch (cm) {
248 case GNUChessGame:
249 if ((error = GameListNewGame(¤tListGame))) {
250 rewind(f);
251 yyskipmoves = FALSE;
252 return(error);
253 }
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);
259 }
260 currentListGame->gameInfo.event = StrSave(yy_text);
261 lastStart = cm;
262 break;
263 case XBoardGame:
264 lastStart = cm;
265 break;
266 case MoveNumberOne:
267 switch (lastStart) {
268 case GNUChessGame:
269 break; /* ignore */
270 case PGNTag:
271 lastStart = cm;
272 break; /* Already started */
273 case (ChessMove) 0:
274 case MoveNumberOne:
275 case XBoardGame:
276 if ((error = GameListNewGame(¤tListGame))) {
277 rewind(f);
278 yyskipmoves = FALSE;
279 return(error);
280 }
281 currentListGame->number = ++gameNumber;
282 currentListGame->offset = offset;
283 if(1) { CopyBoard(boards[scratch], initialPosition); plyNr = 0; currentListGame->moves = PackGame(boards[scratch]); }
284 lastStart = cm;
285 break;
286 default:
287 break; /* impossible */
288 }
289 break;
290 case PGNTag:
291 lastStart = cm;
292 if ((error = GameListNewGame(¤tListGame))) {
293 rewind(f);
294 yyskipmoves = FALSE;
295 return(error);
296 }
297 currentListGame->number = ++gameNumber;
298 currentListGame->offset = offset;
299 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
300 do {
301 yyboardindex = 1;
302 offset = yyoffset();
303 cm = (ChessMove) Myylex();
304 if (cm == PGNTag) {
305 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
306 }
307 } while (cm == PGNTag || cm == Comment);
308 if(1) {
309 int btm=0;
310 if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen, FALSE);
311 else CopyBoard(boards[scratch], initialPosition);
312 plyNr = (btm != 0);
313 currentListGame->moves = PackGame(boards[scratch]);
314 }
315 if(cm != NormalMove) break;
316 case IllegalMove:
317 if(appData.testLegality) break;
318 case NormalMove:
319 /* Allow the first game to start with an unnumbered move */
320 yyskipmoves = FALSE;
321 if (lastStart == (ChessMove) 0) {
322 if ((error = GameListNewGame(¤tListGame))) {
323 rewind(f);
324 yyskipmoves = FALSE;
325 return(error);
326 }
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;
331 }
332 case WhiteCapturesEnPassant:
333 case BlackCapturesEnPassant:
334 case WhitePromotion:
335 case BlackPromotion:
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;
354 plyNr++;
355 ApplyMove(fromX, fromY, toX, toY, currentMoveString[4], boards[scratch]);
356 if(currentListGame && currentListGame->moves) PackMove(fromX, fromY, toX, toY, boards[scratch][toY][toX]);
357 break;
358 case WhiteWins: // [HGM] rescom: save last comment as result details
359 case BlackWins:
360 case GameIsDrawn:
361 case GameUnfinished:
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);
367 }
368 if(yy_text[0] == '{') {
369 char *p;
370 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
371 if((p = strchr(lastComment, '}'))) *p = 0;
372 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
373 }
374 break;
375 default:
376 break;
377 }
378 if(gameNumber % 1000 == 0) {
379 snprintf(buf, MSG_SIZ, _("Reading game file (%d)"), gameNumber);
380 DisplayTitle(buf); DoEvents();
381 }
382 }
383 while (cm != (ChessMove) 0);
384
385 if(currentListGame) {
386 if(!currentListGame->moves) DisplayError("Game cache overflowed\nPosition-searching might not work properly", 0);
387
388 if (appData.debugMode) {
389 for (currentListGame = (ListGame *) gameList.head;
390 currentListGame->node.succ;
391 currentListGame = (ListGame *) currentListGame->node.succ) {
392
393 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
394 currentListGame->number, currentListGame->offset);
395 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
396 }
397 }
398 }
399 if(appData.debugMode) { GetTimeMark(&t2);printf("GameListBuild %ld msec\n", SubtractTimeMarks(&t2,&t)); }
400 quickFlag = 0;
401 PackGame(boards[scratch]); // for appending end-of-game marker.
402 DisplayTitle("WinBoard");
403 rewind(f);
404 yyskipmoves = FALSE;
405 return 0;
406 }
407
408
409 /* Clear an existing GameInfo structure.
410 */
411 void
ClearGameInfo(GameInfo * gameInfo)412 ClearGameInfo (GameInfo *gameInfo)
413 {
414 if (gameInfo->event != NULL) {
415 free(gameInfo->event);
416 }
417 if (gameInfo->site != NULL) {
418 free(gameInfo->site);
419 }
420 if (gameInfo->date != NULL) {
421 free(gameInfo->date);
422 }
423 if (gameInfo->round != NULL) {
424 free(gameInfo->round);
425 }
426 if (gameInfo->white != NULL) {
427 free(gameInfo->white);
428 }
429 if (gameInfo->black != NULL) {
430 free(gameInfo->black);
431 }
432 if (gameInfo->resultDetails != NULL) {
433 free(gameInfo->resultDetails);
434 }
435 if (gameInfo->fen != NULL) {
436 free(gameInfo->fen);
437 }
438 if (gameInfo->timeControl != NULL) {
439 free(gameInfo->timeControl);
440 }
441 if (gameInfo->extraTags != NULL) {
442 free(gameInfo->extraTags);
443 }
444 if (gameInfo->variantName != NULL) {
445 free(gameInfo->variantName);
446 }
447 if (gameInfo->outOfBook != NULL) {
448 free(gameInfo->outOfBook);
449 }
450 GameListInitGameInfo(gameInfo);
451 }
452
453 /* [AS] Replaced by "dynamic" tag selection below */
454 char *
GameListLineOld(int number,GameInfo * gameInfo)455 GameListLineOld (int number, GameInfo *gameInfo)
456 {
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);
467 return ret;
468 }
469
470 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
471
472 char *
GameListLine(int number,GameInfo * gameInfo)473 GameListLine (int number, GameInfo * gameInfo)
474 {
475 char buffer[2*MSG_SIZ];
476 char * buf = buffer;
477 char * glt = appData.gameListTags;
478
479 buf += sprintf( buffer, "%d.", number );
480
481 while( *glt != '\0' ) {
482 *buf++ = ' ';
483
484 switch( *glt ) {
485 case GLT_EVENT:
486 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
487 break;
488 case GLT_SITE:
489 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
490 break;
491 case GLT_DATE:
492 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
493 break;
494 case GLT_ROUND:
495 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
496 break;
497 case GLT_PLAYERS:
498 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
499 buf[ MAX_FIELD_LEN-1 ] = '\0';
500 buf += strlen( buf );
501 *buf++ = '-';
502 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
503 break;
504 case GLT_RESULT:
505 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
506 break;
507 case GLT_WHITE_ELO:
508 if( gameInfo->whiteRating > 0 )
509 sprintf( buf, "%d", gameInfo->whiteRating );
510 else
511 safeStrCpy( buf, "?" , 2*MSG_SIZ);
512 break;
513 case GLT_BLACK_ELO:
514 if( gameInfo->blackRating > 0 )
515 sprintf( buf, "%d", gameInfo->blackRating );
516 else
517 safeStrCpy( buf, "?" , 2*MSG_SIZ);
518 break;
519 case GLT_TIME_CONTROL:
520 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
521 break;
522 case GLT_VARIANT:
523 strncpy( buf, gameInfo->variantName ? gameInfo->variantName : VariantName(gameInfo->variant), MAX_FIELD_LEN );
524 // strncpy( buf, VariantName(gameInfo->variant), MAX_FIELD_LEN );
525 break;
526 case GLT_OUT_OF_BOOK:
527 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
528 break;
529 case GLT_RESULT_COMMENT:
530 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
531 break;
532 default:
533 break;
534 }
535
536 buf[MAX_FIELD_LEN-1] = '\0';
537
538 buf += strlen( buf );
539
540 glt++;
541
542 if( *glt != '\0' ) {
543 *buf++ = ',';
544 }
545 }
546
547 *buf = '\0';
548
549 return strdup( buffer );
550 }
551
552 char *
GameListLineFull(int number,GameInfo * gameInfo)553 GameListLineFull (int number, GameInfo * gameInfo)
554 {
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 : "";
563
564 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
565
566 char *ret = (char *) malloc(len);
567
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 );
570
571 return ret;
572 }
573 // --------------------------------------- Game-List options dialog --------------------------------------
574
575 // back-end
576 typedef struct {
577 char id;
578 char * name;
579 } GLT_Item;
580
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
595 { 0, 0 }
596 };
597
598 char lpUserGLT[LPUSERGLT_SIZE];
599
600 // back-end: convert the tag id-char to a full tag name
601 char *
GLT_FindItem(char id)602 GLT_FindItem (char id)
603 {
604 char * result = 0;
605
606 GLT_Item * list = GLT_ItemInfo;
607
608 while( list->id != 0 ) {
609 if( list->id == id ) {
610 result = list->name;
611 break;
612 }
613
614 list++;
615 }
616
617 return result;
618 }
619
620 // back-end: build the list of tag names
621 void
GLT_TagsToList(char * tags)622 GLT_TagsToList (char *tags)
623 {
624 char * pc = tags;
625
626 GLT_ClearList();
627
628 while( *pc ) {
629 GLT_AddToList( GLT_FindItem(*pc) );
630 pc++;
631 }
632
633 GLT_AddToList( " --- Hidden tags --- " );
634
635 pc = GLT_ALL_TAGS;
636
637 while( *pc ) {
638 if( strchr( tags, *pc ) == 0 ) {
639 GLT_AddToList( GLT_FindItem(*pc) );
640 }
641 pc++;
642 }
643
644 GLT_DeSelectList();
645 }
646
647 // back-end: retrieve item from dialog and translate to id-char
648 char
GLT_ListItemToTag(int index)649 GLT_ListItemToTag (int index)
650 {
651 char result = '\0';
652 char name[MSG_SIZ];
653
654 GLT_Item * list = GLT_ItemInfo;
655
656 if( GLT_GetFromList(index, name) ) {
657 while( list->id != 0 ) {
658 if( strcmp( list->name, name ) == 0 ) {
659 result = list->id;
660 break;
661 }
662
663 list++;
664 }
665 }
666
667 return result;
668 }
669
670 // back-end: add items id-chars one-by-one to temp tags string
671 void
GLT_ParseList()672 GLT_ParseList ()
673 {
674 char * pc = lpUserGLT;
675 int idx = 0;
676 char id;
677
678 do {
679 id = GLT_ListItemToTag( idx );
680 *pc++ = id;
681 idx++;
682 } while( id != '\0' );
683 }
684