Changeset 718 in Sophya for trunk/SophyaLib/NTools/dvlist.cc


Ignore:
Timestamp:
Feb 6, 2000, 4:05:44 PM (26 years ago)
Author:
ansari
Message:

Ajout commentaire/variable ds DVList - Remplacement float/double par r_4 r_8
Ajout classe XNTuple (importe/adapte depuis PEIDA) - Reza 7 Fev 2000

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/NTools/dvlist.cc

    r515 r718  
    2727//      en particulier destinés à être inclus dans d'autres objets
    2828//      PPersist plus complexes. La classe DVList gère des
    29 //      variables de type entier ("int_4"), réél double précision ("double")
     29//      variables de type entier ("int_8"), réél double précision ("r_8")
    3030//      et de type chaine de caracteres ("string, char*", maxi 30 caracteres ).
    3131//      Une classe intermédiaire (*MuTyV*) est utilisée pour représenter une
     
    117117for(it = dvl.mvlist.begin(); it != dvl.mvlist.end(); it++)
    118118  {
    119   switch ((*it).second.typ)
     119  switch ((*it).second.elval.typ)
    120120    {
    121121    case 'I' :
    122       SetI((*it).first, (*it).second.mtv.iv);
     122      SetI((*it).first, (*it).second.elval.mtv.iv);
    123123      break;
    124124    case 'D' :
    125       SetD((*it).first, (*it).second.mtv.dv);
     125      SetD((*it).first, (*it).second.elval.mtv.dv);
    126126      break;
    127127    case 'S' :
    128       SetS((*it).first, (*it).second.mtv.strv);
     128      SetS((*it).first, (*it).second.elval.mtv.strv);
    129129      break;
    130130    default :
     
    138138
    139139//++
    140 // int_4   GetI(string const& key, int_4  def=-1)
    141 // double  GetD(string const& key, double def=-9.e19)
     140// int_8   GetI(string const& key, int_8  def=-1)
     141// r_8     GetD(string const& key, r_8 def=-9.e19)
    142142// string  GetS(string const& key, char* def="")
    143143//      Retourne la valeur de la variable de nom "key" et de type entier, réél,
    144144//      chaine de caracteres.
    145145//      Si la variable n'existe pas, la valeur par défaut "def" est renvoyée.
    146 //--
    147 
    148 /* --Methode-- */
    149 int_4       DVList::GetI(string const& key, int_4 def)
     146// string  GetComment(string const& key)
     147//      Retourne le commentaire associé à la variable de nom "key".
     148//--
     149
     150/* --Methode-- */
     151int_8       DVList::GetI(string const& key, int_8 def)
    150152{
    151153ValList::iterator it = mvlist.find(key);
    152154if (it == mvlist.end())  return(def);
    153 if ( (*it).second.typ != 'I') return(def);
    154 return((*it).second.mtv.iv);
    155 }
    156 
    157 /* --Methode-- */
    158 double      DVList::GetD(string const& key, double def)
     155if ( (*it).second.elval.typ != 'I') return(def);
     156return((*it).second.elval.mtv.iv);
     157}
     158
     159/* --Methode-- */
     160r_8      DVList::GetD(string const& key, r_8 def)
    159161{
    160162ValList::iterator it = mvlist.find(key);
    161163if (it == mvlist.end())  return(def);
    162 if ( (*it).second.typ != 'D') return(def);
    163 return((*it).second.mtv.dv);
     164if ( (*it).second.elval.typ != 'D') return(def);
     165return((*it).second.elval.mtv.dv);
    164166}
    165167
     
    169171ValList::iterator it = mvlist.find(key);
    170172if (it == mvlist.end())  return(def);
    171 if ( (*it).second.typ != 'S') return(def);
    172 return((*it).second.mtv.strv);
    173 }
    174 
    175 //++
    176 // void  SetI(string const& key, int_4  val)
    177 // void  SetD(string const& key, double val)
     173if ( (*it).second.elval.typ != 'S') return(def);
     174return((*it).second.elval.mtv.strv);
     175}
     176
     177/* --Methode-- */
     178string      DVList::GetComment(string const& key)
     179{
     180ValList::iterator it = mvlist.find(key);
     181if (it == mvlist.end())  return("");
     182return((*it).second.elcomm);
     183}
     184
     185//++
     186// void  SetI(string const& key, int_8  val)
     187// void  SetD(string const& key, r_8 val)
    178188// void  SetS(string const& key, char*  val)
    179189// void  SetS(string const& key, string val)
    180190//      Crée la variable de nom "key", de type entier, double, string et
    181191//      lui attribue la valeur "val". Si une variable du même nom existe,
    182 //      sa valeur et eventuellement son type sont modifiés. 
    183 //--
    184 
    185 /* --Methode-- */
    186 void        DVList::SetI(string const& key, int_4 val)
    187 {
    188 Get(key) = (int_4)val;
    189 }
    190 
    191 /* --Methode-- */
    192 void        DVList::SetD(string const& key, double val)
    193 {
    194 Get(key) = (double)val;
     192//      sa valeur et eventuellement son type sont modifiés.  Les noms de
     193//      variables ne doivent pas contenir de caractères spéciaux,
     194//      en particulier pas de CR/LF.
     195// void  SetComment(string const& key, string const& comm)
     196//      Modifie le commentaire associé à la variable de nom "key", si
     197//      celle-ci existe. Le texte du commentaire ne doit pas contenir
     198//      de caractères spéciaux, et en particulier pas de CR/LF.
     199//--
     200
     201/* --Methode-- */
     202void        DVList::SetI(string const& key, int_8 val)
     203{
     204Get(key) = (int_8)val;
     205}
     206
     207/* --Methode-- */
     208void        DVList::SetD(string const& key, r_8 val)
     209{
     210Get(key) = (r_8)val;
    195211}
    196212
     
    209225}
    210226
     227/* --Methode-- */
     228void        DVList::SetComment(string const& key, string const& comm)
     229{
     230ValList::iterator it = mvlist.find(key);
     231if (it == mvlist.end())  return;
     232(*it).second.elcomm = comm;
     233}
    211234
    212235//++
     
    228251if ( (l < 1) || (key.find_first_of(" ") < l) )  return(ddvdum);
    229252ValList::iterator it = mvlist.find(key);
    230 if (it == mvlist.end()) mvlist[key] = (int_4) 0;  // $CHECK$ EA. Ambigu si pas de cast...
     253dvlElement xxx = {(int_8)0 , ""};
     254if (it == mvlist.end()) mvlist[key] = xxx; 
    231255it = mvlist.find(key);
    232256if (it == mvlist.end()) return(ddvdum);
    233 else return((*it).second);
     257else return((*it).second.elval);
    234258}
    235259
     
    256280{
    257281os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n";
    258 os << "Comment: " << comment << "\n";
    259 char buff[128];
     282if (comment.length() > 0)  os << comment << endl;
     283char buff[256];
    260284ValList::const_iterator it;
    261285for(it = mvlist.begin(); it != mvlist.end(); it++)  {
    262   switch ((*it).second.typ)
     286  switch ((*it).second.elval.typ)
    263287    {
    264288    case 'I' :
    265       sprintf(buff, "%s = %d (int) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
     289      sprintf(buff, "%s = %d (int) %s\n", (*it).first.substr(0,64).c_str(),
     290              (*it).second.elval.mtv.iv, (*it).second.elcomm.substr(0,128).c_str());
    266291      break;
    267292    case 'D' :
    268       sprintf(buff, "%s = %.20g (double) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
     293      sprintf(buff, "%s = %.20g (double) %s\n", (*it).first.substr(0,64).c_str(),
     294              (*it).second.elval.mtv.dv, (*it).second.elcomm.substr(0,128).c_str());
    269295      break;
    270296    case 'S' :
    271       sprintf(buff, "%s = %s (string) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
     297      sprintf(buff, "%s = %s (string) %s\n", (*it).first.substr(0,64).c_str(),
     298              (*it).second.elval.mtv.strv, (*it).second.elcomm.substr(0,128).c_str());
    272299      break;
    273300    default :
     
    287314//|     mvu = 66.6;        // et double (= 66.6) maintenant ...   
    288315//|     MuTyV mvi(14);     // On construit une variable entiere = 14
    289 //|     float x = mvi;     // x vaut 14.0
     316//|     r_4 x = mvi;     // x vaut 14.0
    290317//|     MuTyV mvd(44.4);   // Variable double = 44.4
    291318//|     int k = mvd;       // k vaut 44 
     
    305332//|     DVList dvlr("dvlist.ppf");
    306333//|     int k = dvlr["toto"] ;     //  k = 14
    307 //|     double b = dvlr["titi"] ;  //  b = 25.5
     334//|     r_8 b = dvlr["titi"] ;  //  b = 25.5
    308335//|     string s =  dvlr["tata"] ; //  s = "Bonjour, Ca va ?"
    309 //|     float c = dvlr["Hello"] ;  //  c = 77.77
     336//|     r_4 c = dvlr["Hello"] ;  //  c = 77.77
    310337//|     int l =  dvlr["Hello"] ;   //  l = 77
    311338//|     int m =  dvlr["hello"] ;   //  m = 88
     
    321348void        ObjFileIO<DVList>::WriteSelf(POutPersist& s) const
    322349{
    323 char buf[320];
     350char buf[512];
    324351
    325352int lc = dobj->Comment().length();
    326 if (lc > 319) lc = 319;
     353if (lc > 511) lc = 511;
    327354if (lc > 0) {
    328355  sprintf(buf,"Comment: ( %6d ) ", lc);
     
    333360DVList::ValList::const_iterator it;
    334361for(it = dobj->Begin(); it != dobj->End(); it++)  {
    335   switch ((*it).second.typ)
    336     {
     362  switch ((*it).second.elval.typ) {
    337363    case 'I' :
    338       sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv );
     364      sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.iv );
    339365      s.PutLine(buf);
    340366      break;
    341367    case 'D' :
    342       sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv );
     368      sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.dv );
    343369      s.PutLine(buf);
    344370      break;
    345371    case 'S' :
    346       sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv );
     372      sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.strv );
    347373      s.PutLine(buf);
    348374      break;
    349375    default :
    350376      break;
    351     }
    352   }
     377  }
     378// Ecriture eventuelle du commentaire associe
     379  if ((*it).second.elcomm.length() > 0) {
     380    sprintf(buf,"# %s", (*it).second.elcomm.substr(0,256).c_str());
     381    s.PutLine(buf);
     382  }
     383}
    353384
    354385s.PutLine("ZZZZZ--End-of-Varible-List------");
     
    358389void        ObjFileIO<DVList>::ReadSelf(PInPersist& s)
    359390{
    360 char buf[320];
    361 int_4 j,iv;
    362 double dv;
     391char buf[512];
     392int_8 j,iv;
     393r_8 dv;
    363394bool ok=true;
    364395buf[0] = '\0';
    365396dobj->Clear();
    366397
    367 s.GetLine(buf, 319);  // Pour lire les "------- "
     398s.GetLine(buf, 511);  // Pour lire les "------- "
    368399if (buf[0] != '-') {  // Il y a un champ commentaire a lire
    369400  buf[18] ='\0';
    370401  int lc = atoi(buf+11);
    371   if (lc > 319)  {
     402  if (lc > 511)  {
    372403    cerr << "DVList::ReadSelf() Pb/Bug ??  CommentLength= " << lc << endl;
    373     lc = 319;
     404    lc = 511;
    374405    }
    375406  s.GetBytes(buf, lc);
     
    378409  }
    379410
     411string key="";
    380412while(ok) {
    381   s.GetLine(buf, 319);
    382   buf[319] = '\0';
     413  s.GetLine(buf, 511);
     414  buf[511] = '\0';
    383415  if (strncmp(buf,"ZZZZZ",5) == 0)  { ok=false; break; }
     416  if (buf[0] == '#') {
     417    dobj->SetComment(key, buf+2);
     418    continue;
     419  }
    384420  j = posc(buf+2, ' ')+2;
    385421  buf[j] = '\0';
    386   switch (buf[0])
    387     {
     422  switch (buf[0]) {
    388423    case 'I' :
    389       iv = (int_4)atol(buf+j+1);
    390       dobj->SetI(buf+2, iv);
     424      iv = (int_8)atol(buf+j+1);
     425      key = buf+2;
     426      dobj->SetI(key, iv);
    391427      break;
    392428    case 'D' :
    393429      dv = atof(buf+j+1);
    394       dobj->SetD(buf+2, dv);
     430      key = buf+2;
     431      dobj->SetD(key, dv);
    395432      break;
    396433    case 'S' :
    397       dobj->SetS(buf+2, buf+j+1);
     434      key = buf+2;
     435      dobj->SetS(key, buf+j+1);
    398436      break;
    399437    default :
Note: See TracChangeset for help on using the changeset viewer.