source: Sophya/trunk/SophyaLib/NTools/dvlist.cc@ 244

Last change on this file since 244 was 244, checked in by ansari, 26 years ago

beaucoup modifs rz+cmv 22/4/99

File size: 9.9 KB
Line 
1// Classe Dynamic Variable List (DVList) de PEIDA
2// R. Ansari 1997
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4
5#include "machdefs.h"
6#include <stdlib.h>
7#include <stdio.h>
8
9#include "dvlist.h"
10#include "strutil.h"
11
12//++
13// Class DVList
14// Lib Outils++
15// include dvlist.h
16//
17// Cette classe permet de gérer une ensemble de variables (ou paramètres)
18// pouvant être définies dynamiquement à l'execution. Le nom des
19// variables ne doit pas contenir de blancs ("<espace>") et est
20// limité à 64 caractères maximum. Cette classe
21// offre la possibilité de sauvegarder l'ensemble
22// des variables (Nom, Type, Valeur) dans un fichier, ou de
23// recréer l'objet DVList et l'ensemble de ses variables à
24// partir d'un fichier (Objet PPersist). Une zone commentaire (max=320 c.)
25// est associée à chaque objet DVList, accessible à travers
26// la méthode "Comment()". Les objets de cette classe sont
27// en particulier destinés à être inclus dans d'autres objets
28// PPersist plus complexes. La classe DVList gère des
29// variables de type entier ("int_4"), réél double précision ("double")
30// et de type chaine de caracteres ("string, char*", maxi 30 caracteres ).
31// Une classe intermédiaire (*MuTyV*) est utilisée pour représenter une
32// variable et fournit les services de conversion entre les différents types.
33//--
34//--
35//++
36// Links Parents
37// PPersist
38//--
39
40
41char MuTyV::myStrBuf[64]; // Declare static ds le .h
42
43static MuTyV ddvdum(-9.e19);
44
45
46//++
47// Titre Constructeurs
48//--
49
50//++
51// DVList()
52// Constructeur par défaut
53// DVList(DVList& cfd)
54// Constructeur par copie. Le nouvel objet est une copie complète de "cfd"
55// DVList(char* flnm)
56// Constructeur avec initialisation à partir du contenu du fichier (PPF)
57// "flnm". Le fichier doit avoir été créé par la méthode "Write()"
58//--
59
60/* --Methode-- */
61DVList::DVList()
62{
63comment = "";
64}
65
66/* --Methode-- */
67DVList::DVList(DVList& dvl)
68{
69Merge(dvl);
70}
71
72/* --Methode-- */
73DVList::DVList(char *flnm)
74{
75PInPersist s(flnm);
76Read(s);
77}
78
79
80/* --Methode-- */
81DVList::~DVList()
82{
83}
84
85//++
86// Titre Gestion des variables et opérateurs
87//--
88
89//++
90// void Clear()
91// Supprime la définition de toutes les variables de l'objet.
92// DVList& Merge(const DVList& lv)
93// Fusionne l'objet avec la liste des variables de l'objet "lv"
94// DVList& operator= (const DVList& cofr)
95// Remplace la liste des variables de l'objet par celle de l'objet "cofr".
96//--
97
98/* --Methode-- */
99DVList& DVList::operator= (const DVList& dvl)
100{
101Clear();
102return(Merge(dvl));
103}
104
105
106/* --Methode-- */
107void DVList::Clear()
108{
109mvlist.erase(mvlist.begin(), mvlist.end());
110comment = "";
111}
112
113/* --Methode-- */
114DVList& DVList::Merge(const DVList& dvl)
115{
116ValList::const_iterator it;
117for(it = dvl.mvlist.begin(); it != dvl.mvlist.end(); it++)
118 {
119 switch ((*it).second.typ)
120 {
121 case 'I' :
122 SetI((*it).first, (*it).second.mtv.iv);
123 break;
124 case 'D' :
125 SetD((*it).first, (*it).second.mtv.dv);
126 break;
127 case 'S' :
128 SetS((*it).first, (*it).second.mtv.strv);
129 break;
130 default :
131 break;
132 }
133 }
134comment = comment + "\n" + dvl.comment;
135return(*this);
136}
137
138
139//++
140// int_4 GetI(string const& key, int_4 def=-1)
141// double GetD(string const& key, double def=-9.e19)
142// string GetS(string const& key, char* def="")
143// Retourne la valeur de la variable de nom "key" et de type entier, réél,
144// chaine de caracteres.
145// Si la variable n'existe pas, la valeur par défaut "def" est renvoyée.
146//--
147
148/* --Methode-- */
149int_4 DVList::GetI(string const& key, int_4 def)
150{
151ValList::iterator it = mvlist.find(key);
152if (it == mvlist.end()) return(def);
153if ( (*it).second.typ != 'I') return(def);
154return((*it).second.mtv.iv);
155}
156
157/* --Methode-- */
158double DVList::GetD(string const& key, double def)
159{
160ValList::iterator it = mvlist.find(key);
161if (it == mvlist.end()) return(def);
162if ( (*it).second.typ != 'D') return(def);
163return((*it).second.mtv.dv);
164}
165
166/* --Methode-- */
167string DVList::GetS(string const& key, char* def)
168{
169ValList::iterator it = mvlist.find(key);
170if (it == mvlist.end()) return(def);
171if ( (*it).second.typ != 'S') return(def);
172return((*it).second.mtv.strv);
173}
174
175//++
176// void SetI(string const& key, int_4 val)
177// void SetD(string const& key, double val)
178// void SetS(string const& key, char* val)
179// void SetS(string const& key, string val)
180// Crée la variable de nom "key", de type entier, double, string et
181// lui attribue la valeur "val". Si une variable du même nom existe,
182// sa valeur et eventuellement son type sont modifiés.
183//--
184
185/* --Methode-- */
186void DVList::SetI(string const& key, int_4 val)
187{
188Get(key) = (int_4)val;
189}
190
191/* --Methode-- */
192void DVList::SetD(string const& key, double val)
193{
194Get(key) = (double)val;
195}
196
197/* --Methode-- */
198void DVList::SetS(string const& key, char const* val)
199{
200MuTyV div(val);
201Get(key) = div;
202}
203
204/* --Methode-- */
205void DVList::SetS(string const& key, string val)
206{
207MuTyV div(val);
208Get(key) = div;
209}
210
211
212//++
213// MuTyV& Get(string const& key)
214// Renvoie une référence sur l'objet "MuTyV" de la liste avec le nom "key".
215// Si cet objet (variable) n'existe pas, il est créé.
216// MuTyV& operator() (string const& key)
217// MuTyV& operator[] (string const& key)
218//
219// Renvoie la variable de nom "key". Equivalent à "Get(key)".
220// string& Comment()
221// Renvoie une référence sur le champ commentaire de l'objet.
222//--
223
224/* --Methode-- */
225MuTyV& DVList::Get(string const& key)
226{
227size_t l = key.length();
228if ( (l < 1) || (key.find_first_of(" ") < l) ) return(ddvdum);
229ValList::iterator it = mvlist.find(key);
230if (it == mvlist.end()) mvlist[key] = (int_4) 0; // $CHECK$ EA. Ambigu si pas de cast...
231it = mvlist.find(key);
232if (it == mvlist.end()) return(ddvdum);
233else return((*it).second);
234}
235
236//++
237// Titre Entrée-Sortie
238//--
239
240//++
241// void Print()
242// Imprime (sur "cout") la liste des variables et leurs valeurs.
243// void Print(ostream& os)
244// Imprime sur le flot "os" la liste des variables et leurs valeurs.
245// ostream& operator << (ostream& s, DVList& dvl)
246// sortie sur flot "s" (Appel a "Print(s)").
247// void Write(string const& fn)
248// Ecriture d'un fichier PPersist de nom "fn"
249// void Read(string const& fn)
250// Lecture d'un fichier PPersist de nom "fn"
251//--
252
253
254/* --Methode-- */
255void DVList::Print(ostream& os) const
256{
257os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n";
258os << "Comment: " << comment << "\n";
259char buff[128];
260ValList::const_iterator it;
261for(it = mvlist.begin(); it != mvlist.end(); it++) {
262 switch ((*it).second.typ)
263 {
264 case 'I' :
265 sprintf(buff, "%s = %d (int) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
266 break;
267 case 'D' :
268 sprintf(buff, "%s = %.20g (double) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
269 break;
270 case 'S' :
271 sprintf(buff, "%s = %s (string) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
272 break;
273 default :
274 break;
275 }
276 os << (string)buff;
277 }
278os << endl;
279}
280
281
282/* --Methode-- */
283void DVList::WriteSelf(POutPersist& s) const
284{
285char buf[320];
286
287int lc = comment.length();
288if (lc > 319) lc = 319;
289if (lc > 0) {
290 sprintf(buf,"Comment: ( %6d ) ", lc);
291 s.PutLine(buf);
292 s.PutBytes(comment.c_str(), lc);
293 }
294s.PutLine("----Variable-List---------------");
295ValList::const_iterator it;
296for(it = mvlist.begin(); it != mvlist.end(); it++) {
297 switch ((*it).second.typ)
298 {
299 case 'I' :
300 sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
301 s.PutLine(buf);
302 break;
303 case 'D' :
304 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
305 s.PutLine(buf);
306 break;
307 case 'S' :
308 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
309 s.PutLine(buf);
310 break;
311 default :
312 break;
313 }
314 }
315
316s.PutLine("ZZZZZ--End-of-Varible-List------");
317}
318
319/* --Methode-- */
320void DVList::ReadSelf(PInPersist& s)
321{
322char buf[320];
323int_4 j,iv;
324double dv;
325bool ok=true;
326buf[0] = '\0';
327Clear();
328
329s.GetLine(buf, 319); // Pour lire les "------- "
330if (buf[0] != '-') { // Il y a un champ commentaire a lire
331 buf[18] ='\0';
332 int lc = atoi(buf+11);
333 if (lc > 319) {
334 cerr << "DVList::ReadSelf() Pb/Bug ?? CommentLength= " << lc << endl;
335 lc = 319;
336 }
337 s.GetBytes(buf, lc);
338 buf[lc] ='\0';
339 comment = buf;
340 }
341
342while(ok) {
343 s.GetLine(buf, 319);
344 buf[319] = '\0';
345 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; }
346 j = posc(buf+2, ' ')+2;
347 buf[j] = '\0';
348 switch (buf[0])
349 {
350 case 'I' :
351 iv = (int_4)atol(buf+j+1);
352 SetI(buf+2, iv);
353 break;
354 case 'D' :
355 dv = atof(buf+j+1);
356 SetD(buf+2, dv);
357 break;
358 case 'S' :
359 SetS(buf+2, buf+j+1);
360 break;
361 default :
362 break;
363 }
364 }
365
366}
367
368//++
369// Titre Exemples
370// Utilisation des objets *MuTyV* :
371//| MuTyV mvu; // Declaration d'une variable
372//| mvu = 60; // mvu est de type entier (= 60)
373//| mvu = 66.6; // et double (= 66.6) maintenant ...
374//| MuTyV mvi(14); // On construit une variable entiere = 14
375//| float x = mvi; // x vaut 14.0
376//| MuTyV mvd(44.4); // Variable double = 44.4
377//| int k = mvd; // k vaut 44
378//| MuTyV mvs("Bonjour, Ca va ?"); // Variable chaine de caracteres
379//| string s = mvs; // s vaut "Bonjour, Ca va ?"
380// Utilisation des *DVList* :
381//| DVList dvl;
382//| dvl("toto") = 14;
383//| dvl("titi") = 25.5;
384//| dvl("tata") = "Bonjour, Ca va ?";
385// Majuscules et minuscules sont differenciees pour les noms, pas de blanc ...
386//| dvl("hello") = 88;
387//| dvl("Hello") = 77.77;
388//| dvl.Comment() = "Test d'objet DVList, avec variables hello, Hello ";
389//| dvl.Write("dvlist.ppf");
390// Plus loin, ou dans un autre programme, on relit le fichier fabrique plus haut
391//| DVList dvlr("dvlist.ppf");
392//| int k = dvlr["toto"] ; // k = 14
393//| double b = dvlr["titi"] ; // b = 25.5
394//| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?"
395//| float c = dvlr["Hello"] ; // c = 77.77
396//| int l = dvlr["Hello"] ; // l = 77
397//| int m = dvlr["hello"] ; // m = 88
398//--
Note: See TracBrowser for help on using the repository browser.