view regexParser/sequentialSearch.cc @ 230:2081b9d6a179

change var name BufferList to Buffer.
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Mon, 18 Jan 2016 16:41:17 +0900
parents 8be58af605da
children d67649929e96
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 buffer {
    unsigned char *buff;
    unsigned char *buffptr;
    unsigned char *buffend;
    unsigned char *matchBegin;
} Buffer, *BufferPtr;

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

void state1(Buffer buff);

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

void stateMatch(Buffer 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);
    }

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