fixed formatting
[tridge/junkcode.git] / bmgmain.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <sys/fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <unistd.h>
8
9
10 static int hits=0;
11
12 void hit_fn(char *buf)
13 {
14 #if 0
15   printf("hit[%d]=[%5.5s]\n",hits,buf);
16 #endif
17   hits++;
18 }
19
20
21 /* a simple harness to test the above */
22 int main(int argc,char *argv[])
23 {
24   char *target;
25   char *buf;
26   char *fname;
27   int fd;
28   int len;
29   struct timeval t1,t2;
30
31   if (argc < 3) {
32     printf("Usage: bmg string|string|string... file\n");
33     exit(1);
34   }
35    
36   target = argv[1];
37   fname = argv[2];
38   fd = open(fname,O_RDONLY);
39   if (fd < 0) {
40     printf("couldn't open %s\n",fname);
41     exit(1);
42   }
43   len = lseek(fd,0,SEEK_END);
44   lseek(fd,0,SEEK_SET);
45   buf = (char *)malloc(len);
46   if (!buf) {
47     printf("failed to alloc buffer\n");
48     exit(1);
49   }
50   len = read(fd,buf,len);
51   close(fd);
52   printf("Loaded %d bytes\n",len);
53
54   gettimeofday(&t1,NULL);
55   bmg_build_table(target,NULL,0);
56   gettimeofday(&t2,NULL);
57   printf("Table build took %ld msecs\n",
58          (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000);  
59
60   gettimeofday(&t1,NULL);
61   bmg_search(buf,len,hit_fn);
62   gettimeofday(&t2,NULL);
63   printf("Search took %ld msec\n",
64          (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000);  
65   printf("Got %d hits\n",hits);
66   return(0);
67 }
68