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

Last change on this file since 1157 was 1157, checked in by ansari, 25 years ago

Declaration sa_size_t ds machdefs.h - Amelioration/correction DVList et MuTyV - Reza 29/8/2000

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