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 |
francois |
1133 |
if (in==NULL) |
127 |
|
|
{ |
128 |
|
|
std::cerr << "*** OT_PARAMETRES::lire : Erreur de lecture du fichier (" << nom <<") ***" << std::endl; |
129 |
|
|
return 0; |
130 |
|
|
} |
131 |
francois |
283 |
while (!feof(in)) |
132 |
|
|
{ |
133 |
|
|
char chaine[4000]; |
134 |
|
|
char* res=fgets(chaine,4000,in); |
135 |
|
|
if (!feof(in)) |
136 |
|
|
{ |
137 |
|
|
char nom[255],c; |
138 |
|
|
char nom2[255]; |
139 |
|
|
nom2[0]=0; |
140 |
francois |
1028 |
int nb=sscanf(chaine,"%s %s",nom,nom2); |
141 |
|
|
if (nb==2) |
142 |
|
|
if (strcmp(nom,"->")==0) |
143 |
|
|
{ |
144 |
|
|
lire(nom2); |
145 |
francois |
1132 |
ajouter(nom2); |
146 |
francois |
1028 |
continue; |
147 |
|
|
} |
148 |
|
|
nb=sscanf(chaine,"%s %c %s",nom,&c,nom2); |
149 |
francois |
681 |
if (nb!=3) continue; |
150 |
|
|
double val; |
151 |
francois |
738 |
char *aide=strstr(chaine,"//"); |
152 |
|
|
if (aide==NULL) aide=chaine+strlen(chaine)-1; else aide=aide+2; |
153 |
francois |
737 |
for (int i=0;i<strlen(aide);i++) |
154 |
|
|
if (aide[i]=='\n') aide[i]=0; |
155 |
francois |
396 |
int num=sscanf(nom2,"%lf",&val); |
156 |
francois |
737 |
if (num==1) ajouter(nom,nom2,DOUBLE,aide); |
157 |
|
|
else ajouter(nom,nom2,STRING,aide); |
158 |
francois |
283 |
} |
159 |
|
|
} |
160 |
|
|
fclose(in); |
161 |
couturad |
964 |
return 1; // return OK; |
162 |
francois |
258 |
} |
163 |
|
|
|
164 |
francois |
1132 |
void OT_PARAMETRES::ajouter(std::string chaine) |
165 |
|
|
{ |
166 |
|
|
params.push_back(chaine); |
167 |
|
|
} |
168 |
francois |
258 |
|
169 |
|
|
|
170 |
francois |
1042 |
double OT_PARAMETRES::get_valeur(std::string chaine,int num) |
171 |
francois |
258 |
{ |
172 |
francois |
283 |
int taille=nom_param.size(); |
173 |
|
|
for (int i=0;i<taille;i++) |
174 |
|
|
{ |
175 |
|
|
std::string valtmp=nom_param[i]; |
176 |
|
|
if (valtmp==chaine) |
177 |
|
|
{ |
178 |
francois |
1042 |
OT_CHAINE opc; |
179 |
|
|
std::vector<std::string> tab=opc.split(valeur_param[i],';'); |
180 |
francois |
283 |
double val; |
181 |
francois |
1042 |
sscanf(tab[num].c_str(),"%lf",&val); |
182 |
francois |
283 |
return val; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
} |
186 |
|
|
return 0.; |
187 |
francois |
258 |
} |
188 |
|
|
|
189 |
francois |
1042 |
void OT_PARAMETRES::change_valeur(std::string chaine,double val,int num) |
190 |
francois |
512 |
{ |
191 |
|
|
int taille=nom_param.size(); |
192 |
|
|
for (int i=0;i<taille;i++) |
193 |
|
|
{ |
194 |
|
|
std::string valtmp=nom_param[i]; |
195 |
|
|
if (valtmp==chaine) |
196 |
|
|
{ |
197 |
francois |
1042 |
OT_CHAINE opc; |
198 |
|
|
std::vector<std::string> tab=opc.split(valeur_param[i],';'); |
199 |
|
|
char mess[255]; |
200 |
francois |
512 |
sprintf(mess,"%lf",val); |
201 |
francois |
1042 |
tab[num]=mess; |
202 |
|
|
valeur_param[i]=""; |
203 |
|
|
for (int j=0;j<tab.size();j++) |
204 |
|
|
{ |
205 |
|
|
valeur_param[i]=valeur_param[i]+tab[j]; |
206 |
|
|
if (i!=tab.size()-1) |
207 |
|
|
valeur_param[i]=valeur_param[i]+";"; |
208 |
|
|
|
209 |
|
|
} |
210 |
francois |
512 |
} |
211 |
|
|
|
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
void OT_PARAMETRES::change_valeur(std::string chaine,std::string val) |
216 |
|
|
{ |
217 |
|
|
int taille=nom_param.size(); |
218 |
|
|
for (int i=0;i<taille;i++) |
219 |
|
|
{ |
220 |
|
|
std::string valtmp=nom_param[i]; |
221 |
|
|
if (valtmp==chaine) |
222 |
|
|
{ |
223 |
|
|
valeur_param[i]=val; |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
} |
227 |
|
|
} |
228 |
|
|
|
229 |
francois |
603 |
int OT_PARAMETRES::get_num_param(std::string chaine) |
230 |
|
|
{ |
231 |
|
|
int taille=nom_param.size(); |
232 |
|
|
for (int i=0;i<taille;i++) |
233 |
|
|
{ |
234 |
|
|
std::string valtmp=nom_param[i]; |
235 |
|
|
if (valtmp==chaine) |
236 |
|
|
{ |
237 |
|
|
return i; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
} |
241 |
|
|
return -1; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
francois |
258 |
std::string OT_PARAMETRES::get_nom(std::string chaine) |
246 |
|
|
{ |
247 |
francois |
283 |
int taille=nom_param.size(); |
248 |
|
|
for (int i=0;i<taille;i++) |
249 |
|
|
{ |
250 |
|
|
std::string valtmp=nom_param[i]; |
251 |
|
|
if (valtmp==chaine) |
252 |
|
|
{ |
253 |
cuillier |
338 |
return valeur_param[i]; |
254 |
francois |
283 |
} |
255 |
|
|
|
256 |
|
|
} |
257 |
|
|
return ""; |
258 |
francois |
258 |
} |
259 |
|
|
|
260 |
francois |
396 |
int OT_PARAMETRES::get_type(std::string chaine) |
261 |
|
|
{ |
262 |
|
|
int taille=nom_param.size(); |
263 |
|
|
for (int i=0;i<taille;i++) |
264 |
|
|
{ |
265 |
|
|
std::string valtmp=nom_param[i]; |
266 |
|
|
if (valtmp==chaine) |
267 |
|
|
{ |
268 |
|
|
return type_param[i]; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
} |
272 |
|
|
return 0; |
273 |
|
|
} |
274 |
francois |
271 |
double OT_PARAMETRES::get_valeur(int num) |
275 |
|
|
{ |
276 |
francois |
283 |
double val; |
277 |
|
|
sscanf(valeur_param[num].c_str(),"%lf",&val); |
278 |
|
|
return val; |
279 |
francois |
271 |
} |
280 |
|
|
|
281 |
|
|
std::string OT_PARAMETRES::get_nom(int num) |
282 |
|
|
{ |
283 |
francois |
283 |
return nom_param[num]; |
284 |
francois |
271 |
} |
285 |
|
|
|
286 |
francois |
396 |
int OT_PARAMETRES::get_type(int num) |
287 |
|
|
{ |
288 |
|
|
return type_param[num]; |
289 |
|
|
} |
290 |
francois |
271 |
int OT_PARAMETRES::get_nb(void) |
291 |
|
|
{ |
292 |
francois |
283 |
return nom_param.size(); |
293 |
francois |
271 |
} |
294 |
|
|
|
295 |
|
|
|
296 |
francois |
258 |
void OT_PARAMETRES::vide(void) |
297 |
|
|
{ |
298 |
francois |
283 |
nom_param.clear(); |
299 |
|
|
valeur_param.clear(); |
300 |
|
|
aide_param.clear(); |
301 |
francois |
396 |
type_param.clear(); |
302 |
francois |
1132 |
params.clear(); |
303 |
francois |
258 |
} |
304 |
|
|
|