コード例 #1
0
int main(int argc,char** argv){
	sauchar_t* str;
	saidx_t* SA;
	saidx_t* SAI;
	word n;
	saidx_t* LCP;
	int h,j,k;
    int i;
    text* T = callocx(1,sizeof(text));
    text* P = callocx(1,sizeof(text));
    text* Index = callocx(1,sizeof(text));
    if(argc<4 || argc >5){
        fprintf(stderr,"%s","Error\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -create <inputText> <indexFile>\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -search <indexFile> <inputText> <patternFile>\n");
        exit(EXIT_FAILURE);
    }
    if(strcmp(argv[1],"-create")==0){
        T->file = fopen(argv[2],"r");
        Index->file = fopen(argv[3],"wb");

        if(T->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        if(Index->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }

        fseek(T->file,0,SEEK_END);
        T->length = ftell(T->file);
        rewind(T->file);

        str = callocx(sizeof(sauchar_t),T->length);
        SA = callocx(sizeof(saidx_t),T->length);
        SAI = callocx(sizeof(saidx_t),T->length);
        LCP = callocx(sizeof(saidx_t),T->length);
        fread(str,sizeof(sauchar_t),T->length,T->file);
        if(divsufsort(str,SA,T->length)){
            perror("suffix sort error");
            exit(EXIT_FAILURE);
        }

        for(i=0;i<T->length;i++){
            SAI[SA[i]] = i;
        }

        for(i=0;i<T->length;i++){
            h=0;
            if(SAI[i] !=  T->length -1){
                k = SA[SAI[i]+1];
                j=0;
                while(str[i+h]==str[k+h]) h++;
                LCP[SAI[i]] = h;
                if(h) h--;
            }
            else{
                LCP[SAI[i]] = 0;
            }
        }
        if(Index->file==NULL){
            perror("Error");
            exit(EXIT_FAILURE);
        }
        fwrite(&(T->length),sizeof(word),1,Index->file);
        fwrite(SA,sizeof(word),T->length,Index->file);
        fwrite(LCP,sizeof(word),T->length,Index->file);
        fclose(Index->file);
        free(SA);
        free(SAI);
        free (LCP);
        free(str);

    }
    else if(strcmp(argv[1],"-search")==0){
        P->file = fopen(argv[4],"r");
        T->file = fopen(argv[3],"r");
        Index->file = fopen(argv[2],"rb");
        if(T->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        if(P->file == NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        if(Index->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        fseek(T->file,0,SEEK_END);
        T->length = ftell(T->file);
        rewind(T->file);
        T->textStart = 0;
        T->textEnd = T->length-1;
        fseek(P->file,0,SEEK_END);
        P->length = ftell(P->file);
        rewind(P->file);
        fread(&n,sizeof(word),1,Index->file);
        SA = callocx(sizeof(saidx_t),n);
        LCP = callocx(sizeof(saidx_t),n);
        fread(SA,sizeof(saidx_t),n,Index->file);
        fread(LCP,sizeof(saidx_t),n,Index->file);
        searchPattern(T,P,SA,LCP,n);
        free(SA);
        free(LCP);
    }
    else{
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -create <inputText> <indexFile>\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -search <inputText> <indexFile> <patternFile>\n");
        exit(EXIT_FAILURE);
    }
    freeText(T);
    freeText(P);
    freeText(Index);
    return(0);
}
コード例 #2
0
int main(int argc,char** argv){
    word i;
    text* T = callocx(sizeof(text),1);
    text* P = callocx(sizeof(text),1);
    cesa* CSA;
    cesa* CSA2;
    if(argc<4 || argc >5){
        fprintf(stderr,"%s","Error\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -create <inputText> <indexFile>\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -search <indexFile> <inputText> <patternFile>\n");
        exit(EXIT_FAILURE);
    }
    /**preprocess step**/
    initAllBitTables();

    if(strcmp(argv[1],"-create")==0){
        T->file = fopen(argv[2],"r");
        if(T->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        fseek(T->file,0,SEEK_END);
        T->length = ftell(T->file);
        rewind(T->file);
        T->textStart = 0;
        T->textEnd = T->length-1;
        CSA = computeCSA(T);
        computeLcp(CSA);
        saveIndex(CSA,argv[3]);
    }
    else if(strcmp(argv[1],"-search")==0){
        P->file = fopen(argv[4],"r");
        T->file = fopen(argv[3],"r");
        if(T->file==NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }
        if(P->file == NULL){
            perror("Error: ");
            exit(EXIT_FAILURE);
        }

        fseek(T->file,0,SEEK_END);
        T->length = ftell(T->file);
        rewind(T->file);
        T->textStart = 0;
        T->textEnd = T->length-1;

        fseek(P->file,0,SEEK_END);
        P->length = ftell(P->file);
        rewind(P->file);
        CSA = loadIndex(argv[2]);
        CSA->P = P;
        CSA->T = T;
        searchPattern(CSA);
    }
    else{
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -create <inputText> <indexFile>\n");
        fprintf(stderr,"%s%s%s","Usage = ",argv[0]," -search <indexFile> <inputText> <patternFile>\n");
        exit(EXIT_FAILURE);
    }
    freeText(T);
    freeText(P);
    freeCSA(CSA);
    return(0);
}
コード例 #3
0
ファイル: thrcode.c プロジェクト: sauliusg/starta
THRCODE *new_thrcode( cexception_t *ex )
{
    return callocx( 1, sizeof(THRCODE), ex );
}