Initial checkin. I created this by combining the XBoard 4.2.6 and
[xboard.git] / gamelist.c
1 /*
2  * gamelist.c -- Functions to manage a gamelist
3  * XBoard $Id$
4  *
5  * Copyright 1995 Free Software Foundation, Inc.
6  *
7  * ------------------------------------------------------------------------
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21  * ------------------------------------------------------------------------
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <errno.h>
28 #if STDC_HEADERS
29 # include <stdlib.h>
30 # include <string.h>
31 #else /* not STDC_HEADERS */
32 # if HAVE_STRING_H
33 #  include <string.h>
34 # else /* not HAVE_STRING_H */
35 #  include <strings.h>
36 # endif /* not HAVE_STRING_H */
37 #endif /* not STDC_HEADERS */
38
39 #include "common.h"
40 #include "frontend.h"
41 #include "backend.h"
42 #include "parser.h"
43
44
45 /* Variables
46  */
47 List gameList;
48
49
50 /* Local function prototypes
51  */
52 static void GameListDeleteGame P((ListGame *));
53 static ListGame *GameListCreate P((void));
54 static void GameListFree P((List *));
55 static int GameListNewGame P((ListGame **));
56
57 /* Delete a ListGame; implies removint it from a list.
58  */
59 static void GameListDeleteGame(listGame)
60     ListGame *listGame;
61 {
62     if (listGame) {
63         if (listGame->gameInfo.event) free(listGame->gameInfo.event);
64         if (listGame->gameInfo.site) free(listGame->gameInfo.site);
65         if (listGame->gameInfo.date) free(listGame->gameInfo.date);
66         if (listGame->gameInfo.round) free(listGame->gameInfo.round);
67         if (listGame->gameInfo.white) free(listGame->gameInfo.white);
68         if (listGame->gameInfo.black) free(listGame->gameInfo.black);
69         if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
70         if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
71         if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
72         if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
73         ListNodeFree((ListNode *) listGame);
74     }
75 }
76
77
78 /* Free the previous list of games.
79  */
80 static void GameListFree(gameList)
81     List *gameList;
82 {
83     while (!ListEmpty(gameList))
84     {
85         GameListDeleteGame((ListGame *) gameList->head);
86     }
87 }
88
89
90
91 /* Initialize a new GameInfo structure.
92  */
93 void GameListInitGameInfo(gameInfo)
94     GameInfo *gameInfo;
95 {
96     gameInfo->event = NULL;
97     gameInfo->site = NULL;
98     gameInfo->date = NULL;
99     gameInfo->round = NULL;
100     gameInfo->white = NULL;
101     gameInfo->black = NULL;
102     gameInfo->result = GameUnfinished;
103     gameInfo->fen = NULL;
104     gameInfo->resultDetails = NULL;
105     gameInfo->timeControl = NULL;
106     gameInfo->extraTags = NULL;
107     gameInfo->whiteRating = -1; /* unknown */
108     gameInfo->blackRating = -1; /* unknown */
109     gameInfo->variant = VariantNormal;
110 }
111
112
113 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
114  *
115  * Note, that the ListGame is *not* added to any list
116  */
117 static ListGame *GameListCreate()
118
119 {
120     ListGame *listGame;
121
122     if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
123         GameListInitGameInfo(&listGame->gameInfo);
124     }
125     return(listGame);
126 }
127
128
129 /* Creates a new game for the gamelist.
130  */
131 static int GameListNewGame(listGamePtr)
132      ListGame **listGamePtr;
133 {
134     if (!(*listGamePtr = (ListGame *) GameListCreate())) {
135         GameListFree(&gameList);
136         return(ENOMEM);
137     }
138     ListAddTail(&gameList, (ListNode *) *listGamePtr);
139     return(0);
140 }
141
142
143 /* Build the list of games in the open file f.
144  * Returns 0 for success or error number.
145  */
146 int GameListBuild(f)
147     FILE *f;
148 {
149     ChessMove cm, lastStart;
150     int gameNumber;
151     ListGame *currentListGame = NULL;
152     int error;
153     int offset;
154
155     GameListFree(&gameList);
156     yynewfile(f);
157     gameNumber = 0;
158
159     lastStart = (ChessMove) 0;
160     yyskipmoves = TRUE;
161     do {
162         yyboardindex = 1;
163         offset = yyoffset();
164         cm = (ChessMove) yylex();
165         switch (cm) {
166           case GNUChessGame:
167             if ((error = GameListNewGame(&currentListGame))) {
168                 rewind(f);
169                 yyskipmoves = FALSE;
170                 return(error);
171             }
172             currentListGame->number = ++gameNumber;
173             currentListGame->offset = offset;
174             if (currentListGame->gameInfo.event != NULL) {
175                 free(currentListGame->gameInfo.event);
176             }
177             currentListGame->gameInfo.event = StrSave(yy_text);
178             lastStart = cm;
179             break;
180           case XBoardGame:
181             lastStart = cm;
182             break;
183           case MoveNumberOne:
184             switch (lastStart) {
185               case GNUChessGame:
186                 break;          /*  ignore  */
187               case PGNTag:
188                 lastStart = cm;
189                 break;          /*  Already started */
190               case (ChessMove) 0:
191               case MoveNumberOne:
192               case XBoardGame:
193                 if ((error = GameListNewGame(&currentListGame))) {
194                     rewind(f);
195                     yyskipmoves = FALSE;
196                     return(error);
197                 }
198                 currentListGame->number = ++gameNumber;
199                 currentListGame->offset = offset;
200                 lastStart = cm;
201                 break;
202               default:
203                 break;          /*  impossible  */
204             }
205             break;
206           case PGNTag:
207             lastStart = cm;
208             if ((error = GameListNewGame(&currentListGame))) {
209                 rewind(f);
210                 yyskipmoves = FALSE;
211                 return(error);
212             }
213             currentListGame->number = ++gameNumber;
214             currentListGame->offset = offset;
215             ParsePGNTag(yy_text, &currentListGame->gameInfo);
216             do {
217                 yyboardindex = 1;
218                 offset = yyoffset();
219                 cm = (ChessMove) yylex();
220                 if (cm == PGNTag) {
221                     ParsePGNTag(yy_text, &currentListGame->gameInfo);
222                 }
223             } while (cm == PGNTag || cm == Comment);
224             break;
225           default:
226             break;
227         }
228     }
229     while (cm != (ChessMove) 0);
230
231
232     if (appData.debugMode) {
233         for (currentListGame = (ListGame *) gameList.head;
234              currentListGame->node.succ;
235              currentListGame = (ListGame *) currentListGame->node.succ) {
236
237             fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
238                     currentListGame->number, currentListGame->offset);
239             PrintPGNTags(debugFP, &currentListGame->gameInfo);
240         }
241     }
242
243     rewind(f);
244     yyskipmoves = FALSE;
245     return 0;
246 }
247
248
249 /* Clear an existing GameInfo structure.
250  */
251 void ClearGameInfo(gameInfo)
252     GameInfo *gameInfo;
253 {
254     if (gameInfo->event != NULL) {
255         free(gameInfo->event);
256     }
257     if (gameInfo->site != NULL) {
258         free(gameInfo->site);
259     }
260     if (gameInfo->date != NULL) {
261         free(gameInfo->date);
262     }
263     if (gameInfo->round != NULL) {
264         free(gameInfo->round);
265     }
266     if (gameInfo->white != NULL) {
267         free(gameInfo->white);
268     }
269     if (gameInfo->black != NULL) {
270         free(gameInfo->black);
271     }
272     if (gameInfo->resultDetails != NULL) {
273         free(gameInfo->resultDetails);
274     }
275     if (gameInfo->fen != NULL) {
276         free(gameInfo->fen);
277     }
278     if (gameInfo->timeControl != NULL) {
279         free(gameInfo->timeControl);
280     }
281     if (gameInfo->extraTags != NULL) {
282         free(gameInfo->extraTags);
283     }
284
285     GameListInitGameInfo(gameInfo);
286 }
287
288 char *
289 GameListLine(number, gameInfo)
290      int number;
291      GameInfo *gameInfo;
292 {
293     char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
294                      gameInfo->event : gameInfo->site ? gameInfo->site : "?";
295     char *white = gameInfo->white ? gameInfo->white : "?";
296     char *black = gameInfo->black ? gameInfo->black : "?";
297     char *date = gameInfo->date ? gameInfo->date : "?";
298     int len = 10 + strlen(event) + 2 + strlen(white) + 1 + 
299       strlen(black) + 11 + strlen(date) + 1;
300     char *ret = (char *) malloc(len);
301     sprintf(ret, "%d. %s, %s-%s, %s, %s",
302             number, event, white, black, PGNResult(gameInfo->result), date);
303     return ret;
304 }
305