| [1080] | 1 | // Classe MuTyV : Variable multi-type numerique
 | 
|---|
 | 2 | //                  R. Ansari  1997-2000
 | 
|---|
 | 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
 | 4 | 
 | 
|---|
 | 5 | 
 | 
|---|
| [2615] | 6 | #include "sopnamsp.h"
 | 
|---|
| [1080] | 7 | #include "mutyv.h"
 | 
|---|
 | 8 | #include <stdio.h>
 | 
|---|
 | 9 | 
 | 
|---|
| [2322] | 10 | #include <iostream>
 | 
|---|
| [1080] | 11 | #include <string.h>
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | /*!
 | 
|---|
 | 14 |    \class SOPHYA::MuTyV
 | 
|---|
| [1607] | 15 |    \ingroup BaseTools
 | 
|---|
| [1080] | 16 |    Simple utility class which can be used to hold values of type
 | 
|---|
| [2826] | 17 |    string, integer (\b int_8), float (\b r_8), complex and 
 | 
|---|
 | 18 |    date/time (TimeStamp). Date/time values are kept internally 
 | 
|---|
 | 19 |    as double precision values and are thus less precise than 
 | 
|---|
 | 20 |    TimeStamp objects. 
 | 
|---|
| [1080] | 21 |    It provides also easy conversion methods between these types. 
 | 
|---|
 | 22 | 
 | 
|---|
| [2826] | 23 |    \sa SOPHYA::TimeStamp
 | 
|---|
 | 24 | 
 | 
|---|
| [1080] | 25 |    \code
 | 
|---|
 | 26 |    //  ------- Using MuTyV objects ------- 
 | 
|---|
 | 27 |    MuTyV mvu;         // MuTyV variable declaration 
 | 
|---|
 | 28 |    mvu = 60;          // mvu contains the integer value 60
 | 
|---|
 | 29 |    mvu = 66.6;        // and now the double value 66.6 
 | 
|---|
 | 30 |    string ds = mvu;   // ds contains the string "66.6"
 | 
|---|
 | 31 |    MuTyV mvi(14);     // New MuTyV variable containing integer value 14
 | 
|---|
 | 32 |    r_4 x = mvi;       // x has the value 14.0
 | 
|---|
 | 33 |    MuTyV mvs("Bonjour !");  // mvs contains the string "Bonjour !"
 | 
|---|
 | 34 |    string s = mvs;          // s contains "Bonjour !"
 | 
|---|
| [2826] | 35 |    TimeStamp ts;      // Current date/time
 | 
|---|
 | 36 |    MuTyV mvt = ts;
 | 
|---|
 | 37 |    s = mvt;           // s contains the current date in string format
 | 
|---|
| [1080] | 38 |    \endcode
 | 
|---|
 | 39 | */
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | 
 | 
|---|
| [1559] | 42 | static void mutyv_decodestr(string const &, double& r, double& im);
 | 
|---|
 | 43 | static inline void mutyv_decodestr_cp(const char * si, double& r, double& im)
 | 
|---|
 | 44 | {
 | 
|---|
 | 45 |   r = im = 0.;
 | 
|---|
 | 46 |   string s = si;
 | 
|---|
 | 47 |   mutyv_decodestr(s, r, im);
 | 
|---|
 | 48 | }
 | 
|---|
| [1157] | 49 | 
 | 
|---|
| [1080] | 50 | /* --Methode-- */
 | 
|---|
 | 51 | MuTyV::MuTyV(MuTyV const & a)
 | 
