Mercurial > hg > Applications > Grep
comparison regexParser/sequentialSearch.cc @ 231:d67649929e96
add grepWalk
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 19 Jan 2016 12:54:11 +0900 |
parents | 2081b9d6a179 |
children | 1a34e702776a |
comparison
equal
deleted
inserted
replaced
230:2081b9d6a179 | 231:d67649929e96 |
---|---|
2 #include <string.h> | 2 #include <string.h> |
3 #include <unistd.h> | 3 #include <unistd.h> |
4 #include <fcntl.h> | 4 #include <fcntl.h> |
5 #include <sys/stat.h> | 5 #include <sys/stat.h> |
6 #include <sys/mman.h> | 6 #include <sys/mman.h> |
7 #include <sys/types.h> | |
8 | 7 |
9 typedef struct buffer { | 8 #include "sequentialSearch.h" |
10 unsigned char *buff; | |
11 unsigned char *buffptr; | |
12 unsigned char *buffend; | |
13 unsigned char *matchBegin; | |
14 } Buffer, *BufferPtr; | |
15 | |
16 typedef struct { | |
17 caddr_t file_mmap; | |
18 off_t size; | |
19 } st_mmap_t; | |
20 | 9 |
21 void state1(Buffer buff); | 10 void state1(Buffer buff); |
22 | 11 |
23 void stateSkip(Buffer buff) { | 12 void stateSkip(Buffer buff) { |
24 buff.matchBegin = buff.buffptr; | 13 buff.matchBegin = buff.buffptr; |
29 fwrite(buff.matchBegin,buff.buffptr-buff.matchBegin,1,stdout); | 18 fwrite(buff.matchBegin,buff.buffptr-buff.matchBegin,1,stdout); |
30 puts("\n"); | 19 puts("\n"); |
31 stateSkip(buff); | 20 stateSkip(buff); |
32 } | 21 } |
33 | 22 |
34 #include "state.cc" | 23 st_mmap_t createSt_mmap(char* filename,int fd) { |
35 int main(int argc, char **argv) { | 24 st_mmap_t st_mmap; |
36 char *filename; | |
37 for (int i = 1; i < argc; i++) { | |
38 if (strcmp(argv[i],"-file") == 0) { | |
39 filename = argv[i+1]; i++; | |
40 } | |
41 } | |
42 | |
43 int map = MAP_PRIVATE; | 25 int map = MAP_PRIVATE; |
44 st_mmap_t st_mmap; | |
45 struct stat sb; | 26 struct stat sb; |
46 int fd; | |
47 if ((fd=open(filename,O_RDONLY,0666))==0) { | 27 if ((fd=open(filename,O_RDONLY,0666))==0) { |
48 perror(""); | 28 perror(""); |
49 fprintf(stderr,"can't open %s\n",filename); | 29 fprintf(stderr,"can't open %s\n",filename); |
50 } | 30 } |
51 | 31 |
57 unsigned char *file_mmap = (unsigned char*)mmap(NULL,st_mmap.size,PROT_WRITE|PROT_READ,map,fd,(off_t)0); | 37 unsigned char *file_mmap = (unsigned char*)mmap(NULL,st_mmap.size,PROT_WRITE|PROT_READ,map,fd,(off_t)0); |
58 if (file_mmap == NULL) { | 38 if (file_mmap == NULL) { |
59 perror(""); | 39 perror(""); |
60 fprintf(stderr,"cannot mmap %s\n",filename); | 40 fprintf(stderr,"cannot mmap %s\n",filename); |
61 } | 41 } |
42 st_mmap.file_mmap = file_mmap; | |
43 return st_mmap; | |
44 } | |
62 | 45 |
46 Buffer createBuffer(st_mmap_t st_mmap) { | |
63 Buffer buff; | 47 Buffer buff; |
64 buff.buff = buff.buffptr = buff.matchBegin = file_mmap; | 48 buff.buff = buff.buffptr = buff.matchBegin = st_mmap.file_mmap; |
65 buff.buffend = buff.buff + st_mmap.size; | 49 buff.buffend = buff.buff + st_mmap.size; |
50 return buff; | |
51 } | |
52 | |
53 #include "state.cc" | |
54 int main(int argc, char **argv) { | |
55 char *filename; | |
56 for (int i = 1; i < argc; i++) { | |
57 if (strcmp(argv[i],"-file") == 0) { | |
58 filename = argv[i+1]; i++; | |
59 } | |
60 } | |
61 | |
62 int fd; | |
63 st_mmap_t st_mmap = createSt_mmap(filename,fd); | |
64 Buffer buff = createBuffer(st_mmap); | |
66 stateSkip(buff); | 65 stateSkip(buff); |
67 close(fd); | 66 close(fd); |
68 return 0; | 67 return 0; |
69 } | 68 } |