source: Sophya/trunk/SophyaLib/BaseTools/mutyv.cc@ 3090

Last change on this file since 3090 was 2884, checked in by ansari, 20 years ago

Modifs pour compilation avec g++ 4 (V >= 3.4) - Reza 4 Jan 2006

File size: 4.9 KB
Line 
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
42static void mutyv_decodestr(string const &, double& r, double& im);
43static 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-- */
51MuTyV::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-- */
59MuTyV::~MuTyV()
60{
61 if (strv) delete strv;
62}
63
64/* --Methode-- */
65MuTyV::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-- */
74MuTyV::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-- */
83MuTyV::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-- */
93MuTyV & 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-- */
104const 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-- */
115string 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-- */
126TimeStamp 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-- */
137MuTyV::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-- */
152string & 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-- */
168MuTyV::operator TimeStamp() const
169{
170 return TimeStamp(dv);
171}
172
173/* --Methode-- */
174TimeStamp& MuTyV::Convert(TimeStamp& x) const
175{
176 x = TimeStamp(dv);
177 return x;
178}
179
180static void mutyv_decodestr(string const & s, double& r, double& im)
181 // decodage d'une chaine contenant une ou deux valeurs
182{
183 r = im = 0.;
184 size_t l = s.length();
185 size_t p = s.find_first_not_of(" \t",0);
186 size_t q,q2;
187 if (p >= l) return;
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);
204 if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
205 return;
206 r = atof(s.substr(p,q-p).c_str());
207 im = 0.;
208}
209
210
211
212
Note: See TracBrowser for help on using the repository browser.