Mercurial > hg > Game > Cerium
view example/regex/ppe/Exec.cc @ 1204:ffef576db4b3 draft
modify example/regex/
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 16 Jul 2011 17:25:40 +0900 |
parents | 7b866a392adc |
children |
line wrap: on
line source
#include <stdio.h> #include <string.h> #include "Exec.h" #include "Func.h" #include "grep.h" /* これは必須 */ SchedDefineTask(Exec); typedef unsigned char UCHAR; typedef unsigned char* UCHARP; 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; } UCHARP get_word_beg(UCHARP p, UCHARP beg) { while(p > beg) { if ((*--p) == ' ') return p+1; } return beg; } void print_line(UCHARP beg, UCHARP end) { fwrite(beg, sizeof(char), (end - beg + 1), stdout); } 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); } } 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) { UCHARP c = buf++; if (buf > end) return reject(beg, buf, end, env); switch(*c) { case 10: return reject(beg, buf, end, env); case 104: return s2(beg, buf, end, env); break; default: return s1(beg, buf, end, env); break; } } void s2(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { UCHARP c = buf++; if (buf > end) return reject(beg, buf, end, env); switch(*c) { case 10: return reject(beg, buf, end, env); case 104: return s4(beg, buf, end, env); break; default: return s3(beg, buf, end, env); break; } } void s3(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { UCHARP c = buf++; if (buf > end) return reject(beg, buf, end, env); switch(*c) { case 10: return reject(beg, buf, end, env); case 104: return s2(beg, buf, end, env); break; case 115: return s5(beg, buf, end, env); break; default: return s1(beg, buf, end, env); break; } } void s4(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) { UCHARP c = buf++; if (buf > end) return reject(beg, buf, end, env); switch(*c) { case 10: return reject(beg, buf, end, env); case 104: return s2(beg, buf, end, env); break; case 115: return s5(beg, buf, end, env); break; default: return s3(beg, buf, end, env); break; } } 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; UCHARP ret = (buf < end) ? (UCHARP) memchr(buf, ' ', (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); beg = get_word_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)); ret = (UCHARP)memchr(buf, ' ', (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); } static int run(SchedTask *s, void *rbuf, void *wbuf) { // char *i_data = (char *)rbuf; char *i_data = (char *)s->get_input(rbuf, 0); int length = (int)s->get_inputSize(0); ENV *env = (ENV*)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; }