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
|
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include "config.h"
#include "pttstruct.h"
typedef struct hash_t {
char *brdname;
struct hash_t *next;
} hash_t;
FILE *fout;
hash_t *hash_tbl[65536];
int counter;
void usage() {
fprintf(stderr, "Usage:\n\n"
"merge_board <output file> [input file1] [input file2] ...\n");
}
unsigned int string_hash(unsigned char *s) {
unsigned int v=0;
while(*s) {
v = (v << 8) | (v >> 24);
v ^= toupper(*s++); /* note this is case insensitive */
}
return (v * 2654435769UL) >> (32 - 16);
}
int is_exist(char *brdname) {
int i;
hash_t *n;
i = string_hash(brdname);
for(n = hash_tbl[i]; n != NULL; n = n->next)
if(strcasecmp(brdname, n->brdname) == 0)
return 1;
return 0;
}
void add_hash(char *brdname) {
int i;
hash_t *n;
i = string_hash(brdname);
n = malloc(sizeof(*n));
n->brdname = strdup(brdname);
n->next = hash_tbl[i];
hash_tbl[i] = n;
}
void merge_board(boardheader_t *b) {
if(!is_exist(b->brdname)) {
fwrite(b, sizeof(*b), 1, fout);
add_hash(b->brdname);
++counter;
}
}
void merge_file(char *fname) {
FILE *fin;
boardheader_t b;
if((fin = fopen(fname, "r")) == NULL) {
perror(fname);
return;
}
counter = 0;
while(fread(&b, sizeof(b), 1, fin) == 1)
if(b.brdname[0])
merge_board(&b);
printf("merge from %s: %d boards\n", fname, counter);
fclose(fin);
}
int main(int argc, char **argv) {
int i;
if(argc < 2) {
usage();
return 1;
}
bzero(hash_tbl, sizeof(hash_tbl));
if((fout = fopen(argv[1], "w")) == NULL) {
perror(argv[1]);
return 2;
}
for(i = 2; i < argc; ++i)
merge_file(argv[i]);
fclose(fout);
printf("Done\n");
return 0;
}
|