[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>
|
---|
[3619] | 9 | #include <stdlib.h>
|
---|
[1080] | 10 |
|
---|
[2322] | 11 | #include <iostream>
|
---|
[1080] | 12 | #include <string.h>
|
---|
| 13 |
|
---|
| 14 | /*!
|
---|
| 15 | \class SOPHYA::MuTyV
|
---|
[1607] | 16 | \ingroup BaseTools
|
---|
[1080] | 17 | Simple utility class which can be used to hold values of type
|
---|
[2826] | 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.
|
---|
[1080] | 22 | It provides also easy conversion methods between these types.
|
---|
| 23 |
|
---|
[2826] | 24 | \sa SOPHYA::TimeStamp
|
---|
| 25 |
|
---|
[1080] | 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 !"
|
---|
[2826] | 36 | TimeStamp ts; // Current date/time
|
---|
| 37 | MuTyV mvt = ts;
|
---|
| 38 | s = mvt; // s contains the current date in string format
|
---|
[1080] | 39 | \endcode
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 |
|
---|
[1559] | 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 | }
|
---|
[1157] | 50 |
|
---|
[1080] | 51 | /* --Methode-- */
|
---|
| 52 | MuTyV::MuTyV(MuTyV const & a)
|
---|
| 53 | {
|
---|
| 54 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
[1310] | 55 | if (typ == MTVString) strv = new string(*(a.strv));
|
---|
[1080] | 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 | {
|
---|
[1310] | 68 | typ = MTVString;
|
---|
[1080] | 69 | strv = new string(s);
|
---|
[1157] | 70 | mutyv_decodestr(s, dv, dv_im);
|
---|
[1080] | 71 | iv = (int_8)dv;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /* --Methode-- */
|
---|
| 75 | MuTyV::MuTyV(string const& s)
|
---|
| 76 | {
|
---|
[1310] | 77 | typ = MTVString;
|
---|
[1080] | 78 | strv = new string(s);
|
---|
[1157] | 79 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
[1080] | 80 | iv = (int_8)dv;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* --Methode-- */
|
---|
[2826] | 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-- */
|
---|
[1080] | 94 | MuTyV & MuTyV::operator= (MuTyV const & a)
|
---|
| 95 | {
|
---|
| 96 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
[1310] | 97 | if (typ == MTVString) {
|
---|
[1225] | 98 | if (strv) *strv = *(a.strv);
|
---|
| 99 | else strv = new string(*(a.strv));
|
---|
| 100 | }
|
---|
[1080] | 101 | return(*this);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /* --Methode-- */
|
---|
[2826] | 105 | const char * MuTyV::operator= (const char* s)
|
---|
[1080] | 106 | {
|
---|
[1310] | 107 | typ = MTVString;
|
---|
[1225] | 108 | if (strv) *strv = s;
|
---|
| 109 | else strv = new string(s);
|
---|
[1157] | 110 | mutyv_decodestr(s, dv, dv_im);
|
---|
[1080] | 111 | iv = (int_8)dv;
|
---|
| 112 | return(s);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /* --Methode-- */
|
---|
[2826] | 116 | string const & MuTyV::operator= (string const& s)
|
---|
[1080] | 117 | {
|
---|
[1310] | 118 | typ = MTVString;
|
---|
[1225] | 119 | if (strv) *strv = s;
|
---|
| 120 | else strv = new string(s);
|
---|
[1157] | 121 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
[1080] | 122 | iv = (int_8)dv;
|
---|
| 123 | return(s);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /* --Methode-- */
|
---|
[2826] | 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-- */
|
---|
[1080] | 138 | MuTyV::operator string() const
|
---|
| 139 | {
|
---|
[1310] | 140 | if (typ == MTVString) return(*strv);
|
---|
[2826] | 141 | else if (typ == MTVTimeStamp) { return TimeStamp(dv).ToString(); }
|
---|
[1080] | 142 | else {
|
---|
| 143 | char buff[96];
|
---|
[1310] | 144 | if (typ == MTVInteger) sprintf(buff,"%ld", (long)iv);
|
---|
[1559] | 145 | else if (typ == MTVFloat) sprintf(buff,"%g", dv);
|
---|
| 146 | else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
|
---|
[1080] | 147 | else buff[0] = '\0';
|
---|
| 148 | return(string(buff));
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[2826] | 152 | /* --Methode-- */
|
---|
[2884] | 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-- */
|
---|
[2826] | 169 | MuTyV::operator TimeStamp() const
|
---|
| 170 | {
|
---|
| 171 | return TimeStamp(dv);
|
---|
| 172 | }
|
---|
[1080] | 173 |
|
---|
[2884] | 174 | /* --Methode-- */
|
---|
| 175 | TimeStamp& MuTyV::Convert(TimeStamp& x) const
|
---|
| 176 | {
|
---|
| 177 | x = TimeStamp(dv);
|
---|
| 178 | return x;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[1559] | 181 | static void mutyv_decodestr(string const & s, double& r, double& im)
|
---|
[1157] | 182 | // decodage d'une chaine contenant une ou deux valeurs
|
---|
| 183 | {
|
---|
| 184 | r = im = 0.;
|
---|
| 185 | size_t l = s.length();
|
---|
[1559] | 186 | size_t p = s.find_first_not_of(" \t",0);
|
---|
| 187 | size_t q,q2;
|
---|
[1157] | 188 | if (p >= l) return;
|
---|
[1559] | 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);
|
---|
[1157] | 205 | if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
|
---|
| 206 | return;
|
---|
| 207 | r = atof(s.substr(p,q-p).c_str());
|
---|
[1559] | 208 | im = 0.;
|
---|
[1157] | 209 | }
|
---|
[1080] | 210 |
|
---|
| 211 |
|
---|
[1157] | 212 |
|
---|
| 213 |
|
---|