updated copyright to reflect A. Scotte as copyright holder
[xboard.git] / gamelist.c
1 /*
2  * gamelist.c -- Functions to manage a gamelist
3  *
4  * Copyright 1995,2009 Free Software Foundation, Inc.
5  *
6  * Enhancements Copyright 2005 Alessandro Scotti
7  *
8  * ------------------------------------------------------------------------
9  *
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.
14  *
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.
19  *
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/.  
22  *
23  *------------------------------------------------------------------------
24  ** See the file ChangeLog for a revision history.  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <errno.h>
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 # include <string.h>
33 #else /* not STDC_HEADERS */
34 # if HAVE_STRING_H
35 #  include <string.h>
36 # else /* not HAVE_STRING_H */
37 #  include <strings.h>
38 # endif /* not HAVE_STRING_H */
39 #endif /* not STDC_HEADERS */
40
41 #include "common.h"
42 #include "frontend.h"
43 #include "backend.h"
44 #include "parser.h"
45
46
47 /* Variables
48  */
49 List gameList;
50
51
52 /* Local function prototypes
53  */
54 static void GameListDeleteGame P((ListGame *));
55 static ListGame *GameListCreate P((void));
56 static void GameListFree P((List *));
57 static int GameListNewGame P((ListGame **));
58
59 /* Delete a ListGame; implies removint it from a list.
60  */
61 static void GameListDeleteGame(listGame)
62     ListGame *listGame;
63 {
64     if (listGame) {
65         if (listGame->gameInfo.event) free(listGame->gameInfo.event);
66         if (listGame->gameInfo.site) free(listGame->gameInfo.site);
67         if (listGame->gameInfo.date) free(listGame->gameInfo.date);
68         if (listGame->gameInfo.round) free(listGame->gameInfo.round);
69         if (listGame->gameInfo.white) free(listGame->gameInfo.white);
70         if (listGame->gameInfo.black) free(listGame->gameInfo.black);
71         if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
72         if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
73         if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
74         if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
75         if (listGame->gameInfo.outOfBook) free(listGame->gameInfo.outOfBook);
76         ListNodeFree((ListNode *) listGame);
77     }
78 }
79
80
81 /* Free the previous list of games.
82  */
83 static void GameListFree(gameList)
84     List *gameList;
85 {
86     while (!ListEmpty(gameList))
87     {
88         GameListDeleteGame((ListGame *) gameList->head);
89     }
90 }
91
92
93
94 /* Initialize a new GameInfo structure.
95  */
96 void GameListInitGameInfo(gameInfo)
97     GameInfo *gameInfo;
98 {
99     gameInfo->event = NULL;
100     gameInfo->site = NULL;
101     gameInfo->date = NULL;
102     gameInfo->round = NULL;
103     gameInfo->white = NULL;
104     gameInfo->black = NULL;
105     gameInfo->result = GameUnfinished;
106     gameInfo->fen = NULL;
107     gameInfo->resultDetails = NULL;
108     gameInfo->timeControl = NULL;
109     gameInfo->extraTags = NULL;
110     gameInfo->whiteRating = -1; /* unknown */
111     gameInfo->blackRating = -1; /* unknown */
112     gameInfo->variant = VariantNormal;
113     gameInfo->outOfBook = NULL;
114 }
115
116
117 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
118  *
119  * Note, that the ListGame is *not* added to any list
120  */
121 static ListGame *GameListCreate()
122
123 {
124     ListGame *listGame;
125
126     if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
127         GameListInitGameInfo(&listGame->gameInfo);
128     }
129     return(listGame);
130 }
131
132
133 /* Creates a new game for the gamelist.
134  */
135 static int GameListNewGame(listGamePtr)
136      ListGame **listGamePtr;
137 {
138     if (!(*listGamePtr = (ListGame *) GameListCreate())) {
139         GameListFree(&gameList);
140         return(ENOMEM);
141     }
142     ListAddTail(&gameList, (ListNode *) *listGamePtr);
143     return(0);
144 }
145
146
147 /* Build the list of games in the open file f.
148  * Returns 0 for success or error number.
149  */
150 int GameListBuild(f)
151     FILE *f;
152 {
153     ChessMove cm, lastStart;
154     int gameNumber;
155     ListGame *currentListGame = NULL;
156     int error;
157     int offset;
158
159     GameListFree(&gameList);
160     yynewfile(f);
161     gameNumber = 0;
162
163     lastStart = (ChessMove) 0;
164     yyskipmoves = FALSE;
165     do {
166         yyboardindex = 0;
167         offset = yyoffset();
168         cm = (ChessMove) yylex();
169         switch (cm) {
170           case GNUChessGame:
171             if ((error = GameListNewGame(&currentListGame))) {
172                 rewind(f);
173                 yyskipmoves = FALSE;
174                 return(error);
175             }
176             currentListGame->number = ++gameNumber;
177             currentListGame->offset = offset;
178             if (currentListGame->gameInfo.event != NULL) {
179                 free(currentListGame->gameInfo.event);
180             }
181             currentListGame->gameInfo.event = StrSave(yy_text);
182             lastStart = cm;
183             break;
184           case XBoardGame:
185             lastStart = cm;
186             break;
187           case MoveNumberOne:
188             switch (lastStart) {
189               case GNUChessGame:
190                 break;          /*  ignore  */
191               case PGNTag:
192                 lastStart = cm;
193                 break;          /*  Already started */
194               case (ChessMove) 0:
195               case MoveNumberOne:
196               case XBoardGame:
197                 if ((error = GameListNewGame(&currentListGame))) {
198                     rewind(f);
199                     yyskipmoves = FALSE;
200                     return(error);
201                 }
202                 currentListGame->number = ++gameNumber;
203                 currentListGame->offset = offset;
204                 lastStart = cm;
205                 break;
206               default:
207                 break;          /*  impossible  */
208             }
209             break;
210           case PGNTag:
211             lastStart = cm;
212             if ((error = GameListNewGame(&currentListGame))) {
213                 rewind(f);
214                 yyskipmoves = FALSE;
215                 return(error);
216             }
217             currentListGame->number = ++gameNumber;
218             currentListGame->offset = offset;
219             ParsePGNTag(yy_text, &currentListGame->gameInfo);
220             do {
221                 yyboardindex = 1;
222                 offset = yyoffset();
223                 cm = (ChessMove) yylex();
224                 if (cm == PGNTag) {
225                     ParsePGNTag(yy_text, &currentListGame->gameInfo);
226                 }
227             } while (cm == PGNTag || cm == Comment);
228             break;
229           case NormalMove:
230             /* Allow the first game to start with an unnumbered move */
231             yyskipmoves = TRUE;
232             if (lastStart == (ChessMove) 0) {
233               if ((error = GameListNewGame(&currentListGame))) {
234                 rewind(f);
235                 yyskipmoves = FALSE;
236                 return(error);
237               }
238               currentListGame->number = ++gameNumber;
239               currentListGame->offset = offset;
240               lastStart = MoveNumberOne;
241             }
242             break;
243           default:
244             break;
245         }
246     }
247     while (cm != (ChessMove) 0);
248
249
250     if (appData.debugMode) {
251         for (currentListGame = (ListGame *) gameList.head;
252              currentListGame->node.succ;
253              currentListGame = (ListGame *) currentListGame->node.succ) {
254
255             fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
256                     currentListGame->number, currentListGame->offset);
257             PrintPGNTags(debugFP, &currentListGame->gameInfo);
258         }
259     }
260
261     rewind(f);
262     yyskipmoves = FALSE;
263     return 0;
264 }
265
266
267 /* Clear an existing GameInfo structure.
268  */
269 void ClearGameInfo(gameInfo)
270     GameInfo *gameInfo;
271 {
272     if (gameInfo->event != NULL) {
273         free(gameInfo->event);
274     }
275     if (gameInfo->site != NULL) {
276         free(gameInfo->site);
277     }
278     if (gameInfo->date != NULL) {
279         free(gameInfo->date);
280     }
281     if (gameInfo->round != NULL) {
282         free(gameInfo->round);
283     }
284     if (gameInfo->white != NULL) {
285         free(gameInfo->white);
286     }
287     if (gameInfo->black != NULL) {
288         free(gameInfo->black);
289     }
290     if (gameInfo->resultDetails != NULL) {
291         free(gameInfo->resultDetails);
292     }
293     if (gameInfo->fen != NULL) {
294         free(gameInfo->fen);
295     }
296     if (gameInfo->timeControl != NULL) {
297         free(gameInfo->timeControl);
298     }
299     if (gameInfo->extraTags != NULL) {
300         free(gameInfo->extraTags);
301     }
302     if (gameInfo->outOfBook != NULL) {
303         free(gameInfo->outOfBook);
304     }
305
306     GameListInitGameInfo(gameInfo);
307 }
308
309 /* [AS] Replaced by "dynamic" tag selection below */
310 char *
311 GameListLineOld(number, gameInfo)
312      int number;
313      GameInfo *gameInfo;
314 {
315     char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
316                      gameInfo->event : gameInfo->site ? gameInfo->site : "?";
317     char *white = gameInfo->white ? gameInfo->white : "?";
318     char *black = gameInfo->black ? gameInfo->black : "?";
319     char *date = gameInfo->date ? gameInfo->date : "?";
320     int len = 10 + strlen(event) + 2 + strlen(white) + 1 + 
321       strlen(black) + 11 + strlen(date) + 1;
322     char *ret = (char *) malloc(len);
323     sprintf(ret, "%d. %s, %s-%s, %s, %s",
324             number, event, white, black, PGNResult(gameInfo->result), date);
325     return ret;
326 }
327
328 #define MAX_FIELD_LEN   64  /* To avoid overflowing the buffer */
329
330 char * GameListLine( int number, GameInfo * gameInfo )
331 {
332     char buffer[1024];
333     char * buf = buffer;
334     char * glt = appData.gameListTags;
335     
336     buf += sprintf( buffer, "%d.", number );
337
338     while( *glt != '\0' ) {
339         *buf++ = ' ';
340
341         switch( *glt ) {
342         case GLT_EVENT:
343             strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
344             break;
345         case GLT_SITE:
346             strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
347             break;
348         case GLT_DATE:
349             strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
350             break;
351         case GLT_ROUND:
352             strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
353             break;
354         case GLT_PLAYERS:
355             strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
356             buf[ MAX_FIELD_LEN-1 ] = '\0';
357             buf += strlen( buf );
358             *buf++ = '-';
359             strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
360             break;
361         case GLT_RESULT:
362             strcpy( buf, PGNResult(gameInfo->result) );
363             break;
364         case GLT_WHITE_ELO:
365             if( gameInfo->whiteRating > 0 )
366                 sprintf( buf, "%d", gameInfo->whiteRating );
367             else
368                 strcpy( buf, "?" );
369             break;
370         case GLT_BLACK_ELO:
371             if( gameInfo->blackRating > 0 )
372                 sprintf( buf, "%d", gameInfo->blackRating );
373             else
374                 strcpy( buf, "?" );
375             break;
376         case GLT_TIME_CONTROL:
377             strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
378             break;
379         case GLT_VARIANT:
380             break;
381         case GLT_OUT_OF_BOOK:
382             strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
383             break;
384         default:
385             break;
386         }
387
388         buf[MAX_FIELD_LEN-1] = '\0';
389
390         buf += strlen( buf );
391
392         glt++;
393
394         if( *glt != '\0' ) {
395             *buf++ = ',';
396         }
397     }
398
399     *buf = '\0';
400
401     return strdup( buffer );
402 }
403
404 char * GameListLineFull( int number, GameInfo * gameInfo )
405 {
406     char * event = gameInfo->event ? gameInfo->event : "?";
407     char * site = gameInfo->site ? gameInfo->site : "?";
408     char * white = gameInfo->white ? gameInfo->white : "?";
409     char * black = gameInfo->black ? gameInfo->black : "?";
410     char * round = gameInfo->round ? gameInfo->round : "?";
411     char * date = gameInfo->date ? gameInfo->date : "?";
412     char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
413     
414     int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob);
415
416     char *ret = (char *) malloc(len);
417
418     sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"", number, event, site, round, white, black, PGNResult(gameInfo->result), date, oob );
419
420     return ret;
421 }