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