#include "dataIO.h"

//==========================================================
dataIO::dataIO(){
	
	
}

//==========================================================
void dataIO::load_data(){
	
	//reset
	nl_counter = 0;
	char_count = 0;
	counter = 0;
	
	//get file name from user
	printf("Enter name of new file to load\n");
	scanf("%s", fileIn);
	
	//load data from txt file
	of = fopen(fileIn, "r");
	ch = getc(of);
	while (ch != EOF)
	{
		//keep track of what line we are on
		if(ch == '\n'){
			nl_counter++;
			comma_count = 0;	
		}
		
		
		//NUM_POINTS
		if(nl_counter == 0){
		
			char_n_pts[counter] = ch;
		}
		
		//X_POINTS
		if(nl_counter == 1 and ch != '\n'){
		
			temp_char[char_count] = ch;
			
			char_count++;
			
			if(ch == ','){
				//convert temp string to int
				temp = atoi(temp_char);
				//save in x_pts array
				x_pts[comma_count] = float(temp);
				//printf("x_pts = %f\n", x_pts[comma_count]);
				
				//clear for next and count
				char_count = 0;
				for (int i=0; i<9; i++){
					temp_char[i] = 'a';
				}
				comma_count++;
			}
		}
		
		//Y_POINTS
		if(nl_counter == 2 and ch != '\n'){
		
			temp_char[char_count] = ch;
			
			char_count++;
			
			if(ch == ','){
				//convert temp string to int
				temp = atoi(temp_char);
				//save in y_pts array
				y_pts[comma_count] = float(temp);
				//printf("y_pts = %f\n", y_pts[comma_count]);
				
				//clear for next and count
				char_count = 0;
				for (int i=0; i<9; i++){
					temp_char[i] = 'a';
				}
				comma_count++;
			}
		}
		
		//T_POINTS
		if(nl_counter == 3 and ch != '\n'){
		
			temp_char[char_count] = ch;
			
			char_count++;
			
			if(ch == ','){
				//convert temp string to int
				//temp = atoi(temp_char);
				//save in t_pts array
				t_pts[comma_count] = atoi(temp_char);
				//printf("t_pts = %d\n", t_pts[comma_count]);
				
				//clear for next and count
				char_count = 0;
				for (int i=0; i<9; i++){
					temp_char[i] = 'a';
				}
				comma_count++;
			}
		}
		
		/*
		//SKIP_POINTS
		if(nl_counter == 4 and ch != '\n'){
		
			temp_char[char_count] = ch;
			
			char_count++;
			
			if(ch == ','){
				//convert temp string to int
				//temp = atoi(temp_char);
				//save in t_pts array
				skip_pts[comma_count] = atoi(temp_char);
				//printf("t_pts = %d\n", t_pts[comma_count]);
				
				//clear for next and count
				char_count = 0;
				for (int i=0; i<9; i++){
					temp_char[i] = 'a';
				}
				comma_count++;
			}
		}
		*/
		
		counter++;
		ch = getc(of);
		
	}
	
	//convert string to int
	n_pts = atoi(char_n_pts);
	printf("n_pts = %d\n", n_pts);
	
	//printf("char_n_pts = %c\n",char_n_pts);
	//printf("n_pts = %n", n_pts); 
	
}
//==========================================================
void dataIO::save_data(float * xPos, float * yPos, int * timePos, int * skipPos, int nPoints){
	//printf("numPoints = %d\n", nPoints);
	
	printf("Enter name of new file to create (should end in .graf)\n");
	scanf("%s", fileOut);
	sv = fopen(fileOut, "w");
	
	//numPoints
	fprintf(sv, "%d", nPoints);
	putc('\n', sv);
	
	//x data
	for(int i=0; i<nPoints; i++){
		fprintf(sv, "%d", int(xPos[i]));
		putc(',', sv);
		
	}
	putc('\n', sv);
	
	//y data
	for(int i=0; i<nPoints; i++){
		fprintf(sv, "%d", int(yPos[i]));
		putc(',', sv);
		
	}
	putc('\n', sv);
	
	//t data
	for(int i=0; i<nPoints; i++){
		fprintf(sv, "%d", timePos[i]);
		putc(',', sv);
		
	}
	putc('\n', sv);
	
	/*
	//skip points
	for(int i=0; i<nPoints; i++){
		fprintf(sv, "%d", skipPos[i]);
		//printf("sssskip_pts = %d\n", skipPos[i]);
		putc(',', sv);
		
	}
	putc('\n', sv);
	*/
	
	
	fclose(sv);

}

//=========================================================

