From ae31e19f92e717919ac8e3db9039eb38d2b89aae Mon Sep 17 00:00:00 2001 From: in2 Date: Thu, 7 Mar 2002 15:13:44 +0000 Subject: Initial revision git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@1 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- util/merge_board.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 util/merge_board.c (limited to 'util/merge_board.c') diff --git a/util/merge_board.c b/util/merge_board.c new file mode 100644 index 00000000..743ffc14 --- /dev/null +++ b/util/merge_board.c @@ -0,0 +1,106 @@ +/* $Id: merge_board.c,v 1.1 2002/03/07 15:13:46 in2 Exp $ */ +#include +#include +#include +#include +#include +#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 [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; +} -- cgit v1.2.3