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 */
52 /* Local function prototypes
54 static void GameListDeleteGame P((ListGame *));
55 static ListGame *GameListCreate P((void));
56 static void GameListFree P((List *));
57 static int GameListNewGame P((ListGame **));
59 /* [AS] Wildcard pattern matching */
61 HasPattern( const char * text, const char * pattern )
63 while( *pattern != '\0' ) {
64 if( *pattern == '*' ) {
65 while( *pattern == '*' ) {
69 if( *pattern == '\0' ) {
73 while( *text != '\0' ) {
74 if( HasPattern( text, pattern ) ) {
80 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
93 SearchPattern( const char * text, const char * pattern )
95 Boolean result = TRUE;
97 if( pattern != NULL && *pattern != '\0' ) {
98 if( *pattern == '*' ) {
99 result = HasPattern( text, pattern );
104 while( *text != '\0' ) {
105 if( HasPattern( text, pattern ) ) {
117 /* Delete a ListGame; implies removint it from a list.
119 static void GameListDeleteGame(listGame)
123 if (listGame->gameInfo.event) free(listGame->gameInfo.event);
124 if (listGame->gameInfo.site) free(listGame->gameInfo.site);
125 if (listGame->gameInfo.date) free(listGame->gameInfo.date);
126 if (listGame->gameInfo.round) free(listGame->gameInfo.round);
127 if (listGame->gameInfo.white) free(listGame->gameInfo.white);
128 if (listGame->gameInfo.black) free(listGame->gameInfo.black);
129 if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
130 if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
131 if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
132 if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
133 if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
134 ListNodeFree((ListNode *) listGame);
139 /* Free the previous list of games.
141 static void GameListFree(gameList)
144 while (!ListEmpty(gameList))
146 GameListDeleteGame((ListGame *) gameList->head);
152 /* Initialize a new GameInfo structure.
154 void GameListInitGameInfo(gameInfo)
157 gameInfo->event = NULL;
158 gameInfo->site = NULL;
159 gameInfo->date = NULL;
160 gameInfo->round = NULL;
161 gameInfo->white = NULL;
162 gameInfo->black = NULL;
163 gameInfo->result = GameUnfinished;
164 gameInfo->fen = NULL;
165 gameInfo->resultDetails = NULL;
166 gameInfo->timeControl = NULL;
167 gameInfo->extraTags = NULL;
168 gameInfo->whiteRating = -1; /* unknown */
169 gameInfo->blackRating = -1; /* unknown */
170 gameInfo->variant = VariantNormal;
171 gameInfo->outOfBook = NULL;
172 gameInfo->resultDetails = NULL;
176 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
178 * Note, that the ListGame is *not* added to any list
180 static ListGame *GameListCreate()
185 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
186 GameListInitGameInfo(&listGame->gameInfo);
192 /* Creates a new game for the gamelist.
194 static int GameListNewGame(listGamePtr)
195 ListGame **listGamePtr;
197 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
198 GameListFree(&gameList);
201 ListAddTail(&gameList, (ListNode *) *listGamePtr);
206 /* Build the list of games in the open file f.
207 * Returns 0 for success or error number.
212 ChessMove cm, lastStart;
214 ListGame *currentListGame = NULL;
217 char lastComment[MSG_SIZ];
219 GameListFree(&gameList);
223 lastStart = (ChessMove) 0;
228 cm = (ChessMove) Myylex();
231 if ((error = GameListNewGame(¤tListGame))) {
236 currentListGame->number = ++gameNumber;
237 currentListGame->offset = offset;
238 if (currentListGame->gameInfo.event != NULL) {
239 free(currentListGame->gameInfo.event);
241 currentListGame->gameInfo.event = StrSave(yy_text);
253 break; /* Already started */
257 if ((error = GameListNewGame(¤tListGame))) {
262 currentListGame->number = ++gameNumber;
263 currentListGame->offset = offset;
267 break; /* impossible */
272 if ((error = GameListNewGame(¤tListGame))) {
277 currentListGame->number = ++gameNumber;
278 currentListGame->offset = offset;
279 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
283 cm = (ChessMove) Myylex();
285 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
287 } while (cm == PGNTag || cm == Comment);
290 /* Allow the first game to start with an unnumbered move */
292 if (lastStart == (ChessMove) 0) {
293 if ((error = GameListNewGame(¤tListGame))) {
298 currentListGame->number = ++gameNumber;
299 currentListGame->offset = offset;
300 lastStart = MoveNumberOne;
303 case WhiteWins: // [HGM] rescom: save last comment as result details
307 if (currentListGame->gameInfo.resultDetails != NULL) {
308 free(currentListGame->gameInfo.resultDetails);
310 if(yy_text[0] == '{') { char *p;
311 safeStrCpy(lastComment, yy_text+1, sizeof(lastComment)/sizeof(lastComment[0]));
312 if(p = strchr(lastComment, '}')) *p = 0;
313 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
320 while (cm != (ChessMove) 0);
323 if (appData.debugMode) {
324 for (currentListGame = (ListGame *) gameList.head;
325 currentListGame->node.succ;
326 currentListGame = (ListGame *) currentListGame->node.succ) {
328 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
329 currentListGame->number, currentListGame->offset);
330 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
340 /* Clear an existing GameInfo structure.
342 void ClearGameInfo(gameInfo)
345 if (gameInfo->event != NULL) {
346 free(gameInfo->event);
348 if (gameInfo->site != NULL) {
349 free(gameInfo->site);
351 if (gameInfo->date != NULL) {
352 free(gameInfo->date);
354 if (gameInfo->round != NULL) {
355 free(gameInfo->round);
357 if (gameInfo->white != NULL) {
358 free(gameInfo->white);
360 if (gameInfo->black != NULL) {
361 free(gameInfo->black);
363 if (gameInfo->resultDetails != NULL) {
364 free(gameInfo->resultDetails);
366 if (gameInfo->fen != NULL) {
369 if (gameInfo->timeControl != NULL) {
370 free(gameInfo->timeControl);
372 if (gameInfo->extraTags != NULL) {
373 free(gameInfo->extraTags);
375 if (gameInfo->outOfBook != NULL) {
376 free(gameInfo->outOfBook);
378 GameListInitGameInfo(gameInfo);
381 /* [AS] Replaced by "dynamic" tag selection below */
383 GameListLineOld(number, gameInfo)
387 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
388 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
389 char *white = gameInfo->white ? gameInfo->white : "?";
390 char *black = gameInfo->black ? gameInfo->black : "?";
391 char *date = gameInfo->date ? gameInfo->date : "?";
392 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
393 strlen(black) + 11 + strlen(date) + 1;
394 char *ret = (char *) malloc(len);
395 sprintf(ret, "%d. %s, %s-%s, %s, %s",
396 number, event, white, black, PGNResult(gameInfo->result), date);
400 #define MAX_FIELD_LEN 80 /* To avoid overflowing the buffer */
402 char * GameListLine( int number, GameInfo * gameInfo )
404 char buffer[2*MSG_SIZ];
406 char * glt = appData.gameListTags;
408 buf += sprintf( buffer, "%d.", number );
410 while( *glt != '\0' ) {
415 strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
418 strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
421 strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
424 strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
427 strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
428 buf[ MAX_FIELD_LEN-1 ] = '\0';
429 buf += strlen( buf );
431 strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
434 safeStrCpy( buf, PGNResult(gameInfo->result), 2*MSG_SIZ );
437 if( gameInfo->whiteRating > 0 )
438 sprintf( buf, "%d", gameInfo->whiteRating );
440 safeStrCpy( buf, "?" , 2*MSG_SIZ);
443 if( gameInfo->blackRating > 0 )
444 sprintf( buf, "%d", gameInfo->blackRating );
446 safeStrCpy( buf, "?" , 2*MSG_SIZ);
448 case GLT_TIME_CONTROL:
449 strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
453 case GLT_OUT_OF_BOOK:
454 strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
456 case GLT_RESULT_COMMENT:
457 strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
463 buf[MAX_FIELD_LEN-1] = '\0';
465 buf += strlen( buf );
476 return strdup( buffer );
479 char * GameListLineFull( int number, GameInfo * gameInfo )
481 char * event = gameInfo->event ? gameInfo->event : "?";
482 char * site = gameInfo->site ? gameInfo->site : "?";
483 char * white = gameInfo->white ? gameInfo->white : "?";
484 char * black = gameInfo->black ? gameInfo->black : "?";
485 char * round = gameInfo->round ? gameInfo->round : "?";
486 char * date = gameInfo->date ? gameInfo->date : "?";
487 char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
488 char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
490 int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
492 char *ret = (char *) malloc(len);
494 sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
495 number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
499 // --------------------------------------- Game-List options dialog --------------------------------------
507 // back-end: translation table tag id-char <-> full tag name
508 static GLT_Item GLT_ItemInfo[] = {
509 { GLT_EVENT, "Event" },
510 { GLT_SITE, "Site" },
511 { GLT_DATE, "Date" },
512 { GLT_ROUND, "Round" },
513 { GLT_PLAYERS, "Players" },
514 { GLT_RESULT, "Result" },
515 { GLT_WHITE_ELO, "White Rating" },
516 { GLT_BLACK_ELO, "Black Rating" },
517 { GLT_TIME_CONTROL,"Time Control" },
518 { GLT_VARIANT, "Variant" },
519 { GLT_OUT_OF_BOOK,PGN_OUT_OF_BOOK },
520 { GLT_RESULT_COMMENT, "Result Comment" }, // [HGM] rescom
524 char lpUserGLT[LPUSERGLT_SIZE];
526 // back-end: convert the tag id-char to a full tag name
527 char * GLT_FindItem( char id )
531 GLT_Item * list = GLT_ItemInfo;
533 while( list->id != 0 ) {
534 if( list->id == id ) {
545 // back-end: build the list of tag names
547 GLT_TagsToList( char * tags )
554 GLT_AddToList( GLT_FindItem(*pc) );
558 GLT_AddToList( " --- Hidden tags --- " );
563 if( strchr( tags, *pc ) == 0 ) {
564 GLT_AddToList( GLT_FindItem(*pc) );
572 // back-end: retrieve item from dialog and translate to id-char
574 GLT_ListItemToTag( int index )
579 GLT_Item * list = GLT_ItemInfo;
581 if( GLT_GetFromList(index, name) ) {
582 while( list->id != 0 ) {
583 if( strcmp( list->name, name ) == 0 ) {
595 // back-end: add items id-chars one-by-one to temp tags string
599 char * pc = lpUserGLT;
604 id = GLT_ListItemToTag( idx );
607 } while( id != '\0' );