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

Last change on this file since 2879 was 2826, checked in by ansari, 20 years ago

1/ Correction bug TimeStamp::ToDays() + petites amelioration
2/ Prise en compte du type TimeStamp dans MuTyV (sous forme de r_8 en interne)
3/ Adaptation DVList a modifs MuTyV (typ TimeStamp) et R/W PPersist

+ Petites corrections et MAJ num.version , Reza 2 Nov 2005

File size: 4.4 KB
RevLine 
[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]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}
[1157]49
[1080]50/* --Methode-- */
51MuTyV::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-- */
59MuTyV::~MuTyV()
60{
61 if (strv) delete strv;
62}
63
64/* --Methode-- */
65MuTyV::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-- */
74MuTyV::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]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-- */
[1080]93MuTyV & 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]104const 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]115string 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]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-- */
[1080]137MuTyV::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-- */
152MuTyV::operator TimeStamp() const
153{
154 return TimeStamp(dv);
155}
[1080]156
[1559]157static void mutyv_decodestr(string const & s, double& r, double& im)
[1157]158 // decodage d'une chaine contenant une ou deux valeurs
159{
160 r = im = 0.;
161 size_t l = s.length();
[1559]162 size_t p = s.find_first_not_of(" \t",0);
163 size_t q,q2;
[1157]164 if (p >= l) return;
[1559]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);
[1157]181 if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
182 return;
183 r = atof(s.substr(p,q-p).c_str());
[1559]184 im = 0.;
[1157]185}
[1080]186
187
[1157]188
189
Note: See TracBrowser for help on using the repository browser.