File: sgfgen.c

package info (click to toggle)
gnugo 3.8-9
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 17,412 kB
  • ctags: 4,302
  • sloc: ansic: 56,445; perl: 3,771; lisp: 2,804; sh: 722; makefile: 706; python: 682; awk: 113; sed: 22
file content (99 lines) | stat: -rw-r--r-- 3,330 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see       *
 * http://www.gnu.org/software/gnugo/ for more information.          *
 *                                                                   *
 * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,   *
 * 2008 and 2009 by the Free Software Foundation.                    *
 *                                                                   *
 * 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 - version 3 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 in file COPYING 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., 51 Franklin Street, Fifth Floor,       *
 * Boston, MA 02111, USA.                                            *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * A little program to convert the SGF Spec into something usable in a
 * parser- basically, the two letters moved into a 'short'
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 128

static short str2short(char *str);

/*
 * combine two characters into a short.
 */

static short
str2short(char *str)
{
  return (str[0] | str[1] << 8);
}

int 
main(void)
{
  char instring[MAX_LINE];
  char sgf_tag[MAX_LINE];
  char comment[MAX_LINE];
  int i;

  while (fgets(instring, MAX_LINE, stdin)) {
    i = strlen(instring) - 1;
      
    /* Remove trailing newline and spaces */
    while (i >= 0 
	   && (instring[i] == ' ' || instring[i] == 10))
      instring[i--] = 0;
      
    /* Blank lines stay blank lines */
    if (strcmp(instring, "") == 0) {
      fprintf(stdout, "\n");
      continue;
    }
      
    sscanf(instring, "%4s %75s", sgf_tag, comment);
    strncpy(sgf_tag, instring, 4);
    if (strlen(instring) > 4) 
      strncpy(comment, instring+4, MAX_LINE-4);
    else
      strcpy(comment, "");

    /* outdated and modified notations. */
    /* just shift everything over */
    if (sgf_tag[0] == '*' || sgf_tag[0] == '!') {
      sgf_tag[0] = sgf_tag[1];
      sgf_tag[1] = sgf_tag[2];
      sgf_tag[2] = sgf_tag[3];
    }
      
    /* If its not a real tag, just take it as a comment. */
    if (sgf_tag[0] < 'A' || sgf_tag[0] > 'Z') {
      fprintf(stdout, "/* %s */\n", instring);
      continue;
    }
    else {
      /* otherwise, write the tag and value to the file */
      if (strlen(comment))
	fprintf(stdout, "     /* %s */\n", comment);
      fprintf(stdout, "#define SGF%s %5d\n", sgf_tag, str2short(sgf_tag));
    }
  }
  
  /* Needs 0 exit status or else make fails. */
  return EXIT_SUCCESS;
}