2 * pgntags.c -- Functions to manage PGN tags
4 * Copyright 1995, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
6 * Enhancements Copyright 2005 Alessandro Scotti
8 * ------------------------------------------------------------------------
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.
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.
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/. *
23 * ------------------------------------------------------------------------
25 * This file could well be a part of backend.c, but I prefer it this
37 #else /* not STDC_HEADERS */
40 # else /* not HAVE_STRING_H */
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
51 /* Parse PGN tags; returns 0 for success or error number
54 ParsePGNTag (char *tag, GameInfo *gameInfo)
56 char *name, *value, *p, *oldTags;
61 while (!isalpha(*name) && !isdigit(*name)) {
65 while (*p != ' ' && *p != '\t' && *p != '\n') {
69 value = strchr(p + 1, '"') + 1;
70 p = strrchr(value, '"');
73 if (StrCaseCmp(name, "Event") == 0) {
74 success = StrSavePtr(value, &gameInfo->event) != NULL;
75 } else if (StrCaseCmp(name, "Site") == 0) {
76 success = StrSavePtr(value, &gameInfo->site) != NULL;
77 } else if (StrCaseCmp(name, "Date") == 0) {
78 success = StrSavePtr(value, &gameInfo->date) != NULL;
79 } else if (StrCaseCmp(name, "Round") == 0) {
80 success = StrSavePtr(value, &gameInfo->round) != NULL;
81 } else if (StrCaseCmp(name, "White") == 0) {
82 success = StrSavePtr(value, &gameInfo->white) != NULL;
83 } else if (StrCaseCmp(name, "Black") == 0) {
84 success = StrSavePtr(value, &gameInfo->black) != NULL;
86 /* Fold together the various ways of denoting White/Black rating */
87 else if ((StrCaseCmp(name, "WhiteElo")==0) ||
88 (StrCaseCmp(name, "WhiteUSCF")==0) ) {
90 gameInfo->whiteRating = atoi( value );
91 } else if ((StrCaseCmp(name, "BlackElo")==0) ||
92 (StrCaseCmp(name, "BlackUSCF")==0)) {
94 gameInfo->blackRating = atoi( value );
96 else if (StrCaseCmp(name, "Result") == 0) {
97 if (strcmp(value, "1-0") == 0)
98 gameInfo->result = WhiteWins;
99 else if (strcmp(value, "0-1") == 0)
100 gameInfo->result = BlackWins;
101 else if (strcmp(value, "1/2-1/2") == 0)
102 gameInfo->result = GameIsDrawn;
104 gameInfo->result = GameUnfinished;
106 } else if (StrCaseCmp(name, "TimeControl") == 0) {
107 // int tc, mps, inc = -1;
108 // if(sscanf(value, "%d/%d", &mps, &tc) == 2 || )
109 success = StrSavePtr(value, &gameInfo->timeControl) != NULL;
110 } else if (StrCaseCmp(name, "FEN") == 0) {
111 success = StrSavePtr(value, &gameInfo->fen) != NULL;
112 } else if (StrCaseCmp(name, "SetUp") == 0) {
113 /* ignore on input; presence of FEN governs */
115 } else if (StrCaseCmp(name, "Variant") == 0) {
116 /* xboard-defined extension */
117 gameInfo->variant = StringToVariant(value);
119 } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
120 /* [AS] Out of book annotation */
121 success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
123 if (gameInfo->extraTags == NULL) {
126 oldTags = gameInfo->extraTags;
128 /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
129 len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
130 if ((p = (char *) malloc(len)) != NULL) {
131 sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
132 if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
133 gameInfo->extraTags = p;
139 return(success ? 0 : ENOMEM);
143 /* Print game info */
145 PrintPGNTags (FILE *fp, GameInfo *gameInfo)
147 fprintf(fp, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
148 fprintf(fp, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
149 fprintf(fp, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
150 fprintf(fp, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
151 fprintf(fp, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
152 fprintf(fp, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
153 fprintf(fp, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
154 if (gameInfo->whiteRating >= 0)
155 fprintf(fp, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
156 if (gameInfo->blackRating >= 0)
157 fprintf(fp, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
158 if (gameInfo->timeControl)
159 fprintf(fp, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
160 if (gameInfo->variant != VariantNormal)
161 fprintf(fp, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
162 if (gameInfo->extraTags)
163 fputs(gameInfo->extraTags, fp);
167 /* Return a non-static buffer with a games info.
170 PGNTags (GameInfo *gameInfo)
176 // First calculate the needed buffer size.
177 // Then we don't have to check the buffer size later.
178 len = 12 + 11 + 11 + 12 + 12 + 12 + 25 + 1; // The first 7 tags
179 if (gameInfo->event) len += strlen(gameInfo->event);
180 if (gameInfo->site) len += strlen(gameInfo->site);
181 if (gameInfo->date) len += strlen(gameInfo->date);
182 if (gameInfo->round) len += strlen(gameInfo->round);
183 if (gameInfo->white) len += strlen(gameInfo->white);
184 if (gameInfo->black) len += strlen(gameInfo->black);
185 if (gameInfo->whiteRating >= 0) len += 40;
186 if (gameInfo->blackRating >= 0) len += 40;
187 if (gameInfo->timeControl) len += strlen(gameInfo->timeControl) + 20;
188 if (gameInfo->variant != VariantNormal) len += 50;
189 if (gameInfo->extraTags) len += strlen(gameInfo->extraTags);
196 p += sprintf(p, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
197 p += sprintf(p, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
198 p += sprintf(p, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
199 p += sprintf(p, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
200 p += sprintf(p, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
201 p += sprintf(p, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
202 p += sprintf(p, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
203 if (gameInfo->whiteRating >= 0)
204 p += sprintf(p, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
205 if (gameInfo->blackRating >= 0)
206 p += sprintf(p, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
207 if (gameInfo->timeControl)
208 p += sprintf(p, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
209 if (gameInfo->variant != VariantNormal)
210 p += sprintf(p, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
211 if (gameInfo->extraTags)
212 strcpy(p, gameInfo->extraTags);
217 /* Returns pointer to a static string with a result.
220 PGNResult (ChessMove result)
235 /* Returns 0 for success, nonzero for error */
237 ReplaceTags (char *tags, GameInfo *gameInfo)
242 ClearGameInfo(gameInfo);
246 moveType = (ChessMove) Myylex();
247 if (moveType == (ChessMove) 0) {
249 } else if (moveType == PGNTag) {
250 err = ParsePGNTag(yy_text, gameInfo);
251 if (err != 0) return err;
254 /* just one problem...if there is a result in the new tags,
255 * DisplayMove() won't ever show it because ClearGameInfo() set
256 * gameInfo->resultDetails to NULL. So we must plug something in if there
259 if (gameInfo->result != GameUnfinished) {
260 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
261 gameInfo->resultDetails = strdup("");