updated copyright to reflect A. Scotte as copyright holder
[xboard.git] / pgntags.c
1 /*
2  * pgntags.c -- Functions to manage PGN tags
3  *
4  * Copyright 1995,2009 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 static char *PGNTagsStatic P((GameInfo *));
51
52
53
54 /* Parse PGN tags; returns 0 for success or error number
55  */
56 int ParsePGNTag(tag, gameInfo)
57     char *tag;
58     GameInfo *gameInfo;
59 {
60     char *name, *value, *p, *oldTags;
61     int len;
62     int success;
63
64     name = tag;
65     while (!isalpha(*name) && !isdigit(*name)) {
66         name++;
67     }
68     p = name;
69     while (*p != ' ' && *p != '\t' && *p != '\n') {
70         p++;
71     }
72     *p = NULLCHAR;
73     value = strchr(p + 1, '"') + 1;
74     p = strrchr(value, '"');
75     *p = NULLCHAR;
76
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;
89     }
90     /* Fold together the various ways of denoting White/Black rating */
91     else if ((StrCaseCmp(name, "WhiteElo")==0) ||
92              (StrCaseCmp(name, "WhiteUSCF")==0) ) {
93       success = TRUE;
94       gameInfo->whiteRating = atoi( value );
95     } else if ((StrCaseCmp(name, "BlackElo")==0) ||
96                (StrCaseCmp(name, "BlackUSCF")==0)) {
97       success = TRUE;
98       gameInfo->blackRating = atoi( value );
99     }
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;
107         else
108             gameInfo->result = GameUnfinished;
109         success = TRUE;
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, PGN_OUT_OF_BOOK) == 0) {
120         /* [AS] Out of book annotation */
121         success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
122     } else {
123         if (gameInfo->extraTags == NULL) {
124             oldTags = "";
125         } else {
126             oldTags = gameInfo->extraTags;
127         }
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;
134             success = TRUE;
135         } else {
136             success = FALSE;
137         }
138     }
139     return(success ? 0 : ENOMEM);
140 }
141
142
143
144 /* Return a static buffer with a game's data.
145  */
146 static char *PGNTagsStatic(gameInfo)
147     GameInfo *gameInfo;
148 {
149     static char buf[8192];
150     char buf1[MSG_SIZ];
151
152     buf[0] = NULLCHAR;
153
154     sprintf(buf1, "[Event \"%s\"]\n",
155             gameInfo->event ? gameInfo->event : "?");
156     strcat(buf, buf1);
157     sprintf(buf1, "[Site \"%s\"]\n",
158             gameInfo->site ? gameInfo->site : "?");
159     strcat(buf, buf1);
160     sprintf(buf1, "[Date \"%s\"]\n",
161             gameInfo->date ? gameInfo->date : "?");
162     strcat(buf, buf1);
163     sprintf(buf1, "[Round \"%s\"]\n",
164             gameInfo->round ? gameInfo->round : "-");
165     strcat(buf, buf1);
166     sprintf(buf1, "[White \"%s\"]\n",
167             gameInfo->white ? gameInfo->white : "?");
168     strcat(buf, buf1);
169     sprintf(buf1, "[Black \"%s\"]\n",
170             gameInfo->black ? gameInfo->black : "?");
171     strcat(buf, buf1);
172     sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
173     strcat(buf, buf1);
174  
175     if (gameInfo->whiteRating >= 0 ) {
176         sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
177         strcat(buf, buf1);
178     }
179     if ( gameInfo->blackRating >= 0 ) {
180         sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
181         strcat(buf, buf1);
182     }    
183     if (gameInfo->timeControl != NULL) {
184         sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
185         strcat(buf, buf1);
186     }
187     if (gameInfo->variant != VariantNormal) {
188         sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
189         strcat(buf, buf1);
190     }
191     if (gameInfo->extraTags != NULL) {
192         strcat(buf, gameInfo->extraTags);
193     }
194     return buf;
195 }
196
197
198  
199 /* Print game info
200  */
201 void PrintPGNTags(fp, gameInfo)
202      FILE *fp;
203      GameInfo *gameInfo;
204 {
205     fprintf(fp, "%s", PGNTagsStatic(gameInfo));
206 }
207
208
209 /* Return a non-static buffer with a games info.
210  */
211 char *PGNTags(gameInfo)
212     GameInfo *gameInfo;
213 {
214     return StrSave(PGNTagsStatic(gameInfo));
215 }
216
217
218 /* Returns pointer to a static string with a result.
219  */
220 char *PGNResult(result)
221      ChessMove result;
222 {
223     switch (result) {
224       case GameUnfinished:
225       default:
226         return "*";
227       case WhiteWins:
228         return "1-0";
229       case BlackWins:
230         return "0-1";
231       case GameIsDrawn:
232         return "1/2-1/2";
233     }
234 }  
235
236 /* Returns 0 for success, nonzero for error */
237 int
238 ReplaceTags(tags, gameInfo)
239      char *tags;
240      GameInfo *gameInfo;
241 {
242     ChessMove moveType;
243     int err;
244
245     ClearGameInfo(gameInfo);
246     yynewstr(tags);
247     for (;;) {
248         yyboardindex = 0;
249         moveType = (ChessMove) yylex();
250         if (moveType == (ChessMove) 0) {
251             break;
252         } else if (moveType == PGNTag) {
253             err = ParsePGNTag(yy_text, gameInfo);
254             if (err != 0) return err;
255         } 
256     }
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
260      * is a result.
261      */
262     if (gameInfo->result != GameUnfinished) {
263       if (gameInfo->resultDetails) free(gameInfo->resultDetails);
264       gameInfo->resultDetails = strdup("");
265     }
266     return 0;
267 }