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

Last change on this file since 2146 was 1607, checked in by cmv, 24 years ago

after base restructuration cmv 31/7/01

File size: 3.6 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 "mutyv.h"
7#include <stdio.h>
8
9#include <iostream.h>
10#include <string.h>
11
12/*!
13 \class SOPHYA::MuTyV
14 \ingroup BaseTools
15 Simple utility class which can be used to hold values of type
16 string, integer (\b int_8) or float (\b r_8) as well as complex,
17 It provides also easy conversion methods between these types.
18
19 \code
20 // ------- Using MuTyV objects -------
21 MuTyV mvu; // MuTyV variable declaration
22 mvu = 60; // mvu contains the integer value 60
23 mvu = 66.6; // and now the double value 66.6
24 string ds = mvu; // ds contains the string "66.6"
25 MuTyV mvi(14); // New MuTyV variable containing integer value 14
26 r_4 x = mvi; // x has the value 14.0
27 MuTyV mvs("Bonjour !"); // mvs contains the string "Bonjour !"
28 string s = mvs; // s contains "Bonjour !"
29
30 \endcode
31*/
32
33
34static void mutyv_decodestr(string const &, double& r, double& im);
35static inline void mutyv_decodestr_cp(const char * si, double& r, double& im)
36{
37 r = im = 0.;
38 string s = si;
39 mutyv_decodestr(s, r, im);
40}
41
42/* --Methode-- */
43MuTyV::MuTyV(MuTyV const & a)
44{
45 typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
46 if (typ == MTVString) strv = new string(*(a.strv));
47 else strv = NULL;
48}
49
50/* --Methode-- */
51MuTyV::~MuTyV()
52{
53 if (strv) delete strv;
54}
55
56/* --Methode-- */
57MuTyV::MuTyV(char const* s)
58{
59 typ = MTVString;
60 strv = new string(s);
61 mutyv_decodestr(s, dv, dv_im);
62 iv = (int_8)dv;
63}
64
65/* --Methode-- */
66MuTyV::MuTyV(string const& s)
67{
68 typ = MTVString;
69 strv = new string(s);
70 mutyv_decodestr(s.c_str(), dv, dv_im);
71 iv = (int_8)dv;
72}
73
74/* --Methode-- */
75MuTyV & MuTyV::operator= (MuTyV const & a)
76{
77 typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
78 if (typ == MTVString) {
79 if (strv) *strv = *(a.strv);
80 else strv = new string(*(a.strv));
81 }
82 return(*this);
83}
84
85/* --Methode-- */
86char * MuTyV::operator= (char* s)
87{
88 typ = MTVString;
89 if (strv) *strv = s;
90 else strv = new string(s);
91 mutyv_decodestr(s, dv, dv_im);
92 iv = (int_8)dv;
93 return(s);
94}
95
96/* --Methode-- */
97string & MuTyV::operator= (string& s)
98{
99 typ = MTVString;
100 if (strv) *strv = s;
101 else strv = new string(s);
102 mutyv_decodestr(s.c_str(), dv, dv_im);
103 iv = (int_8)dv;
104 return(s);
105}
106
107/* --Methode-- */
108MuTyV::operator string() const
109{
110 if (typ == MTVString) return(*strv);
111 else {
112 char buff[96];
113 if (typ == MTVInteger) sprintf(buff,"%ld", (long)iv);
114 else if (typ == MTVFloat) sprintf(buff,"%g", dv);
115 else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
116 else buff[0] = '\0';
117 return(string(buff));
118 }
119}
120
121
122
123static void mutyv_decodestr(string const & s, double& r, double& im)
124 // decodage d'une chaine contenant une ou deux valeurs
125{
126 r = im = 0.;
127 size_t l = s.length();
128 size_t p = s.find_first_not_of(" \t",0);
129 size_t q,q2;
130 if (p >= l) return;
131 if (s[p] == '(') { // C'est un complexe
132 if ((q2=s.find(')',p)) >= l) return;
133 size_t pz = s.find_first_not_of(" \t",p+1);
134 size_t qz = q2;
135 if ((q=s.find(',',pz)) < q2) qz = q;
136 if (isdigit(s[pz]) || !(s[pz] == '+') || (s[pz] == '-') )
137 r = atof(s.substr(pz,qz-pz).c_str());
138 else return;
139 if (qz == q) {
140 pz = s.find_first_not_of(" \t",qz+1);
141 if (isdigit(s[pz]) || (s[pz] == '+') || (s[pz] == '-') )
142 im = atof(s.substr(pz,q2-pz).c_str());
143 }
144 }
145
146 q = s.find_first_of(" \t",p+1);
147 if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
148 return;
149 r = atof(s.substr(p,q-p).c_str());
150 im = 0.;
151}
152
153
154
155
Note: See TracBrowser for help on using the repository browser.