#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); } std::string getFromString(const char* buf){ std::string n(buf); printf("%s\n",n.c_str()); return 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); } ///* Specialization for string */ //template<> //int parseDropSpec(FILE* input, const char* keyword, const size_t bufLen,const int* dropType, std::vector& dropPara){ // 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){ // //printf("Comment!:%s\n",buf); // } // else{ // ret=strtok(buf,"="); // if(strcmp(ret,keyword)==0){ // /*offset buf by keyword size + 1 for the "="*/ // /* Second argument in the strncpy function is the address !!! */ // /* Note: current version of code can only take dropType =0 or 1 */ // strncpy(buf1, buf+strlen(keyword)+1, bufLen); // printf("%10s: ",keyword); // if(*dropType ==0){ // /* Convert from char* to string */ // std::string str(buf1); // dropPara.push_back(str); // printf("%s\n",dropPara[0].c_str()); // }else{ // std::istringstream iss(buf1) ; // std::string token; // while(std::getline(iss,token,',')){ // //std::stod(token); // dropPara.push_back(token); // } // printf("%s,%s\n",dropPara[0].c_str(),dropPara[1].c_str()); // } // rewind(input); // return(0); // } // } // } // rewind(input); // return(-1); //}