ECF 1.5
readpar.cpp
1
2//
3// Class for reading parameters from text files
4// 2001/02/15
5//
6// Domagoj Jakobovic
7// FEEC, Zagreb
8// http://www.zemris.fer.hr/~yeti
9//
11
12#include"readpar.h"
13#include<string.h>
14
15ReadPar::ReadPar(void)
16{
17}
18
19ReadPar::~ReadPar(void)
20{
21}
22
23int ReadPar::OpenFile(const char *fname)
24{ if((fp=fopen(fname,"r"))==NULL)
25 return 0;
26 return 1;
27}
28
29int ReadPar::CloseFile(void)
30{ fclose(fp);
31 return 1;
32}
33
34// povratne vrijednosti:
35// 0 - nije nadjen parametar
36// -1 - nadjen ali bez vrijednosti
37// 1 - nadjen i procitana vrijednost
38int ReadPar::ReadParameter(const char *par, int type, void *val, int vector, int item)
39{ int pos=0, *ip=(int*)val, len = (int) strlen(par);
40 double *dp=(double*)val;
41 char *cp=(char*)val;
42 rewind(fp);
43 while(fgets(buf,MAX_LINE-1,fp)!=NULL && item>0)
44 { if(!strncmp(par, buf, len) && (buf[len]==TAB || buf[len]==SPACE || buf[len]==NEWLINE))
45 //if(!strncmp(par, buf, len))
46 { if(item==1)
47 for(int i=0;i<vector;i++)
48 { while(buf[pos]==SPACE || buf[pos]==TAB) // postavljanje na slijedecu prazninu
49 pos++;
50 while(buf[pos]!=SPACE && buf[pos]!=TAB)
51 pos++;
52 switch(type)
53 { case 0:
54 if(sscanf(buf+pos,"%s",cp)==EOF) return -1; // nije procitana vrijednost
55 cp+=strlen(cp); break;
56 case 1:
57 if(sscanf(buf+pos,"%d",ip)==EOF) return -1;
58 ip++; break;
59 case 2:
60 if(sscanf(buf+pos,"%lf",dp)==EOF) return -1;
61 dp++; break;
62 }
63 }
64 item--;
65 }
66 }
67 if(pos==0) return 0;
68 else return 1;
69}
type
Data types used for configuration file parameters.
Definition: Registry.h:16