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

//##### 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;
}


int main() {
	FILE* fp1;
	FILE* fp2;

	char linestring[100];
	//char copystring[100];

	char workstring[100];
	char temp[10];
        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

	//02-Jan-79,227.15,110.816,226.8,110.932

	int valid = 0;
	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( "./prices.txt", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening ./spx.txt\n");
                //return;
                exit( 1 );
        }

//printf("opened prices.txt\n");
        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] != '\n' && linestring[i] != '\0') { 

				//if(linestring[i] == '\t') {
				//	temp[0]=' ';
				//	temp[1]='\0';
				if( (workstring[i-1] == ' ') && (linestring[i] == ' ' || linestring[i] == '\t') ) {
					//do nothing
				}else{
					//24-Dec-99,287.8,178.26,-,-
					if( (linestring[i] == '-' || linestring[i] == ',' ) && i < 10) {
						temp[0]=' ';
						temp[1]='\0';
						//valid = 1;
					}else if(linestring[i] == '-' && i >= 10) {
						temp[0]='\*';
						//temp[1]='*';
						temp[1]='\0';

					}else if(linestring[i] == ',' && i >= 10) {

						temp[0]=' ';
						temp[1]='\0';
					}else if(linestring[i] == '\t') {
						temp[0]=' ';
						temp[1]='\0';
								
					}else{
						temp[0]=linestring[i];
						temp[1]='\0';
						//valid = 1;
					}
					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

//workstring: 02 Jan 79,227.15,110.816,226.8,110.932
//02-Jan-79,227.15,110.816,226.8,110.932
//Date,$,£,Euros,$,£,Euros
		token = strtok(workstring, search);

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

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

		token = strtok(NULL, search);
		  //year
		year=atoi(token);		
		if(year > 78) {
			year+=1900;
		}else{
			year+=2000;
		}


		token = strtok(NULL, search);
		  //open
		open=atof(token);
		if(open != open) {
			open = 0;
		}

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

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

                if(year >= 2000) {
			token = strtok(NULL, search);
		}



		token = strtok(NULL, search);
		  //close
		close=atof(token);
		if(close != close) {
			close = 0;
		}




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

		//Oct 16, 2009    1,094.67        1,094.67        1,081.53        1,087.68        0

		//if(year == 2000) {

		//printf("got first line: %s %d, %d %lf %lf\n", temp, day, year, open, close );
		//}

		if (fp2 = fopen("./full.txt", "a")) {
			fprintf(fp2, "%s %d, %d %lf %lf\n", temp, day, year, open, close );


		} fclose(fp2);

		/*
		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 0;

}
