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

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

Implementation PPersist par deleguation pour DVList Reza 28/04/99

File size: 10.3 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// Classe pour la gestion de persistance
285// ObjFileIO<DVList>
286
287
288//++
289// Titre Exemples
290// Utilisation des objets *MuTyV* :
291//| MuTyV mvu; // Declaration d'une variable
292//| mvu = 60; // mvu est de type entier (= 60)
293//| mvu = 66.6; // et double (= 66.6) maintenant ...
294//| MuTyV mvi(14); // On construit une variable entiere = 14
295//| float x = mvi; // x vaut 14.0
296//| MuTyV mvd(44.4); // Variable double = 44.4
297//| int k = mvd; // k vaut 44
298//| MuTyV mvs("Bonjour, Ca va ?"); // Variable chaine de caracteres
299//| string s = mvs; // s vaut "Bonjour, Ca va ?"
300// Utilisation des *DVList* :
301//| DVList dvl;
302//| dvl("toto") = 14;
303//| dvl("titi") = 25.5;
304//| dvl("tata") = "Bonjour, Ca va ?";
305// Majuscules et minuscules sont differenciees pour les noms, pas de blanc ...
306//| dvl("hello") = 88;
307//| dvl("Hello") = 77.77;
308//| dvl.Comment() = "Test d'objet DVList, avec variables hello, Hello ";
309//| dvl.Write("dvlist.ppf");
310// Plus loin, ou dans un autre programme, on relit le fichier fabrique plus haut
311//| DVList dvlr("dvlist.ppf");
312//| int k = dvlr["toto"] ; // k = 14
313//| double b = dvlr["titi"] ; // b = 25.5
314//| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?"
315//| float c = dvlr["Hello"] ; // c = 77.77
316//| int l = dvlr["Hello"] ; // l = 77
317//| int m = dvlr["hello"] ; // m = 88
318//--
319
320
321
322/* --Methode-- */
323void ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
324{
325char buf[320];
326
327int lc = dobj->Comment().length();
328if (lc > 319) lc = 319;
329if (lc > 0) {
330 sprintf(buf,"Comment: ( %6d ) ", lc);
331 s.PutLine(buf);
332 s.PutBytes(dobj->Comment().c_str(), lc);
333 }
334s.PutLine("----Variable-List---------------");
335DVList::ValList::const_iterator it;
336for(it = dobj->Begin(); it != dobj->End(); it++) {
337 switch ((*it).second.typ)
338 {
339 case 'I' :
340 sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
341 s.PutLine(buf);
342 break;
343 case 'D' :
344 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
345 s.PutLine(buf);
346 break;
347 case 'S' :
348 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
349 s.PutLine(buf);
350 break;
351 default :
352 break;
353 }
354 }
355
356s.PutLine("ZZZZZ--End-of-Varible-List------");
357}
358
359/* --Methode-- */
360void ObjFileIO<DVList>::ReadSelf(PInPersist& s)
361{
362char buf[320];
363int_4 j,iv;
364double dv;
365bool ok=true;
366buf[0] = '\0';
367dobj->Clear();
368
369s.GetLine(buf, 319); // Pour lire les "------- "
370if (buf[0] != '-') { // Il y a un champ commentaire a lire
371 buf[18] ='\0';
372 int lc = atoi(buf+11);
373 if (lc > 319) {
374 cerr << "DVList::ReadSelf() Pb/Bug ?? CommentLength= " << lc << endl;
375 lc = 319;
376 }
377 s.GetBytes(buf, lc);
378 buf[lc] ='\0';
379 dobj->Comment() = buf;
380 }
381
382while(ok) {
383 s.GetLine(buf, 319);
384 buf[319] = '\0';
385 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; }
386 j = posc(buf+2, ' ')+2;
387 buf[j] = '\0';
388 switch (buf[0])
389 {
390 case 'I' :
391 iv = (int_4)atol(buf+j+1);
392 dobj->SetI(buf+2, iv);
393 break;
394 case 'D' :
395 dv = atof(buf+j+1);
396 dobj->SetD(buf+2, dv);
397 break;
398 case 'S' :
399 dobj->SetS(buf+2, buf+j+1);
400 break;
401 default :
402 break;
403 }
404 }
405}
406
407#ifdef __CXX_PRAGMA_TEMPLATES__
408#pragma define_template ObjFileIO<DVList>
409#endif
410
411#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
412template class ObjFileIO<DVList>;
413#endif
Note: See TracBrowser for help on using the repository browser.