1 /*
2 * Copyright 2006 Michael Maurer <mjmaurer@yahoo.com>,
3 * Brian Goetz <brian@quiotix.com>,
4 * Loic Dachary <loic@dachary.org>,
5 * Tim Showalter <tjs@psaux.com>
6 *
7 * This program gives you software freedom; you can copy, convey,
8 * propagate, redistribute and/or modify this program under the terms of
9 * the GNU General Public License (GPL) as published by the Free Software
10 * Foundation (FSF), either version 3 of the License, or (at your option)
11 * any later version of the GPL published by the FSF.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program in a file in the toplevel directory called "GPLv3".
20 * If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "poker_defs.h"
26 #include "mktable.h"
27
28
29 static const char *gTableName, *gFileName, *gTableType;
30 static int gTableSize;
31 static int gInUse = 0, gHeaderDone=0, gLineCount;
32 static FILE *gCFile;
33
34 void
MakeTable_begin(const char * tableName,const char * fileName,const char * tableType,int tableSize)35 MakeTable_begin(const char *tableName,
36 const char *fileName,
37 const char *tableType,
38 int tableSize) {
39 char fnBuf[128];
40
41 if (gInUse)
42 fprintf(stderr, "MakeTable_begin called before previous call to _end\n");
43 gTableName = tableName;
44 gFileName = fileName;
45 gTableType = tableType;
46 gTableSize = tableSize;
47 gInUse = 1;
48 gLineCount = 0;
49 gHeaderDone = 0;
50
51 strcpy(fnBuf, fileName);
52 strcat(fnBuf, ".c");
53 gCFile = fopen(fnBuf, "w");
54 if (!gCFile)
55 fprintf(stderr, "Could not open %s for writing\n", fnBuf);
56 else {
57 fprintf(gCFile, "#include \"poker_defs.h\"\n\n");
58 fprintf(gCFile, "/* \n * Table %s\n */\n\n", gTableName);
59 };
60 }
61
62
63 static void
outputHeader(void)64 outputHeader(void) {
65 if (gCFile) {
66 fprintf(gCFile, "%s %s[%d] = { \n", gTableType, gTableName, gTableSize);
67 };
68 gHeaderDone = 1;
69 }
70
71 void
MakeTable_comment(const char * commentString)72 MakeTable_comment(const char *commentString) {
73 if (!gInUse)
74 fprintf(stderr, "MakeTable_comment called before _begin\n");
75 else if (gHeaderDone)
76 fprintf(stderr, "MakeTable_comment called after first table entry\n");
77 else
78 if (gCFile)
79 fprintf(gCFile, "/*\n%s\n */\n\n", commentString);
80 }
81
82 void
MakeTable_extraCode(const char * codeString)83 MakeTable_extraCode(const char *codeString) {
84 if (!gInUse)
85 fprintf(stderr, "MakeTable_extraCode called before _begin\n");
86 else if (gHeaderDone)
87 fprintf(stderr, "MakeTable_extraCode called after first table entry\n");
88 else
89 if (gCFile)
90 fprintf(gCFile, "%s \n", codeString);
91 }
92
93 void
MakeTable_outputString(const char * string)94 MakeTable_outputString(const char *string) {
95 if (!gHeaderDone)
96 outputHeader();
97 ++gLineCount;
98 if (gCFile)
99 fprintf(gCFile, " %s %s\n", string,
100 (gLineCount == gTableSize ? "" : ","));
101 }
102
103 #ifdef USE_INT64
104 void
MakeTable_outputUInt64(uint64 arg)105 MakeTable_outputUInt64(uint64 arg) {
106 uint32 high, low;
107 char buf[80];
108
109 high = arg >> 32;
110 low = (uint32) arg;
111 #if defined(MSDOS)
112 sprintf(buf, " { 0x%08x%08x } ", high, low);
113 #else
114 #if defined(WIN32) && !defined(__MINGW32__)
115 sprintf(buf, " { 0x%08x%08xi64 } ", high, low);
116 #else
117 sprintf(buf, " { 0x%08x%08xLL } ", high, low);
118 #endif
119 #endif
120 MakeTable_outputString(buf);
121 }
122 #endif
123
124 void
MakeTable_outputUInt32(uint32 arg)125 MakeTable_outputUInt32(uint32 arg) {
126 char buf[80];
127 sprintf(buf, "0x%08x", arg);
128 MakeTable_outputString(buf);
129 }
130
131 void
MakeTable_outputUInt16(uint16 arg)132 MakeTable_outputUInt16(uint16 arg) {
133 char buf[80];
134 sprintf(buf, "0x%04x", arg);
135 MakeTable_outputString(buf);
136 }
137
138 void
MakeTable_outputUInt8(uint8 arg)139 MakeTable_outputUInt8(uint8 arg) {
140 char buf[80];
141 sprintf(buf, "0x%02x", arg);
142 MakeTable_outputString(buf);
143 }
144
145 void
MakeTable_end(void)146 MakeTable_end(void) {
147 if (gCFile)
148 fprintf(gCFile, "};\n");
149 fclose(gCFile);
150
151 gInUse = 0;
152 }
153
154
155