source: Sophya/trunk/SophyaLib/BaseTools/dvlist.cc@ 1130

Last change on this file since 1130 was 1080, checked in by ansari, 25 years ago

Classe MuTyV separe et etendu pour support complexe - Reza 24/7/2000

File size: 15.8 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 SysTools
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_8"), réél double précision ("r_8")
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
41static MuTyV ddvdum(-9.e19);
42
43
44/*!
45 \class SOPHYA::DVList
46 \ingroup SysTools
47 This class can be used to construct list of values associated with names.
48 Variables names should not contain space characters and is limited to 64
49 characters. The DVList class uses \b SOPHYA::MuTyV objects to hold values
50 of type string, integer (\b int_8) or float (\b r_8). A comment string
51 can be associated with each variable name. A global comment string
52 can be attached to the DVList object. DVList objects can conveniently be
53 used to represent FITS headers. The class \b SOPHYA::ObjFileIO<DVList>
54 handles serialisation for DVList. (See SOPHYA::PPersist ).
55
56 // ------- Using DVList objects ------
57 DVList dvl;
58 dvl("toto") = 14; // Integer type value (=14) named toto
59 dvl("titi") = 25.5; // float type value (=25.5) named titi
60 dvl("tata") = "Bonjour !"; // string type value (="Bonjour !") named tata
61 // Upper and lower case letters are distinguished
62 dvl("hello") = 88;
63 dvl("Hello") = 77.77;
64 dvl.Comment() = "DVList test object, with values named hello, Hello ";
65 // Saving the dvl object into a PPF file
66 POutStream os("dvl.ppf");
67 os << dvl;
68 // later on ...
69 DVList dvlr;
70 PInStream is("dvl.ppf");
71 is << dvlr;
72 int k = dvlr["toto"] ; // k = 14
73 r_8 b = dvlr["titi"] ; // b = 25.5
74 string s = dvlr["tata"] ; // s = "Bonjour !"
75 int m = dvlr["hello"] ; // m = 88
76
77 \endcode
78*/
79
80//++
81// Titre Constructeurs
82//--
83
84//++
85// DVList()
86// Constructeur par défaut
87// DVList(DVList& cfd)
88// Constructeur par copie. Le nouvel objet est une copie complète de "cfd"
89// DVList(char* flnm)
90// Constructeur avec initialisation à partir du contenu du fichier (PPF)
91// "flnm". Le fichier doit avoir été créé par la méthode "Write()"
92//--
93
94/* --Methode-- */
95/*! Default constructor */
96DVList::DVList()
97{
98comment = "";
99}
100
101/* --Methode-- */
102/*! copy constructor */
103DVList::DVList(const DVList& dvl)
104{
105Merge(dvl);
106}
107
108/* --Methode-- */
109/*! Copy constructor - Object initialized using the PPF file \b flnm */
110DVList::DVList(char *flnm)
111{
112PInPersist s(flnm);
113ObjFileIO<DVList> fiodvl(this);
114fiodvl.Read(s);
115}
116
117
118/* --Methode-- */
119DVList::~DVList()
120{
121}
122
123//++
124// Titre Gestion des variables et opérateurs
125//--
126
127//++
128// void Clear()
129// Supprime la définition de toutes les variables de l'objet.
130// DVList& Merge(const DVList& lv)
131// Fusionne l'objet avec la liste des variables de l'objet "lv"
132// DVList& operator= (const DVList& cofr)
133// Remplace la liste des variables de l'objet par celle de l'objet "cofr".
134//--
135
136/* --Methode-- */
137/*! Copy operator - Replaces the variables list with the list from \b dvl */
138DVList& DVList::operator= (const DVList& dvl)
139{
140Clear();
141return(Merge(dvl));
142}
143
144
145/* --Methode-- */
146/*! Resets the object and clears the variable list and global comment */
147void DVList::Clear()
148{
149mvlist.erase(mvlist.begin(), mvlist.end());
150comment = "";
151}
152
153/* --Methode-- */
154/*! Appends the values from the object \b dvl to the objects list */
155DVList& DVList::Merge(const DVList& dvl)
156{
157ValList::const_iterator it;
158for(it = dvl.mvlist.begin(); it != dvl.mvlist.end(); it++)
159 {
160 switch ((*it).second.elval.typ)
161 {
162 case 'I' :
163 SetI((*it).first, (*it).second.elval.iv);
164 break;
165 case 'D' :
166 SetD((*it).first, (*it).second.elval.dv);
167 break;
168 case 'Z' :
169 SetZ((*it).first, complex<r_8>((*it).second.elval.dv, (*it).second.elval.dv_im));
170 break;
171 case 'S' :
172 SetS((*it).first, *((*it).second.elval.strv));
173 break;
174 default :
175 break;
176 }
177 }
178comment = comment + "\n" + dvl.comment;
179return(*this);
180}
181
182
183//++
184// int_8 GetI(string const& key, int_8 def=-1)
185// r_8 GetD(string const& key, r_8 def=-9.e19)
186// string GetS(string const& key, char* def="")
187// Retourne la valeur de la variable de nom "key" et de type entier, réél,
188// chaine de caracteres.
189// Si la variable n'existe pas, la valeur par défaut "def" est renvoyée.
190// string GetComment(string const& key)
191// Retourne le commentaire associé à la variable de nom "key".
192//--
193
194/* --Methode-- */
195/*! Returns the value corresponding to name \b key, converted to integer
196 Default value \b def is returned if name \b key not found */
197int_8 DVList::GetI(string const& key, int_8 def)
198{
199ValList::iterator it = mvlist.find(key);
200if (it == mvlist.end()) return(def);
201return((*it).second.elval.iv);
202}
203
204/* --Methode-- */
205/*! Returns the value corresponding to name \b key, converted to double
206 Default value \b def is returned if name \b key not found */
207r_8 DVList::GetD(string const& key, r_8 def)
208{
209ValList::iterator it = mvlist.find(key);
210if (it == mvlist.end()) return(def);
211return((*it).second.elval.dv);
212}
213
214/* --Methode-- */
215/*! Returns the value corresponding to name \b key, converted to complex
216 Default value \b def is returned if name \b key not found */
217complex<r_8> DVList::GetZ(string const& key, complex<r_8> def)
218{
219ValList::iterator it = mvlist.find(key);
220if (it == mvlist.end()) return(def);
221return((*it).second.elval.dv);
222}
223
224/* --Methode-- */
225/*! Returns the value corresponding to name \b key, converted to string
226 Default value \b def is returned if name \b key not found */
227string DVList::GetS(string const& key, char* def)
228{
229ValList::iterator it = mvlist.find(key);
230if (it == mvlist.end()) return(def);
231return(*((*it).second.elval.strv));
232}
233
234/* --Methode-- */
235/*! Returns the comment associated with name \b key */
236string DVList::GetComment(string const& key)
237{
238ValList::iterator it = mvlist.find(key);
239if (it == mvlist.end()) return("");
240return((*it).second.elcomm);
241}
242
243//++
244// void SetI(string const& key, int_8 val)
245// void SetD(string const& key, r_8 val)
246// void SetZ(string const& key, complex<r_8> val)
247// void SetS(string const& key, char* val)
248// void SetS(string const& key, string val)
249// Crée la variable de nom "key", de type entier, double, complexe, string et
250// lui attribue la valeur "val". Si une variable du même nom existe,
251// sa valeur et eventuellement son type sont modifiés. Les noms de
252// variables ne doivent pas contenir de caractères spéciaux,
253// en particulier pas de CR/LF.
254// void SetComment(string const& key, string const& comm)
255// Modifie le commentaire associé à la variable de nom "key", si
256// celle-ci existe. Le texte du commentaire ne doit pas contenir
257// de caractères spéciaux, et en particulier pas de CR/LF.
258//--
259
260/* --Methode-- */
261/*! Appends or sets the integer value \b val in the list with name \b key */
262void DVList::SetI(string const& key, int_8 val)
263{
264Get(key) = (int_8)val;
265}
266
267/* --Methode-- */
268void DVList::SetD(string const& key, r_8 val)
269/*! Appends or sets the double value \b val in the list with name \b key */
270{
271Get(key) = (r_8)val;
272}
273
274/* --Methode-- */
275void DVList::SetZ(string const& key, complex<r_8> val)
276/*! Appends or sets the complex value \b val in the list with name \b key */
277{
278Get(key) = val;
279}
280
281/* --Methode-- */
282/*! Appends or sets the string value \b val in the list with name \b key */
283void DVList::SetS(string const& key, char const* val)
284{
285MuTyV div(val);
286Get(key) = div;
287}
288
289/* --Methode-- */
290/*! Appends or sets the string value \b val in the list with name \b key */
291void DVList::SetS(string const& key, string const& val)
292{
293MuTyV div(val);
294Get(key) = div;
295}
296
297/* --Methode-- */
298/*! Assigns the comment \b comm with the name \b key .
299 Does nothing if the entry with name is not present in the list */
300void DVList::SetComment(string const& key, string const& comm)
301{
302ValList::iterator it = mvlist.find(key);
303if (it == mvlist.end()) return;
304(*it).second.elcomm = comm;
305}
306
307//++
308// MuTyV& Get(string const& key)
309// Renvoie une référence sur l'objet "MuTyV" de la liste avec le nom "key".
310// Si cet objet (variable) n'existe pas, il est créé.
311// MuTyV& operator() (string const& key)
312// MuTyV& operator[] (string const& key)
313//
314// Renvoie la variable de nom "key". Equivalent à "Get(key)".
315// string& Comment()
316// Renvoie une référence sur le champ commentaire de l'objet.
317//--
318
319/* --Methode-- */
320/*! Return the MuTyV value associated with name \b key .
321 Adds an entry of type integer in the list if \b key is not present in the list */
322MuTyV& DVList::Get(string const& key)
323{
324size_t l = key.length();
325if ( (l < 1) || (key.find_first_of(" ") < l) ) return(ddvdum);
326// dvlElement xxx = {(int_8)0 , ""}; marche pas sur mac/CW (!) - cf DY
327dvlElement xxx; xxx.elval = (int_8)0; xxx.elcomm = "";
328ValList::iterator it = mvlist.find(key);
329if (it == mvlist.end()) mvlist[key] = xxx;
330it = mvlist.find(key);
331if (it == mvlist.end()) return(ddvdum);
332else return((*it).second.elval);
333}
334
335//++
336// Titre Entrée-Sortie
337//--
338
339//++
340// void Print()
341// Imprime (sur "cout") la liste des variables et leurs valeurs.
342// void Print(ostream& os)
343// Imprime sur le flot "os" la liste des variables et leurs valeurs.
344// ostream& operator << (ostream& s, DVList& dvl)
345// sortie sur flot "s" (Appel a "Print(s)").
346//--
347
348/* --Methode-- */
349/*! Prints a brief description of object on on the output stream \b os */
350void DVList::Show(ostream& os) const
351{
352os << "DVList::Show() - NVar= " << (int)mvlist.size() << "\n";
353os << comment << endl;
354}
355
356/* --Methode-- */
357/*! Prints the list of variables on the output stream \b os */
358void DVList::Print(ostream& os) const
359{
360os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n";
361if (comment.length() > 0) os << comment << endl;
362char buff[256];
363ValList::const_iterator it;
364for(it = mvlist.begin(); it != mvlist.end(); it++) {
365 switch ((*it).second.elval.typ)
366 {
367 case 'I' :
368 sprintf(buff, "%s = %ld (int) %s\n", (*it).first.substr(0,64).c_str(),
369 (long)((*it).second.elval.iv), (*it).second.elcomm.substr(0,128).c_str());
370 break;
371 case 'D' :
372 sprintf(buff, "%s = %.20g (double) %s\n", (*it).first.substr(0,64).c_str(),
373 (*it).second.elval.dv, (*it).second.elcomm.substr(0,128).c_str());
374 break;
375 case 'Z' :
376 sprintf(buff, "%s = %.20g %.20g i (complex) %s\n", (*it).first.substr(0,64).c_str(),
377 (*it).second.elval.dv, (*it).second.elval.dv_im, (*it).second.elcomm.substr(0,128).c_str());
378 break;
379 case 'S' :
380 sprintf(buff, "%s = %s (string) %s\n", (*it).first.substr(0,64).c_str(),
381 (*it).second.elval.strv->c_str(), (*it).second.elcomm.substr(0,128).c_str());
382 break;
383 default :
384 break;
385 }
386 os << (string)buff;
387 }
388os << endl;
389}
390
391
392//++
393// Titre Exemples
394// Utilisation des objets *MuTyV* :
395//| MuTyV mvu; // Declaration d'une variable
396//| mvu = 60; // mvu est de type entier (= 60)
397//| mvu = 66.6; // et double (= 66.6) maintenant ...
398//| MuTyV mvi(14); // On construit une variable entiere = 14
399//| r_4 x = mvi; // x vaut 14.0
400//| MuTyV mvd(44.4); // Variable double = 44.4
401//| int k = mvd; // k vaut 44
402//| MuTyV mvs("Bonjour, Ca va ?"); // Variable chaine de caracteres
403//| string s = mvs; // s vaut "Bonjour, Ca va ?"
404// Utilisation des *DVList* :
405//| DVList dvl;
406//| dvl("toto") = 14;
407//| dvl("titi") = 25.5;
408//| dvl("tata") = "Bonjour, Ca va ?";
409// Majuscules et minuscules sont differenciees pour les noms, pas de blanc ...
410//| dvl("hello") = 88;
411//| dvl("Hello") = 77.77;
412//| dvl.Comment() = "Test d'objet DVList, avec variables hello, Hello ";
413//| dvl.Write("dvlist.ppf");
414// Plus loin, ou dans un autre programme, on relit le fichier fabrique plus haut
415//| DVList dvlr("dvlist.ppf");
416//| int k = dvlr["toto"] ; // k = 14
417//| r_8 b = dvlr["titi"] ; // b = 25.5
418//| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?"
419//| r_4 c = dvlr["Hello"] ; // c = 77.77
420//| int l = dvlr["Hello"] ; // l = 77
421//| int m = dvlr["hello"] ; // m = 88
422//--
423
424
425//----------------------------------------------------------
426// Classe pour la gestion de persistance
427// ObjFileIO<DVList>
428//----------------------------------------------------------
429
430/* --Methode-- */
431void ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
432{
433char buf[512];
434string sfw;
435
436int lc = dobj->Comment().length();
437if (lc > 0) {
438 sprintf(buf,"Comment: ( %6d ) ", lc);
439 sfw = buf; s.PutStr(sfw);
440 s.PutStr(dobj->Comment());
441 }
442sfw = "----Variable-List---------------"; s.PutStr(sfw);
443DVList::ValList::const_iterator it;
444for(it = dobj->Begin(); it != dobj->End(); it++) {
445 switch ((*it).second.elval.typ) {
446 case 'I' :
447 sprintf(buf,"I %s %ld", (*it).first.substr(0,64).c_str(), (long)((*it).second.elval.iv) );
448 sfw = buf; s.PutStr(sfw);
449 break;
450 case 'D' :
451 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.elval.dv );
452 sfw = buf; s.PutStr(sfw);
453 break;
454 case 'Z' :
455 sprintf(buf,"Z %s %.20g %.20g", (*it).first.substr(0,64).c_str(), (*it).second.elval.dv,
456 (*it).second.elval.dv_im);
457 sfw = buf; s.PutStr(sfw);
458 break;
459 case 'S' :
460 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.elval.strv->c_str() );
461 sfw = buf; s.PutStr(sfw);
462 break;
463 default :
464 break;
465 }
466// Ecriture eventuelle du commentaire associe
467 if ((*it).second.elcomm.length() > 0) {
468 sprintf(buf,"# %s", (*it).second.elcomm.substr(0,256).c_str());
469 sfw = buf; s.PutStr(sfw);
470 }
471}
472
473sfw = "ZZZZZ--End-of-Varible-List------"; s.PutStr(sfw);
474}
475
476/* --Methode-- */
477void ObjFileIO<DVList>::ReadSelf(PInPersist& s)
478{
479char buf[512];
480string sfr;
481int_8 j,iv,k;
482r_8 dv, dvi;
483complex<r_8> z;
484bool ok=true;
485buf[0] = '\0';
486dobj->Clear();
487
488s.GetStr(sfr); // Pour lire les "------- "
489if (sfr[0] != '-') { // Il y a un champ commentaire a lire
490 s.GetStr(sfr);
491 dobj->Comment() = sfr;
492 }
493
494string key="";
495while(ok) {
496 s.GetStr(sfr);
497 strncpy(buf, sfr.c_str(), 512);
498 buf[511] = '\0';
499 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; }
500 if (buf[0] == '#') {
501 dobj->SetComment(key, buf+2);
502 continue;
503 }
504 j = posc(buf+2, ' ')+2;
505 buf[j] = '\0';
506 switch (buf[0]) {
507 case 'I' :
508 iv = (int_8)atol(buf+j+1);
509 key = buf+2;
510 dobj->SetI(key, iv);
511 break;
512 case 'D' :
513 dv = atof(buf+j+1);
514 key = buf+2;
515 dobj->SetD(key, dv);
516 break;
517 case 'Z' :
518 k = posc(buf+j+1, ' ')+j+1;
519 buf[k] = '\0';
520 dv = atof(buf+j+1);
521 dvi = atof(buf+k+1);
522 key = buf+2;
523 z = complex<r_8>(dv, dvi);
524 dobj->SetZ(key, z);
525 break;
526 case 'S' :
527 key = buf+2;
528 dobj->SetS(key, buf+j+1);
529 break;
530 default :
531 break;
532 }
533 }
534}
535
536#ifdef __CXX_PRAGMA_TEMPLATES__
537#pragma define_template ObjFileIO<DVList>
538#endif
539
540#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
541template class ObjFileIO<DVList>;
542#endif
Note: See TracBrowser for help on using the repository browser.