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

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

Bug ds DVList corrige ds PEIDA Reza 10/9/99

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