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

Last change on this file since 2760 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
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
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
[1559]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}
[1157]42
[1080]43/* --Methode-- */
44MuTyV::MuTyV(MuTyV const & a)
45{
46 typ = a.typ; iv = a.iv; dv = a.dv; dv_im = a.dv_im;
[1310]47 if (typ == MTVString) strv = new string(*(a.strv));
[1080]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{
[1310]60 typ = MTVString;
[1080]61 strv = new string(s);
[1157]62 mutyv_decodestr(s, dv, dv_im);
[1080]63 iv = (int_8)dv;
64}
65
66/* --Methode-- */
67MuTyV::MuTyV(string const& s)
68{
[1310]69 typ = MTVString;
[1080]70 strv = new string(s);
[1157]71 mutyv_decodestr(s.c_str(), dv, dv_im);
[1080]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;
[1310]79 if (typ == MTVString) {
[1225]80 if (strv) *strv = *(a.strv);
81 else strv = new string(*(a.strv));
82 }
[1080]83 return(*this);
84}
85
86/* --Methode-- */
87char * MuTyV::operator= (char* s)
88{
[1310]89 typ = MTVString;
[1225]90 if (strv) *strv = s;
91 else strv = new string(s);
[1157]92 mutyv_decodestr(s, dv, dv_im);
[1080]93 iv = (int_8)dv;
94 return(s);
95}
96
97/* --Methode-- */
98string & MuTyV::operator= (string& s)
99{
[1310]100 typ = MTVString;
[1225]101 if (strv) *strv = s;
102 else strv = new string(s);
[1157]103 mutyv_decodestr(s.c_str(), dv, dv_im);
[1080]104 iv = (int_8)dv;
105 return(s);
106}
107
108/* --Methode-- */
109MuTyV::operator string() const
110{
[1310]111 if (typ == MTVString) return(*strv);
[1080]112 else {
113 char buff[96];
[1310]114 if (typ == MTVInteger) sprintf(buff,"%ld", (long)iv);
[1559]115 else if (typ == MTVFloat) sprintf(buff,"%g", dv);
116 else if (typ == MTVComplex) sprintf(buff,"(%g,%g)", dv, dv_im);
[1080]117 else buff[0] = '\0';
118 return(string(buff));
119 }
120}
121
122
[1559]123
124static void mutyv_decodestr(string const & s, double& r, double& im)
[1157]125 // decodage d'une chaine contenant une ou deux valeurs
126{
127 r = im = 0.;
128 size_t l = s.length();
[1559]129 size_t p = s.find_first_not_of(" \t",0);
130 size_t q,q2;
[1157]131 if (p >= l) return;
[1559]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);
[1157]148 if (!isdigit(s[p]) && !(s[p] == '+') && !(s[p] == '-') )
149 return;
150 r = atof(s.substr(p,q-p).c_str());
[1559]151 im = 0.;
[1157]152}
[1080]153
154
[1157]155
156
Note: See TracBrowser for help on using the repository browser.