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,2009 Free Software Foundation, Inc.
7 * ------------------------------------------------------------------------
9 * GNU XBoard is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
14 * GNU XBoard is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see http://www.gnu.org/licenses/. *
22 * ------------------------------------------------------------------------
24 * This file could well be a part of backend.c, but I prefer it this
36 #else /* not STDC_HEADERS */
39 # else /* not HAVE_STRING_H */
41 # endif /* not HAVE_STRING_H */
42 #endif /* not STDC_HEADERS */
49 static char *PGNTagsStatic P((GameInfo *));
53 /* Parse PGN tags; returns 0 for success or error number
55 int ParsePGNTag(tag, gameInfo)
59 char *name, *value, *p, *oldTags;
64 while (!isalpha(*name) && !isdigit(*name)) {
68 while (*p != ' ' && *p != '\t' && *p != '\n') {
72 value = strchr(p + 1, '"') + 1;
73 p = strrchr(value, '"');
76 if (StrCaseCmp(name, "Event") == 0) {
77 success = StrSavePtr(value, &gameInfo->event) != NULL;
78 } else if (StrCaseCmp(name, "Site") == 0) {
79 success = StrSavePtr(value, &gameInfo->site) != NULL;
80 } else if (StrCaseCmp(name, "Date") == 0) {
81 success = StrSavePtr(value, &gameInfo->date) != NULL;
82 } else if (StrCaseCmp(name, "Round") == 0) {
83 success = StrSavePtr(value, &gameInfo->round) != NULL;
84 } else if (StrCaseCmp(name, "White") == 0) {
85 success = StrSavePtr(value, &gameInfo->white) != NULL;
86 } else if (StrCaseCmp(name, "Black") == 0) {
87 success = StrSavePtr(value, &gameInfo->black) != NULL;
89 /* Fold together the various ways of denoting White/Black rating */
90 else if ((StrCaseCmp(name, "WhiteElo")==0) ||
91 (StrCaseCmp(name, "WhiteUSCF")==0) ) {
93 gameInfo->whiteRating = atoi( value );
94 } else if ((StrCaseCmp(name, "BlackElo")==0) ||
95 (StrCaseCmp(name, "BlackUSCF")==0)) {
97 gameInfo->blackRating = atoi( value );
99 else if (StrCaseCmp(name, "Result") == 0) {
100 if (strcmp(value, "1-0") == 0)
101 gameInfo->result = WhiteWins;
102 else if (strcmp(value, "0-1") == 0)
103 gameInfo->result = BlackWins;
104 else if (strcmp(value, "1/2-1/2") == 0)
105 gameInfo->result = GameIsDrawn;
107 gameInfo->result = GameUnfinished;
109 } else if (StrCaseCmp(name, "FEN") == 0) {
110 success = StrSavePtr(value, &gameInfo->fen) != NULL;
111 } else if (StrCaseCmp(name, "SetUp") == 0) {
112 /* ignore on input; presence of FEN governs */
114 } else if (StrCaseCmp(name, "Variant") == 0) {
115 /* xboard-defined extension */
116 gameInfo->variant = StringToVariant(value);
118 } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
119 /* [AS] Out of book annotation */
120 success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
122 if (gameInfo->extraTags == NULL) {
125 oldTags = gameInfo->extraTags;
127 /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
128 len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
129 if ((p = (char *) malloc(len)) != NULL) {
130 sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
131 if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
132 gameInfo->extraTags = p;
138 return(success ? 0 : ENOMEM);
143 /* Return a static buffer with a game's data.
145 static char *PGNTagsStatic(gameInfo)
148 static char buf[8192];
153 sprintf(buf1, "[Event \"%s\"]\n",
154 gameInfo->event ? gameInfo->event : "?");
156 sprintf(buf1, "[Site \"%s\"]\n",
157 gameInfo->site ? gameInfo->site : "?");
159 sprintf(buf1, "[Date \"%s\"]\n",
160 gameInfo->date ? gameInfo->date : "?");
162 sprintf(buf1, "[Round \"%s\"]\n",
163 gameInfo->round ? gameInfo->round : "-");
165 sprintf(buf1, "[White \"%s\"]\n",
166 gameInfo->white ? gameInfo->white : "?");
168 sprintf(buf1, "[Black \"%s\"]\n",
169 gameInfo->black ? gameInfo->black : "?");
171 sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
174 if (gameInfo->whiteRating >= 0 ) {
175 sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
178 if ( gameInfo->blackRating >= 0 ) {
179 sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
182 if (gameInfo->timeControl != NULL) {
183 sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
186 if (gameInfo->variant != VariantNormal) {
187 sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
190 if (gameInfo->extraTags != NULL) {
191 strcat(buf, gameInfo->extraTags);
200 void PrintPGNTags(fp, gameInfo)
204 fprintf(fp, "%s", PGNTagsStatic(gameInfo));
208 /* Return a non-static buffer with a games info.
210 char *PGNTags(gameInfo)
213 return StrSave(PGNTagsStatic(gameInfo));
217 /* Returns pointer to a static string with a result.
219 char *PGNResult(result)
235 /* Returns 0 for success, nonzero for error */
237 ReplaceTags(tags, gameInfo)
244 ClearGameInfo(gameInfo);
248 moveType = (ChessMove) yylex();
249 if (moveType == (ChessMove) 0) {
251 } else if (moveType == PGNTag) {
252 err = ParsePGNTag(yy_text, gameInfo);
253 if (err != 0) return err;
256 /* just one problem...if there is a result in the new tags,
257 * DisplayMove() won't ever show it because ClearGameInfo() set
258 * gameInfo->resultDetails to NULL. So we must plug something in if there
261 if (gameInfo->result != GameUnfinished) {
262 if (gameInfo->resultDetails) free(gameInfo->resultDetails);
263 gameInfo->resultDetails = strdup("");