11 #include "book_merge.h"
37 static void book_clear (book_t * book);
39 static void book_open (book_t * book, const char file_name[], const char mode[]);
40 static void book_close (book_t * book);
42 static bool read_entry (book_t * book, entry_t * entry, int n);
43 static void write_entry (book_t * book, const entry_t * entry);
45 static uint64 read_integer (FILE * file, int size);
46 static void write_integer (FILE * file, int size, uint64 n);
52 void book_merge(int argc, char * argv[]) {
55 const char * in_file_1;
56 const char * in_file_2;
57 const char * out_file;
64 my_string_clear(&in_file_1);
67 my_string_clear(&in_file_2);
70 my_string_set(&out_file,"out.bin");
72 for (i = 1; i < argc; i++) {
76 } else if (my_string_equal(argv[i],"merge-book")) {
80 } else if (my_string_equal(argv[i],"-in1")) {
83 if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
85 my_string_set(&in_file_1,argv[i]);
87 } else if (my_string_equal(argv[i],"-in2")) {
90 if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
92 my_string_set(&in_file_2,argv[i]);
94 } else if (my_string_equal(argv[i],"-out")) {
97 if (argv[i] == NULL) my_fatal("book_merge(): missing argument\n");
99 my_string_set(&out_file,argv[i]);
103 my_fatal("book_merge(): unknown option \"%s\"\n",argv[i]);
111 book_open(In1,in_file_1,"rb");
112 book_open(In2,in_file_2,"rb");
113 book_open(Out,out_file,"wb");
122 b1 = read_entry(In1,e1,i1);
123 b2 = read_entry(In2,e2,i2);
127 } else if (!b1 && !b2) {
131 } else if (b1 && !b2) {
136 } else if (b2 && !b1) {
147 } else if (e1->key < e2->key) {
150 } else if (e1->key > e2->key) {
154 ASSERT(e1->key==e2->key);
166 printf("skipped %d entr%s.\n",skip,(skip>1)?"ies":"y");
174 static void book_clear(book_t * book) {
184 static void book_open(book_t * book, const char file_name[], const char mode[]) {
187 ASSERT(file_name!=NULL);
190 book->file = fopen(file_name,mode);
191 if (book->file == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno));
193 if (fseek(book->file,0,SEEK_END) == -1) {
194 my_fatal("book_open(): fseek(): %s\n",strerror(errno));
197 book->size = ftell(book->file) / 16;
202 static void book_close(book_t * book) {
206 if (fclose(book->file) == EOF) {
207 my_fatal("book_close(): fclose(): %s\n",strerror(errno));
213 static bool read_entry(book_t * book, entry_t * entry, int n) {
218 if (n < 0 || n >= book->size) return FALSE;
220 ASSERT(n>=0&&n<book->size);
222 if (fseek(book->file,n*16,SEEK_SET) == -1) {
223 my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
226 entry->key = read_integer(book->file,8);
227 entry->move = read_integer(book->file,2);
228 entry->count = read_integer(book->file,2);
229 entry->n = read_integer(book->file,2);
230 entry->sum = read_integer(book->file,2);
237 static void write_entry(book_t * book, const entry_t * entry) {
242 write_integer(book->file,8,entry->key);
243 write_integer(book->file,2,entry->move);
244 write_integer(book->file,2,entry->count);
245 write_integer(book->file,2,entry->n);
246 write_integer(book->file,2,entry->sum);
251 static uint64 read_integer(FILE * file, int size) {
258 ASSERT(size>0&&size<=8);
262 for (i = 0; i < size; i++) {
268 my_fatal("read_integer(): fgetc(): EOF reached\n");
270 my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
283 static void write_integer(FILE * file, int size, uint64 n) {
289 ASSERT(size>0&&size<=8);
290 ASSERT(size==8||n>>(size*8)==0);
292 for (i = size-1; i >= 0; i--) {
294 b = (n >> (i*8)) & 0xFF;
297 if (fputc(b,file) == EOF) {
298 my_fatal("write_integer(): fputc(): %s\n",strerror(errno));
303 // end of book_merge.cpp