Commit | Line | Data |
---|---|---|
7eeb782e AT |
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 | /* Compile Monte Carlo local pattern database. This produces mcpat.c. */ | |
25 | ||
26 | /* See also mc_*.db and engine/montecarlo.c. */ | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | ||
32 | #include "liberty.h" | |
33 | ||
34 | /* Trim leading path, a possible mc_ prefix, and a possible .db suffix | |
35 | * from the filename. The caller has to free the returned pointer when | |
36 | * it's no longer needed. | |
37 | * | |
38 | * FIXME: This code is quite ugly and should be possible to clean up. | |
39 | */ | |
40 | static char * | |
41 | copy_and_trim_name(const char *filename) | |
42 | { | |
43 | int name_length = strlen(filename); | |
44 | char *name = malloc(name_length + 1); | |
45 | char *start = name; | |
46 | char *p; | |
47 | char *name2; | |
48 | ||
49 | strcpy(name, filename); | |
50 | ||
51 | p = strrchr(name, '/'); | |
52 | if (p) { | |
53 | p++; | |
54 | name_length -= (p - name); | |
55 | start = p; | |
56 | } | |
57 | ||
58 | if (strncmp(start, "mc_", 3) == 0) { | |
59 | start += 3; | |
60 | name_length -= 3; | |
61 | } | |
62 | ||
63 | if (strncmp(start + name_length - 3, ".db", 3) == 0) | |
64 | start[name_length - 3] = '\0'; | |
65 | ||
66 | name2 = malloc(name_length + 1); | |
67 | strcpy(name2, start); | |
68 | free(name); | |
69 | ||
70 | return name2; | |
71 | } | |
72 | ||
73 | int | |
74 | main(int argc, char *argv[]) | |
75 | { | |
76 | int N = mc_get_size_of_pattern_values_table(); | |
77 | unsigned int *values; | |
78 | int i; | |
79 | int k; | |
80 | char *name; | |
81 | ||
82 | if (argc < 2) { | |
83 | fprintf(stderr, "Usage: ...\n"); | |
84 | exit(EXIT_FAILURE); | |
85 | } | |
86 | ||
87 | printf("\ | |
88 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\\\n\ | |
89 | * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *\n\ | |
90 | * http://www.gnu.org/software/gnugo/ for more information. *\n\ | |
91 | * *\n\ | |
92 | * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *\n\ | |
93 | * 2008 and 2009 by the Free Software Foundation. *\n\ | |
94 | * *\n\ | |
95 | * This program is free software; you can redistribute it and/or *\n\ | |
96 | * modify it under the terms of the GNU General Public License as *\n\ | |
97 | * published by the Free Software Foundation - version 3 *\n\ | |
98 | * or (at your option) any later version *\n\ | |
99 | * *\n\ | |
100 | * This program is distributed in the hope that it will be useful, *\n\ | |
101 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *\n\ | |
102 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *\n\ | |
103 | * GNU General Public License in file COPYING for more details. *\n\ | |
104 | * *\n\ | |
105 | * You should have received a copy of the GNU General Public *\n\ | |
106 | * License along with this program; if not, write to the Free *\n\ | |
107 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *\n\ | |
108 | * Boston, MA 02111, USA. *\n\ | |
109 | \\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */\n\n"); | |
110 | ||
111 | printf("/* This file is automatically generated by mkmcpat. Do not\n"); | |
112 | printf(" * edit it directly. Instead, edit the pattern databases\n"); | |
113 | printf(" * mc_*.db.\n"); | |
114 | printf(" */\n\n"); | |
115 | printf("#include <stdio.h> /* for NULL */\n"); | |
116 | printf("#include \"liberty.h\"\n\n"); | |
117 | printf("#include \"patterns.h\"\n\n"); | |
118 | ||
119 | values = malloc(N * sizeof(*values)); | |
120 | ||
121 | for (i = 1; i < argc; i++) { | |
122 | if (!mc_load_patterns_from_db(argv[i], values)) | |
123 | exit(EXIT_FAILURE); | |
124 | ||
125 | name = copy_and_trim_name(argv[i]); | |
126 | ||
127 | printf("static const unsigned int %s_values[] = {\n", name); | |
128 | for (k = 0; k < N; k++) { | |
129 | printf("%u, ", values[k]); | |
130 | if (k % 16 == 15) | |
131 | printf("\n"); | |
132 | } | |
133 | printf("\n};\n\n"); | |
134 | ||
135 | free(name); | |
136 | } | |
137 | ||
138 | printf("struct mc_pattern_database mc_pattern_databases[] = {\n"); | |
139 | for (i = 1; i < argc; i++) { | |
140 | name = copy_and_trim_name(argv[i]); | |
141 | printf(" {\"%s\", %s_values},\n", name, name); | |
142 | free(name); | |
143 | } | |
144 | printf(" {NULL, NULL}};\n"); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Local Variables: | |
151 | * tab-width: 8 | |
152 | * c-basic-offset: 2 | |
153 | * End: | |
154 | */ |