//	This source and similar text sequences method apply to GNU General Public License. 
//			Copyright (C) 2001-2016 Jasenko Dzinleski 

//		This program is free software; you can redistribute it
//	and/or modify it under the terms of the GNU General Public License as
//	published by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version. 

//	This program is distributed in the hope that it will be useful, but
//	WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//	General Public License for more details. 

//	You should have received a copy of the GNU General Public License along
//	with this program; if not, write to the Free Software Foundation, Inc.,
//	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

//  		Text Context Similar Sequences 
//		written by Dzinleski Jasenko  May , 2017



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

FILE	*f1,*f2,*f3,*f4,*f5;

char	infn[256]="fasta__.txt";

int 	fbuff[3][4096];
char 	sq_[4096];

char 	ps[256]="out_d";
char 	ips[256]="iout_d";
char 	ss[256]=".txt";

char	sv[256];
char	sv_[256][256];int sv_i,sv_j;
char 	ssl[256];

char	pv_[2][9][3];

int p_2(char infn_[256], char outfn_[256], int sl, char ssl_[256])
{

int	a,b;
int	d,e;
int	i,j,k,l,m,n;
int	fb;
char 	outfn__[256];

	for(i=0;i<256;++i){outfn__[i]='\0';}
	f1=fopen(infn_,"rb");
	strcat(outfn__,outfn_);
	outfn__[strlen(outfn__)]='_';outfn__[strlen(outfn__)]='l';
	strcat(outfn__,ssl);
	strcat(outfn__,ss);
	f2=fopen(outfn__,"wb");
	fb=getc(f1);
	while(!feof(f1))
	{
		for(i=0;i<256;++i){sv[i]='\0';}i=0;
		while((fb!=10)&&(fb!=13)&&!feof(f1)){sv[i]=fb;++i;fb=getc(f1);}
		while((fb==10)||(fb==13)&&!feof(f1)){fb=getc(f1);}
		for(i=0;i<256;++i){for(j=0;j<256;++j){sv_[i][j]='\0';}}sv_i=0;sv_j=0;
		for(i=0;i<strlen(sv);++i){if(sv[i]=='_'){if(sv_i){sv_i=0;++sv_j;}}else{sv_[sv_j][sv_i]=sv[i];++sv_i;}}
		for(i=0;i<sv_j;++i){if(strlen(sv_[i])==sl){fprintf(f2,"%05d%c%s%c%c",strlen(sv_[i]),(char)9,sv_[i],(char)13,(char)10);}}
	}
	fclose(f1);
	fclose(f2);
	return(0);

}

int p_1(int l,char outfn[256],char outifn[256])
{

	int	i,j,k,m;
	int	fbyte;
	int	a,b,c;

	f1=fopen(infn,"rb");
	f3=fopen(outfn,"wb");
	fbuff[0][0]=1;
	while(!feof(f1))
	{
		
		while(!feof(f1)&&(fbuff[0][0]<4096)){fbuff[0][fbuff[0][0]]=getc(f1);fbuff[1][fbuff[0][0]]=0;fbuff[2][fbuff[0][0]]=0;++fbuff[0][0];}

		i=1;
		while(i<fbuff[0][0]-l-27)
		{

		a=0;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		++a;b=-1;
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];
		pv_[0][a][++b]=fbuff[0][a*3+b+i];

		a=0;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		++a;b=-1;
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];
		pv_[1][a][++b]=fbuff[0][l+a*3+b+i];

		b=0;while(b<9){
		for(a=0;a<3;++a){
		j=-1;
		if(!fbuff[1][++j+i+b*3]&&!fbuff[1][++j+i+b*3]&&!fbuff[1][++j+i+b*3])
		{
		j=-1;
		if
		(
			(pv_[0][b][++j]==pv_[1][a][j])&&
			(pv_[0][b][++j]==pv_[1][a][j])&&
			(pv_[0][b][++j]==pv_[1][a][j])
		){j=-1;fbuff[1][++j+i+b*3]=1;fbuff[1][++j+i+b*3]=1;fbuff[1][++j+i+b*3]=1;}
		}
		}++b;}

		++i;

		}
		for(i=1;i<fbuff[0][0]-l-27;++i)
		{
			if(fbuff[1][i]){fprintf(f3,"%c",fbuff[0][i]);}else{if(fbuff[0][i]==13||fbuff[0][i]==10){fprintf(f3,"%c",fbuff[0][i]);}else{fprintf(f3,"_");}}
		}
		j=1;for(i=fbuff[0][0]-l-27;i<fbuff[0][0];++i){fbuff[0][j]=fbuff[0][i];++j;}fbuff[0][0]=j;
	}
	fclose(f1);
	fclose(f3);

	return(0);
}

