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