2 * pgntags.c -- Functions to manage PGN tags
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);
118 if (gameInfo->extraTags == NULL) {
121 oldTags = gameInfo->extraTags;
123 /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
124 len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
125 if ((p = (char *) malloc(len)) != NULL) {
126 sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
127 if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
128 gameInfo->extraTags = p;
134 return(success ? 0 : ENOMEM);
139 /* Return a static buffer with a game's data.
141 static char *PGNTagsStatic(gameInfo)
144 static char buf[8192];
149 sprintf(buf1, "[Event \"%s\"]\n",
150 gameInfo->event ? gameInfo->event : "?");
152 sprintf(buf1, "[Site \"%s\"]\n",
153 gameInfo->site ? gameInfo->site : "?");
155 sprintf(buf1, "[Date \"%s\"]\n",
156 gameInfo->date ? gameInfo->date : "?");
158 sprintf(buf1, "[Round \"%s\"]\n",
159 gameInfo->round ? gameInfo->round : "-");
161 sprintf(buf1, "[White \"%s\"]\n",
162 gameInfo->white ? gameInfo->white : "?");
164 sprintf(buf1, "[Black \"%s\"]\n",
165 gameInfo->black ? gameInfo->black : "?");
167 sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
170 if (gameInfo->whiteRating >= 0 ) {
171 sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
174 if ( gameInfo->blackRating >= 0 ) {
175 sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
178 if (gameInfo->timeControl != NULL) {
179 sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
182 if (gameInfo->variant != VariantNormal) {
183 sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
186 if (gameInfo->extraTags != NULL) {
187 strcat(buf, gameInfo->extraTags);
196 void PrintPGNTags(fp, gameInfo)
200 fprintf(fp, "%s", PGNTagsStatic(gameInfo));
204 /* Return a non-static buffer with a games info.
206 char *PGNTags(gameInfo)
209 return StrSave(PGNTagsStatic(gameInfo));
213 /* Returns pointer to a static string with a result.
215 char *PGNResult(result)
231 /* Returns 0 for success, nonzero for error */
233 ReplaceTags(tags, gameInfo)
240 ClearGameInfo(gameInfo);
244 moveType = (ChessMove) yylex();
245 if (moveType == (ChessMove) 0) {
247 } else if (moveType == PGNTag) {
248 err = ParsePGNTag(yy_text, gameInfo);
249 if (err != 0) return err;
252 /* just one problem...if there is a result in the new tags,
253 * DisplayMove() won't ever show it because ClearGameInfo() set
254 * gameInfo->resultDetails to NULL. So we must plug something in if there
257 if (gameInfo->result != GameUnfinished) {
258 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
259 gameInfo->resultDetails = strdup("");