#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "table.h"

void usage(void){
	printf("\n\tlsc-file [output file] [input file]\n\n");

}

//search()  return matching index number, -1 for no match

int search(char *string){
	int i;
	//int ret_val=-1;
	for(i=0; i<6; i++){
		
		//printf("string: %s\n", table[i].series);
		if( strcmp(string, table[i].series) == 0 ){
			//ret_val=i;
			return(i);		
		}

	}
	return(-1);
}


/*
struct two_chars{
	char a;
	char b;
};


struct custom{
        unsigned word : 12;
};

//struct custom long_char;

//char *table[4096];


struct translation_structure{
	//char instr[100];
	char tablestr[100];
	int a;
	int b;
	//unsigned longchar : 12;

}table[]={
	//256 entire char set
	{"0", 0, 0},
	{"1", 1},
	{"2", 2},
	{"3", 3},
	{"4", 4},
	{"5", 5},
	{"6", 6},
	{"7", 7},
	{"8", 8},
	{"9", 9},
	{"10", 10},
	{"01210288110934", 11}
	//2048 repeats of all chars up to 8x
	
	//1792 common sequences
};



static struct long_char table_lookup(char *string){

	struct long_char return_val;
	int element = 0;
	while(element < 4096){
 		if( strcmp(sting, table[element]) ){
			return_val.word = element;
		}

	}


}





int pattern_match(char current_char){

	static char last_char;
	if( strcmp(last_char, current_char) == 0){
		
		return((int)1);
	}else{
		
		return((int)0);
	}


}
*/

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

	char *input_file;
	char *output_file;
	char compare[100];
	char copystr[100];
	FILE *file_handle1;
	FILE *file_handle2;
	char temp;
	int counter1, counter2;
	int character, series;
	int num1, num2, num3;
	int num1_bak;
	int search_len;
	int search_ret1, search_ret2;
	//struct two_chars return_words;
	//struct custom long_char;

	if(argc != 3){
		usage();
		exit(1);
	}else{
		input_file = (char *)malloc( strlen(argv[2])*sizeof(char) );
		output_file = (char *)malloc( strlen(argv[1])*sizeof(char) );

		//input_file = (const char *)calloc(strlen(argv[1]), sizeof(char));
		//output_file = (const char *)calloc(strlen(argv[2]), sizeof(char));
		//strcpy(input_file, argv[1]);
		//strcpy(output_file, argv[2]);
		(void)sprintf(input_file, "%s", argv[2]);
		(void)sprintf(output_file, "%s", argv[1]);

		//input_file = argv[1];
		//output_file = argv[2];

	}

	printf("input_file: %s %d %d\n", input_file, (int)strlen(argv[2]), (int)strlen(input_file));
	printf("output_file: %s %d %d\n", output_file, (int)strlen(argv[1]), (int)strlen(output_file));

                file_handle1 = fopen(input_file , "r");
                if(file_handle1 != NULL){  

			file_handle2 = fopen(output_file,"w");
			//series=0;
			if(file_handle2 != NULL){        
				//series=1;
				character=getc(file_handle1);
				while(character != EOF){
					//if(series == 0){
					//	character = num1;
					//}else{
					//	character=getc(file_handle1);
					//}
					
					//character=(file_handle1);
					num1=getc(file_handle1);

					//for(character=getc(file_handle1); character != EOF; character=getc(file_handle1) ){
					//character=getc(file_handle1);
					//run until match is not met, then return previous match and ...
				
					//num2 =
					//num3 = 

					series = 0;
					while(num1 == character && series < 100){
						num1=getc(file_handle1);
						series++;
					}
					
					//rewind the file counter and check for largest string match from structure
					//compare
//printf("test 3\n");
					if(series != 0){
						fseek ( file_handle1 ,  (series*-1) , SEEK_CUR );
					}
//printf("test 4\n");
					num1_bak=num1;
					//temp=character;					
					strcpy(compare, &character);
					//temp=num1;
					strcat(compare, &num1);
//printf("test 5\n");
					//search_len=0;
					//search_ret1=search(&compare);
//printf("test 6\n");
					search_ret2=-1;
					for(search_len=0; search_len < 156 && num1 != EOF; search_len++){
//printf("test %d\n", search_len);
						search_ret1=search(compare);
//printf("here 1\n");
						if(search_ret1 != -1){
printf("match found\n");
							if(strlen(table[search_ret1].series)>4 )
								search_ret2=search_ret1;
printf("strlen %d\n", (int)strlen(table[search_ret1].series));
						}

						//search_ret2=search_ret1;
						num1=getc(file_handle1);
//printf("here 2\n");
						strcat(compare, &num1);
//printf("here 3\n");
						//search_ret1=search(&compare);
						//search_ret2=search_ret1;
						//search_len++;
					}

					//search()  return matching index number, -1 for no match

					if(series >= strlen(table[search_ret2].series)){
						//printf("test 1\n");
						//if(search_len != 0){
printf("here 1\n");
						fseek ( file_handle1 ,  (search_len*-1) , SEEK_CUR );
printf("here 2\n");
						//}
						num1 = num1_bak;
						fprintf(file_handle2, "%c%c", character, series);
					}else{
						printf("test 2 %d\n", search_ret2);
						//if(search_ret1 != -1
						fprintf(file_handle2, "%c%c", table[search_ret2].a, table[search_ret2].b);
						fseek ( file_handle1 ,  (search_len*-1)+strlen(table[search_ret2].series) , SEEK_CUR );
						//fseek ( file_handle1 , search_len*-1, SEEK_CUR );
						num1=getc(file_handle1);
					}

					
										

					//search for repeating sequences

					//for(num1=getc(file_handle1); num1 == character; num1=getc(file_handle1) ){	
					//	series++;
						//num2 = 256*series + character;
					//}

					//num2 = 256*series + character;					
					
					//num2 =  
					//num3 = 
					
					//fprintf(file_handle2, "%c%c", character, series);
					

                                        //if(series == 0){
                                        character = num1;
                                        //}else{
                                        //        character=getc(file_handle1);
                                        //}  

					//if(series > 0){
					//	character=num1;
					//}

					//num1

				//while( strcpy(compare, getc(file_handle1));
				//while(match(compare)


				//for(counter2=0; counter2 < 16 && character[counter2] != EOF; counter2++){
				//	character[counter2]=getc(file_handle1);
				//}

				//counter2 = 0;
				//for(character[0]=getc(file_handle1); character[0] != EOF; character[0]=getc(file_handle1)){
				//	counter2++;
				//}
				//printf("input file is %d Bytes\n", counter2);

				//if(counter2 > 15){
					//character=getc(file_handle1);
					
					//for(counter1=0; counter1<11; counter1++){
					//	if( table[counter1].instring[0] == character ){
					//		printf("match");
					//		long_char.word = table[counter1].longchar;
					//		//fprintf(file_handle2, "%c", long_char.word);
					//	}
					//}
					//printf("writing");
                                	//fprintf(file_handle2, "%c", long_char.word);                                
				                        		
				
				}

				//fclose(file_handle1);
                        	//fclose(file_handle2);
			}else{
				printf("error opening output file!\n");
				exit(1);
			}
                }else{
			printf("error opening input file!\n");
			exit(1);
		}

	fclose(file_handle1);
	fclose(file_handle2);

	return 0;
}
