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 |
|
---|
10 | #include <iostream>
|
---|
11 | #include <string.h>
|
---|
12 |
|
---|
13 | /*!
|
---|
14 | \class SOPHYA::MuTyV
|
---|
15 | \ingroup BaseTools
|
---|
16 | Simple utility class which can be used to hold values of type
|
---|
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.
|
---|
21 | It provides also easy conversion methods between these types.
|
---|
22 |
|
---|
23 | \sa SOPHYA::TimeStamp
|
---|
24 |
|
---|
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 !"
|
---|
35 | TimeStamp ts; // Current date/time
|
---|
36 | MuTyV mvt = ts;
|
---|
37 | s = mvt; // s contains the current date in string format
|
---|
38 | \endcode
|
---|
39 | */
|
---|
40 |
|
---|
41 |
|
---|
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 | }
|
---|
49 |
|
---|
50 | /* --Methode-- */
|
---|
51 | MuTyV::MuTyV(MuTyV const & a)
|
---|
52 | {
|
---|
53 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
54 | if (typ == MTVString) strv = new string(*(a.strv));
|
---|
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 | {
|
---|
67 | typ = MTVString;
|
---|
68 | strv = new string(s);
|
---|
69 | mutyv_decodestr(s, dv, dv_im);
|
---|
70 | iv = (int_8)dv;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /* --Methode-- */
|
---|
74 | MuTyV::MuTyV(string const& s)
|
---|
75 | {
|
---|
76 | typ = MTVString;
|
---|
77 | strv = new string(s);
|
---|
78 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
79 | iv = (int_8)dv;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* --Methode-- */
|
---|
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-- */
|
---|
93 | MuTyV & MuTyV::operator= (MuTyV const & a)
|
---|
94 | {
|
---|
95 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
96 | if (typ == MTVString) {
|
---|
97 | if (strv) *strv = *(a.strv);
|
---|
98 | else strv = new string(*(a.strv));
|
---|
99 | }
|
---|
100 | return(*this);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* --Methode-- */
|
---|
104 | const char * MuTyV::operator= (const char* s)
|
---|
105 | {
|
---|
106 | typ = MTVString;
|
---|
107 | if (strv) *strv = s;
|
---|
108 | else strv = new string(s);
|
---|
109 | mutyv_decodestr(s, dv, dv_im);
|
---|
110 | iv = (int_8)dv;
|
---|
111 | return(s);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* --Methode-- */
|
---|
115 | string const & MuTyV::operator= (string const& s)
|
---|
116 | {
|
---|
117 | typ = MTVString;
|
---|
118 | if (strv) *strv = s;
|
---|
119 | else strv = new string(s);
|
---|
120 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
121 | iv = (int_8)dv;
|
---|
122 | return(s);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* --Methode-- */
|
---|
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-- */
|
---|
137 | MuTyV::operator string() const
|
---|
138 | {
|
---|
139 | if (typ == MTVString) return(*strv);
|
---|
140 | else if (typ == MTVTimeStamp) { return TimeStamp(dv).ToString(); }
|
---|
141 | else {
|
---|
142 | char buff[96];
|
---|
143 | if (typ == MTVInteger) sprintf(buff,"%ld", (long)iv);
|
---|
144 | else if (typ == MTVFloat) sprintf(buff,"%g", dv);
|
---|
145 | else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
|
---|
146 | else buff[0] = '\0';
|
---|
147 | return(string(buff));
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* --Methode-- */
|
---|
152 | MuTyV::operator TimeStamp() const
|
---|
153 | {
|
---|
154 | return TimeStamp(dv);
|
---|
155 | }
|
---|
156 |
|
---|
157 | static void mutyv_decodestr(string const & s, double& r, double& im)
|
---|
158 | // decodage d'une chaine contenant une ou deux valeurs
|
---|
159 | {
|
---|
160 | r = im = 0.;
|
---|
161 | size_t l = s.length();
|
---|
162 | size_t p = s.find_first_not_of(" \t",0);
|
---|
163 | size_t q,q2;
|
---|
164 | if (p >= l) return;
|
---|
165 | if (s[p] == '(') { // C'est un complexe
|
---|
166 | if ((q2=s.find(')',p)) >= l) return;
|
---|
167 | size_t pz = s.find_first_not_of(" \t",p+1);
|
---|
168 | size_t qz = q2;
|
---|
169 | if ((q=s.find(',',pz)) < q2) qz = q;
|
---|
170 | if (isdigit(s[pz]) || !(s[pz] == '+') || (s[pz] == '-') )
|
---|
171 | r = atof(s.substr(pz,qz-pz).c_str());
|
---|
172 | else return;
|
---|
173 | if (qz == q) {
|
---|
174 | pz = s.find_first_not_of(" \t",qz+1);
|
---|
175 | if (isdigit(s[pz]) || (s[pz] == '+') || (s[pz] == '-') )
|
---|
176 | im = atof(s.substr(pz,q2-pz).c_str());
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | q = s.find_first_of(" \t",p+1);
|
---|
181 | if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
|
---|
182 | return;
|
---|
183 | r = atof(s.substr(p,q-p).c_str());
|
---|
184 | im = 0.;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|