2 * pgntags.c -- Functions to manage PGN tags
4 * Copyright 1995, 2009, 2010 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, "TimeControl") == 0) {
111 // int tc, mps, inc = -1;
112 // if(sscanf(value, "%d/%d", &mps, &tc) == 2 || )
113 success = StrSavePtr(value, &gameInfo->timeControl) != NULL;
114 } else if (StrCaseCmp(name, "FEN") == 0) {
115 success = StrSavePtr(value, &gameInfo->fen) != NULL;
116 } else if (StrCaseCmp(name, "SetUp") == 0) {
117 /* ignore on input; presence of FEN governs */
119 } else if (StrCaseCmp(name, "Variant") == 0) {
120 /* xboard-defined extension */
121 gameInfo->variant = StringToVariant(value);
123 } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
124 /* [AS] Out of book annotation */
125 success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
127 if (gameInfo->extraTags == NULL) {
130 oldTags = gameInfo->extraTags;
132 /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
133 len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
134 if ((p = (char *) malloc(len)) != NULL) {
135 sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
136 if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
137 gameInfo->extraTags = p;
143 return(success ? 0 : ENOMEM);
148 /* Return a static buffer with a game's data.
150 static char *PGNTagsStatic(gameInfo)
153 static char buf[8192];
158 snprintf(buf1, MSG_SIZ, "[Event \"%s\"]\n",
159 gameInfo->event ? gameInfo->event : "?");
161 snprintf(buf1, MSG_SIZ, "[Site \"%s\"]\n",
162 gameInfo->site ? gameInfo->site : "?");
164 snprintf(buf1, MSG_SIZ, "[Date \"%s\"]\n",
165 gameInfo->date ? gameInfo->date : "?");
167 snprintf(buf1, MSG_SIZ, "[Round \"%s\"]\n",
168 gameInfo->round ? gameInfo->round : "-");
170 snprintf(buf1, MSG_SIZ, "[White \"%s\"]\n",
171 gameInfo->white ? gameInfo->white : "?");
173 snprintf(buf1, MSG_SIZ, "[Black \"%s\"]\n",
174 gameInfo->black ? gameInfo->black : "?");
176 snprintf(buf1, MSG_SIZ, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
179 if (gameInfo->whiteRating >= 0 ) {
180 snprintf(buf1, MSG_SIZ, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
183 if ( gameInfo->blackRating >= 0 ) {
184 snprintf(buf1, MSG_SIZ, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
187 if (gameInfo->timeControl != NULL) {
188 snprintf(buf1, MSG_SIZ, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
191 if (gameInfo->variant != VariantNormal) {
192 snprintf(buf1, MSG_SIZ, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
195 if (gameInfo->extraTags != NULL) {
196 strcat(buf, gameInfo->extraTags);
205 void PrintPGNTags(fp, gameInfo)
209 fprintf(fp, "%s", PGNTagsStatic(gameInfo));
213 /* Return a non-static buffer with a games info.
215 char *PGNTags(gameInfo)
218 return StrSave(PGNTagsStatic(gameInfo));
222 /* Returns pointer to a static string with a result.
224 char *PGNResult(result)
240 /* Returns 0 for success, nonzero for error */
242 ReplaceTags(tags, gameInfo)
249 ClearGameInfo(gameInfo);
253 moveType = (ChessMove) Myylex();
254 if (moveType == (ChessMove) 0) {
256 } else if (moveType == PGNTag) {
257 err = ParsePGNTag(yy_text, gameInfo);
258 if (err != 0) return err;
261 /* just one problem...if there is a result in the new tags,
262 * DisplayMove() won't ever show it because ClearGameInfo() set
263 * gameInfo->resultDetails to NULL. So we must plug something in if there
266 if (gameInfo->result != GameUnfinished) {
267 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
268 gameInfo->resultDetails = strdup("");