Mercurial > hg > Applications > Grep
view regex/main.cc @ 42:cdb4fd81c31f
add regex mode
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Mar 2015 23:59:24 +0900 |
parents | e1c5ecbf8836 |
children | ead0a307449e |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <math.h> #include <string.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include "regex.h" const char *usr_help_str = "Usage: ./regex [-file filename] [-sw SearchWord]\n"; const char *usr_help_mode = "Please add -bm or -regex"; extern int *createBMskiptable(BMDataPtr); extern void *BMmethod(BMDataPtr,ResultPtr); int main(int argc, char* argv[]) { char *filename = 0; char *searchWord = 0; bool bmSearchFlag = false; bool regexFlag = false; // check argument for (int i = 0; argv[i]; i++) { if (strcmp(argv[i], "-file") == 0) { filename = (char*)argv[i+1]; i++; }else if (strcmp(argv[i], "-sw") == 0) { searchWord = (char*)argv[i+1]; i++; }else if (strcmp(argv[i], "-bm") == 0) { bmSearchFlag = true; }else if (strcmp(argv[i], "-regex") == 0) { regexFlag = true; } } // prepare file read if (filename == 0) { puts(usr_help_str); exit(1); } struct stat sb; long fd = 0; char *textfile; if ((fd=open(filename,O_RDONLY,0666))==0) { fprintf(stderr,"can't open %s\n",filename); } if (fstat(fd,&sb)) { fprintf(stderr,"can't fstat %s\n",filename); } textfile = (char*)malloc(sb.st_size); read(fd,textfile,sb.st_size); if (textfile == (char*)-1) { fprintf(stderr,"Can't mmap file\n"); perror(NULL); exit(0); } if (bmSearchFlag == true) { BMDataPtr bmdata = (BMDataPtr)malloc(sizeof(BMData)); ResultPtr result = (ResultPtr)malloc(sizeof(Result)); bmdata->readText = textfile; bmdata->readTextLen = sb.st_size; bmdata->skipTable = (int*)malloc(sizeof(int)*256); // prepare Boyer Moore Search bmdata->searchWord = searchWord; bmdata->searchWordLen = strlen((const char*)bmdata->searchWord); bmdata->skipTable = createBMskiptable(bmdata); BMmethod(bmdata,result); printf("sword: %s len: %d\n",bmdata->searchWord,bmdata->searchWordLen); printf("Match : %d\n",result->matchNum); free(result); free(bmdata); }else if (regexFlag == true) { }else{ puts(usr_help_mode); exit(1); } return 0; }