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