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