#define tempdir "temp\\"

#define dbfile "db"
#define maxstr 2000
#define maxparam 202
#define maxtab 500

#define graphdir "graph\\"

    char name[maxstr];  // program name
    char id[maxstr];    // id
    char ttime[maxstr];   //time
    char numf[maxstr];  // # of file resutl
    char param[maxstr];
    char program[maxstr];
    int num=-1;
    char author[maxstr];
    char desc[maxstr];
    
    char outdir[maxstr];    
    char datadir[maxstr];    
    char ana[maxstr];    

    char tmp[maxstr];    
//remove the EOF charactor
void rem(char s[]){
    int i,j;
    if (s[strlen(s)-1]=='\n') s[strlen(s)-1]=0;
    j=0;
    while(s[j]==' ') j++;
    for (i=j;i<strlen(s);i++) s[i-j]=s[i];
    s[i-j]=0;
}

// look for a record in the database
// return 1 if success, 0 otherwise
int readDatabase(char *arg){
    FILE *f;
    f=fopen(dbfile,"r");
    
    int ok=0;
//    printf("%s\n",param);
    while(fgets(name,maxstr,f)){                
        rem(name);
        
        fgets(id,maxstr,f);
        rem(id);        
        fgets(author,maxstr,f);
        rem(author);
        
        fgets(desc,maxstr,f);
        rem(desc);
        fgets(param,maxstr,f);
        rem(param);
        fgets(outdir,maxstr,f);
        rem(outdir);
                
        fgets(datadir,maxstr,f);
        rem(datadir);
        fgets(program,maxstr,f);
        rem(program);
        fgets(ana,maxstr,f);
        rem(ana);
        
        fgets(ttime,maxstr,f);
        rem(ttime);
        fgets(numf,maxstr,f);
        rem(numf);
        
        if (strcmp(name,arg)==0||strcmp(id,arg)==0){
            ok=1;break;
        }
    }
    
    sscanf(numf,"%i",&num);
    fclose(f);
    
    strcpy(tmp,param);
    
    return ok;
}

char paramset[maxparam][maxstr][20];
char paramname[maxparam][20];
int nparam;
int dd[maxparam];
int n;

// 0: normal, 1: stepping 
int type(char *line){
    int i;
    for (i=0;i<strlen(line);i++)
        if (line[i]==';') return 1;
    return 0;
}

// read parameters into paramset
void readParam(char *fname){
    FILE *f;
    f=fopen(fname,"r");
    char line[maxstr];
    
    nparam=0;
//    sscanf(f,"%i",&n);
    
    while(fgets(line,maxstr,f)){
        rem(line);
        printf("%s\n",line);

        char *s=(char*)strtok(line,":,-: ");
        dd[nparam]=0;
        strcpy(paramname[nparam],s);
        
        if (type(line)==0){
            
            s=(char*)strtok(NULL,":,-; ");         
            
            while(s!=NULL){
                strcpy(paramset[nparam][dd[nparam]],s);
                s=(char*)strtok(NULL,":,-: ");
                dd[nparam]++;
            }
        }else{
            s=(char*)strtok(NULL,":,-: ");
            int first,last,step;
            
            sscanf(s,"%s",&first);
            
            s=(char*)strtok(NULL,":,-: ");
            sscanf(s,"%s",&last);
            
            s=(char*)strtok(NULL,":,-: ");
            sscanf(s,"%s",&step);
            
            while(first<=last){
                sprintf(paramset[nparam][dd[nparam]],"%i",first);
                dd[nparam]++;
                first+=step;
            }
        }
        
        nparam++;
    }

    fclose(f);
}
