#include "parse.h"
void getFromString (const char* buf, int* n){
	*n=atoi(buf);
	printf("%d\n",*n);
}

void getFromString (const char* buf, size_t* n){
	*n=(size_t)(atoi(buf));
	printf("%lu\n",*n);
}

void getFromString (const char* buf, double* n){
	*n=(double)(atof(buf));
	printf("%15.6e\n",*n);
}

void getFromString (const char* buf, char* n){
	sscanf(buf,"%s",n);
	printf("%s\n",n);
}

/*Extract droplet species and mole fractions*/
int parseDrop(FILE* input, const char* keyword,char dropSpec[][10],double dropMole[],const size_t bufLen){
	char buf[bufLen];
    char buf1[bufLen];
	char comment[1];
	char *ret;
	while (fgets(buf,bufLen, input)!=NULL){
		comment[0]=buf[0];
		if(strncmp(comment,"#",1)==0){
		}
		else{
            strcpy(buf1,buf); 
			ret=strtok(buf,"=");
            //DEBUG
            //printf("Current KEYWORD in input: %20s \n",ret);
			if(strcmp(ret,keyword)==0){
                    char* modifiedFuel = NULL;
                    char* equalSign = strstr(buf1,"=");
                    if(equalSign!= NULL){
                        modifiedFuel = new char [strlen(equalSign)+1];
                        strcpy(modifiedFuel,equalSign+1);
                        //DEBUG
                        //printf("modifiedFuel:%20s \n",modifiedFuel);
                        char* token = strtok(modifiedFuel,",");
                        int index = 0 ; 
                        char* list[2]; 
                        while(token!= NULL){
                            //DEBUG
                            //printf("TOKEN :%20s \n",token);
                            list[index] = token; 
                            token = strtok(NULL,",");
                            index++;
                        }
                        //for (int i=0;i<2;i++){
                        //    printf("%20s",list[i]);
                        //}
                        for(int i=0;i<2;i++){
                            char* name= strtok(list[i],":"); 
                            char* value = strtok(NULL,":");
                            //DEBUG
                           // printf("Name:%10s,Value:%10s \n",name,value);
                            strcpy(dropSpec[i],name);
                            dropMole[i]=std::stod(value);
                           // printf("In the dropArray,Name:%10s,Value:%.3f\n",dropSpec[i],dropMole[i]);
                        }
                        delete[] modifiedFuel;
                   } 
                   printf("%10s:%10s:%.3f,%10s:%.3f\n",keyword,dropSpec[0],dropMole[0],dropSpec[1],dropMole[1]);
                   //printf("IF statement is execuated. \n");
		           rewind(input);
                   //delete[] modifiedFuel;
			       return(0);
            }
		}
	}
	rewind(input);
	return(-1);
}
