00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include"readpar.h"
00013 #include<string.h>
00014
00015 ReadPar::ReadPar(void)
00016 {
00017 }
00018
00019 ReadPar::~ReadPar(void)
00020 {
00021 }
00022
00023 int ReadPar::OpenFile(const char *fname)
00024 { if((fp=fopen(fname,"r"))==NULL)
00025 return 0;
00026 return 1;
00027 }
00028
00029 int ReadPar::CloseFile(void)
00030 { fclose(fp);
00031 return 1;
00032 }
00033
00034
00035
00036
00037
00038 int ReadPar::ReadParameter(const char *par, int type, void *val, int vector, int item)
00039 { int pos=0, *ip=(int*)val, len = (int) strlen(par);
00040 double *dp=(double*)val;
00041 char *cp=(char*)val;
00042 rewind(fp);
00043 while(fgets(buf,MAX_LINE-1,fp)!=NULL && item>0)
00044 { if(!strncmp(par, buf, len) && (buf[len]==TAB || buf[len]==SPACE || buf[len]==NEWLINE))
00045
00046 { if(item==1)
00047 for(int i=0;i<vector;i++)
00048 { while(buf[pos]==SPACE || buf[pos]==TAB)
00049 pos++;
00050 while(buf[pos]!=SPACE && buf[pos]!=TAB)
00051 pos++;
00052 switch(type)
00053 { case 0:
00054 if(sscanf(buf+pos,"%s",cp)==EOF) return -1;
00055 cp+=strlen(cp); break;
00056 case 1:
00057 if(sscanf(buf+pos,"%d",ip)==EOF) return -1;
00058 ip++; break;
00059 case 2:
00060 if(sscanf(buf+pos,"%lf",dp)==EOF) return -1;
00061 dp++; break;
00062 }
00063 }
00064 item--;
00065 }
00066 }
00067 if(pos==0) return 0;
00068 else return 1;
00069 }