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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#include "db.h"
#include <stdio.h> /* for fopen(), etc. */
#include <errno.h> /* for strerror(), etc. */
#include <string.h> /* for strncmp(), etc. */
#include <stdint.h> /* for uint32_t */
#include <stdlib.h> /* for atoi() */
#include "dict.h"
char db_err[256];
static uint64_t dbHash(const void *key)
{
return dictGenHashFunction(key,8);
}
static int dbKeyCompare(void *privdata, const void *key1, const void *key2)
{
return memcmp(key1,key2,8) == 0;
}
static void dbFree(void *privdata, void *val)
{
free(val);
}
static dictType titleDB = {
dbHash,
NULL,
NULL,
dbKeyCompare,
dbFree,
dbFree
};
/* Add a bank to the database.
*
* Returns 0 on success, -1 on error. */
int add_bank(dict *db, const char name[4], uint32_t id, dbval *data)
{
uint32_t *buf = malloc(sizeof(uint32_t)*2);
memcpy(buf,name,4);
buf[1] = id;
if (dictAdd(db, buf, data) != DICT_OK) {
sprintf(db_err, "failed to add bank to database!\n");
goto err;
}
return 0;
err:
free(buf);
return -1;
}
/* Get a bank from the database.
*
* Returns a pointer to the first value in the bank, or NULL if the bank
* doesn't exist. */
dbval *get_bank(dict *db, const char name[4], uint32_t id)
{
uint32_t buf[2];
memcpy(buf,name,4);
buf[1] = id;
return dictFetchValue(db, buf);
}
/* Function to iterate over the fields in a title bank text file. Works sort of
* like strtok(). Example:
*
* char *item = iter_field(line);
* while (item) {
* // do something with item
* item = iter_field(NULL);
* }
*/
static char *iter_field(char *str)
{
static char *ptr;
static char buf[81];
if (!str) str = ptr;
ptr = buf;
while (*str != '\x0' && ptr < (buf + 81)) {
if (ptr == (buf + 2) && buf[0] == '#' && buf[1] == '.') {
ptr = buf;
while (*str != '\x0' && *str++ != '#');
continue;
}
if (*str == ' ' || *str == '\n') {
str++;
if (ptr == buf) continue;
break;
}
*ptr++ = *str++;
}
if (ptr == buf) return NULL;
*ptr = '\x0';
ptr = str;
return buf;
}
/* Create a new database. */
dict *db_init(void)
{
dict *db = dictCreate(&titleDB, NULL);
return db;
}
/* Free a database. */
void db_free(dict *db)
{
dictRelease(db);
}
/* Load a title bank file into the database `db`.
*
* Returns 0 on success, -1 on error. */
int load_file(dict *db, const char *filename)
{
int i, index;
char *item, *s;
char line[256];
char idh[4];
uint32_t idn;
int mul;
int64_t value;
double float_value;
dbval *buf;
int buf_size;
buf_size = 10;
buf = malloc(buf_size*sizeof(dbval));
if (!buf) {
strcpy(db_err,strerror(errno));
return -1;
}
FILE *f = fopen(filename, "r");
if (!f) {
sprintf(db_err, "unable to open file '%s': %s", filename, strerror(errno));
return -1;
}
index = 0;
while (fgets(line, sizeof(line), f)) {
if (!strncmp(line,"*---",4)) {
/* Comment. */
continue;
} else if (!strncmp(line,"*.--",4)) {
/* Comment. */
continue;
} else if (!strncmp(line, "*LOG",4)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*PRI",4)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*US",3)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*KI",3)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*ANYWAY",7)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*FIN",4)) {
/* Control line which we don't care about. */
continue;
} else if (!strncmp(line, "*DO",3)) {
if (index > 0) {
/* Save previous bank. */
buf = realloc(buf,sizeof(dbval)*index);
if (add_bank(db, idh, idn, buf) == DICT_ERR) {
sprintf(db_err, "bank '%.4s' already exists in the database", idh);
goto err;
}
buf_size = 10;
buf = malloc(buf_size*sizeof(dbval));
index = 0;
}
item = iter_field(line);
item = iter_field(NULL);
strncpy(idh,item,4);
item = iter_field(NULL);
idn = atoi(item);
continue;
}
item = iter_field(line);
while (item) {
mul = 1;
if ((s = strchr(item,'*'))) {
*s = '\0';
mul = atoi(item);
item = s+1;
}
while (buf_size < (index+mul)*sizeof(dbval)) {
buf_size *= 2;
buf = realloc(buf, buf_size*sizeof(dbval));
}
if (!strncmp(item,"#x",2)) {
/* Hexadecimal input. */
value = strtol(item+2,NULL,16);
for (i = 0; i < mul; i++) buf[index++].u32 = value;
} else if (strchr(item,'E') || strchr(item,'.')) {
/* Floating point input. */
float_value = strtod(item,NULL);
for (i = 0; i < mul; i++) buf[index++].f = float_value;
} else {
/* Assume it's an integer. */
value = strtol(item,NULL,10);
for (i = 0; i < mul; i++) buf[index++].u32 = value;
}
item = iter_field(NULL);
}
}
buf = realloc(buf,sizeof(dbval)*index);
if (add_bank(db, idh, idn, buf) == DICT_ERR) {
sprintf(db_err, "bank '%.4s' already exists in the database", idh);
goto err;
}
fclose(f);
return 0;
err:
if (buf) free(buf);
fclose(f);
return -1;
}
|