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

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

Changement de codage du type ds MuTyV (en prevision d'introduction de
nouveaux types codes ds MuTyV + Modif ObjFIO<DVList> - Reza 9/11/2000

File size: 17.5 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.Type())
161 {
162 case MuTyV::MTVInteger :
163 SetI((*it).first, (*it).second.elval.iv);
164 break;
165 case MuTyV::MTVFloat :
166 SetD((*it).first, (*it).second.elval.dv);
167 break;
168 case MuTyV::MTVComplex :
169 SetZ((*it).first, complex<r_8>((*it).second.elval.dv, (*it).second.elval.dv_im));
170 break;
171 case MuTyV::MTVString :
172 SetS((*it).first, *((*it).second.elval.strv));
173 break;
174 default :
175 break;
176 }
177 SetComment((*it).first, (*it).second.elcomm);
178 }
179comment = comment + "\n" + dvl.comment;
180return(*this);
181}
182
183
184//++
185// int_8 GetI(string const& key, int_8 def=-1)
186// r_8 GetD(string const& key, r_8 def=-9.e19)
187// string GetS(string const& key, char* def="")
188// Retourne la valeur de la variable de nom "key" et de type entier, réél,
189// chaine de caracteres.
190// Si la variable n'existe pas, la valeur par défaut "def" est renvoyée.
191// string GetComment(string const& key)
192// Retourne le commentaire associé à la variable de nom "key".
193//--
194
195/* --Methode-- */
196/*! Returns the value corresponding to name \b key, converted to integer
197 Default value \b def is returned if name \b key not found */
198int_8 DVList::GetI(string const& key, int_8 def) const
199{
200ValList::const_iterator it = mvlist.find(key);
201if (it == mvlist.end()) return(def);
202return( (int_8)((*it).second.elval) );
203}
204
205/* --Methode-- */
206/*! Returns the value corresponding to name \b key, converted to double
207 Default value \b def is returned if name \b key not found */
208r_8 DVList::GetD(string const& key, r_8 def) const
209{
210ValList::const_iterator it = mvlist.find(key);
211if (it == mvlist.end()) return(def);
212return( (r_8)((*it).second.elval) );
213}
214
215/* --Methode-- */
216/*! Returns the value corresponding to name \b key, converted to complex
217 Default value \b def is returned if name \b key not found */
218complex<r_8> DVList::GetZ(string const& key, complex<r_8> def) const
219{
220ValList::const_iterator it = mvlist.find(key);
221if (it == mvlist.end()) return(def);
222#if defined(__GNUG__)
223complex<r_8> z;
224z = (*it).second.elval;
225return(z);
226#else
227return( (complex<r_8>)(*it).second.elval );
228#endif
229}
230
231/* --Methode-- */
232/*! Returns the value corresponding to name \b key, converted to string
233 Default value \b def is returned if name \b key not found */
234string DVList::GetS(string const& key, char* def) const
235{
236ValList::const_iterator it = mvlist.find(key);
237if (it == mvlist.end()) return(def);
238return( (string)((*it).second.elval) );
239}
240
241/* --Methode-- */
242/*! Returns the comment associated with name \b key */
243string DVList::GetComment(string const& key) const
244{
245ValList::const_iterator it = mvlist.find(key);
246if (it == mvlist.end()) return("");
247return((*it).second.elcomm);
248}
249
250//++
251// void SetI(string const& key, int_8 val)
252// void SetD(string const& key, r_8 val)
253// void SetZ(string const& key, complex<r_8> val)
254// void SetS(string const& key, char* val)
255// void SetS(string const& key, string val)
256// Crée la variable de nom "key", de type entier, double, complexe, string et
257// lui attribue la valeur "val". Si une variable du même nom existe,
258// sa valeur et eventuellement son type sont modifiés. Les noms de
259// variables ne doivent pas contenir de caractères spéciaux,
260// en particulier pas de CR/LF.
261// void SetComment(string const& key, string const& comm)
262// Modifie le commentaire associé à la variable de nom "key", si
263// celle-ci existe. Le texte du commentaire ne doit pas contenir
264// de caractères spéciaux, et en particulier pas de CR/LF.
265//--
266
267/* --Methode-- */
268/*! Removes the definition and value associated with the name \b key.
269 Return \c true if the \b key is found in the list, \c false otherwise. */
270bool DVList::DeleteKey(string const& key)
271{
272ValList::iterator it = mvlist.find(key);
273if (it == mvlist.end()) return(false);
274mvlist.erase(it);
275return(true);
276}
277
278/* --Methode-- */
279/*! Return \c true if the \b key is found in the list, \c false otherwise. */
280bool DVList::HasKey(string const& key) const
281{
282ValList::const_iterator it = mvlist.find(key);
283if (it == mvlist.end()) return(false);
284return(true);
285}
286
287
288/* --Methode-- */
289/*! Appends or sets the integer value \b val in the list with name \b key */
290void DVList::SetI(string const& key, int_8 val)
291{
292Get(key) = (int_8)val;
293}
294
295/* --Methode-- */
296void DVList::SetD(string const& key, r_8 val)
297/*! Appends or sets the double value \b val in the list with name \b key */
298{
299Get(key) = (r_8)val;
300}
301
302/* --Methode-- */
303void DVList::SetZ(string const& key, complex<r_8> val)
304/*! Appends or sets the complex value \b val in the list with name \b key */
305{
306Get(key) = val;
307}
308
309/* --Methode-- */
310/*! Appends or sets the string value \b val in the list with name \b key */
311void DVList::SetS(string const& key, char const* val)
312{
313MuTyV div(val);
314Get(key) = div;
315}
316
317/* --Methode-- */
318/*! Appends or sets the string value \b val in the list with name \b key */
319void DVList::SetS(string const& key, string const& val)
320{
321MuTyV div(val);
322Get(key) = div;
323}
324
325/* --Methode-- */
326/*! Assigns the comment \b comm with the name \b key .
327 Does nothing if the entry with name is not present in the list */
328void DVList::SetComment(string const& key, string const& comm)
329{
330ValList::iterator it = mvlist.find(key);
331if (it == mvlist.end()) return;
332(*it).second.elcomm = comm;
333}
334
335//++
336// MuTyV& Get(string const& key)
337// Renvoie une référence sur l'objet "MuTyV" de la liste avec le nom "key".
338// Si cet objet (variable) n'existe pas, il est créé.
339// MuTyV& operator() (string const& key)
340// MuTyV& operator[] (string const& key)
341//
342// Renvoie la variable de nom "key". Equivalent à "Get(key)".
343// string& Comment()
344// Renvoie une référence sur le champ commentaire de l'objet.
345//--
346
347/* --Methode-- */
348/*! Return the MuTyV value associated with name \b key .
349 Integer 0 is returned if \b key is not present in the list */
350MuTyV DVList::Get(string const& key) const
351{
352ValList::const_iterator it = mvlist.find(key);
353if (it == mvlist.end()) return(MuTyV(0));
354else return((*it).second.elval);
355}
356
357/* --Methode-- */
358/*! Return the MuTyV value associated with name \b key .
359 Adds an entry of type integer in the list if \b key is not present in the list */
360MuTyV& DVList::Get(string const& key)
361{
362size_t l = key.length();
363if ( (l < 1) || (key.find_first_of(" ") < l) ) return(ddvdum);
364// dvlElement xxx = {(int_8)0 , ""}; marche pas sur mac/CW (!) - cf DY
365dvlElement xxx; xxx.elval = (int_8)0; xxx.elcomm = "";
366ValList::iterator it = mvlist.find(key);
367if (it == mvlist.end()) mvlist[key] = xxx;
368it = mvlist.find(key);
369if (it == mvlist.end()) return(ddvdum);
370else return((*it).second.elval);
371}
372
373//++
374// Titre Entrée-Sortie
375//--
376
377//++
378// void Print()
379// Imprime (sur "cout") la liste des variables et leurs valeurs.
380// void Print(ostream& os)
381// Imprime sur le flot "os" la liste des variables et leurs valeurs.
382// ostream& operator << (ostream& s, DVList& dvl)
383// sortie sur flot "s" (Appel a "Print(s)").
384//--
385
386/* --Methode-- */
387/*! Prints a brief description of object on on the output stream \b os */
388void DVList::Show(ostream& os) const
389{
390os << "DVList::Show() - NVar= " << (int)mvlist.size() << "\n";
391os << comment << endl;
392}
393
394/* --Methode-- */
395/*! Prints the list of variables on the output stream \b os */
396void DVList::Print(ostream& os) const
397{
398os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n";
399if (comment.length() > 0) os << comment << endl;
400char buff[1024];
401ValList::const_iterator it;
402for(it = mvlist.begin(); it != mvlist.end(); it++) {
403 switch ((*it).second.elval.Type())
404 {
405 case MuTyV::MTVInteger :
406 sprintf(buff, "%s = %ld (int) %s\n", (*it).first.substr(0,64).c_str(),
407 (long)((*it).second.elval.iv), (*it).second.elcomm.substr(0,128).c_str());
408 break;
409 case MuTyV::MTVFloat :
410 sprintf(buff, "%s = %.20g (double) %s\n", (*it).first.substr(0,64).c_str(),
411 (*it).second.elval.dv, (*it).second.elcomm.substr(0,128).c_str());
412 break;
413 case MuTyV::MTVComplex :
414 sprintf(buff, "%s = %.20g %.20g i (complex) %s\n", (*it).first.substr(0,64).c_str(),
415 (*it).second.elval.dv, (*it).second.elval.dv_im, (*it).second.elcomm.substr(0,128).c_str());
416 break;
417 case MuTyV::MTVString :
418 sprintf(buff, "%s = %s (string) %s\n", (*it).first.substr(0,64).c_str(),
419 (*it).second.elval.strv->substr(0,800).c_str(), (*it).second.elcomm.substr(0,128).c_str());
420 break;
421 default :
422 break;
423 }
424 os << (string)buff;
425 }
426os << endl;
427}
428
429
430//++
431// Titre Exemples
432// Utilisation des objets *MuTyV* :
433//| MuTyV mvu; // Declaration d'une variable
434//| mvu = 60; // mvu est de type entier (= 60)
435//| mvu = 66.6; // et double (= 66.6) maintenant ...
436//| MuTyV mvi(14); // On construit une variable entiere = 14
437//| r_4 x = mvi; // x vaut 14.0
438//| MuTyV mvd(44.4); // Variable double = 44.4
439//| int k = mvd; // k vaut 44
440//| MuTyV mvs("Bonjour, Ca va ?"); // Variable chaine de caracteres
441//| string s = mvs; // s vaut "Bonjour, Ca va ?"
442// Utilisation des *DVList* :
443//| DVList dvl;
444//| dvl("toto") = 14;
445//| dvl("titi") = 25.5;
446//| dvl("tata") = "Bonjour, Ca va ?";
447// Majuscules et minuscules sont differenciees pour les noms, pas de blanc ...
448//| dvl("hello") = 88;
449//| dvl("Hello") = 77.77;
450//| dvl.Comment() = "Test d'objet DVList, avec variables hello, Hello ";
451//| dvl.Write("dvlist.ppf");
452// Plus loin, ou dans un autre programme, on relit le fichier fabrique plus haut
453//| DVList dvlr("dvlist.ppf");
454//| int k = dvlr["toto"] ; // k = 14
455//| r_8 b = dvlr["titi"] ; // b = 25.5
456//| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?"
457//| r_4 c = dvlr["Hello"] ; // c = 77.77
458//| int l = dvlr["Hello"] ; // l = 77
459//| int m = dvlr["hello"] ; // m = 88
460//--
461
462
463//----------------------------------------------------------
464// Classe pour la gestion de persistance
465// ObjFileIO<DVList>
466//----------------------------------------------------------
467
468/* --Methode-- */
469void ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
470{
471char buf[1024];
472string sfw;
473int lc = dobj->Comment().length();
474// itab - 0 : Version, 1 : NVar, 2 : Comment length, 3 reserved
475uint_4 itab[4];
476itab[0] = 2; // Version number = 2
477itab[1] = dobj->Size();
478itab[2] = lc;
479itab[3] = 0;
480s.Put(itab, 4);
481
482if (lc > 0) s.PutStr(dobj->Comment());
483sfw = "\n----Variable-List---------------\n";
484s.PutStr(sfw);
485
486DVList::ValList::const_iterator it;
487for(it = dobj->Begin(); it != dobj->End(); it++) {
488 switch ((*it).second.elval.Type()) {
489 case MuTyV::MTVInteger :
490 sprintf(buf,"I %s %ld\n", (*it).first.substr(0,64).c_str(), (long)((*it).second.elval.iv) );
491 sfw = buf; s.PutStr(sfw);
492 break;
493 case MuTyV::MTVFloat :
494 sprintf(buf,"F %s %.20g\n", (*it).first.substr(0,64).c_str(), (*it).second.elval.dv );
495 sfw = buf; s.PutStr(sfw);
496 break;
497 case MuTyV::MTVComplex :
498 sprintf(buf,"Z %s %.20g %.20g\n", (*it).first.substr(0,64).c_str(), (*it).second.elval.dv,
499 (*it).second.elval.dv_im);
500 sfw = buf; s.PutStr(sfw);
501 break;
502 case MuTyV::MTVString :
503 sprintf(buf,"S %s %s\n", (*it).first.substr(0,64).c_str(), (*it).second.elval.strv->substr(0,960).c_str() );
504 sfw = buf; s.PutStr(sfw);
505 break;
506 default :
507 break;
508 }
509// Ecriture eventuelle du commentaire associe
510 if ((*it).second.elcomm.length() > 0) {
511 sprintf(buf,"# %s", (*it).second.elcomm.substr(0,256).c_str());
512 sfw = buf; s.PutStr(sfw);
513 }
514}
515
516sfw = "ZZZZZ--End-of-Varible-List------"; s.PutStr(sfw);
517}
518
519/* --Methode-- */
520void ObjFileIO<DVList>::ReadSelf(PInPersist& s)
521{
522char buf[1024];
523string sfr;
524int_8 j,iv,k;
525r_8 dv, dvi;
526complex<r_8> z;
527bool ok=true;
528buf[0] = '\0';
529dobj->Clear();
530
531// itab - 0 : Version, 1 : NVar, 2 : Comment length, 3 reserved
532uint_4 itab[4];
533s.Get(itab, 4);
534if (itab[2] > 0) { // Il y a un champ commentaire a lire
535 s.GetStr(sfr);
536 dobj->Comment() = sfr;
537 }
538
539s.GetStr(sfr); // Pour lire les "------- "
540
541string key="";
542while(ok) {
543 s.GetStr(sfr);
544 strncpy(buf, sfr.c_str(), 1024);
545 buf[1023] = '\0';
546 j = strlen(buf)-1;
547 if ( (j >= 0) && (buf[j] == '\n') ) buf[j] = '\0';
548 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; }
549 if (buf[0] == '#') {
550 dobj->SetComment(key, buf+2);
551 continue;
552 }
553 j = posc(buf+2, ' ')+2;
554 buf[j] = '\0';
555 switch (buf[0]) {
556 case 'I' :
557 iv = (int_8)atol(buf+j+1);
558 key = buf+2;
559 dobj->SetI(key, iv);
560 break;
561 case 'F' :
562 dv = atof(buf+j+1);
563 key = buf+2;
564 dobj->SetD(key, dv);
565 break;
566 case 'Z' :
567 k = posc(buf+j+1, ' ')+j+1;
568 buf[k] = '\0';
569 dv = atof(buf+j+1);
570 dvi = atof(buf+k+1);
571 key = buf+2;
572 z = complex<r_8>(dv, dvi);
573 dobj->SetZ(key, z);
574 break;
575 case 'S' :
576 key = buf+2;
577 dobj->SetS(key, buf+j+1);
578 break;
579 default :
580 break;
581 }
582 }
583if (dobj->Size() != itab[1]) // Probleme !!!
584 throw FileFormatExc("ObjFileIO<DVList>::ReadSelf() Error in Nb. Variables !");
585}
586
587#ifdef __CXX_PRAGMA_TEMPLATES__
588#pragma define_template ObjFileIO<DVList>
589#endif
590
591#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
592template class ObjFileIO<DVList>;
593#endif
Note: See TracBrowser for help on using the repository browser.