forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_merge.c
304 lines (204 loc) · 5.67 KB
/
book_merge.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// book_merge.c
// includes
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "book_merge.h"
#include "util.h"
// types
typedef struct {
FILE * file;
int size;
} book_t;
typedef struct {
uint64 key;
uint16 move;
uint16 count;
uint16 n;
uint16 sum;
} entry_t;
// variables
static book_t In1[1];
static book_t In2[1];
static book_t Out[1];
// prototypes
static void book_clear (book_t * book);
static void book_open (book_t * book, const char file_name[], const char mode[]);
static void book_close (book_t * book);
static bool read_entry (book_t * book, entry_t * entry, int n);
static void write_entry (book_t * book, const entry_t * entry);
static uint64 read_integer (FILE * file, int size);
static void write_integer (FILE * file, int size, uint64 n);
// functions
// book_merge()
void book_merge(int argc, char * argv[]) {
int i;
const char * in_file_1;
const char * in_file_2;
const char * out_file;
int i1, i2;
bool b1, b2;
entry_t e1[1], e2[1];
int skip;
in_file_1 = NULL;
my_string_clear(&in_file_1);
in_file_2 = NULL;
my_string_clear(&in_file_2);
out_file = NULL;
my_string_set(&out_file,"out.bin");
for (i = 1; i < argc; i++) {
if (FALSE) {
} else if (my_string_equal(argv[i],"merge-book")) {
// skip
} else if (my_string_equal(argv[i],"-in1")) {
i++;
if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
my_string_set(&in_file_1,argv[i]);
} else if (my_string_equal(argv[i],"-in2")) {
i++;
if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
my_string_set(&in_file_2,argv[i]);
} else if (my_string_equal(argv[i],"-out")) {
i++;
if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
my_string_set(&out_file,argv[i]);
} else {
my_fatal("book_merge(): unknown option \"%s\"\n",argv[i]);
}
}
book_clear(In1);
book_clear(In2);
book_clear(Out);
book_open(In1,in_file_1,"rb");
book_open(In2,in_file_2,"rb");
book_open(Out,out_file,"wb");
skip = 0;
i1 = 0;
i2 = 0;
while (TRUE) {
b1 = read_entry(In1,e1,i1);
b2 = read_entry(In2,e2,i2);
if (FALSE) {
} else if (!b1 && !b2) {
break;
} else if (b1 && !b2) {
write_entry(Out,e1);
i1++;
} else if (b2 && !b1) {
write_entry(Out,e2);
i2++;
} else {
ASSERT(b1);
ASSERT(b2);
if (FALSE) {
} else if (e1->key < e2->key) {
write_entry(Out,e1);
i1++;
} else if (e1->key > e2->key) {
write_entry(Out,e2);
i2++;
} else {
ASSERT(e1->key==e2->key);
skip++;
i2++;
}
}
}
book_close(In1);
book_close(In2);
book_close(Out);
if (skip != 0) {
printf("skipped %d entr%s.\n",skip,(skip>1)?"ies":"y");
}
printf("done!\n");
}
// book_clear()
static void book_clear(book_t * book) {
ASSERT(book!=NULL);
book->file = NULL;
book->size = 0;
}
// book_open()
static void book_open(book_t * book, const char file_name[], const char mode[]) {
ASSERT(book!=NULL);
ASSERT(file_name!=NULL);
ASSERT(mode!=NULL);
book->file = fopen(file_name,mode);
if (book->file == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno));
if (fseek(book->file,0,SEEK_END) == -1) {
my_fatal("book_open(): fseek(): %s\n",strerror(errno));
}
book->size = ftell(book->file) / 16;
}
// book_close()
static void book_close(book_t * book) {
ASSERT(book!=NULL);
if (fclose(book->file) == EOF) {
my_fatal("book_close(): fclose(): %s\n",strerror(errno));
}
}
// read_entry()
static bool read_entry(book_t * book, entry_t * entry, int n) {
ASSERT(book!=NULL);
ASSERT(entry!=NULL);
if (n < 0 || n >= book->size) return FALSE;
ASSERT(n>=0&&n<book->size);
if (fseek(book->file,n*16,SEEK_SET) == -1) {
my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
}
entry->key = read_integer(book->file,8);
entry->move = read_integer(book->file,2);
entry->count = read_integer(book->file,2);
entry->n = read_integer(book->file,2);
entry->sum = read_integer(book->file,2);
return TRUE;
}
// write_entry()
static void write_entry(book_t * book, const entry_t * entry) {
ASSERT(book!=NULL);
ASSERT(entry!=NULL);
write_integer(book->file,8,entry->key);
write_integer(book->file,2,entry->move);
write_integer(book->file,2,entry->count);
write_integer(book->file,2,entry->n);
write_integer(book->file,2,entry->sum);
}
// read_integer()
static uint64 read_integer(FILE * file, int size) {
uint64 n;
int i;
int b;
ASSERT(file!=NULL);
ASSERT(size>0&&size<=8);
n = 0;
for (i = 0; i < size; i++) {
b = fgetc(file);
if (b == EOF) {
if (feof(file)) {
my_fatal("read_integer(): fgetc(): EOF reached\n");
} else { // error
my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
}
}
ASSERT(b>=0&&b<256);
n = (n << 8) | b;
}
return n;
}
// write_integer()
static void write_integer(FILE * file, int size, uint64 n) {
int i;
int b;
ASSERT(file!=NULL);
ASSERT(size>0&&size<=8);
ASSERT(size==8||n>>(size*8)==0);
for (i = size-1; i >= 0; i--) {
b = (n >> (i*8)) & 0xFF;
ASSERT(b>=0&&b<256);
if (fputc(b,file) == EOF) {
my_fatal("write_integer(): fputc(): %s\n",strerror(errno));
}
}
}
// end of book_merge.cpp