| 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ |
| 2 | * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see * |
| 3 | * http://www.gnu.org/software/gnugo/ for more information. * |
| 4 | * * |
| 5 | * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
| 6 | * 2008 and 2009 by the Free Software Foundation. * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or * |
| 9 | * modify it under the terms of the GNU General Public License as * |
| 10 | * published by the Free Software Foundation - version 3 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 in file COPYING for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public * |
| 19 | * License along with this program; if not, write to the Free * |
| 20 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 21 | * Boston, MA 02111, USA. * |
| 22 | \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 23 | |
| 24 | /* |
| 25 | * A little program to convert the SGF Spec into something usable in a |
| 26 | * parser- basically, the two letters moved into a 'short' |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #define MAX_LINE 128 |
| 34 | |
| 35 | static short str2short(char *str); |
| 36 | |
| 37 | /* |
| 38 | * combine two characters into a short. |
| 39 | */ |
| 40 | |
| 41 | static short |
| 42 | str2short(char *str) |
| 43 | { |
| 44 | return (str[0] | str[1] << 8); |
| 45 | } |
| 46 | |
| 47 | int |
| 48 | main(void) |
| 49 | { |
| 50 | char instring[MAX_LINE]; |
| 51 | char sgf_tag[MAX_LINE]; |
| 52 | char comment[MAX_LINE]; |
| 53 | int i; |
| 54 | |
| 55 | while (fgets(instring, MAX_LINE, stdin)) { |
| 56 | i = strlen(instring) - 1; |
| 57 | |
| 58 | /* Remove trailing newline and spaces */ |
| 59 | while (i >= 0 |
| 60 | && (instring[i] == ' ' || instring[i] == 10)) |
| 61 | instring[i--] = 0; |
| 62 | |
| 63 | /* Blank lines stay blank lines */ |
| 64 | if (strcmp(instring, "") == 0) { |
| 65 | fprintf(stdout, "\n"); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | sscanf(instring, "%4s %75s", sgf_tag, comment); |
| 70 | strncpy(sgf_tag, instring, 4); |
| 71 | if (strlen(instring) > 4) |
| 72 | strncpy(comment, instring+4, MAX_LINE-4); |
| 73 | else |
| 74 | strcpy(comment, ""); |
| 75 | |
| 76 | /* outdated and modified notations. */ |
| 77 | /* just shift everything over */ |
| 78 | if (sgf_tag[0] == '*' || sgf_tag[0] == '!') { |
| 79 | sgf_tag[0] = sgf_tag[1]; |
| 80 | sgf_tag[1] = sgf_tag[2]; |
| 81 | sgf_tag[2] = sgf_tag[3]; |
| 82 | } |
| 83 | |
| 84 | /* If its not a real tag, just take it as a comment. */ |
| 85 | if (sgf_tag[0] < 'A' || sgf_tag[0] > 'Z') { |
| 86 | fprintf(stdout, "/* %s */\n", instring); |
| 87 | continue; |
| 88 | } |
| 89 | else { |
| 90 | /* otherwise, write the tag and value to the file */ |
| 91 | if (strlen(comment)) |
| 92 | fprintf(stdout, " /* %s */\n", comment); |
| 93 | fprintf(stdout, "#define SGF%s %5d\n", sgf_tag, str2short(sgf_tag)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Needs 0 exit status or else make fails. */ |
| 98 | return EXIT_SUCCESS; |
| 99 | } |