updated copyright for 2016
[xboard.git] / pgntags.c
1 /*
2  * pgntags.c -- Functions to manage PGN tags
3  *
4  * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
5  * Software Foundation, Inc.
6  *
7  * Enhancements Copyright 2005 Alessandro Scotti
8  *
9  * ------------------------------------------------------------------------
10  *
11  * GNU XBoard is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or (at
14  * your option) any later version.
15  *
16  * GNU XBoard is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see http://www.gnu.org/licenses/.  *
23  *
24  * ------------------------------------------------------------------------
25  *
26  * This file could well be a part of backend.c, but I prefer it this
27  * way.
28  */
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 # include <string.h>
38 #else /* not STDC_HEADERS */
39 # if HAVE_STRING_H
40 #  include <string.h>
41 # else /* not HAVE_STRING_H */
42 #  include <strings.h>
43 # endif /* not HAVE_STRING_H */
44 #endif /* not STDC_HEADERS */
45
46 #include "common.h"
47 #include "frontend.h"
48 #include "backend.h"
49 #include "parser.h"
50
51
52 /* Parse PGN tags; returns 0 for success or error number
53  */
54 int
55 ParsePGNTag (char *tag, GameInfo *gameInfo)
56 {
57     char *name, *value, *p, *oldTags;
58     int len;
59     int success;
60
61     name = tag;
62     while (!isalpha(*name) && !isdigit(*name)) {
63         name++;
64     }
65     p = name;
66     while (*p != ' ' && *p != '\t' && *p != '\n') {
67         p++;
68     }
69     *p = NULLCHAR;
70     value = strchr(p + 1, '"') + 1;
71     p = strrchr(value, '"');
72     *p = NULLCHAR;
73
74     if (StrCaseCmp(name, "Event") == 0) {
75         success = StrSavePtr(value, &gameInfo->event) != NULL;
76     } else if (StrCaseCmp(name, "Site") == 0) {
77         success = StrSavePtr(value, &gameInfo->site) != NULL;
78     } else if (StrCaseCmp(name, "Date") == 0) {
79         success = StrSavePtr(value, &gameInfo->date) != NULL;
80     } else if (StrCaseCmp(name, "Round") == 0) {
81         success = StrSavePtr(value, &gameInfo->round) != NULL;
82     } else if (StrCaseCmp(name, "White") == 0) {
83         success = StrSavePtr(value, &gameInfo->white) != NULL;
84     } else if (StrCaseCmp(name, "Black") == 0) {
85         success = StrSavePtr(value, &gameInfo->black) != NULL;
86     }
87     /* Fold together the various ways of denoting White/Black rating */
88     else if ((StrCaseCmp(name, "WhiteElo")==0) ||
89              (StrCaseCmp(name, "WhiteUSCF")==0) ) {
90       success = TRUE;
91       gameInfo->whiteRating = atoi( value );
92     } else if ((StrCaseCmp(name, "BlackElo")==0) ||
93                (StrCaseCmp(name, "BlackUSCF")==0)) {
94       success = TRUE;
95       gameInfo->blackRating = atoi( value );
96     }
97     else if (StrCaseCmp(name, "Result") == 0) {
98         if (strcmp(value, "1-0") == 0)
99             gameInfo->result = WhiteWins;
100         else if (strcmp(value, "0-1") == 0)
101             gameInfo->result = BlackWins;
102         else if (strcmp(value, "1/2-1/2") == 0)
103             gameInfo->result = GameIsDrawn;
104         else
105             gameInfo->result = GameUnfinished;
106         success = TRUE;
107     } else if (StrCaseCmp(name, "TimeControl") == 0) {
108 //      int tc, mps, inc = -1;
109 //      if(sscanf(value, "%d/%d", &mps, &tc) == 2 || )
110         success = StrSavePtr(value, &gameInfo->timeControl) != NULL;
111     } else if (StrCaseCmp(name, "FEN") == 0) {
112         success = StrSavePtr(value, &gameInfo->fen) != NULL;
113     } else if (StrCaseCmp(name, "SetUp") == 0) {
114         /* ignore on input; presence of FEN governs */
115         success = TRUE;
116     } else if (StrCaseCmp(name, "Variant") == 0) {
117         /* xboard-defined extension */
118         success = StrSavePtr(value, &gameInfo->variantName) != NULL;
119         if(*value && strcmp(value, engineVariant)) // keep current engine-defined variant if it matches
120             gameInfo->variant = StringToVariant(value);
121     } else if (StrCaseCmp(name, "VariantMen") == 0) {
122         /* for now ignore this tag, as we have no method yet */
123         /* for assigning the pieces to XBoard pictograms     */
124         success = TRUE;
125     } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
126         /* [AS] Out of book annotation */
127         success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
128     } else {
129         if (gameInfo->extraTags == NULL) {
130             oldTags = "";
131         } else {
132             oldTags = gameInfo->extraTags;
133         }
134         /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
135         len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
136         if ((p = (char *) malloc(len))  !=  NULL) {
137             sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
138             if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
139             gameInfo->extraTags = p;
140             success = TRUE;
141         } else {
142             success = FALSE;
143         }
144     }
145     return(success ? 0 : ENOMEM);
146 }
147
148
149 /* Print game info */
150 void
151 PrintPGNTags (FILE *fp, GameInfo *gameInfo)
152 {
153     char *p;
154     fprintf(fp, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
155     fprintf(fp, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
156     fprintf(fp, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
157     fprintf(fp, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
158     fprintf(fp, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
159     fprintf(fp, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
160     fprintf(fp, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
161     if (gameInfo->whiteRating >= 0)
162         fprintf(fp, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
163     if (gameInfo->blackRating >= 0)
164         fprintf(fp, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
165     if (gameInfo->timeControl)
166         fprintf(fp, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
167     if (gameInfo->variant != VariantNormal)
168         fprintf(fp, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
169     if (*(p = CollectPieceDescriptors()))
170         fprintf(fp, "[VariantMen \"%s\"]\n", p);
171     if (gameInfo->extraTags)
172         fputs(gameInfo->extraTags, fp);
173 }
174
175
176 /* Return a non-static buffer with a games info.
177  */
178 char *
179 PGNTags (GameInfo *gameInfo)
180 {
181     size_t len;
182     char *buf;
183     char *p;
184
185     // First calculate the needed buffer size.
186     // Then we don't have to check the buffer size later.
187     len = 12 + 11 + 11 + 12 + 12 + 12 + 25 + 1; // The first 7 tags
188     if (gameInfo->event) len += strlen(gameInfo->event);
189     if (gameInfo->site)  len += strlen(gameInfo->site);
190     if (gameInfo->date)  len += strlen(gameInfo->date);
191     if (gameInfo->round) len += strlen(gameInfo->round);
192     if (gameInfo->white) len += strlen(gameInfo->white);
193     if (gameInfo->black) len += strlen(gameInfo->black);
194     if (gameInfo->whiteRating >= 0) len += 40;
195     if (gameInfo->blackRating >= 0) len += 40;
196     if (gameInfo->timeControl) len += strlen(gameInfo->timeControl) + 20;
197     if (gameInfo->variant != VariantNormal) len += 50;
198     if (gameInfo->extraTags) len += strlen(gameInfo->extraTags);
199
200     buf = malloc(len);
201     if (!buf)
202         return 0;
203
204     p = buf;
205     p += sprintf(p, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
206     p += sprintf(p, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
207     p += sprintf(p, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
208     p += sprintf(p, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
209     p += sprintf(p, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
210     p += sprintf(p, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
211     p += sprintf(p, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
212     if (gameInfo->whiteRating >= 0)
213         p += sprintf(p, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
214     if (gameInfo->blackRating >= 0)
215         p += sprintf(p, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
216     if (gameInfo->timeControl)
217         p += sprintf(p, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
218     if (gameInfo->variant != VariantNormal)
219         p += sprintf(p, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
220     if (gameInfo->extraTags)
221         strcpy(p, gameInfo->extraTags);
222     return buf;
223 }
224
225
226 /* Returns pointer to a static string with a result.
227  */
228 char *
229 PGNResult (ChessMove result)
230 {
231     switch (result) {
232       case GameUnfinished:
233       default:
234         return "*";
235       case WhiteWins:
236         return "1-0";
237       case BlackWins:
238         return "0-1";
239       case GameIsDrawn:
240         return "1/2-1/2";
241     }
242 }
243
244 /* Returns 0 for success, nonzero for error */
245 int
246 ReplaceTags (char *tags, GameInfo *gameInfo)
247 {
248     ChessMove moveType;
249     int err;
250
251     ClearGameInfo(gameInfo);
252     yynewstr(tags);
253     for (;;) {
254         yyboardindex = 0;
255         moveType = (ChessMove) Myylex();
256         if (moveType == (ChessMove) 0) {
257             break;
258         } else if (moveType == PGNTag) {
259             err = ParsePGNTag(yy_text, gameInfo);
260             if (err != 0) return err;
261         }
262     }
263     /* just one problem...if there is a result in the new tags,
264      * DisplayMove() won't ever show it because ClearGameInfo() set
265      * gameInfo->resultDetails to NULL. So we must plug something in if there
266      * is a result.
267      */
268     if (gameInfo->result != GameUnfinished) {
269       if (gameInfo->resultDetails) free(gameInfo->resultDetails);
270       gameInfo->resultDetails = strdup("");
271     }
272     return 0;
273 }