File: mktable.c

package info (click to toggle)
poker-eval 134.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,544 kB
  • ctags: 1,587
  • sloc: ansic: 15,340; sh: 8,928; cs: 785; makefile: 276
file content (153 lines) | stat: -rw-r--r-- 3,756 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 *  Copyright 2006 Michael Maurer <mjmaurer@yahoo.com>, 
 *                 Brian Goetz <brian@quiotix.com>, 
 *                 Loic Dachary <loic@dachary.org>, 
 *                 Tim Showalter <tjs@psaux.com>
 *
 *  This package 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 2 dated June, 1991.
 *
 *  This package 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 package; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */
#include <stdio.h>
#include <string.h>

#include "poker_defs.h"
#include "mktable.h"


static const char *gTableName, *gFileName, *gTableType;
static int gTableSize;
static int gInUse = 0, gHeaderDone=0, gLineCount;
static FILE *gCFile;

void
MakeTable_begin(const char *tableName,
                const char *fileName, 
                const char *tableType,
                int tableSize) {
  char fnBuf[128];

  if (gInUse) 
    fprintf(stderr, "MakeTable_begin called before previous call to _end\n");
  gTableName = tableName;
  gFileName  = fileName;
  gTableType = tableType;
  gTableSize = tableSize;
  gInUse = 1;
  gLineCount = 0;
  gHeaderDone = 0;

  strcpy(fnBuf, fileName);
  strcat(fnBuf, ".c");
  gCFile = fopen(fnBuf, "w");
  if (!gCFile) 
    fprintf(stderr, "Could not open %s for writing\n", fnBuf);
  else {
    fprintf(gCFile, "#include \"poker_defs.h\"\n\n");
    fprintf(gCFile, "/* \n * Table %s\n */\n\n", gTableName);
  };
}


static void 
outputHeader(void) {
  if (gCFile) {
    fprintf(gCFile, "%s %s[%d] = { \n", gTableType, gTableName, gTableSize);
  };
  gHeaderDone = 1;
}

void
MakeTable_comment(const char *commentString) {
  if (!gInUse) 
    fprintf(stderr, "MakeTable_comment called before _begin\n");
  else if (gHeaderDone) 
    fprintf(stderr, "MakeTable_comment called after first table entry\n");
  else 
    if (gCFile) 
      fprintf(gCFile, "/*\n%s\n */\n\n", commentString);
}

void
MakeTable_extraCode(const char *codeString) {
  if (!gInUse) 
    fprintf(stderr, "MakeTable_extraCode called before _begin\n");
  else if (gHeaderDone) 
    fprintf(stderr, "MakeTable_extraCode called after first table entry\n");
  else 
    if (gCFile) 
      fprintf(gCFile, "%s \n", codeString);
}

void 
MakeTable_outputString(const char *string) {
  if (!gHeaderDone)
    outputHeader();
  ++gLineCount;
  if (gCFile)
    fprintf(gCFile, "     %s %s\n", string,
            (gLineCount == gTableSize ? "" : ","));
}

#ifdef USE_INT64
void 
MakeTable_outputUInt64(uint64 arg) {
  uint32 high, low;
  char buf[80];

  high = arg >> 32;
  low  = (uint32) arg;
#if defined(MSDOS)
  sprintf(buf, " { 0x%08x%08x } ", high, low);
#else
#if defined(WIN32) && !defined(__MINGW32__)
  sprintf(buf, " { 0x%08x%08xi64 } ", high, low);
#else
  sprintf(buf, " { 0x%08x%08xLL } ", high, low);
#endif
#endif
  MakeTable_outputString(buf);
}
#endif

void 
MakeTable_outputUInt32(uint32 arg) {
  char buf[80];
  sprintf(buf, "0x%08x", arg);
  MakeTable_outputString(buf);
}

void 
MakeTable_outputUInt16(uint16 arg) {
  char buf[80];
  sprintf(buf, "0x%04x", arg);
  MakeTable_outputString(buf);
}

void 
MakeTable_outputUInt8(uint8 arg) {
  char buf[80];
  sprintf(buf, "0x%02x", arg);
  MakeTable_outputString(buf);
}

void 
MakeTable_end(void) {
  if (gCFile) 
    fprintf(gCFile, "};\n");
  fclose(gCFile);

  gInUse = 0;
}