[220] | 1 | // Classe Dynamic Variable List (DVList) de PEIDA
|
---|
| 2 | // R. Ansari 1997
|
---|
| 3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 4 |
|
---|
[244] | 5 | #include "machdefs.h"
|
---|
[220] | 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 | char MuTyV::myStrBuf[64]; // Declare static ds le .h
|
---|
| 41 |
|
---|
| 42 | static MuTyV ddvdum(-9.e19);
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | //++
|
---|
| 46 | // Titre Constructeurs
|
---|
| 47 | //--
|
---|
| 48 |
|
---|
| 49 | //++
|
---|
| 50 | // DVList()
|
---|
| 51 | // Constructeur par défaut
|
---|
| 52 | // DVList(DVList& cfd)
|
---|
| 53 | // Constructeur par copie. Le nouvel objet est une copie complète de "cfd"
|
---|
| 54 | // DVList(char* flnm)
|
---|
| 55 | // Constructeur avec initialisation à partir du contenu du fichier (PPF)
|
---|
| 56 | // "flnm". Le fichier doit avoir été créé par la méthode "Write()"
|
---|
| 57 | //--
|
---|
| 58 |
|
---|
| 59 | /* --Methode-- */
|
---|
| 60 | DVList::DVList()
|
---|
| 61 | {
|
---|
| 62 | comment = "";
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /* --Methode-- */
|
---|
| 66 | DVList::DVList(DVList& dvl)
|
---|
| 67 | {
|
---|
| 68 | Merge(dvl);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /* --Methode-- */
|
---|
| 72 | DVList::DVList(char *flnm)
|
---|
| 73 | {
|
---|
| 74 | PInPersist s(flnm);
|
---|
[278] | 75 | ObjFileIO<DVList> fiodvl(this);
|
---|
| 76 | fiodvl.Read(s);
|
---|
[220] | 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /* --Methode-- */
|
---|
| 81 | DVList::~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-- */
|
---|
| 99 | DVList& DVList::operator= (const DVList& dvl)
|
---|
| 100 | {
|
---|
| 101 | Clear();
|
---|
| 102 | return(Merge(dvl));
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | /* --Methode-- */
|
---|
| 107 | void DVList::Clear()
|
---|
| 108 | {
|
---|
| 109 | mvlist.erase(mvlist.begin(), mvlist.end());
|
---|
| 110 | comment = "";
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /* --Methode-- */
|
---|
| 114 | DVList& DVList::Merge(const DVList& dvl)
|
---|
| 115 | {
|
---|
| 116 | ValList::const_iterator it;
|
---|
| 117 | for(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 | }
|
---|
| 134 | comment = comment + "\n" + dvl.comment;
|
---|
| 135 | return(*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-- */
|
---|
| 149 | int_4 DVList::GetI(string const& key, int_4 def)
|
---|
| 150 | {
|
---|
| 151 | ValList::iterator it = mvlist.find(key);
|
---|
| 152 | if (it == mvlist.end()) return(def);
|
---|
| 153 | if ( (*it).second.typ != 'I') return(def);
|
---|
| 154 | return((*it).second.mtv.iv);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /* --Methode-- */
|
---|
| 158 | double DVList::GetD(string const& key, double def)
|
---|
| 159 | {
|
---|
| 160 | ValList::iterator it = mvlist.find(key);
|
---|
| 161 | if (it == mvlist.end()) return(def);
|
---|
| 162 | if ( (*it).second.typ != 'D') return(def);
|
---|
| 163 | return((*it).second.mtv.dv);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /* --Methode-- */
|
---|
| 167 | string DVList::GetS(string const& key, char* def)
|
---|
| 168 | {
|
---|
| 169 | ValList::iterator it = mvlist.find(key);
|
---|
| 170 | if (it == mvlist.end()) return(def);
|
---|
| 171 | if ( (*it).second.typ != 'S') return(def);
|
---|
| 172 | return((*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-- */
|
---|
| 186 | void DVList::SetI(string const& key, int_4 val)
|
---|
| 187 | {
|
---|
| 188 | Get(key) = (int_4)val;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /* --Methode-- */
|
---|
| 192 | void DVList::SetD(string const& key, double val)
|
---|
| 193 | {
|
---|
| 194 | Get(key) = (double)val;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /* --Methode-- */
|
---|
| 198 | void DVList::SetS(string const& key, char const* val)
|
---|
| 199 | {
|
---|
| 200 | MuTyV div(val);
|
---|
| 201 | Get(key) = div;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* --Methode-- */
|
---|
| 205 | void DVList::SetS(string const& key, string val)
|
---|
| 206 | {
|
---|
| 207 | MuTyV div(val);
|
---|
| 208 | Get(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-- */
|
---|
| 225 | MuTyV& DVList::Get(string const& key)
|
---|
| 226 | {
|
---|
| 227 | size_t l = key.length();
|
---|
| 228 | if ( (l < 1) || (key.find_first_of(" ") < l) ) return(ddvdum);
|
---|
| 229 | ValList::iterator it = mvlist.find(key);
|
---|
| 230 | if (it == mvlist.end()) mvlist[key] = (int_4) 0; // $CHECK$ EA. Ambigu si pas de cast...
|
---|
| 231 | it = mvlist.find(key);
|
---|
| 232 | if (it == mvlist.end()) return(ddvdum);
|
---|
| 233 | else 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-- */
|
---|
| 255 | void DVList::Print(ostream& os) const
|
---|
| 256 | {
|
---|
| 257 | os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n";
|
---|
| 258 | os << "Comment: " << comment << "\n";
|
---|
| 259 | char buff[128];
|
---|
| 260 | ValList::const_iterator it;
|
---|
| 261 | for(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 | }
|
---|
| 278 | os << endl;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
[278] | 282 | //++
|
---|
| 283 | // Titre Exemples
|
---|
| 284 | // Utilisation des objets *MuTyV* :
|
---|
| 285 | //| MuTyV mvu; // Declaration d'une variable
|
---|
| 286 | //| mvu = 60; // mvu est de type entier (= 60)
|
---|
| 287 | //| mvu = 66.6; // et double (= 66.6) maintenant ...
|
---|
| 288 | //| MuTyV mvi(14); // On construit une variable entiere = 14
|
---|
| 289 | //| float x = mvi; // x vaut 14.0
|
---|
| 290 | //| MuTyV mvd(44.4); // Variable double = 44.4
|
---|
| 291 | //| int k = mvd; // k vaut 44
|
---|
| 292 | //| MuTyV mvs("Bonjour, Ca va ?"); // Variable chaine de caracteres
|
---|
| 293 | //| string s = mvs; // s vaut "Bonjour, Ca va ?"
|
---|
| 294 | // Utilisation des *DVList* :
|
---|
| 295 | //| DVList dvl;
|
---|
| 296 | //| dvl("toto") = 14;
|
---|
| 297 | //| dvl("titi") = 25.5;
|
---|
| 298 | //| dvl("tata") = "Bonjour, Ca va ?";
|
---|
| 299 | // Majuscules et minuscules sont differenciees pour les noms, pas de blanc ...
|
---|
| 300 | //| dvl("hello") = 88;
|
---|
| 301 | //| dvl("Hello") = 77.77;
|
---|
| 302 | //| dvl.Comment() = "Test d'objet DVList, avec variables hello, Hello ";
|
---|
| 303 | //| dvl.Write("dvlist.ppf");
|
---|
| 304 | // Plus loin, ou dans un autre programme, on relit le fichier fabrique plus haut
|
---|
| 305 | //| DVList dvlr("dvlist.ppf");
|
---|
| 306 | //| int k = dvlr["toto"] ; // k = 14
|
---|
| 307 | //| double b = dvlr["titi"] ; // b = 25.5
|
---|
| 308 | //| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?"
|
---|
| 309 | //| float c = dvlr["Hello"] ; // c = 77.77
|
---|
| 310 | //| int l = dvlr["Hello"] ; // l = 77
|
---|
| 311 | //| int m = dvlr["hello"] ; // m = 88
|
---|
| 312 | //--
|
---|
| 313 |
|
---|
| 314 |
|
---|
[404] | 315 | //----------------------------------------------------------
|
---|
| 316 | // Classe pour la gestion de persistance
|
---|
| 317 | // ObjFileIO<DVList>
|
---|
| 318 | //----------------------------------------------------------
|
---|
[278] | 319 |
|
---|
[220] | 320 | /* --Methode-- */
|
---|
[278] | 321 | void ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
|
---|
[220] | 322 | {
|
---|
| 323 | char buf[320];
|
---|
| 324 |
|
---|
[278] | 325 | int lc = dobj->Comment().length();
|
---|
[220] | 326 | if (lc > 319) lc = 319;
|
---|
| 327 | if (lc > 0) {
|
---|
| 328 | sprintf(buf,"Comment: ( %6d ) ", lc);
|
---|
| 329 | s.PutLine(buf);
|
---|
[278] | 330 | s.PutBytes(dobj->Comment().c_str(), lc);
|
---|
[220] | 331 | }
|
---|
| 332 | s.PutLine("----Variable-List---------------");
|
---|
[278] | 333 | DVList::ValList::const_iterator it;
|
---|
| 334 | for(it = dobj->Begin(); it != dobj->End(); it++) {
|
---|
[220] | 335 | switch ((*it).second.typ)
|
---|
| 336 | {
|
---|
| 337 | case 'I' :
|
---|
| 338 | sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
|
---|
| 339 | s.PutLine(buf);
|
---|
| 340 | break;
|
---|
| 341 | case 'D' :
|
---|
| 342 | sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
|
---|
| 343 | s.PutLine(buf);
|
---|
| 344 | break;
|
---|
| 345 | case 'S' :
|
---|
| 346 | sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
|
---|
| 347 | s.PutLine(buf);
|
---|
| 348 | break;
|
---|
| 349 | default :
|
---|
| 350 | break;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | s.PutLine("ZZZZZ--End-of-Varible-List------");
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | /* --Methode-- */
|
---|
[278] | 358 | void ObjFileIO<DVList>::ReadSelf(PInPersist& s)
|
---|
[220] | 359 | {
|
---|
| 360 | char buf[320];
|
---|
| 361 | int_4 j,iv;
|
---|
| 362 | double dv;
|
---|
| 363 | bool ok=true;
|
---|
| 364 | buf[0] = '\0';
|
---|
[278] | 365 | dobj->Clear();
|
---|
[220] | 366 |
|
---|
| 367 | s.GetLine(buf, 319); // Pour lire les "------- "
|
---|
| 368 | if (buf[0] != '-') { // Il y a un champ commentaire a lire
|
---|
| 369 | buf[18] ='\0';
|
---|
| 370 | int lc = atoi(buf+11);
|
---|
| 371 | if (lc > 319) {
|
---|
| 372 | cerr << "DVList::ReadSelf() Pb/Bug ?? CommentLength= " << lc << endl;
|
---|
| 373 | lc = 319;
|
---|
| 374 | }
|
---|
| 375 | s.GetBytes(buf, lc);
|
---|
| 376 | buf[lc] ='\0';
|
---|
[278] | 377 | dobj->Comment() = buf;
|
---|
[220] | 378 | }
|
---|
| 379 |
|
---|
| 380 | while(ok) {
|
---|
| 381 | s.GetLine(buf, 319);
|
---|
| 382 | buf[319] = '\0';
|
---|
| 383 | if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; }
|
---|
| 384 | j = posc(buf+2, ' ')+2;
|
---|
| 385 | buf[j] = '\0';
|
---|
| 386 | switch (buf[0])
|
---|
| 387 | {
|
---|
| 388 | case 'I' :
|
---|
| 389 | iv = (int_4)atol(buf+j+1);
|
---|
[278] | 390 | dobj->SetI(buf+2, iv);
|
---|
[220] | 391 | break;
|
---|
| 392 | case 'D' :
|
---|
| 393 | dv = atof(buf+j+1);
|
---|
[278] | 394 | dobj->SetD(buf+2, dv);
|
---|
[220] | 395 | break;
|
---|
| 396 | case 'S' :
|
---|
[278] | 397 | dobj->SetS(buf+2, buf+j+1);
|
---|
[220] | 398 | break;
|
---|
| 399 | default :
|
---|
| 400 | break;
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[278] | 405 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 406 | #pragma define_template ObjFileIO<DVList>
|
---|
| 407 | #endif
|
---|
| 408 |
|
---|
| 409 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 410 | template class ObjFileIO<DVList>;
|
---|
| 411 | #endif
|
---|