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 SysTools
|
---|
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 |
|
---|
34 | static void mutyv_decodestr(const char * si, double& r, double& im);
|
---|
35 |
|
---|
36 | /* --Methode-- */
|
---|
37 | MuTyV::MuTyV(MuTyV const & a)
|
---|
38 | {
|
---|
39 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
40 | if (typ == MTVString) strv = new string(*(a.strv));
|
---|
41 | else strv = NULL;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* --Methode-- */
|
---|
45 | MuTyV::~MuTyV()
|
---|
46 | {
|
---|
47 | if (strv) delete strv;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* --Methode-- */
|
---|
51 | MuTyV::MuTyV(char const* s)
|
---|
52 | {
|
---|
53 | typ = MTVString;
|
---|
54 | strv = new string(s);
|
---|
55 | mutyv_decodestr(s, dv, dv_im);
|
---|
56 | iv = (int_8)dv;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* --Methode-- */
|
---|
60 | MuTyV::MuTyV(string const& s)
|
---|
61 | {
|
---|
62 | typ = MTVString;
|
---|
63 | strv = new string(s);
|
---|
64 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
65 | iv = (int_8)dv;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* --Methode-- */
|
---|
69 | MuTyV & MuTyV::operator= (MuTyV const & a)
|
---|
70 | {
|
---|
71 | typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
|
---|
72 | if (typ == MTVString) {
|
---|
73 | if (strv) *strv = *(a.strv);
|
---|
74 | else strv = new string(*(a.strv));
|
---|
75 | }
|
---|
76 | return(*this);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* --Methode-- */
|
---|
80 | char * MuTyV::operator= (char* s)
|
---|
81 | {
|
---|
82 | typ = MTVString;
|
---|
83 | if (strv) *strv = s;
|
---|
84 | else strv = new string(s);
|
---|
85 | mutyv_decodestr(s, dv, dv_im);
|
---|
86 | iv = (int_8)dv;
|
---|
87 | return(s);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* --Methode-- */
|
---|
91 | string & MuTyV::operator= (string& s)
|
---|
92 | {
|
---|
93 | typ = MTVString;
|
---|
94 | if (strv) *strv = s;
|
---|
95 | else strv = new string(s);
|
---|
96 | mutyv_decodestr(s.c_str(), dv, dv_im);
|
---|
97 | iv = (int_8)dv;
|
---|
98 | return(s);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* --Methode-- */
|
---|
102 | MuTyV::operator string() const
|
---|
103 | {
|
---|
104 | if (typ == MTVString) return(*strv);
|
---|
105 | else {
|
---|
106 | char buff[96];
|
---|
107 | if (typ == MTVInteger) sprintf(buff,"%ld", (long)iv);
|
---|
108 | else if (typ == MTVFloat) sprintf(buff,"%.20g", dv);
|
---|
109 | else if (typ == MTVComplex) sprintf(buff,"(%.20g , %.20g)", dv, dv_im);
|
---|
110 | else buff[0] = '\0';
|
---|
111 | return(string(buff));
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static void mutyv_decodestr(const char * si, double& r, double& im)
|
---|
117 | // decodage d'une chaine contenant une ou deux valeurs
|
---|
118 | {
|
---|
119 | r = im = 0.;
|
---|
120 | string s = si;
|
---|
121 | size_t l = s.length();
|
---|
122 | size_t p = s.find_first_not_of(" ()\t",0);
|
---|
123 | if (p >= l) return;
|
---|
124 | size_t q = s.find_first_of(" ()\t",p+1);
|
---|
125 |
|
---|
126 |
|
---|
127 | if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
|
---|
128 | return;
|
---|
129 | r = atof(s.substr(p,q-p).c_str());
|
---|
130 |
|
---|
131 | p = s.find_first_not_of(" ()\t",q+1);
|
---|
132 | if (p >= l) return;
|
---|
133 | q = s.find_first_of(" ()\t",p+1);
|
---|
134 | if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
|
---|
135 | return;
|
---|
136 | im = atof(s.substr(p,q-p).c_str());
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|