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