changes from H.G. Muller; version 4.3.13
[xboard.git] / pgntags.c
index 6317c8b..f21bccc 100644 (file)
--- a/pgntags.c
+++ b/pgntags.c
-/*
- * pgntags.c -- Functions to manage PGN tags
- * XBoard $Id: pgntags.c,v 2.1 2003/10/27 19:21:00 mann Exp $
- *
- * Copyright 1995 Free Software Foundation, Inc.
- *
- * ------------------------------------------------------------------------
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
- * ------------------------------------------------------------------------
- *
- * This file could well be a part of backend.c, but I prefer it this
- * way.
- */
-
-#include "config.h"
-
-#include <stdio.h>
-#include <errno.h>
-#include <ctype.h>
-#if STDC_HEADERS
-# include <stdlib.h>
-# include <string.h>
-#else /* not STDC_HEADERS */
-# if HAVE_STRING_H
-#  include <string.h>
-# else /* not HAVE_STRING_H */
-#  include <strings.h>
-# endif /* not HAVE_STRING_H */
-#endif /* not STDC_HEADERS */
-
-#include "common.h"
-#include "frontend.h"
-#include "backend.h"
-#include "parser.h"
-
-static char *PGNTagsStatic P((GameInfo *));
-
-
-
-/* Parse PGN tags; returns 0 for success or error number
- */
-int ParsePGNTag(tag, gameInfo)
-    char *tag;
-    GameInfo *gameInfo;
-{
-    char *name, *value, *p, *oldTags;
-    int len;
-    int success;
-
-    name = tag;
-    while (!isalpha(*name) && !isdigit(*name)) {
-       name++;
-    }
-    p = name;
-    while (*p != ' ' && *p != '\t' && *p != '\n') {
-       p++;
-    }
-    *p = NULLCHAR;
-    value = strchr(p + 1, '"') + 1;
-    p = strrchr(value, '"');
-    *p = NULLCHAR;
-
-    if (StrCaseCmp(name, "Event") == 0) {
-       success = StrSavePtr(value, &gameInfo->event) != NULL;
-    } else if (StrCaseCmp(name, "Site") == 0) {
-       success = StrSavePtr(value, &gameInfo->site) != NULL;
-    } else if (StrCaseCmp(name, "Date") == 0) {
-       success = StrSavePtr(value, &gameInfo->date) != NULL;
-    } else if (StrCaseCmp(name, "Round") == 0) {
-       success = StrSavePtr(value, &gameInfo->round) != NULL;
-    } else if (StrCaseCmp(name, "White") == 0) {
-       success = StrSavePtr(value, &gameInfo->white) != NULL;
-    } else if (StrCaseCmp(name, "Black") == 0) {
-       success = StrSavePtr(value, &gameInfo->black) != NULL;
-    }
-    /* Fold together the various ways of denoting White/Black rating */
-    else if ((StrCaseCmp(name, "WhiteElo")==0) ||
-            (StrCaseCmp(name, "WhiteUSCF")==0) ) {
-      success = TRUE;
-      gameInfo->whiteRating = atoi( value );
-    } else if ((StrCaseCmp(name, "BlackElo")==0) ||
-              (StrCaseCmp(name, "BlackUSCF")==0)) {
-      success = TRUE;
-      gameInfo->blackRating = atoi( value );
-    }
-    else if (StrCaseCmp(name, "Result") == 0) {
-       if (strcmp(value, "1-0") == 0)
-           gameInfo->result = WhiteWins;
-       else if (strcmp(value, "0-1") == 0)
-           gameInfo->result = BlackWins;
-       else if (strcmp(value, "1/2-1/2") == 0)
-           gameInfo->result = GameIsDrawn;
-       else
-           gameInfo->result = GameUnfinished;
-       success = TRUE;
-    } else if (StrCaseCmp(name, "FEN") == 0) {
-       success = StrSavePtr(value, &gameInfo->fen) != NULL;
-    } else if (StrCaseCmp(name, "SetUp") == 0) {
-       /* ignore on input; presence of FEN governs */
-       success = TRUE;
-    } else if (StrCaseCmp(name, "Variant") == 0) {
-        /* xboard-defined extension */
-        gameInfo->variant = StringToVariant(value);
-       success = TRUE;
-    } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
-        /* [AS] Out of book annotation */
-        success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
-    } else {
-       if (gameInfo->extraTags == NULL) {
-           oldTags = "";
-       } else {
-           oldTags = gameInfo->extraTags;
-       }
-       /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
-       len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
-       if ((p = (char *) malloc(len))  !=  NULL) {
-           sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
-           if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
-           gameInfo->extraTags = p;
-           success = TRUE;
-       } else {
-           success = FALSE;
-       }
-    }
-    return(success ? 0 : ENOMEM);
-}
-
-
-
-/* Return a static buffer with a game's data.
- */
-static char *PGNTagsStatic(gameInfo)
-    GameInfo *gameInfo;
-{
-    static char buf[8192];
-    char buf1[MSG_SIZ];
-
-    buf[0] = NULLCHAR;
-
-    sprintf(buf1, "[Event \"%s\"]\n",
-           gameInfo->event ? gameInfo->event : "?");
-    strcat(buf, buf1);
-    sprintf(buf1, "[Site \"%s\"]\n",
-           gameInfo->site ? gameInfo->site : "?");
-    strcat(buf, buf1);
-    sprintf(buf1, "[Date \"%s\"]\n",
-           gameInfo->date ? gameInfo->date : "?");
-    strcat(buf, buf1);
-    sprintf(buf1, "[Round \"%s\"]\n",
-           gameInfo->round ? gameInfo->round : "-");
-    strcat(buf, buf1);
-    sprintf(buf1, "[White \"%s\"]\n",
-           gameInfo->white ? gameInfo->white : "?");
-    strcat(buf, buf1);
-    sprintf(buf1, "[Black \"%s\"]\n",
-           gameInfo->black ? gameInfo->black : "?");
-    strcat(buf, buf1);
-    sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
-    strcat(buf, buf1);
-    if (gameInfo->whiteRating >= 0 ) {
-       sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );
-       strcat(buf, buf1);
-    }
-    if ( gameInfo->blackRating >= 0 ) {
-       sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );
-       strcat(buf, buf1);
-    }    
-    if (gameInfo->timeControl != NULL) {
-       sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
-       strcat(buf, buf1);
-    }
-    if (gameInfo->variant != VariantNormal) {
-        sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
-       strcat(buf, buf1);
-    }
-    if (gameInfo->extraTags != NULL) {
-       strcat(buf, gameInfo->extraTags);
-    }
-    return buf;
-}
-
-
-/* Print game info
- */
-void PrintPGNTags(fp, gameInfo)
-     FILE *fp;
-     GameInfo *gameInfo;
-{
-    fprintf(fp, "%s", PGNTagsStatic(gameInfo));
-}
-
-
-/* Return a non-static buffer with a games info.
- */
-char *PGNTags(gameInfo)
-    GameInfo *gameInfo;
-{
-    return StrSave(PGNTagsStatic(gameInfo));
-}
-
-
-/* Returns pointer to a static string with a result.
- */
-char *PGNResult(result)
-     ChessMove result;
-{
-    switch (result) {
-      case GameUnfinished:
-      default:
-       return "*";
-      case WhiteWins:
-       return "1-0";
-      case BlackWins:
-       return "0-1";
-      case GameIsDrawn:
-       return "1/2-1/2";
-    }
-}  
-
-/* Returns 0 for success, nonzero for error */
-int
-ReplaceTags(tags, gameInfo)
-     char *tags;
-     GameInfo *gameInfo;
-{
-    ChessMove moveType;
-    int err;
-
-    ClearGameInfo(gameInfo);
-    yynewstr(tags);
-    for (;;) {
-       yyboardindex = 0;
-       moveType = (ChessMove) yylex();
-       if (moveType == (ChessMove) 0) {
-           break;
-       } else if (moveType == PGNTag) {
-           err = ParsePGNTag(yy_text, gameInfo);
-           if (err != 0) return err;
-       } 
-    }
-    /* just one problem...if there is a result in the new tags,
-     * DisplayMove() won't ever show it because ClearGameInfo() set
-     * gameInfo->resultDetails to NULL. So we must plug something in if there
-     * is a result.
-     */
-    if (gameInfo->result != GameUnfinished) {
-      if (gameInfo->resultDetails) free(gameInfo->resultDetails);
-      gameInfo->resultDetails = strdup("");
-    }
-    return 0;
-}
+/*\r
+ * pgntags.c -- Functions to manage PGN tags\r
+ * XBoard $Id: pgntags.c,v 2.1 2003/10/27 19:21:00 mann Exp $\r
+ *\r
+ * Copyright 1995 Free Software Foundation, Inc.\r
+ *\r
+ * ------------------------------------------------------------------------\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.\r
+ * ------------------------------------------------------------------------\r
+ *\r
+ * This file could well be a part of backend.c, but I prefer it this\r
+ * way.\r
+ */\r
+\r
+#include "config.h"\r
+\r
+#include <stdio.h>\r
+#include <errno.h>\r
+#include <ctype.h>\r
+#if STDC_HEADERS\r
+# include <stdlib.h>\r
+# include <string.h>\r
+#else /* not STDC_HEADERS */\r
+# if HAVE_STRING_H\r
+#  include <string.h>\r
+# else /* not HAVE_STRING_H */\r
+#  include <strings.h>\r
+# endif /* not HAVE_STRING_H */\r
+#endif /* not STDC_HEADERS */\r
+\r
+#include "common.h"\r
+#include "frontend.h"\r
+#include "backend.h"\r
+#include "parser.h"\r
+\r
+static char *PGNTagsStatic P((GameInfo *));\r
+\r
+\r
+\r
+/* Parse PGN tags; returns 0 for success or error number\r
+ */\r
+int ParsePGNTag(tag, gameInfo)\r
+    char *tag;\r
+    GameInfo *gameInfo;\r
+{\r
+    char *name, *value, *p, *oldTags;\r
+    int len;\r
+    int success;\r
+\r
+    name = tag;\r
+    while (!isalpha(*name) && !isdigit(*name)) {\r
+       name++;\r
+    }\r
+    p = name;\r
+    while (*p != ' ' && *p != '\t' && *p != '\n') {\r
+       p++;\r
+    }\r
+    *p = NULLCHAR;\r
+    value = strchr(p + 1, '"') + 1;\r
+    p = strrchr(value, '"');\r
+    *p = NULLCHAR;\r
+\r
+    if (StrCaseCmp(name, "Event") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->event) != NULL;\r
+    } else if (StrCaseCmp(name, "Site") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->site) != NULL;\r
+    } else if (StrCaseCmp(name, "Date") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->date) != NULL;\r
+    } else if (StrCaseCmp(name, "Round") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->round) != NULL;\r
+    } else if (StrCaseCmp(name, "White") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->white) != NULL;\r
+    } else if (StrCaseCmp(name, "Black") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->black) != NULL;\r
+    }\r
+    /* Fold together the various ways of denoting White/Black rating */\r
+    else if ((StrCaseCmp(name, "WhiteElo")==0) ||\r
+            (StrCaseCmp(name, "WhiteUSCF")==0) ) {\r
+      success = TRUE;\r
+      gameInfo->whiteRating = atoi( value );\r
+    } else if ((StrCaseCmp(name, "BlackElo")==0) ||\r
+              (StrCaseCmp(name, "BlackUSCF")==0)) {\r
+      success = TRUE;\r
+      gameInfo->blackRating = atoi( value );\r
+    }\r
+    else if (StrCaseCmp(name, "Result") == 0) {\r
+       if (strcmp(value, "1-0") == 0)\r
+           gameInfo->result = WhiteWins;\r
+       else if (strcmp(value, "0-1") == 0)\r
+           gameInfo->result = BlackWins;\r
+       else if (strcmp(value, "1/2-1/2") == 0)\r
+           gameInfo->result = GameIsDrawn;\r
+       else\r
+           gameInfo->result = GameUnfinished;\r
+       success = TRUE;\r
+    } else if (StrCaseCmp(name, "FEN") == 0) {\r
+       success = StrSavePtr(value, &gameInfo->fen) != NULL;\r
+    } else if (StrCaseCmp(name, "SetUp") == 0) {\r
+       /* ignore on input; presence of FEN governs */\r
+       success = TRUE;\r
+    } else if (StrCaseCmp(name, "Variant") == 0) {\r
+        /* xboard-defined extension */\r
+        gameInfo->variant = StringToVariant(value);\r
+       success = TRUE;\r
+    } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {\r
+        /* [AS] Out of book annotation */\r
+        success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;\r
+    } else {\r
+       if (gameInfo->extraTags == NULL) {\r
+           oldTags = "";\r
+       } else {\r
+           oldTags = gameInfo->extraTags;\r
+       }\r
+       /* Buffer size includes 7 bytes of space for [ ""]\n\0 */\r
+       len = strlen(oldTags) + strlen(value) + strlen(name) + 7;\r
+       if ((p = (char *) malloc(len))  !=  NULL) {\r
+           sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);\r
+           if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);\r
+           gameInfo->extraTags = p;\r
+           success = TRUE;\r
+       } else {\r
+           success = FALSE;\r
+       }\r
+    }\r
+    return(success ? 0 : ENOMEM);\r
+}\r
+\r
+\r
+\r
+/* Return a static buffer with a game's data.\r
+ */\r
+static char *PGNTagsStatic(gameInfo)\r
+    GameInfo *gameInfo;\r
+{\r
+    static char buf[8192];\r
+    char buf1[MSG_SIZ];\r
+\r
+    buf[0] = NULLCHAR;\r
+\r
+    sprintf(buf1, "[Event \"%s\"]\n",\r
+           gameInfo->event ? gameInfo->event : "?");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[Site \"%s\"]\n",\r
+           gameInfo->site ? gameInfo->site : "?");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[Date \"%s\"]\n",\r
+           gameInfo->date ? gameInfo->date : "?");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[Round \"%s\"]\n",\r
+           gameInfo->round ? gameInfo->round : "-");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[White \"%s\"]\n",\r
+           gameInfo->white ? gameInfo->white : "?");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[Black \"%s\"]\n",\r
+           gameInfo->black ? gameInfo->black : "?");\r
+    strcat(buf, buf1);\r
+    sprintf(buf1, "[Result \"%s\"]\n", PGNResult(gameInfo->result));\r
+    strcat(buf, buf1);\r
\r
+    if (gameInfo->whiteRating >= 0 ) {\r
+       sprintf(buf1, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating );\r
+       strcat(buf, buf1);\r
+    }\r
+    if ( gameInfo->blackRating >= 0 ) {\r
+       sprintf(buf1, "[BlackElo \"%d\"]\n", gameInfo->blackRating );\r
+       strcat(buf, buf1);\r
+    }    \r
+    if (gameInfo->timeControl != NULL) {\r
+       sprintf(buf1, "[TimeControl \"%s\"]\n", gameInfo->timeControl);\r
+       strcat(buf, buf1);\r
+    }\r
+    if (gameInfo->variant != VariantNormal) {\r
+        sprintf(buf1, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));\r
+       strcat(buf, buf1);\r
+    }\r
+    if (gameInfo->extraTags != NULL) {\r
+       strcat(buf, gameInfo->extraTags);\r
+    }\r
+    return buf;\r
+}\r
+\r
+\r
\r
+/* Print game info\r
+ */\r
+void PrintPGNTags(fp, gameInfo)\r
+     FILE *fp;\r
+     GameInfo *gameInfo;\r
+{\r
+    fprintf(fp, "%s", PGNTagsStatic(gameInfo));\r
+}\r
+\r
+\r
+/* Return a non-static buffer with a games info.\r
+ */\r
+char *PGNTags(gameInfo)\r
+    GameInfo *gameInfo;\r
+{\r
+    return StrSave(PGNTagsStatic(gameInfo));\r
+}\r
+\r
+\r
+/* Returns pointer to a static string with a result.\r
+ */\r
+char *PGNResult(result)\r
+     ChessMove result;\r
+{\r
+    switch (result) {\r
+      case GameUnfinished:\r
+      default:\r
+       return "*";\r
+      case WhiteWins:\r
+       return "1-0";\r
+      case BlackWins:\r
+       return "0-1";\r
+      case GameIsDrawn:\r
+       return "1/2-1/2";\r
+    }\r
+}  \r
+\r
+/* Returns 0 for success, nonzero for error */\r
+int\r
+ReplaceTags(tags, gameInfo)\r
+     char *tags;\r
+     GameInfo *gameInfo;\r
+{\r
+    ChessMove moveType;\r
+    int err;\r
+\r
+    ClearGameInfo(gameInfo);\r
+    yynewstr(tags);\r
+    for (;;) {\r
+       yyboardindex = 0;\r
+       moveType = (ChessMove) yylex();\r
+       if (moveType == (ChessMove) 0) {\r
+           break;\r
+       } else if (moveType == PGNTag) {\r
+           err = ParsePGNTag(yy_text, gameInfo);\r
+           if (err != 0) return err;\r
+       } \r
+    }\r
+    /* just one problem...if there is a result in the new tags,\r
+     * DisplayMove() won't ever show it because ClearGameInfo() set\r
+     * gameInfo->resultDetails to NULL. So we must plug something in if there\r
+     * is a result.\r
+     */\r
+    if (gameInfo->result != GameUnfinished) {\r
+      if (gameInfo->resultDetails) free(gameInfo->resultDetails);\r
+      gameInfo->resultDetails = strdup("");\r
+    }\r
+    return 0;\r
+}\r