int main(int argc,char *argv[])
{
int 	i,j;
char 	fn[256];
char 	fn_[256];
char 	ifn[256];
char 	ifn_[256];

	for(i=57;i<=500;++i)
	{
		for(j=0;j<256;++j){fn[j]='\0';fn_[j]='\0';ifn[j]='\0';ifn_[j]='\0';ssl[j]='\0';}
		if(i<=9)
		{
		strcat(fn,ps);strcat(ifn,ips);
		ssl[strlen(ssl)]=(char)(48+i);
		strcat(fn,ssl);strcat(ifn,ssl);
		strcat(fn,ss);strcat(ifn,ss);
		strcat(fn_,ps);strcat(ifn_,ips);
		strcat(fn_,ssl);strcat(ifn_,ssl);
		}else{
		if(i<=99)
		{
		strcat(fn,ps);strcat(ifn,ips);
		ssl[strlen(ssl)]=(char)(48+((int)i/(int)10));
		ssl[strlen(ssl)]=(char)(48+i-(int)10*((int)i/(int)10));
		strcat(fn,ssl);strcat(ifn,ssl);
		strcat(fn,ss);strcat(ifn,ss);
		strcat(fn_,ps);strcat(ifn_,ips);
		strcat(fn_,ssl);strcat(ifn_,ssl);
		}else{
		if(i<=999)
		{
		strcat(fn,ps);strcat(ifn,ips);
		ssl[strlen(ssl)]=(char)(48+((int)i/(int)100));
		ssl[strlen(ssl)]=(char)(48+(i-(int)100*((int)i/(int)100))/(int)10);
		ssl[strlen(ssl)]=(char)(48+((int)i-(int)100*((int)i/(int)100))-(int)10*(((int)i-(int)100*((int)i/(int)100))/(int)10));
		strcat(fn,ssl);strcat(ifn,ssl);
		strcat(fn,ss);strcat(ifn,ss);
		strcat(fn_,ps);strcat(ifn_,ips);
		strcat(fn_,ssl);strcat(ifn_,ssl);
		}else{
		strcat(fn,ps);strcat(ifn,ips);
		ssl[strlen(ssl)]=(char)(48+((int)i/(int)1000));
		ssl[strlen(ssl)]=(char)(48+(i-(int)1000*((int)i/(int)1000))/(int)100);
		ssl[strlen(ssl)]=(char)(48+((((int)i-(int)1000*((int)i/(int)1000))-(int)100*(((int)i-(int)1000*((int)i/(int)1000))/(int)100))/10));
		ssl[strlen(ssl)]=(char)(48+((int)i-(int)1000*((int)i/(int)1000))-(int)100*(((int)i-(int)1000*((int)i/(int)1000))/(int)100)
		-(int)10*((((int)i-(int)1000*((int)i/(int)1000))-(int)100*(((int)i-(int)1000*((int)i/(int)1000))/(int)100))/10));		
		strcat(fn,ssl);strcat(ifn,ssl);
		strcat(fn,ss);strcat(ifn,ss);
		strcat(fn_,ps);strcat(ifn_,ips);
		strcat(fn_,ssl);strcat(ifn_,ssl);
		}
		}
		}
		printf("%s\n",fn);p_1(i,fn,ifn);
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+4);p_2(fn,fn_,4,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+5);p_2(fn,fn_,5,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+6);p_2(fn,fn_,6,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+7);p_2(fn,fn_,7,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+8);p_2(fn,fn_,8,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+9);p_2(fn,fn_,9,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)10/(int)10));
		ssl[strlen(ssl)]=(char)(48+10-(int)10*((int)10/(int)10));p_2(fn,fn_,10,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)11/(int)10));
		ssl[strlen(ssl)]=(char)(48+11-(int)10*((int)11/(int)10));p_2(fn,fn_,11,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)12/(int)10));
		ssl[strlen(ssl)]=(char)(48+12-(int)10*((int)12/(int)10));p_2(fn,fn_,12,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)13/(int)10));
		ssl[strlen(ssl)]=(char)(48+13-(int)10*((int)13/(int)10));p_2(fn,fn_,13,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)14/(int)10));
		ssl[strlen(ssl)]=(char)(48+14-(int)10*((int)14/(int)10));p_2(fn,fn_,14,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)15/(int)10));
		ssl[strlen(ssl)]=(char)(48+15-(int)10*((int)15/(int)10));p_2(fn,fn_,15,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)16/(int)10));
		ssl[strlen(ssl)]=(char)(48+16-(int)10*((int)16/(int)10));p_2(fn,fn_,16,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)17/(int)10));
		ssl[strlen(ssl)]=(char)(48+17-(int)10*((int)17/(int)10));p_2(fn,fn_,17,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)18/(int)10));
		ssl[strlen(ssl)]=(char)(48+18-(int)10*((int)18/(int)10));p_2(fn,fn_,18,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)19/(int)10));
		ssl[strlen(ssl)]=(char)(48+19-(int)10*((int)19/(int)10));p_2(fn,fn_,19,ssl);
		//
		for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)20/(int)10));
		ssl[strlen(ssl)]=(char)(48+20-(int)10*((int)20/(int)10));p_2(fn,fn_,20,ssl);
        	remove(fn);remove(ifn);
		//
	}
	return(0);
}