//##### For date conversion ie: Jan -> 1
int months1(const char *month)
{
        int var;
        if (0 == strcmp( month, "Jan" )){
                var = 1;
        }else if ( 0 == strcmp( month, "Feb" ) ) {
                        var = 2;
        }else if ( 0 == strcmp( month, "Mar" ) ) {
                        var = 3;
        }else if ( 0 == strcmp( month, "Apr" ) ) {
                        var = 4;
        }else if ( 0 == strcmp( month, "May" ) ) {
                        var = 5;
        }else if ( 0 == strcmp( month, "Jun" ) ) {
                        var = 6;
        }else if ( 0 == strcmp( month, "Jul" ) ) {
                        var = 7;
        }else if ( 0 == strcmp( month, "Aug" ) ) {
                        var = 8;
        }else if ( 0 == strcmp( month, "Sep" ) ) {
                        var = 9;
        }else if ( 0 == strcmp( month, "Oct" ) ) {
                        var = 10;
        }else if ( 0 == strcmp( month, "Nov" ) ) {
                        var = 11;
        }else if ( 0 == strcmp( month, "Dec" ) ) {
                        var = 12;
        }else{
                printf("Invalid Month String!\n");
                exit (1);
        }
        return var;
}


void read_file() {
	FILE* fp1;
	char linestring[100];
	//char copystring[100];
	char workstring[100];
	char temp[5];
        const char *token;
        //const char *line;
        const char *search = " ";

	//Date 		    Open 	   High 	    Low 	    Close 	Volume
	//Oct 12, 2009    1,071.63        1,079.46        1,071.63        1,076.18        0
	//Jan 2, 1979     96.11   96.96   95.22   96.73   183,400

	int i;
	int month;
	int day;
	int year;
	//int mcount = 0; 
	//int dcount = 0;
	int ycount = 0;
	double open;
	double high;
	double low;
	double close;
	double volume;


        if( ( fp1 = fopen( "./spx.txt", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening ./spx.txt\n");
                //return;
                exit( 1 );
        }

        while( fgets(linestring, sizeof(linestring), fp1) != NULL)
        {
		//token = strtok(linestring, search);
		//token = strtok(NULL, search);

		memset ((void*) temp, '\0', sizeof (temp));
		//strcpy(copystring, linestring);

		//if comma ignore it, if white space ignore all but 1 (remove commas and extra spaces, for new string)
		temp[0]=linestring[0];
		temp[1]='\0';
		memset ((void*) workstring, '\0', sizeof (workstring));
		strcpy(workstring, temp);
				

//printf("linestring: %s\n", linestring);
//printf("linestring size: %lu\n", sizeof(linestring) );
		for(i=1; i <= strlen(linestring); i++) {

			//memset ((void*) temp, '\0', sizeof (temp));
			if(linestring[i] != ',' && linestring[i] != '\t' && linestring[i] != '\n' && linestring[i] != '\0') { 

				if( (workstring[i-1] == ' ') && (workstring[i] == ' ') ) {
					//do nothing
				}else{
					//if(linestring[i] == ' ') {
					temp[0]=linestring[i];
					temp[1]='\0';
					strcat(workstring, temp);
					//printf("did strcat, workstring: %s\n", workstring);
				}	
			}	 			
		}


//printf("workstring: %s\n", workstring);

		//divide string by spaces and put into array of structures

		token = strtok(workstring, search);
		  //month
		month = months1(token);

		token = strtok(NULL, search);
		  //day
		day=atoi(token);		

		token = strtok(NULL, search);
		  //year
		year=atoi(token);		

		token = strtok(NULL, search);
		  //open
		open=atof(token);

		token = strtok(NULL, search);
		  //high
		high=atof(token);

		token = strtok(NULL, search);
		  //low
		low=atof(token);

		token = strtok(NULL, search);
		  //close
		close=atof(token);

		token = strtok(NULL, search);
		  //volume
		volume=atof(token);


		//loacte element location based on date
		//apply data values

		//if (year[ycount].year == year)

		//for(counter1=1; counter1<32; counter1++) {
			//if(counter1 == day) { 
		//ycount = abs(1979 - year);
/*
				year[ycount].month[month].day[day].open = open;
				year[ycount].month[month].day[day].high = high;
				year[ycount].month[month].day[day].low = low;
				year[ycount].month[month].day[day].close = close;
				year[ycount].month[month].day[day].volume = volume;
				year[ycount].month[month].day[day].offday = 0;
			//} 
			//else {
			//	year[ycount].month[mcount].day[dcount].offday = 1;
			//}
		//}

		//if(mcount == 12 && day is equal to last day in month) { ycount++; }
*/


		//printf("workstring: %s\n", workstring);

		//ycount = ( (ycount*365) + ((month - 1)*31) + day );
		days[ycount].year=year;
		days[ycount].month=month;
		days[ycount].day=day;
		days[ycount].open = open;
		days[ycount].high = high;
		days[ycount].low = low;
		days[ycount].close = close;
		days[ycount].volume = volume;
		//days[ycount].offday = 0;
		ycount++;


	}
	fclose(fp1);

	return;

}
