Updated all files to GPL version 3.
[xboard.git] / pgntags.c
1 /*
2  * pgntags.c -- Functions to manage PGN tags
3  * XBoard $Id: pgntags.c,v 2.1 2003/10/27 19:21:00 mann Exp $
4  *
5  * Copyright 1995,2009 Free Software Foundation, Inc.
6  *
7  * ------------------------------------------------------------------------
8  *
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.
13  *
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.
18  *
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/.  *
21  *
22  * ------------------------------------------------------------------------
23  *
24  * This file could well be a part of backend.c, but I prefer it this
25  * way.
26  */
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #if STDC_HEADERS
34 # include <stdlib.h>
35 # include <string.h>
36 #else /* not STDC_HEADERS */
37 # if HAVE_STRING_H
38 #  include <string.h>
39 # else /* not HAVE_STRING_H */
40 #  include <strings.h>
41 # endif /* not HAVE_STRING_H */
42 #endif /* not STDC_HEADERS */
43
44 #include "common.h"
45 #include "frontend.h"
46 #include "backend.h"
47 #include "parser.h"
48
49 static char *PGNTagsStatic P((GameInfo *));
50
51
52
53 /* Parse PGN tags; returns 0 for success or error number
54  */
55 int ParsePGNTag(tag, gameInfo)
56     char *tag;
57     GameInfo *gameInfo;
58 {
59     char *name, *value, *p, *oldTags;
60     int len;
61     int success;
62
63     name = tag;
64     while (!isalpha(*name) && !isdigit(*name)) {
65         name++;
66     }
67     p = name;
68     while (*p != ' ' && *p != '\t' && *p != '\n') {
69         p++;
70     }
71     *p = NULLCHAR;
72     value = strchr(p + 1, '"') + 1;
73     p = strrchr(value, '"');
74     *p = NULLCHAR;
75
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;
88     }
89     /* Fold together the various ways of denoting White/Black rating */
90     else if ((StrCaseCmp(name, "WhiteElo")==0) ||
91              (StrCaseCmp(name, "WhiteUSCF")==0) ) {
92       success = TRUE;
93       gameInfo->whiteRating = atoi( value );
94     } else if ((StrCaseCmp(name, "BlackElo")==0) ||
95                (StrCaseCmp(name, "BlackUSCF")==0)) {
96       success = TRUE;
97       gameInfo->blackRating = atoi( value );
98     }
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;
106         else
107             gameInfo->result = GameUnfinished;
108         success = TRUE;
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 */
113         success = TRUE;
114     } else if (StrCaseCmp(name, "Variant") == 0) {
115         /* xboard-defined extension */
116         gameInfo->variant = StringToVariant(value);
117         success = TRUE;
118     } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
119         /* [AS] Out of book annotation */
120         success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
121     } else {
122         if (gameInfo->extraTags == NULL) {
123             oldTags = "";
124         } else {
125             oldTags = gameInfo->extraTags;
126         }
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;
133             success = TRUE;
134         } else {
135             success = FALSE;
136         }
137     }
138     return(success ? 0 : ENOMEM);
139 }
140
141
142
143 /* Return a static buffer with a game's data.
144  */
145 static char *PGNTagsStatic(gameInfo)
146     GameInfo *gameInfo;
147 {
148     static char buf[8192];
149     char buf1[MSG_SIZ];
150
151     buf[0] = NULLCHAR;
152
153     sprintf(buf1, "[Event \"%s\"]\n",
154             gameInfo->event ? gameInfo->event : "?");
155     strcat(buf, buf1);
156     sprintf(buf1, "[Site \"%s\"]\n",
157             gameInfo->site ? gameInfo->site : "?");
158     strcat(buf, buf1);
159     sprintf(buf1, "[Date \"%s\"]\n",
160             gameInfo->date ? gameInfo->date : "?");
161     strcat(buf, buf1);
162     sprintf(buf1, "[Round \"%s\"]\n",
163             gameInfo->round ? gameInfo->round : "-");
164     strcat(buf, buf1);
165     sprintf(buf1, "[White \"%s\"]\n",
166             gameInfo->white ? gameInfo->white : "?");
167     strcat(buf, buf1);
168     sprintf(buf1, "[Black \"%s\"]\n",
169             gameInfo->black ? gameInfo->black : "?");
170     strcat(buf, buf1);
171     sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
172     strcat(buf, buf1);
173  
174     if (gameInfo->whiteRating >= 0 ) {
175         sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
176         strcat(buf, buf1);
177     }
178     if ( gameInfo->blackRating >= 0 ) {
179         sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
180         strcat(buf, buf1);
181     }    
182     if (gameInfo->timeControl != NULL) {
183         sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
184         strcat(buf, buf1);
185     }
186     if (gameInfo->variant != VariantNormal) {
187         sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
188         strcat(buf, buf1);
189     }
190     if (gameInfo->extraTags != NULL) {
191         strcat(buf, gameInfo->extraTags);
192     }
193     return buf;
194 }
195
196
197  
198 /* Print game info
199  */
200 void PrintPGNTags(fp, gameInfo)
201      FILE *fp;
202      GameInfo *gameInfo;
203 {
204     fprintf(fp, "%s", PGNTagsStatic(gameInfo));
205 }
206
207
208 /* Return a non-static buffer with a games info.
209  */
210 char *PGNTags(gameInfo)
211     GameInfo *gameInfo;
212 {
213     return StrSave(PGNTagsStatic(gameInfo));
214 }
215
216
217 /* Returns pointer to a static string with a result.
218  */
219 char *PGNResult(result)
220      ChessMove result;
221 {
222     switch (result) {
223       case GameUnfinished:
224       default:
225         return "*";
226       case WhiteWins:
227         return "1-0";
228       case BlackWins:
229         return "0-1";
230       case GameIsDrawn:
231         return "1/2-1/2";
232     }
233 }  
234
235 /* Returns 0 for success, nonzero for error */
236 int
237 ReplaceTags(tags, gameInfo)
238      char *tags;
239      GameInfo *gameInfo;
240 {
241     ChessMove moveType;
242     int err;
243
244     ClearGameInfo(gameInfo);
245     yynewstr(tags);
246     for (;;) {
247         yyboardindex = 0;
248         moveType = (ChessMove) yylex();
249         if (moveType == (ChessMove) 0) {
250             break;
251         } else if (moveType == PGNTag) {
252             err = ParsePGNTag(yy_text, gameInfo);
253             if (err != 0) return err;
254         } 
255     }
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
259      * is a result.
260      */
261     if (gameInfo->result != GameUnfinished) {
262       if (gameInfo->resultDetails) free(gameInfo->resultDetails);
263       gameInfo->resultDetails = strdup("");
264     }
265     return 0;
266 }