changes from Alessandro Scotti from 20051129
[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 {
118         if (gameInfo->extraTags == NULL) {
119             oldTags = "";
120         } else {
121             oldTags = gameInfo->extraTags;
122         }
123         /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
124         len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
125         if ((p = (char *) malloc(len))  !=  NULL) {
126             sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
127             if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
128             gameInfo->extraTags = p;
129             success = TRUE;
130         } else {
131             success = FALSE;
132         }
133     }
134     return(success ? 0 : ENOMEM);
135 }
136
137
138
139 /* Return a static buffer with a game's data.
140  */
141 static char *PGNTagsStatic(gameInfo)
142     GameInfo *gameInfo;
143 {
144     static char buf[8192];
145     char buf1[MSG_SIZ];
146
147     buf[0] = NULLCHAR;
148
149     sprintf(buf1, "[Event \"%s\"]\n",
150             gameInfo->event ? gameInfo->event : "?");
151     strcat(buf, buf1);
152     sprintf(buf1, "[Site \"%s\"]\n",
153             gameInfo->site ? gameInfo->site : "?");
154     strcat(buf, buf1);
155     sprintf(buf1, "[Date \"%s\"]\n",
156             gameInfo->date ? gameInfo->date : "?");
157     strcat(buf, buf1);
158     sprintf(buf1, "[Round \"%s\"]\n",
159             gameInfo->round ? gameInfo->round : "-");
160     strcat(buf, buf1);
161     sprintf(buf1, "[White \"%s\"]\n",
162             gameInfo->white ? gameInfo->white : "?");
163     strcat(buf, buf1);
164     sprintf(buf1, "[Black \"%s\"]\n",
165             gameInfo->black ? gameInfo->black : "?");
166     strcat(buf, buf1);
167     sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
168     strcat(buf, buf1);
169  
170     if (gameInfo->whiteRating >= 0 ) {
171         sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
172         strcat(buf, buf1);
173     }
174     if ( gameInfo->blackRating >= 0 ) {
175         sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
176         strcat(buf, buf1);
177     }    
178     if (gameInfo->timeControl != NULL) {
179         sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
180         strcat(buf, buf1);
181     }
182     if (gameInfo->variant != VariantNormal) {
183         sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
184         strcat(buf, buf1);
185     }
186     if (gameInfo->extraTags != NULL) {
187         strcat(buf, gameInfo->extraTags);
188     }
189     return buf;
190 }
191
192
193  
194 /* Print game info
195  */
196 void PrintPGNTags(fp, gameInfo)
197      FILE *fp;
198      GameInfo *gameInfo;
199 {
200     fprintf(fp, "%s", PGNTagsStatic(gameInfo));
201 }
202
203
204 /* Return a non-static buffer with a games info.
205  */
206 char *PGNTags(gameInfo)
207     GameInfo *gameInfo;
208 {
209     return StrSave(PGNTagsStatic(gameInfo));
210 }
211
212
213 /* Returns pointer to a static string with a result.
214  */
215 char *PGNResult(result)
216      ChessMove result;
217 {
218     switch (result) {
219       case GameUnfinished:
220       default:
221         return "*";
222       case WhiteWins:
223         return "1-0";
224       case BlackWins:
225         return "0-1";
226       case GameIsDrawn:
227         return "1/2-1/2";
228     }
229 }  
230
231 /* Returns 0 for success, nonzero for error */
232 int
233 ReplaceTags(tags, gameInfo)
234      char *tags;
235      GameInfo *gameInfo;
236 {
237     ChessMove moveType;
238     int err;
239
240     ClearGameInfo(gameInfo);
241     yynewstr(tags);
242     for (;;) {
243         yyboardindex = 0;
244         moveType = (ChessMove) yylex();
245         if (moveType == (ChessMove) 0) {
246             break;
247         } else if (moveType == PGNTag) {
248             err = ParsePGNTag(yy_text, gameInfo);
249             if (err != 0) return err;
250         } 
251     }
252     /* just one problem...if there is a result in the new tags,
253      * DisplayMove() won't ever show it because ClearGameInfo() set
254      * gameInfo->resultDetails to NULL. So we must plug something in if there
255      * is a result.
256      */
257     if (gameInfo->result != GameUnfinished) {
258       if (gameInfo->resultDetails) free(gameInfo->resultDetails);
259       gameInfo->resultDetails = strdup("");
260     }
261     return 0;
262 }