#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"SP.h"



void initSP (SP* sps, char** argv) {

	int i; 
	char W[1000];

	FILE* fSP;
	fSP = fopen(argv[2],"r");
	
	for (i=0; i < Nsp; i++) {
		fgets(W, 1000, fSP);
		sps[i]._length = strlen(W) - 1;
		sps[i]._seq = (char*)malloc((sps[i]._length+1)*sizeof(char));
		sps[i]._table = (int*)malloc(sps[i]._length*sizeof(int));

		// the sr index of hits - dynamic with search
		sps[i]._sr = (int*)malloc(1*sizeof(int));
		// the frame index of hits for a given sr - dynamic with search
		sps[i]._frame = (int*)malloc(1*sizeof(int));
		
		strcpy(sps[i]._seq, W);
		KMPTable (sps[i]._seq, sps[i]._table, sps[i]._length);

		sps[i]._count = 0;

    }
	fclose(fSP);
		
}



void KMPTable (char* W, int* T, int size) {
	int pos, cnd;
    pos = 2; //current position in T
    cnd = 0; //zero-based index in W (of the next character of the current candidate substring)

    T[0] = -1; T[1] = 0;

	//while is less than the length of W
    while (pos < size) {
        //first case: the substring continues
        if (W[pos - 1] == W[cnd]) { 
			T[pos] = cnd + 1; 
			pos = pos + 1; 
			cnd = cnd + 1;
		}
        //second case: it doesn't, but we can fall back
        else if (cnd > 0) {
			cnd = T[cnd];
		}
		//third case: we have run out of candidates. Note cnd = 0
		else {
			T[pos] = 0;
			pos = pos + 1;
		}
	}
}



void printSP(SP* sps, char** argv) {
	int i, j;
	FILE* fo;
	fo = fopen(argv[3],"w");
	for (i=0; i < Nsp; i++) {
		
		fprintf(fo,"%d\t", sps[i]._count);
		for (j=0; j < sps[i]._count; j++) {
			fprintf(fo,"%d\t", sps[i]._sr[j]);
		}
		for (j=0; j < sps[i]._count; j++) {
			fprintf(fo,"%d\t", sps[i]._frame[j]);
		}
		fprintf(fo,"\n");

	}
	fclose(fo);
}



void freeSP(SP* sps) {
	int i;
	for(i=0; i < Nsp; i++) {
		free(sps[i]._table);
	}
}
