view regex/main.cc @ 117:166136236891 pairPro

add header files
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Wed, 25 Nov 2015 14:58:03 +0900
parents 62739627f8ec
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "regex.h"

const char *usr_help_str = "Usage: ./regex [-file filename] [-sw SearchWord]\n";
const char *usr_help_mode = "Please add -bm or -regex";
extern int *createBMskiptable(BMDataPtr);
extern void *BMmethod(BMDataPtr,ResultPtr);
extern void *regex(RegexDataPtr);

int main(int argc, char* argv[]) {

    char *filename = 0;
    char *searchWord = 0;
    bool bmSearchFlag = false;
    bool regexFlag = false;

    // check argument
    for (int i = 0; argv[i]; i++) {
        if (strcmp(argv[i], "-file") == 0) {
            filename = (char*)argv[i+1]; i++;
        }else if (strcmp(argv[i], "-sw") == 0) {
            searchWord = (char*)argv[i+1]; i++;
        }else if (strcmp(argv[i], "-bm") == 0) {
            bmSearchFlag = true;
        }else if (strcmp(argv[i], "-regex") == 0) {
            regexFlag = true;
        }
    }

    // prepare file read
    if (filename == 0) {
        puts(usr_help_str);
        exit(1);
    }

    struct stat sb;
    long fd = 0;
    char *textfile;

    if ((fd=open(filename,O_RDONLY,0666))==0) {
        fprintf(stderr,"can't open %s\n",filename);
    }

    if (fstat(fd,&sb)) {
        fprintf(stderr,"can't fstat %s\n",filename);
    }

    textfile = (char*)malloc(sb.st_size);
    read(fd,textfile,sb.st_size);

    if (textfile == (char*)-1) {
        fprintf(stderr,"Can't mmap file\n");
        perror(NULL);
        exit(0);
    }

    if (bmSearchFlag == true) {
        BMDataPtr bmdata = (BMDataPtr)malloc(sizeof(BMData));
        ResultPtr result = (ResultPtr)malloc(sizeof(Result));

        bmdata->readText = textfile;
        bmdata->readTextLen = sb.st_size;
        bmdata->skipTable = (int*)malloc(sizeof(int)*256);

        // prepare Boyer Moore Search
        bmdata->searchWord = searchWord;
        bmdata->searchWordLen = strlen((const char*)bmdata->searchWord);
        bmdata->skipTable = createBMskiptable(bmdata);

        BMmethod(bmdata,result);

        printf("sword: %s len: %d\n",bmdata->searchWord,bmdata->searchWordLen);
        printf("Match : %d\n",result->matchNum);

        free(result);
        free(bmdata);
    }else if (regexFlag == true) {
        RegexDataPtr regexD = (RegexDataPtr)malloc(sizeof(RegexData));
        regexD->readText = textfile;
        regexD->readTextLen = sb.st_size;
        regexD->searchWord = searchWord;
        regexD->searchWordLen = strlen((const char*)regexD->searchWord);
        regex(regexD);
        free(regexD);
    }else{
        puts(usr_help_mode);
        exit(1);
    }

    return 0;
}