| [1080] | 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 | 
 | 
|---|
| [1157] | 34 | static void mutyv_decodestr(const char * si, double& r, double& im);
 | 
|---|
 | 35 | 
 | 
|---|
| [1080] | 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 == 'S')  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 = 'S';
 | 
|---|
 | 54 |   strv = new string(s);
 | 
|---|
| [1157] | 55 |   mutyv_decodestr(s, dv, dv_im);
 | 
|---|
| [1080] | 56 |   iv = (int_8)dv;
 | 
|---|
 | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /* --Methode-- */
 | 
|---|
 | 60 | MuTyV::MuTyV(string const& s)
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 |   typ = 'S';
 | 
|---|
 | 63 |   strv = new string(s);
 | 
|---|
| [1157] | 64 |   mutyv_decodestr(s.c_str(), dv, dv_im);
 | 
|---|
| [1080] | 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 == 'S')  strv = new string(*(a.strv));
 | 
|---|
 | 73 |   else strv = NULL;
 | 
|---|
 | 74 |   return(*this);
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | /* --Methode-- */
 | 
|---|
 | 78 | char * MuTyV::operator= (char* s)
 | 
|---|
 | 79 | {
 | 
|---|
 | 80 |   typ = 'S';
 | 
|---|
 | 81 |   strv = new string(s);
 | 
|---|
| [1157] | 82 |   mutyv_decodestr(s, dv, dv_im);
 | 
|---|
| [1080] | 83 |   iv = (int_8)dv;
 | 
|---|
 | 84 |   return(s);
 | 
|---|
 | 85 | }
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | /* --Methode-- */
 | 
|---|
 | 88 | string & MuTyV::operator= (string& s)
 | 
|---|
 | 89 | {
 | 
|---|
 | 90 |   typ = 'S';
 | 
|---|
 | 91 |   strv = new string(s);
 | 
|---|
| [1157] | 92 |   mutyv_decodestr(s.c_str(), dv, dv_im);
 | 
|---|
| [1080] | 93 |   iv = (int_8)dv;
 | 
|---|
 | 94 |   return(s);
 | 
|---|
 | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | /* --Methode-- */
 | 
|---|
 | 98 | MuTyV::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);
 | 
|---|
| [1157] | 105 |     else if (typ == 'Z') sprintf(buff,"(%.20g , %.20g)", dv, dv_im);
 | 
|---|
| [1080] | 106 |     else buff[0] = '\0';
 | 
|---|
 | 107 |     return(string(buff));
 | 
|---|
 | 108 |   }
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | 
 | 
|---|
| [1157] | 112 | static 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());
 | 
|---|
| [1080] | 126 | 
 | 
|---|
| [1157] | 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 | }
 | 
|---|
| [1080] | 134 | 
 | 
|---|
 | 135 | 
 | 
|---|
| [1157] | 136 | 
 | 
|---|
 | 137 | 
 | 
|---|