Adjusted alternative joining method to obey keepLineBreaksICS
[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     gameInfo->resultDetails = NULL;
115 }
116
117
118 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
119  *
120  * Note, that the ListGame is *not* added to any list
121  */
122 static ListGame *GameListCreate()
123
124 {
125     ListGame *listGame;
126
127     if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
128         GameListInitGameInfo(&listGame->gameInfo);
129     }
130     return(listGame);
131 }
132
133
134 /* Creates a new game for the gamelist.
135  */
136 static int GameListNewGame(listGamePtr)
137      ListGame **listGamePtr;
138 {
139     if (!(*listGamePtr = (ListGame *) GameListCreate())) {
140         GameListFree(&gameList);
141         return(ENOMEM);
142     }
143     ListAddTail(&gameList, (ListNode *) *listGamePtr);
144     return(0);
145 }
146
147
148 /* Build the list of games in the open file f.
149  * Returns 0 for success or error number.
150  */
151 int GameListBuild(f)
152     FILE *f;
153 {
154     ChessMove cm, lastStart;
155     int gameNumber;
156     ListGame *currentListGame = NULL;
157     int error;
158     int offset;
159     char lastComment[MSG_SIZ];
160
161     GameListFree(&gameList);
162     yynewfile(f);
163     gameNumber = 0;
164
165     lastStart = (ChessMove) 0;
166     yyskipmoves = FALSE;
167     do {
168         yyboardindex = 0;
169         offset = yyoffset();
170         cm = (ChessMove) yylex();
171         switch (cm) {
172           case GNUChessGame:
173             if ((error = GameListNewGame(&currentListGame))) {
174                 rewind(f);
175                 yyskipmoves = FALSE;
176                 return(error);
177             }
178             currentListGame->number = ++gameNumber;
179             currentListGame->offset = offset;
180             if (currentListGame->gameInfo.event != NULL) {
181                 free(currentListGame->gameInfo.event);
182             }
183             currentListGame->gameInfo.event = StrSave(yy_text);
184             lastStart = cm;
185             break;
186           case XBoardGame:
187             lastStart = cm;
188             break;
189           case MoveNumberOne:
190             switch (lastStart) {
191               case GNUChessGame:
192                 break;          /*  ignore  */
193               case PGNTag:
194                 lastStart = cm;
195                 break;          /*  Already started */
196               case (ChessMove) 0:
197               case MoveNumberOne:
198               case XBoardGame:
199                 if ((error = GameListNewGame(&currentListGame))) {
200                     rewind(f);
201                     yyskipmoves = FALSE;
202                     return(error);
203                 }
204                 currentListGame->number = ++gameNumber;
205                 currentListGame->offset = offset;
206                 lastStart = cm;
207                 break;
208               default:
209                 break;          /*  impossible  */
210             }
211             break;
212           case PGNTag:
213             lastStart = cm;
214             if ((error = GameListNewGame(&currentListGame))) {
215                 rewind(f);
216                 yyskipmoves = FALSE;
217                 return(error);
218             }
219             currentListGame->number = ++gameNumber;
220             currentListGame->offset = offset;
221             ParsePGNTag(yy_text, &currentListGame->gameInfo);
222             do {
223                 yyboardindex = 1;
224                 offset = yyoffset();
225                 cm = (ChessMove) yylex();
226                 if (cm == PGNTag) {
227                     ParsePGNTag(yy_text, &currentListGame->gameInfo);
228                 }
229             } while (cm == PGNTag || cm == Comment);
230             break;
231           case NormalMove:
232             /* Allow the first game to start with an unnumbered move */
233             yyskipmoves = TRUE;
234             if (lastStart == (ChessMove) 0) {
235               if ((error = GameListNewGame(&currentListGame))) {
236                 rewind(f);
237                 yyskipmoves = FALSE;
238                 return(error);
239               }
240               currentListGame->number = ++gameNumber;
241               currentListGame->offset = offset;
242               lastStart = MoveNumberOne;
243             }
244             break;
245         case WhiteWins: // [HGM] rescom: save last comment as result details
246         case BlackWins:
247         case GameIsDrawn:
248         case GameUnfinished:
249             if (currentListGame->gameInfo.resultDetails != NULL) {
250                 free(currentListGame->gameInfo.resultDetails);
251             }
252             if(yy_text[0] == '{') { char *p;
253                 strcpy(lastComment, yy_text+1);
254                 if(p = strchr(lastComment, '}')) *p = 0;
255                 currentListGame->gameInfo.resultDetails = StrSave(lastComment);
256             }
257             break;
258           default:
259             break;
260         }
261     }
262     while (cm != (ChessMove) 0);
263
264
265     if (appData.debugMode) {
266         for (currentListGame = (ListGame *) gameList.head;
267              currentListGame->node.succ;
268              currentListGame = (ListGame *) currentListGame->node.succ) {
269
270             fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
271                     currentListGame->number, currentListGame->offset);
272             PrintPGNTags(debugFP, &currentListGame->gameInfo);
273         }
274     }
275
276     rewind(f);
277     yyskipmoves = FALSE;
278     return 0;
279 }
280
281
282 /* Clear an existing GameInfo structure.
283  */
284 void ClearGameInfo(gameInfo)
285     GameInfo *gameInfo;
286 {
287     if (gameInfo->event != NULL) {
288         free(gameInfo->event);
289     }
290     if (gameInfo->site != NULL) {
291         free(gameInfo->site);
292     }
293     if (gameInfo->date != NULL) {
294         free(gameInfo->date);
295     }
296     if (gameInfo->round != NULL) {
297         free(gameInfo->round);
298     }
299     if (gameInfo->white != NULL) {
300         free(gameInfo->white);
301     }
302     if (gameInfo->black != NULL) {
303         free(gameInfo->black);
304     }
305     if (gameInfo->resultDetails != NULL) {
306         free(gameInfo->resultDetails);
307     }
308     if (gameInfo->fen != NULL) {
309         free(gameInfo->fen);
310     }
311     if (gameInfo->timeControl != NULL) {
312         free(gameInfo->timeControl);
313     }
314     if (gameInfo->extraTags != NULL) {
315         free(gameInfo->extraTags);
316     }
317     if (gameInfo->outOfBook != NULL) {
318         free(gameInfo->outOfBook);
319     }
320     GameListInitGameInfo(gameInfo);
321 }
322
323 /* [AS] Replaced by "dynamic" tag selection below */
324 char *
325 GameListLineOld(number, gameInfo)
326      int number;
327      GameInfo *gameInfo;
328 {
329     char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
330                      gameInfo->event : gameInfo->site ? gameInfo->site : "?";
331     char *white = gameInfo->white ? gameInfo->white : "?";
332     char *black = gameInfo->black ? gameInfo->black : "?";
333     char *date = gameInfo->date ? gameInfo->date : "?";
334     int len = 10 + strlen(event) + 2 + strlen(white) + 1 + 
335       strlen(black) + 11 + strlen(date) + 1;
336     char *ret = (char *) malloc(len);
337     sprintf(ret, "%d. %s, %s-%s, %s, %s",
338             number, event, white, black, PGNResult(gameInfo->result), date);
339     return ret;
340 }
341
342 #define MAX_FIELD_LEN   80  /* To avoid overflowing the buffer */
343
344 char * GameListLine( int number, GameInfo * gameInfo )
345 {
346     char buffer[1024];
347     char * buf = buffer;
348     char * glt = appData.gameListTags;
349     
350     buf += sprintf( buffer, "%d.", number );
351
352     while( *glt != '\0' ) {
353         *buf++ = ' ';
354
355         switch( *glt ) {
356         case GLT_EVENT:
357             strncpy( buf, gameInfo->event ? gameInfo->event : "?", MAX_FIELD_LEN );
358             break;
359         case GLT_SITE:
360             strncpy( buf, gameInfo->site ? gameInfo->site : "?", MAX_FIELD_LEN );
361             break;
362         case GLT_DATE:
363             strncpy( buf, gameInfo->date ? gameInfo->date : "?", MAX_FIELD_LEN );
364             break;
365         case GLT_ROUND:
366             strncpy( buf, gameInfo->round ? gameInfo->round : "?", MAX_FIELD_LEN );
367             break;
368         case GLT_PLAYERS:
369             strncpy( buf, gameInfo->white ? gameInfo->white : "?", MAX_FIELD_LEN );
370             buf[ MAX_FIELD_LEN-1 ] = '\0';
371             buf += strlen( buf );
372             *buf++ = '-';
373             strncpy( buf, gameInfo->black ? gameInfo->black : "?", MAX_FIELD_LEN );
374             break;
375         case GLT_RESULT:
376             strcpy( buf, PGNResult(gameInfo->result) );
377             break;
378         case GLT_WHITE_ELO:
379             if( gameInfo->whiteRating > 0 )
380                 sprintf( buf, "%d", gameInfo->whiteRating );
381             else
382                 strcpy( buf, "?" );
383             break;
384         case GLT_BLACK_ELO:
385             if( gameInfo->blackRating > 0 )
386                 sprintf( buf, "%d", gameInfo->blackRating );
387             else
388                 strcpy( buf, "?" );
389             break;
390         case GLT_TIME_CONTROL:
391             strncpy( buf, gameInfo->timeControl ? gameInfo->timeControl : "?", MAX_FIELD_LEN );
392             break;
393         case GLT_VARIANT:
394             break;
395         case GLT_OUT_OF_BOOK:
396             strncpy( buf, gameInfo->outOfBook ? gameInfo->outOfBook : "?", MAX_FIELD_LEN );
397             break;
398         case GLT_RESULT_COMMENT:
399             strncpy( buf, gameInfo->resultDetails ? gameInfo->resultDetails : "res?", MAX_FIELD_LEN );
400             break;
401         default:
402             break;
403         }
404
405         buf[MAX_FIELD_LEN-1] = '\0';
406
407         buf += strlen( buf );
408
409         glt++;
410
411         if( *glt != '\0' ) {
412             *buf++ = ',';
413         }
414     }
415
416     *buf = '\0';
417
418     return strdup( buffer );
419 }
420
421 char * GameListLineFull( int number, GameInfo * gameInfo )
422 {
423     char * event = gameInfo->event ? gameInfo->event : "?";
424     char * site = gameInfo->site ? gameInfo->site : "?";
425     char * white = gameInfo->white ? gameInfo->white : "?";
426     char * black = gameInfo->black ? gameInfo->black : "?";
427     char * round = gameInfo->round ? gameInfo->round : "?";
428     char * date = gameInfo->date ? gameInfo->date : "?";
429     char * oob = gameInfo->outOfBook ? gameInfo->outOfBook : "";
430     char * reason = gameInfo->resultDetails ? gameInfo->resultDetails : "";
431     
432     int len = 64 + strlen(event) + strlen(site) + strlen(white) + strlen(black) + strlen(date) + strlen(oob) + strlen(reason);
433
434     char *ret = (char *) malloc(len);
435
436     sprintf(ret, "%d, \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"",
437         number, event, site, round, white, black, PGNResult(gameInfo->result), reason, date, oob );
438
439     return ret;
440 }