2 * gamelist.c -- Functions to manage a gamelist
5 * Copyright 1995 Free Software Foundation, Inc.
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.
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.
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 * ------------------------------------------------------------------------
31 #else /* not STDC_HEADERS */
34 # else /* not HAVE_STRING_H */
36 # endif /* not HAVE_STRING_H */
37 #endif /* not STDC_HEADERS */
50 /* Local function prototypes
52 static void GameListDeleteGame P((ListGame *));
53 static ListGame *GameListCreate P((void));
54 static void GameListFree P((List *));
55 static int GameListNewGame P((ListGame **));
57 /* Delete a ListGame; implies removint it from a list.
59 static void GameListDeleteGame(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);
78 /* Free the previous list of games.
80 static void GameListFree(gameList)
83 while (!ListEmpty(gameList))
85 GameListDeleteGame((ListGame *) gameList->head);
91 /* Initialize a new GameInfo structure.
93 void GameListInitGameInfo(gameInfo)
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;
113 /* Create empty ListGame; returns ListGame or NULL, if out of memory.
115 * Note, that the ListGame is *not* added to any list
117 static ListGame *GameListCreate()
122 if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
123 GameListInitGameInfo(&listGame->gameInfo);
129 /* Creates a new game for the gamelist.
131 static int GameListNewGame(listGamePtr)
132 ListGame **listGamePtr;
134 if (!(*listGamePtr = (ListGame *) GameListCreate())) {
135 GameListFree(&gameList);
138 ListAddTail(&gameList, (ListNode *) *listGamePtr);
143 /* Build the list of games in the open file f.
144 * Returns 0 for success or error number.
149 ChessMove cm, lastStart;
151 ListGame *currentListGame = NULL;
155 GameListFree(&gameList);
159 lastStart = (ChessMove) 0;
164 cm = (ChessMove) yylex();
167 if ((error = GameListNewGame(¤tListGame))) {
172 currentListGame->number = ++gameNumber;
173 currentListGame->offset = offset;
174 if (currentListGame->gameInfo.event != NULL) {
175 free(currentListGame->gameInfo.event);
177 currentListGame->gameInfo.event = StrSave(yy_text);
189 break; /* Already started */
193 if ((error = GameListNewGame(¤tListGame))) {
198 currentListGame->number = ++gameNumber;
199 currentListGame->offset = offset;
203 break; /* impossible */
208 if ((error = GameListNewGame(¤tListGame))) {
213 currentListGame->number = ++gameNumber;
214 currentListGame->offset = offset;
215 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
219 cm = (ChessMove) yylex();
221 ParsePGNTag(yy_text, ¤tListGame->gameInfo);
223 } while (cm == PGNTag || cm == Comment);
226 /* Allow the first game to start with an unnumbered move */
228 if (lastStart == (ChessMove) 0) {
229 if ((error = GameListNewGame(¤tListGame))) {
234 currentListGame->number = ++gameNumber;
235 currentListGame->offset = offset;
236 lastStart = MoveNumberOne;
243 while (cm != (ChessMove) 0);
246 if (appData.debugMode) {
247 for (currentListGame = (ListGame *) gameList.head;
248 currentListGame->node.succ;
249 currentListGame = (ListGame *) currentListGame->node.succ) {
251 fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
252 currentListGame->number, currentListGame->offset);
253 PrintPGNTags(debugFP, ¤tListGame->gameInfo);
263 /* Clear an existing GameInfo structure.
265 void ClearGameInfo(gameInfo)
268 if (gameInfo->event != NULL) {
269 free(gameInfo->event);
271 if (gameInfo->site != NULL) {
272 free(gameInfo->site);
274 if (gameInfo->date != NULL) {
275 free(gameInfo->date);
277 if (gameInfo->round != NULL) {
278 free(gameInfo->round);
280 if (gameInfo->white != NULL) {
281 free(gameInfo->white);
283 if (gameInfo->black != NULL) {
284 free(gameInfo->black);
286 if (gameInfo->resultDetails != NULL) {
287 free(gameInfo->resultDetails);
289 if (gameInfo->fen != NULL) {
292 if (gameInfo->timeControl != NULL) {
293 free(gameInfo->timeControl);
295 if (gameInfo->extraTags != NULL) {
296 free(gameInfo->extraTags);
299 GameListInitGameInfo(gameInfo);
303 GameListLine(number, gameInfo)
307 char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
308 gameInfo->event : gameInfo->site ? gameInfo->site : "?";
309 char *white = gameInfo->white ? gameInfo->white : "?";
310 char *black = gameInfo->black ? gameInfo->black : "?";
311 char *date = gameInfo->date ? gameInfo->date : "?";
312 int len = 10 + strlen(event) + 2 + strlen(white) + 1 +
313 strlen(black) + 11 + strlen(date) + 1;
314 char *ret = (char *) malloc(len);
315 sprintf(ret, "%d. %s, %s-%s, %s, %s",
316 number, event, white, black, PGNResult(gameInfo->result), date);