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