Mercurial > hg > Game > Cerium
changeset 1200:7b866a392adc draft
mege 1179
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 14 Jul 2011 18:51:10 +0900 |
parents | e55b9d2cb33e (current diff) 2abd68a1ac2a (diff) |
children | dd52af319c40 |
files | example/regex/Makefile.def example/regex/Makefile.macosx example/regex/WordCount.h example/regex/main.cc example/regex/ppe/Exec.cc example/regex/ppe/Print.cc example/regex/word_count_back |
diffstat | 7 files changed, 290 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/example/regex/Makefile.macosx Thu Jul 14 18:16:23 2011 +0900 +++ b/example/regex/Makefile.macosx Thu Jul 14 18:51:10 2011 +0900 @@ -1,13 +1,21 @@ include ./Makefile.def SRCS_TMP = $(wildcard *.cc) +<<<<<<< local SRCS_EXCLUDE = grep_main.cc # 除外するファイルを書く +======= +SRCS_EXCLUDE = # 除外するファイルを書く +>>>>>>> other SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) OBJS = $(SRCS:.cc=.o) TASK_DIR = ppe TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc) +<<<<<<< local TASK_SRCS_EXCLUDE = switchGrep.cc +======= +TASK_SRCS_EXCLUDE = +>>>>>>> other TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP)) TASK_OBJS = $(TASK_SRCS:.cc=.o) @@ -25,7 +33,11 @@ $(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS) link: +<<<<<<< local $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) +======= + $(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS) +>>>>>>> other debug: $(TARGET) sudo gdb ./$(TARGET)
--- a/example/regex/WordCount.h Thu Jul 14 18:16:23 2011 +0900 +++ b/example/regex/WordCount.h Thu Jul 14 18:51:10 2011 +0900 @@ -17,11 +17,21 @@ int file_size; HTaskPtr t_print; +<<<<<<< local +======= + //regexのENVの成分 +>>>>>>> other bool print_count; bool print_filename; int count; char *filename; +<<<<<<< local } WordCount, ENV, *ENVP; +======= +}WordCount; + //} WordCount, ENV; +//typedef ENV *ENVP; +>>>>>>> other
--- a/example/regex/main.cc Thu Jul 14 18:16:23 2011 +0900 +++ b/example/regex/main.cc Thu Jul 14 18:51:10 2011 +0900 @@ -15,6 +15,7 @@ extern void task_init(); +#define _REGENUM_ 3 int all = 0; int use_task_array = 1; int use_task_creater = 0; @@ -158,6 +159,7 @@ fprintf(stderr,"can't fstat %s\n",filename); } + // printf("file size %d\n",(int)sb.st_size); /*sizeをページングサイズの倍数にあわせる*/ @@ -417,14 +419,13 @@ w->out_task_num = out_task_num; printf("task_num %d\n",w->task_num); - // printf("out_task_num %d\n",w->out_task_num); + printf("out_task_num %d\n",w->out_task_num); /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(4byte)を使用 */ w->division_out_size = sizeof(unsigned long long)*4; int out_size = w->division_out_size*out_task_num; w->o_data = (unsigned long long *)manager->allocate(out_size); - /* w->out_size = 4; printf("out size %d\n",out_size); */
--- a/example/regex/ppe/Exec.cc Thu Jul 14 18:16:23 2011 +0900 +++ b/example/regex/ppe/Exec.cc Thu Jul 14 18:51:10 2011 +0900 @@ -2,7 +2,7 @@ #include <string.h> #include "Exec.h" #include "Func.h" -#include "WordCount.h" +#include "grep.h" /* これは必須 */ SchedDefineTask(Exec); @@ -216,13 +216,11 @@ // char *i_data = (char *)rbuf; char *i_data = (char *)s->get_input(rbuf, 0); int length = (int)s->get_inputSize(0); - WordCount *env = (WordCount*)s->get_input(rbuf, 1); - UCHARP beg, buf, end; beg = buf = (UCHARP)i_data; end = beg + length - 1; - + s0(beg, buf, end, (ENVP)env); return 0;
--- a/example/regex/ppe/Print.cc Thu Jul 14 18:16:23 2011 +0900 +++ b/example/regex/ppe/Print.cc Thu Jul 14 18:51:10 2011 +0900 @@ -10,8 +10,6 @@ static int run_print(SchedTask *s, void *rbuf, void *wbuf) { - ENV *w = *(WordCount**)rbuf; - return 0; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/regex/ppe/grep.c Thu Jul 14 18:51:10 2011 +0900 @@ -0,0 +1,263 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> + +typedef unsigned char UCHAR; +typedef unsigned char* UCHARP; + +typedef enum BOOL { + FALSE = 0, + TRUE = 1 +} BOOL; + +typedef struct ENV { + // BOOL print_count; + // BOOL print_filename; + bool print_count; + bool print_filename; + int count; + char *filename; +} *ENVP; + +void filter(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void predict(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +#define __REGEX__ "h.s" + +UCHARP get_line_beg(UCHARP p, UCHARP beg) { + while(p > beg) { + if ((*--p) == '\n') return p+1; + } + return beg; +} + +void print_line(UCHARP beg, UCHARP end) { + fwrite(beg, sizeof(char), (end - beg + 1), stdout); +} + +void grep(int fd, ENVP env) { + caddr_t file_mmap; + UCHARP buf, end, beg; + off_t size; + struct stat sb; + + if (fstat(fd, &sb)) { + fprintf(stderr, "can't fstat %s\n", env->filename); + exit(0); + } + + size = sb.st_size; + file_mmap = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t)0); + + if (file_mmap == (caddr_t)-1) { + fprintf(stderr, "can't mmap %s\n", env->filename); + exit(0); + } + + beg = buf = (UCHARP) file_mmap; + end = beg + size - 1; + env->count = 0; + + s0(beg, beg, end, env); + + munmap(file_mmap, size); + return; +} + +void stdingrep(ENVP env) { + char buf_[BUFSIZ]; + UCHARP buf, beg, end; + buf = beg = (UCHARP) buf_; + + while (fgets(buf_, BUFSIZ, stdin) != NULL) { + env->count = 0; + end = buf + strlen(buf_) - 1; + s0(beg, buf, end, env); + } +} + +int main(int argc, char* argv[]) { + int i, fd, opt, with_filename = 0; + + ENVP env = (ENVP)calloc(sizeof(ENVP), 1); + + while ((opt=getopt(argc, argv, "chH")) != -1) { + switch (opt) { + case 'c': + env->print_count = 1; + break; + case 'h': + with_filename = -1; + break; + case 'H': + with_filename = 1; + break; + default: + fprintf(stderr, "invalid option."); + exit(0); + } + } + + if (optind == argc) { + if (with_filename) { + /* handle -h/-H as help */ + printf("grep pattern: %s\n", __REGEX__); + exit(1); + } else { + stdingrep(env); + } + } else { + switch (with_filename) { + case 0: //default + if (argc == optind + 1) { + /* just grep 1 file. */ + env->print_filename = false; + } else { + /* grep 1 more files. */ + env->print_filename = true; + } + break; + // option + case -1: + env->print_filename = false; + break; + case 1: + env->print_filename = true; + break; + default: break; + } + for (i = optind; i < argc; i++) { + fd = open(argv[i], O_RDONLY, 0666); + if (fd == 0) { + printf("%d, %s\n", i, argv[i]); + fprintf(stderr, "can't open %s:", argv[i]); + continue; + } + env->filename = argv[i]; + grep(fd, env); + close(fd); + } + } + + return 0; +} +void s1(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s2(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s3(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s4(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s5(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); +void s6(UCHARP beg, UCHARP buf, UCHARP end, ENVP env); + +void s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + return filter(beg, buf, end, env); +} + +void filter(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + static const UCHARP key = (UCHARP)"h"; + int i, len = 1; +loop: + while ((buf = (UCHARP)memchr(buf, key[0], end-buf))) { + for(i = 1; i < len; i++) { + if (buf[i] != key[i]) { + buf++; + goto loop; + } + } + goto next; + } + return reject(beg, end, end, env); +next: + return s1(beg, buf, end, env); +} + +void s1(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + static void (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s1, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s2 }; + if (buf > end) return tbl['\n'](beg, buf, end, env); + return tbl[*buf++](beg, buf, end, env); +} + +void s2(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + static void (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s3, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s4}; + if (buf > end) return tbl['\n'](beg, buf, end, env); + return tbl[*buf++](beg, buf, end, env); +} + +void s3(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + static void (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s1, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s2, /* s */ [115] = (void*)s5}; + if (buf > end) return tbl['\n'](beg, buf, end, env); + return tbl[*buf++](beg, buf, end, env); +} + +void s4(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + static void (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s3, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s4, /* s */ [115] = (void*)s6}; + if (buf > end) return tbl['\n'](beg, buf, end, env); + return tbl[*buf++](beg, buf, end, env); +} + +void s5(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + return accept(beg, buf - 1, end, env);} + +void s6(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + return accept(beg, buf - 1, end, env);} + +void +accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) +{ + if (buf < beg) buf = beg; + UCHARP ret = (buf < end) ? (UCHARP) memchr(buf, '\n', (end - buf)) : NULL; + if (env->print_count) { + env->count++; + if (ret == NULL) { + if (env->print_filename) { + printf("%s:", env->filename); + } + printf("%d\n", env->count); + return; + } + beg = buf = ret + 1; + } else { + beg = get_line_beg(buf, beg); + if (env->print_filename) { + printf("%s:", env->filename); + } + if (ret == NULL) { + print_line(beg, end); + return; + } + print_line(beg, ret); + beg = buf = ret + 1; + } + return s0(beg, buf, end, env); +} + +void +reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { + UCHARP ret; + if (buf >= end) { + ret = NULL; + } else { + if (*(buf-1) == '\n') { + ret = buf-1; + } else { + ret = (UCHARP)memchr(buf, '\n', (end - buf)); + } + } + if (ret == NULL) { + if (env->print_count) { + if (env->print_filename) { + printf("%s:", env->filename); + } + printf("%d\n", env->count); + } + return; + } + beg = buf = ret + 1; + return s0(beg, buf, end, env); +}