Mercurial > hg > Applications > Grep
comparison 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 |
comparison
equal
deleted
inserted
replaced
41:e1c5ecbf8836 | 42:cdb4fd81c31f |
---|---|
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include "regex.h" | 10 #include "regex.h" |
11 | 11 |
12 const char *usr_help_str = "Usage: ./regex [-file filename] [-sw SearchWord]\n"; | 12 const char *usr_help_str = "Usage: ./regex [-file filename] [-sw SearchWord]\n"; |
13 const char *usr_help_mode = "Please add -bm or -regex"; | |
13 extern int *createBMskiptable(BMDataPtr); | 14 extern int *createBMskiptable(BMDataPtr); |
14 extern void *BMmethod(BMDataPtr,ResultPtr); | 15 extern void *BMmethod(BMDataPtr,ResultPtr); |
15 | 16 |
16 int main(int argc, char* argv[]) { | 17 int main(int argc, char* argv[]) { |
17 | 18 |
18 char *filename = 0; | 19 char *filename = 0; |
19 char *searchWord = 0; | 20 char *searchWord = 0; |
21 bool bmSearchFlag = false; | |
22 bool regexFlag = false; | |
20 | 23 |
21 // check argument | 24 // check argument |
22 for (int i = 0; argv[i]; i++) { | 25 for (int i = 0; argv[i]; i++) { |
23 if (strcmp(argv[i], "-file") == 0) { | 26 if (strcmp(argv[i], "-file") == 0) { |
24 filename = (char*)argv[i+1]; i++; | 27 filename = (char*)argv[i+1]; i++; |
25 }else if (strcmp(argv[i], "-sw") == 0) { | 28 }else if (strcmp(argv[i], "-sw") == 0) { |
26 searchWord = (char*)argv[i+1]; i++; | 29 searchWord = (char*)argv[i+1]; i++; |
30 }else if (strcmp(argv[i], "-bm") == 0) { | |
31 bmSearchFlag = true; | |
32 }else if (strcmp(argv[i], "-regex") == 0) { | |
33 regexFlag = true; | |
27 } | 34 } |
28 } | 35 } |
29 | 36 |
30 // prepare file read | 37 // prepare file read |
31 if (filename == 0) { | 38 if (filename == 0) { |
43 | 50 |
44 if (fstat(fd,&sb)) { | 51 if (fstat(fd,&sb)) { |
45 fprintf(stderr,"can't fstat %s\n",filename); | 52 fprintf(stderr,"can't fstat %s\n",filename); |
46 } | 53 } |
47 | 54 |
48 BMDataPtr bmdata = (BMDataPtr)malloc(sizeof(BMData)); | |
49 | |
50 textfile = (char*)malloc(sb.st_size); | 55 textfile = (char*)malloc(sb.st_size); |
51 read(fd,textfile,sb.st_size); | 56 read(fd,textfile,sb.st_size); |
52 | |
53 bmdata->readText = textfile; | |
54 bmdata->readTextLen = sb.st_size; | |
55 bmdata->skipTable = (int*)malloc(sizeof(int)*256); | |
56 | 57 |
57 if (textfile == (char*)-1) { | 58 if (textfile == (char*)-1) { |
58 fprintf(stderr,"Can't mmap file\n"); | 59 fprintf(stderr,"Can't mmap file\n"); |
59 perror(NULL); | 60 perror(NULL); |
60 exit(0); | 61 exit(0); |
61 } | 62 } |
62 | 63 |
63 // prepare Boyer Moore Search | 64 if (bmSearchFlag == true) { |
64 bmdata->searchWord = searchWord; | 65 BMDataPtr bmdata = (BMDataPtr)malloc(sizeof(BMData)); |
65 bmdata->searchWordLen = strlen((const char*)bmdata->searchWord); | 66 ResultPtr result = (ResultPtr)malloc(sizeof(Result)); |
66 bmdata->skipTable = createBMskiptable(bmdata); | |
67 | 67 |
68 ResultPtr result = (ResultPtr)malloc(sizeof(Result)); | 68 bmdata->readText = textfile; |
69 bmdata->readTextLen = sb.st_size; | |
70 bmdata->skipTable = (int*)malloc(sizeof(int)*256); | |
69 | 71 |
70 BMmethod(bmdata,result); | 72 // prepare Boyer Moore Search |
73 bmdata->searchWord = searchWord; | |
74 bmdata->searchWordLen = strlen((const char*)bmdata->searchWord); | |
75 bmdata->skipTable = createBMskiptable(bmdata); | |
71 | 76 |
72 printf("sword: %s len: %d\n",bmdata->searchWord,bmdata->searchWordLen); | 77 BMmethod(bmdata,result); |
73 printf("Match : %d\n",result->matchNum); | |
74 | 78 |
75 free(result); | 79 printf("sword: %s len: %d\n",bmdata->searchWord,bmdata->searchWordLen); |
76 free(bmdata); | 80 printf("Match : %d\n",result->matchNum); |
81 | |
82 free(result); | |
83 free(bmdata); | |
84 }else if (regexFlag == true) { | |
85 | |
86 }else{ | |
87 puts(usr_help_mode); | |
88 exit(1); | |
89 } | |
90 | |
77 return 0; | 91 return 0; |
78 } | 92 } |