view regexParser/sequentialSearch.cc @ 227:8be58af605da

fix getNext()
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Fri, 15 Jan 2016 19:11:35 +0900
parents b4022ba23de5
children 2081b9d6a179
line wrap: on
line source

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>

typedef struct bufferList {
    unsigned char *buff;
    unsigned char *buffptr;
    unsigned char *buffend;
    unsigned char *matchBegin;
} BufferList, *BufferListPtr;

typedef struct {
    caddr_t file_mmap;
    off_t size;
} st_mmap_t;

void state1(BufferList buff);

void stateSkip(BufferList buff) {
    buff.matchBegin = buff.buffptr;
    state1(buff);
}

void stateMatch(BufferList buff) {
    fwrite(buff.matchBegin,buff.buffptr-buff.matchBegin,1,stdout);
    puts("\n");
    stateSkip(buff);
}

#include "state.cc"
int main(int argc, char **argv) {
    char *filename;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i],"-file") == 0) {
            filename = argv[i+1]; i++;
        }
    }

    int map = MAP_PRIVATE;
    st_mmap_t st_mmap;
    struct stat sb;
    int fd;
    if ((fd=open(filename,O_RDONLY,0666))==0) {
        perror("");
        fprintf(stderr,"can't open %s\n",filename);
    }

    if (fstat(fd,&sb)) {
        perror("");
        fprintf(stderr,"can't fstat %s\n",filename);
    }
    st_mmap.size = sb.st_size;
    unsigned char *file_mmap = (unsigned char*)mmap(NULL,st_mmap.size,PROT_WRITE|PROT_READ,map,fd,(off_t)0);
    if (file_mmap == NULL) {
        perror("");
        fprintf(stderr,"cannot mmap %s\n",filename);
    }

    BufferList buff;
    buff.buff = buff.buffptr = buff.matchBegin = file_mmap;
    buff.buffend = buff.buff + st_mmap.size;
    stateSkip(buff);
    close(fd);
    return 0;
}