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