2 * pgntags.c -- Functions to manage PGN tags
3 * XBoard $Id: pgntags.c,v 2.1 2003/10/27 19:21:00 mann Exp $
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 * ------------------------------------------------------------------------
23 * This file could well be a part of backend.c, but I prefer it this
35 #else /* not STDC_HEADERS */
38 # else /* not HAVE_STRING_H */
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
48 static char *PGNTagsStatic P((GameInfo *));
52 /* Parse PGN tags; returns 0 for success or error number
54 int ParsePGNTag(tag, gameInfo)
58 char *name, *value, *p, *oldTags;
63 while (!isalpha(*name) && !isdigit(*name)) {
67 while (*p != ' ' && *p != '\t' && *p != '\n') {
71 value = strchr(p + 1, '"') + 1;
72 p = strrchr(value, '"');
75 if (StrCaseCmp(name, "Event") == 0) {
76 success = StrSavePtr(value, &gameInfo->event) != NULL;
77 } else if (StrCaseCmp(name, "Site") == 0) {
78 success = StrSavePtr(value, &gameInfo->site) != NULL;
79 } else if (StrCaseCmp(name, "Date") == 0) {
80 success = StrSavePtr(value, &gameInfo->date) != NULL;
81 } else if (StrCaseCmp(name, "Round") == 0) {
82 success = StrSavePtr(value, &gameInfo->round) != NULL;
83 } else if (StrCaseCmp(name, "White") == 0) {
84 success = StrSavePtr(value, &gameInfo->white) != NULL;
85 } else if (StrCaseCmp(name, "Black") == 0) {
86 success = StrSavePtr(value, &gameInfo->black) != NULL;
88 /* Fold together the various ways of denoting White/Black rating */
89 else if ((StrCaseCmp(name, "WhiteElo")==0) ||
90 (StrCaseCmp(name, "WhiteUSCF")==0) ) {
92 gameInfo->whiteRating = atoi( value );
93 } else if ((StrCaseCmp(name, "BlackElo")==0) ||
94 (StrCaseCmp(name, "BlackUSCF")==0)) {
96 gameInfo->blackRating = atoi( value );
98 else if (StrCaseCmp(name, "Result") == 0) {
99 if (strcmp(value, "1-0") == 0)
100 gameInfo->result = WhiteWins;
101 else if (strcmp(value, "0-1") == 0)
102 gameInfo->result = BlackWins;
103 else if (strcmp(value, "1/2-1/2") == 0)
104 gameInfo->result = GameIsDrawn;
106 gameInfo->result = GameUnfinished;
108 } else if (StrCaseCmp(name, "FEN") == 0) {
109 success = StrSavePtr(value, &gameInfo->fen) != NULL;
110 } else if (StrCaseCmp(name, "SetUp") == 0) {
111 /* ignore on input; presence of FEN governs */
113 } else if (StrCaseCmp(name, "Variant") == 0) {
114 /* xboard-defined extension */
115 gameInfo->variant = StringToVariant(value);
117 } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
118 /* [AS] Out of book annotation */
119 success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
121 if (gameInfo->extraTags == NULL) {
124 oldTags = gameInfo->extraTags;
126 /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
127 len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
128 if ((p = (char *) malloc(len)) != NULL) {
129 sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
130 if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
131 gameInfo->extraTags = p;
137 return(success ? 0 : ENOMEM);
142 /* Return a static buffer with a game's data.
144 static char *PGNTagsStatic(gameInfo)
147 static char buf[8192];
152 sprintf(buf1, "[Event \"%s\"]\n",
153 gameInfo->event ? gameInfo->event : "?");
155 sprintf(buf1, "[Site \"%s\"]\n",
156 gameInfo->site ? gameInfo->site : "?");
158 sprintf(buf1, "[Date \"%s\"]\n",
159 gameInfo->date ? gameInfo->date : "?");
161 sprintf(buf1, "[Round \"%s\"]\n",
162 gameInfo->round ? gameInfo->round : "-");
164 sprintf(buf1, "[White \"%s\"]\n",
165 gameInfo->white ? gameInfo->white : "?");
167 sprintf(buf1, "[Black \"%s\"]\n",
168 gameInfo->black ? gameInfo->black : "?");
170 sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
173 if (gameInfo->whiteRating >= 0 ) {
174 sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
177 if ( gameInfo->blackRating >= 0 ) {
178 sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
181 if (gameInfo->timeControl != NULL) {
182 sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
185 if (gameInfo->variant != VariantNormal) {
186 sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
189 if (gameInfo->extraTags != NULL) {
190 strcat(buf, gameInfo->extraTags);
199 void PrintPGNTags(fp, gameInfo)
203 fprintf(fp, "%s", PGNTagsStatic(gameInfo));
207 /* Return a non-static buffer with a games info.
209 char *PGNTags(gameInfo)
212 return StrSave(PGNTagsStatic(gameInfo));
216 /* Returns pointer to a static string with a result.
218 char *PGNResult(result)
234 /* Returns 0 for success, nonzero for error */
236 ReplaceTags(tags, gameInfo)
243 ClearGameInfo(gameInfo);
247 moveType = (ChessMove) yylex();
248 if (moveType == (ChessMove) 0) {
250 } else if (moveType == PGNTag) {
251 err = ParsePGNTag(yy_text, gameInfo);
252 if (err != 0) return err;
255 /* just one problem...if there is a result in the new tags,
256 * DisplayMove() won't ever show it because ClearGameInfo() set
257 * gameInfo->resultDetails to NULL. So we must plug something in if there
260 if (gameInfo->result != GameUnfinished) {
261 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
262 gameInfo->resultDetails = strdup("");