#include #include #include #include #include #include #include #include #include #include #include #include "rabincarp.h" int file_open( const char *fname ) { int fd; fd = open( fname, O_RDONLY ); if ( fd == -1 ) { printf("Couldn open file %s\n", fname); return -1; } return fd; } int file_read( int fd, char *buf, int size ) { int ret; if ( size < 1 ) return -1; ret = (int)read( fd, buf, size ); return ret; } int file_seek( int fd, off_t pos ) { off_t rc; if (fd<1) { return -1; } rc = lseek( fd, pos, SEEK_SET ); if (rc == -1) return -1; return 0; } int file_close( int fd ) { close( fd ); return 0; } typedef struct match_params { char *fname; char *match_str; int match_len; int buf_size; int verbose; } match_params; void helper( char *progname ) { printf("Usage: %s [OPTS]\n\n" " -f - input file\n" " -m - match string\n" //" -b - set buffer size to read\n" " -v - more extra info in output\n" "Version: 0.0.1 \n" "\n" , progname); } int main(int argc, char **argv) { int c; int i; int fd; int text_len; int ret; char *text = NULL; //search rol_hash_isearch search; //parametrs match_params mp; memset(&mp,0,sizeof(match_params)); while ( (c = getopt(argc, argv, "f:m:b:v")) != -1 ) { switch(c) { case 'f': mp.fname = optarg; break; case 'm': mp.match_str = optarg; mp.match_len = strlen(optarg); break; case 'b': mp.buf_size = atoi(optarg); break; case 'v': mp.verbose = 1; break; default: helper( argv[0] ); exit(1); } } if (argc<2) { helper(argv[0]); return -1; } if (mp.fname == NULL) { printf("Set filename\n"); return -1; } if (mp.match_str == NULL) { printf("Set search string\n"); return -1; } fd = file_open( mp.fname ); if (fd<0) { printf("Couldnt open file %s\n", mp.fname); return -1; } text_len = lseek(fd,0,SEEK_END); lseek(fd, 0,SEEK_SET); text = malloc(text_len); //printf("Read %d bytes\n",read(fd,text,text_len)); ret = read(fd, text, text_len); if (mp.verbose) { printf("Read %d bytes\n",ret); } rlsi_hash_init( &search, 10, 13); while ((i = rlsi_hash_search(&search, mp.match_str, mp.match_len, text, text_len)) == 1) { //printf("Found smth %d\n",rlsi_hash_get(&search)); printf("%d\n",rlsi_hash_get(&search)); } file_close( fd ); return 0; }