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