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