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

Last change on this file since 2805 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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