1 |
francois |
265 |
#include <stdio.h> |
2 |
francois |
1019 |
#include <string.h> |
3 |
couturad |
926 |
#include <iostream> |
4 |
francois |
258 |
#include "ot_parametres.h" |
5 |
francois |
1042 |
#include "ot_chaine.h" |
6 |
francois |
258 |
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
OT_PARAMETRES::OT_PARAMETRES() |
19 |
|
|
{ |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
OT_PARAMETRES::OT_PARAMETRES(OT_PARAMETRES &mdd) |
23 |
|
|
{ |
24 |
francois |
283 |
int taille=nom_param.size(); |
25 |
|
|
for (int i=0;i<taille;i++) |
26 |
|
|
{ |
27 |
|
|
nom_param.insert(nom_param.end(),mdd.nom_param[i]); |
28 |
|
|
valeur_param.insert(valeur_param.end(),mdd.valeur_param[i]); |
29 |
francois |
396 |
aide_param.insert(aide_param.end(),mdd.aide_param[i]); |
30 |
|
|
type_param.insert(type_param.end(),mdd.type_param[i]); |
31 |
francois |
283 |
} |
32 |
francois |
258 |
} |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
OT_PARAMETRES::~OT_PARAMETRES() |
36 |
|
|
{ |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
|
40 |
francois |
396 |
void OT_PARAMETRES::ajouter(std::string chaine,double valeur,int typep,std::string aide) |
41 |
francois |
258 |
{ |
42 |
francois |
603 |
int res=get_num_param(chaine); |
43 |
|
|
if (res==-1) |
44 |
|
|
{ |
45 |
|
|
nom_param.insert(nom_param.end(),chaine); |
46 |
|
|
char mess[255]; |
47 |
|
|
sprintf(mess,"%lf",valeur); |
48 |
|
|
valeur_param.insert(valeur_param.end(),mess); |
49 |
|
|
aide_param.insert(aide_param.end(),aide); |
50 |
|
|
type_param.insert(type_param.end(),typep); |
51 |
|
|
} |
52 |
|
|
else |
53 |
|
|
{ |
54 |
|
|
char mess[255]; |
55 |
|
|
sprintf(mess,"%lf",valeur); |
56 |
|
|
valeur_param[res]=mess; |
57 |
|
|
if (aide.length()>aide_param[res].length()) aide_param[res]=aide; |
58 |
|
|
} |
59 |
francois |
258 |
} |
60 |
|
|
|
61 |
francois |
1042 |
void OT_PARAMETRES::ajouter(std::string chaine,std::vector<double> &valeur,int typep,std::string aide) |
62 |
|
|
{ |
63 |
|
|
int res=get_num_param(chaine); |
64 |
|
|
if (res==-1) |
65 |
|
|
{ |
66 |
|
|
nom_param.insert(nom_param.end(),chaine); |
67 |
|
|
std::string chainetmp=""; |
68 |
|
|
for (int i=0;i<valeur.size();i++) |
69 |
|
|
{ |
70 |
|
|
char mess[255]; |
71 |
|
|
sprintf(mess,"%lf",valeur[i]); |
72 |
|
|
chainetmp=chainetmp+mess; |
73 |
|
|
if (i!=valeur.size()-1) chainetmp=chainetmp+";" ; |
74 |
|
|
} |
75 |
|
|
valeur_param.insert(valeur_param.end(),chainetmp); |
76 |
|
|
aide_param.insert(aide_param.end(),aide); |
77 |
|
|
type_param.insert(type_param.end(),typep); |
78 |
|
|
} |
79 |
|
|
else |
80 |
|
|
{ |
81 |
|
|
std::string chainetmp=""; |
82 |
|
|
for (int i=0;i<valeur.size();i++) |
83 |
|
|
{ |
84 |
|
|
char mess[255]; |
85 |
|
|
sprintf(mess,"%lf",valeur[i]); |
86 |
|
|
chainetmp=chainetmp+mess; |
87 |
|
|
if (i!=valeur.size()-1) chainetmp=chainetmp+";" ; |
88 |
|
|
} |
89 |
|
|
valeur_param[res]=chainetmp; |
90 |
|
|
if (aide.length()>aide_param[res].length()) aide_param[res]=aide; |
91 |
|
|
} |
92 |
|
|
} |
93 |
francois |
396 |
void OT_PARAMETRES::ajouter(std::string chaine,std::string valeurstring,int typep,std::string aide) |
94 |
francois |
258 |
{ |
95 |
francois |
603 |
int res=get_num_param(chaine); |
96 |
|
|
if (res==-1) |
97 |
|
|
{ |
98 |
|
|
nom_param.insert(nom_param.end(),chaine); |
99 |
|
|
valeur_param.insert(valeur_param.end(),valeurstring); |
100 |
|
|
aide_param.insert(aide_param.end(),aide); |
101 |
|
|
type_param.insert(type_param.end(),typep); |
102 |
|
|
} |
103 |
|
|
else |
104 |
|
|
{ |
105 |
|
|
valeur_param[res]=valeurstring; |
106 |
|
|
if (aide.length()>aide_param[res].length()) aide_param[res]=aide; |
107 |
|
|
} |
108 |
|
|
|
109 |
francois |
258 |
} |
110 |
|
|
|
111 |
|
|
void OT_PARAMETRES::enregistrer(char *nom) |
112 |
|
|
{ |
113 |
francois |
283 |
FILE* in=fopen(nom,"wt"); |
114 |
|
|
int taille=nom_param.size(); |
115 |
|
|
for (int i=0;i<taille;i++) |
116 |
|
|
fprintf(in,"%s = %s //%s\n",nom_param[i].c_str(),valeur_param[i].c_str(),aide_param[i].c_str()); |
117 |
francois |
1132 |
taille=params.size(); |
118 |
|
|
for (int i=0;i<taille;i++) |
119 |
|
|
fprintf(in,"-> %s\n",params[i].c_str()); |
120 |
francois |
283 |
fclose(in); |
121 |
francois |
258 |
} |
122 |
|
|
|
123 |
couturad |
964 |
int OT_PARAMETRES::lire(char *nom) |
124 |
francois |
258 |
{ |
125 |
francois |
283 |
FILE* in=fopen(nom,"rt"); |
126 |
couturad |
964 |
if (in==NULL) return 0; // return FAIL |
127 |
couturad |
929 |
// if (in==NULL) |
128 |
|
|
// { |
129 |
|
|
// std::cerr << "*** OT_PARAMETRES::lire : Erreur de lecture du fichier (" << nom <<") ***" << std::endl; |
130 |
|
|
// return; |
131 |
|
|
// } |
132 |
francois |
283 |
while (!feof(in)) |
133 |
|
|
{ |
134 |
|
|
char chaine[4000]; |
135 |
|
|
char* res=fgets(chaine,4000,in); |
136 |
|
|
if (!feof(in)) |
137 |
|
|
{ |
138 |
|
|
char nom[255],c; |
139 |
|
|
char nom2[255]; |
140 |
|
|
nom2[0]=0; |
141 |
francois |
1028 |
int nb=sscanf(chaine,"%s %s",nom,nom2); |
142 |
|
|
if (nb==2) |
143 |
|
|
if (strcmp(nom,"->")==0) |
144 |
|
|
{ |
145 |
|
|
lire(nom2); |
146 |
francois |
1132 |
ajouter(nom2); |
147 |
francois |
1028 |
continue; |
148 |
|
|
} |
149 |
|
|
nb=sscanf(chaine,"%s %c %s",nom,&c,nom2); |
150 |
francois |
681 |
if (nb!=3) continue; |
151 |
|
|
double val; |
152 |
francois |
738 |
char *aide=strstr(chaine,"//"); |
153 |
|
|
if (aide==NULL) aide=chaine+strlen(chaine)-1; else aide=aide+2; |
154 |
francois |
737 |
for (int i=0;i<strlen(aide);i++) |
155 |
|
|
if (aide[i]=='\n') aide[i]=0; |
156 |
francois |
396 |
int num=sscanf(nom2,"%lf",&val); |
157 |
francois |
737 |
if (num==1) ajouter(nom,nom2,DOUBLE,aide); |
158 |
|
|
else ajouter(nom,nom2,STRING,aide); |
159 |
francois |
283 |
} |
160 |
|
|
} |
161 |
|
|
fclose(in); |
162 |
couturad |
964 |
return 1; // return OK; |
163 |
francois |
258 |
} |
164 |
|
|
|
165 |
francois |
1132 |
void OT_PARAMETRES::ajouter(std::string chaine) |
166 |
|
|
{ |
167 |
|
|
params.push_back(chaine); |
168 |
|
|
} |
169 |
francois |
258 |
|
170 |
|
|
|
171 |
francois |
1042 |
double OT_PARAMETRES::get_valeur(std::string chaine,int num) |
172 |
francois |
258 |
{ |
173 |
francois |
283 |
int taille=nom_param.size(); |
174 |
|
|
for (int i=0;i<taille;i++) |
175 |
|
|
{ |
176 |
|
|
std::string valtmp=nom_param[i]; |
177 |
|
|
if (valtmp==chaine) |
178 |
|
|
{ |
179 |
francois |
1042 |
OT_CHAINE opc; |
180 |
|
|
std::vector<std::string> tab=opc.split(valeur_param[i],';'); |
181 |
francois |
283 |
double val; |
182 |
francois |
1042 |
sscanf(tab[num].c_str(),"%lf",&val); |
183 |
francois |
283 |
return val; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
} |
187 |
|
|
return 0.; |
188 |
francois |
258 |
} |
189 |
|
|
|
190 |
francois |
1042 |
void OT_PARAMETRES::change_valeur(std::string chaine,double val,int num) |
191 |
francois |
512 |
{ |
192 |
|
|
int taille=nom_param.size(); |
193 |
|
|
for (int i=0;i<taille;i++) |
194 |
|
|
{ |
195 |
|
|
std::string valtmp=nom_param[i]; |
196 |
|
|
if (valtmp==chaine) |
197 |
|
|
{ |
198 |
francois |
1042 |
OT_CHAINE opc; |
199 |
|
|
std::vector<std::string> tab=opc.split(valeur_param[i],';'); |
200 |
|
|
char mess[255]; |
201 |
francois |
512 |
sprintf(mess,"%lf",val); |
202 |
francois |
1042 |
tab[num]=mess; |
203 |
|
|
valeur_param[i]=""; |
204 |
|
|
for (int j=0;j<tab.size();j++) |
205 |
|
|
{ |
206 |
|
|
valeur_param[i]=valeur_param[i]+tab[j]; |
207 |
|
|
if (i!=tab.size()-1) |
208 |
|
|
valeur_param[i]=valeur_param[i]+";"; |
209 |
|
|
|
210 |
|
|
} |
211 |
francois |
512 |
} |
212 |
|
|
|
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
void OT_PARAMETRES::change_valeur(std::string chaine,std::string val) |
217 |
|
|
{ |
218 |
|
|
int taille=nom_param.size(); |
219 |
|
|
for (int i=0;i<taille;i++) |
220 |
|
|
{ |
221 |
|
|
std::string valtmp=nom_param[i]; |
222 |
|
|
if (valtmp==chaine) |
223 |
|
|
{ |
224 |
|
|
valeur_param[i]=val; |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
|
230 |
francois |
603 |
int OT_PARAMETRES::get_num_param(std::string chaine) |
231 |
|
|
{ |
232 |
|
|
int taille=nom_param.size(); |
233 |
|
|
for (int i=0;i<taille;i++) |
234 |
|
|
{ |
235 |
|
|
std::string valtmp=nom_param[i]; |
236 |
|
|
if (valtmp==chaine) |
237 |
|
|
{ |
238 |
|
|
return i; |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
} |
242 |
|
|
return -1; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
|
246 |
francois |
258 |
std::string OT_PARAMETRES::get_nom(std::string chaine) |
247 |
|
|
{ |
248 |
francois |
283 |
int taille=nom_param.size(); |
249 |
|
|
for (int i=0;i<taille;i++) |
250 |
|
|
{ |
251 |
|
|
std::string valtmp=nom_param[i]; |
252 |
|
|
if (valtmp==chaine) |
253 |
|
|
{ |
254 |
cuillier |
338 |
return valeur_param[i]; |
255 |
francois |
283 |
} |
256 |
|
|
|
257 |
|
|
} |
258 |
|
|
return ""; |
259 |
francois |
258 |
} |
260 |
|
|
|
261 |
francois |
396 |
int OT_PARAMETRES::get_type(std::string chaine) |
262 |
|
|
{ |
263 |
|
|
int taille=nom_param.size(); |
264 |
|
|
for (int i=0;i<taille;i++) |
265 |
|
|
{ |
266 |
|
|
std::string valtmp=nom_param[i]; |
267 |
|
|
if (valtmp==chaine) |
268 |
|
|
{ |
269 |
|
|
return type_param[i]; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
} |
273 |
|
|
return 0; |
274 |
|
|
} |
275 |
francois |
271 |
double OT_PARAMETRES::get_valeur(int num) |
276 |
|
|
{ |
277 |
francois |
283 |
double val; |
278 |
|
|
sscanf(valeur_param[num].c_str(),"%lf",&val); |
279 |
|
|
return val; |
280 |
francois |
271 |
} |
281 |
|
|
|
282 |
|
|
std::string OT_PARAMETRES::get_nom(int num) |
283 |
|
|
{ |
284 |
francois |
283 |
return nom_param[num]; |
285 |
francois |
271 |
} |
286 |
|
|
|
287 |
francois |
396 |
int OT_PARAMETRES::get_type(int num) |
288 |
|
|
{ |
289 |
|
|
return type_param[num]; |
290 |
|
|
} |
291 |
francois |
271 |
int OT_PARAMETRES::get_nb(void) |
292 |
|
|
{ |
293 |
francois |
283 |
return nom_param.size(); |
294 |
francois |
271 |
} |
295 |
|
|
|
296 |
|
|
|
297 |
francois |
258 |
void OT_PARAMETRES::vide(void) |
298 |
|
|
{ |
299 |
francois |
283 |
nom_param.clear(); |
300 |
|
|
valeur_param.clear(); |
301 |
|
|
aide_param.clear(); |
302 |
francois |
396 |
type_param.clear(); |
303 |
francois |
1132 |
params.clear(); |
304 |
francois |
258 |
} |
305 |
|
|
|