2 * pgntags.c -- Functions to manage PGN tags
4 * Copyright 1995,2009 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 */
50 static char *PGNTagsStatic P((GameInfo *));
54 /* Parse PGN tags; returns 0 for success or error number
56 int ParsePGNTag(tag, gameInfo)
60 char *name, *value, *p, *oldTags;
65 while (!isalpha(*name) && !isdigit(*name)) {
69 while (*p != ' ' && *p != '\t' && *p != '\n') {
73 value = strchr(p + 1, '"') + 1;
74 p = strrchr(value, '"');
77 if (StrCaseCmp(name, "Event") == 0) {
78 success = StrSavePtr(value, &gameInfo->event) != NULL;
79 } else if (StrCaseCmp(name, "Site") == 0) {
80 success = StrSavePtr(value, &gameInfo->site) != NULL;
81 } else if (StrCaseCmp(name, "Date") == 0) {
82 success = StrSavePtr(value, &gameInfo->date) != NULL;
83 } else if (StrCaseCmp(name, "Round") == 0) {
84 success = StrSavePtr(value, &gameInfo->round) != NULL;
85 } else if (StrCaseCmp(name, "White") == 0) {
86 success = StrSavePtr(value, &gameInfo->white) != NULL;
87 } else if (StrCaseCmp(name, "Black") == 0) {
88 success = StrSavePtr(value, &gameInfo->black) != NULL;
90 /* Fold together the various ways of denoting White/Black rating */
91 else if ((StrCaseCmp(name, "WhiteElo")==0) ||
92 (StrCaseCmp(name, "WhiteUSCF")==0) ) {
94 gameInfo->whiteRating = atoi( value );
95 } else if ((StrCaseCmp(name, "BlackElo")==0) ||
96 (StrCaseCmp(name, "BlackUSCF")==0)) {
98 gameInfo->blackRating = atoi( value );
100 else if (StrCaseCmp(name, "Result") == 0) {
101 if (strcmp(value, "1-0") == 0)
102 gameInfo->result = WhiteWins;
103 else if (strcmp(value, "0-1") == 0)
104 gameInfo->result = BlackWins;
105 else if (strcmp(value, "1/2-1/2") == 0)
106 gameInfo->result = GameIsDrawn;
108 gameInfo->result = GameUnfinished;
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);
144 /* Return a static buffer with a game's data.
146 static char *PGNTagsStatic(gameInfo)
149 static char buf[8192];
154 sprintf(buf1, "[Event \"%s\"]\n",
155 gameInfo->event ? gameInfo->event : "?");
157 sprintf(buf1, "[Site \"%s\"]\n",
158 gameInfo->site ? gameInfo->site : "?");
160 sprintf(buf1, "[Date \"%s\"]\n",
161 gameInfo->date ? gameInfo->date : "?");
163 sprintf(buf1, "[Round \"%s\"]\n",
164 gameInfo->round ? gameInfo->round : "-");
166 sprintf(buf1, "[White \"%s\"]\n",
167 gameInfo->white ? gameInfo->white : "?");
169 sprintf(buf1, "[Black \"%s\"]\n",
170 gameInfo->black ? gameInfo->black : "?");
172 sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
175 if (gameInfo->whiteRating >= 0 ) {
176 sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
179 if ( gameInfo->blackRating >= 0 ) {
180 sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
183 if (gameInfo->timeControl != NULL) {
184 sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
187 if (gameInfo->variant != VariantNormal) {
188 sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
191 if (gameInfo->extraTags != NULL) {
192 strcat(buf, gameInfo->extraTags);
201 void PrintPGNTags(fp, gameInfo)
205 fprintf(fp, "%s", PGNTagsStatic(gameInfo));
209 /* Return a non-static buffer with a games info.
211 char *PGNTags(gameInfo)
214 return StrSave(PGNTagsStatic(gameInfo));
218 /* Returns pointer to a static string with a result.
220 char *PGNResult(result)
236 /* Returns 0 for success, nonzero for error */
238 ReplaceTags(tags, gameInfo)
245 ClearGameInfo(gameInfo);
249 moveType = (ChessMove) yylex();
250 if (moveType == (ChessMove) 0) {
252 } else if (moveType == PGNTag) {
253 err = ParsePGNTag(yy_text, gameInfo);
254 if (err != 0) return err;
257 /* just one problem...if there is a result in the new tags,
258 * DisplayMove() won't ever show it because ClearGameInfo() set
259 * gameInfo->resultDetails to NULL. So we must plug something in if there
262 if (gameInfo->result != GameUnfinished) {
263 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
264 gameInfo->resultDetails = strdup("");