|---|
 | 52 | {
 | 
|---|
 | 53 |   typ = a.typ;  iv = a.iv;  dv = a.dv;  dv_im = a.dv_im; 
 | 
|---|
| [1310] | 54 |   if (typ == MTVString)  strv = new string(*(a.strv));
 | 
|---|
| [1080] | 55 |   else strv = NULL;
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | /* --Methode-- */
 | 
|---|
 | 59 | MuTyV::~MuTyV()
 | 
|---|
 | 60 | {
 | 
|---|
 | 61 |   if (strv) delete strv;
 | 
|---|
 | 62 | }
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | /* --Methode-- */
 | 
|---|
 | 65 | MuTyV::MuTyV(char const* s)
 | 
|---|
 | 66 | {
 | 
|---|
| [1310] | 67 |   typ = MTVString;
 | 
|---|
| [1080] | 68 |   strv = new string(s);
 | 
|---|
| [1157] | 69 |   mutyv_decodestr(s, dv, dv_im);
 | 
|---|
| [1080] | 70 |   iv = (int_8)dv;
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | /* --Methode-- */
 | 
|---|
 | 74 | MuTyV::MuTyV(string const& s)
 | 
|---|
 | 75 | {
 | 
|---|
| [1310] | 76 |   typ = MTVString;
 | 
|---|
| [1080] | 77 |   strv = new string(s);
 | 
|---|
| [1157] | 78 |   mutyv_decodestr(s.c_str(), dv, dv_im);
 | 
|---|
| [1080] | 79 |   iv = (int_8)dv;
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | /* --Methode-- */
 | 
|---|
| [2826] | 83 | MuTyV::MuTyV(TimeStamp const& ts)
 | 
|---|
 | 84 | {
 | 
|---|
 | 85 |   typ = MTVTimeStamp;
 | 
|---|
 | 86 |   dv = ts.ToDays();
 | 
|---|
 | 87 |   dv_im = 0.;
 | 
|---|
 | 88 |   iv = (int_8)dv;
 | 
|---|
 | 89 |   strv = NULL;
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | /* --Methode-- */
 | 
|---|
| [1080] | 93 | MuTyV & MuTyV::operator= (MuTyV const & a)
 | 
|---|
 | 94 | {
 | 
|---|
 | 95 |   typ = a.typ;  iv = a.iv;  dv = a.dv;  dv_im = a.dv_im; 
 | 
|---|
| [1310] | 96 |   if (typ == MTVString)  { 
 | 
|---|
| [1225] | 97 |     if (strv) *strv = *(a.strv);  
 | 
|---|
 | 98 |     else strv = new string(*(a.strv));
 | 
|---|
 | 99 |   }
 | 
|---|
| [1080] | 100 |   return(*this);
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | /* --Methode-- */
 | 
|---|
| [2826] | 104 | const char * MuTyV::operator= (const char* s)
 | 
|---|
| [1080] | 105 | {
 | 
|---|
| [1310] | 106 |   typ = MTVString;
 | 
|---|
| [1225] | 107 |   if (strv) *strv = s;  
 | 
|---|
 | 108 |   else strv = new string(s);
 | 
|---|
| [1157] | 109 |   mutyv_decodestr(s, dv, dv_im);
 | 
|---|
| [1080] | 110 |   iv = (int_8)dv;
 | 
|---|
 | 111 |   return(s);
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | /* --Methode-- */
 | 
|---|
| [2826] | 115 | string const & MuTyV::operator= (string const& s)
 | 
|---|
| [1080] | 116 | {
 | 
|---|
| [1310] | 117 |   typ = MTVString;
 | 
|---|
| [1225] | 118 |   if (strv) *strv = s;  
 | 
|---|
 | 119 |   else strv = new string(s);
 | 
|---|
| [1157] | 120 |   mutyv_decodestr(s.c_str(), dv, dv_im);
 | 
|---|
| [1080] | 121 |   iv = (int_8)dv;
 | 
|---|
 | 122 |   return(s);
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | /* --Methode-- */
 | 
|---|
| [2826] | 126 | TimeStamp const & MuTyV::operator= (TimeStamp const& ts)
 | 
|---|
 | 127 | {
 | 
|---|
 | 128 |   typ = MTVTimeStamp;
 | 
|---|
 | 129 |   dv = ts.ToDays();
 | 
|---|
 | 130 |   dv_im = 0.;
 | 
|---|
 | 131 |   iv = (int_8)dv;
 | 
|---|
 | 132 |   return(ts);
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 | /* --Methode-- */
 | 
|---|
| [1080] | 137 | MuTyV::operator string() const 
 | 
|---|
 | 138 | {
 | 
|---|
| [1310] | 139 |   if (typ == MTVString)  return(*strv);
 | 
|---|
| [2826] | 140 |   else if (typ == MTVTimeStamp) { return TimeStamp(dv).ToString(); }
 | 
|---|
| [1080] | 141 |   else {
 | 
|---|
 | 142 |     char buff[96];
 | 
|---|
| [1310] | 143 |     if (typ == MTVInteger)  sprintf(buff,"%ld", (long)iv);
 | 
|---|
| [1559] | 144 |     else if (typ == MTVFloat) sprintf(buff,"%g", dv);
 | 
|---|
 | 145 |     else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
 | 
|---|
| [1080] | 146 |     else buff[0] = '\0';
 | 
|---|
 | 147 |     return(string(buff));
 | 
|---|
 | 148 |   }
 | 
|---|
 | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
| [2826] | 151 | /* --Methode-- */
 | 
|---|
| [2884] | 152 | string & MuTyV::Convert(string & x) const 
 | 
|---|
 | 153 | {
 | 
|---|
 | 154 |   if (typ == MTVString)  x = *strv;
 | 
|---|
 | 155 |   else if (typ == MTVTimeStamp) { x = TimeStamp(dv).ToString(); }
 | 
|---|
 | 156 |   else {
 | 
|---|
 | 157 |     char buff[96];
 | 
|---|
 | 158 |     if (typ == MTVInteger)  sprintf(buff,"%ld", (long)iv);
 | 
|---|
 | 159 |     else if (typ == MTVFloat) sprintf(buff,"%g", dv);
 | 
|---|
 | 160 |     else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
 | 
|---|
 | 161 |     else buff[0] = '\0';
 | 
|---|
 | 162 |     x = buff;
 | 
|---|
 | 163 |   }
 | 
|---|
 | 164 |   return x;
 | 
|---|
 | 165 | }
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | /* --Methode-- */
 | 
|---|
| [2826] | 168 | MuTyV::operator TimeStamp() const 
 | 
|---|
 | 169 | {
 | 
|---|
 | 170 |   return TimeStamp(dv);
 | 
|---|
 | 171 | }
 | 
|---|
| [1080] | 172 | 
 | 
|---|
| [2884] | 173 | /* --Methode-- */
 | 
|---|
 | 174 | TimeStamp& MuTyV::Convert(TimeStamp& x) const 
 | 
|---|
 | 175 | {
 | 
|---|
 | 176 |   x = TimeStamp(dv);
 | 
|---|
 | 177 |   return x;
 | 
|---|
 | 178 | }
 | 
|---|
 | 179 | 
 | 
|---|
| [1559] | 180 | static void mutyv_decodestr(string const & s, double& r, double& im)
 | 
|---|
| [1157] | 181 |   // decodage d'une chaine contenant une ou deux valeurs
 | 
|---|
 | 182 | {
 | 
|---|
 | 183 |   r = im = 0.;
 | 
|---|
 | 184 |   size_t l = s.length();
 | 
|---|
| [1559] | 185 |   size_t p = s.find_first_not_of(" \t",0);
 | 
|---|
 | 186 |   size_t q,q2;
 | 
|---|
| [1157] | 187 |   if (p >= l) return;
 | 
|---|
| [1559] | 188 |   if (s[p] == '(') {  // C'est un complexe
 | 
|---|
 | 189 |     if ((q2=s.find(')',p)) >= l)  return; 
 | 
|---|
 | 190 |     size_t pz = s.find_first_not_of(" \t",p+1);
 | 
|---|
 | 191 |     size_t qz = q2;
 | 
|---|
 | 192 |     if ((q=s.find(',',pz)) < q2) qz = q;
 | 
|---|
 | 193 |     if (isdigit(s[pz]) || !(s[pz] == '+') || (s[pz] == '-') )  
 | 
|---|
 | 194 |       r = atof(s.substr(pz,qz-pz).c_str());
 | 
|---|
 | 195 |     else return;
 | 
|---|
 | 196 |     if (qz == q) {
 | 
|---|
 | 197 |       pz = s.find_first_not_of(" \t",qz+1);
 | 
|---|
 | 198 |       if (isdigit(s[pz]) || (s[pz] == '+') || (s[pz] == '-') )  
 | 
|---|
 | 199 |         im = atof(s.substr(pz,q2-pz).c_str());      
 | 
|---|
 | 200 |     }
 | 
|---|
 | 201 |   }
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |   q = s.find_first_of(" \t",p+1);
 | 
|---|
| [1157] | 204 |   if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
 | 
|---|
 | 205 |     return;
 | 
|---|
 | 206 |   r = atof(s.substr(p,q-p).c_str());
 | 
|---|
| [1559] | 207 |   im = 0.;
 | 
|---|
| [1157] | 208 | }
 | 
|---|
| [1080] | 209 | 
 | 
|---|
 | 210 | 
 | 
|---|
| [1157] | 211 | 
 | 
|---|
 | 212 | 
 | 
|---|