Changeset 895 in Sophya for trunk/SophyaLib/SUtils/datacards.cc


Ignore:
Timestamp:
Apr 12, 2000, 7:49:54 PM (25 years ago)
Author:
ansari
Message:

Documentation de fichiers - Reza 12/4/2000

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SUtils/datacards.cc

    r754 r895  
    1 // $Id: datacards.cc,v 1.1 2000-03-02 16:10:58 ansari Exp $
     1// $Id: datacards.cc,v 1.2 2000-04-12 17:49:42 ansari Exp $
    22//
    33// Datacards, acquisition EROS II
     
    1818//++
    1919// Class        DataCards
    20 // Lib          Outils++
     20// Lib          SysTools
    2121// include      datacards.h
    2222//
     
    2424//      de mot-cle (lecture d'un fichier par exemple)
    2525//--
     26
    2627
    2728//++
     
    3536//--
    3637
     38/*!
     39    \class SOPHYA::DataCards
     40    This class can be used for decoding program parameters from an ascii
     41    file. Each line of the ascii contains a keyword starting with \b @ .
     42    Lines with any other character in the first column are treated as comments.
     43    Keywords can be followed by 0 or more parameters.
     44    Processing functions (\b ProcCard) can automatically be called for
     45    keywords matching a given pattern. (see \b AddProcF )
     46*/
     47
     48/* Default constructor */
    3749DataCards::DataCards()
    3850{
    3951}
    4052
     53/*! constructor with the specification of a parameter file */
    4154DataCards::DataCards(string const& fn)
    4255{
     
    6376//--
    6477
     78/*!
     79    Adds a new processing function for all keywords matching the
     80    specified pattern.
     81    \param mtch : The pattern - can contain *
     82*/
    6583void
    6684DataCards::AddProcF(ProcCard f, string const& mtch)
     
    85103}
    86104
    87 void
    88 DataCards::Clear()
     105/*! Resets the objects - Suppresses all cards  */
     106void DataCards::Clear()
    89107{
    90108cards.erase(cards.begin(), cards.end());
    91109}
    92110
     111/*! Reads the file named \b fn. if fn=="" the value of \b SOPHYA_DATACARDS is used */
    93112void
    94113DataCards::ReadFile(string const& fn)
     
    96115  string file = fn;
    97116  if (file == "") {
    98     char * efn = getenv("PEIDA_DATACARDS");
     117    char * efn = getenv("SOPHYA_DATACARDS");
    99118    if (efn != NULL)    file = efn; 
    100119  }
     
    104123    DoReadFile(file);
    105124  } catch(...) {
    106     char* wdp = getenv("PEIDA_WORK");
     125    char* wdp = getenv("SOPHYA_WORK");
    107126    if (wdp) {
    108       cerr << "DataCards::ReadFile() Error reading file " << fn << " (Trying with PEIDA_WORK) \n";
     127      cerr << "DataCards::ReadFile() Error reading file " << fn << " (Trying with SOPHYA_WORK) \n";
    109128      file = wdp + file;
    110129      DoReadFile(file);
     
    115134}
    116135
     136/*! Appends a card, represented by the string \b crd to the list of cards */
    117137void
    118138DataCards::AppendCard(string const& crd)
     
    145165}
    146166
    147 void
    148 DataCards::DoReadFile(string const& fn)
    149 {
    150 char line_buff[512];
    151 FILE *fip;
    152 
    153 if ( (fip = fopen(fn.c_str(),"r")) == NULL ) 
    154   throw IOExc("DataCards::DoReadFile() fopen Error ") ;
    155 while (fgets(line_buff,511,fip) != NULL)
    156   {
    157   line_buff[strlen(line_buff)-1] = '\0';   /*  LF/CR de la fin */
    158   string line(line_buff);
    159   AppendCard(line);
    160   }
    161 }
    162 
    163 int
    164 DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks)
    165 {
    166 int l,lk;
    167 int rc = 0;
    168 // On verifie si le "pattern" correspond
    169 bool mtch = false;
    170 l = cpf.patt.length();
    171 if (cpf.patt == "*")  mtch = true;
    172 else if (cpf.patt[0] == '*')   
    173   {
    174   lk = key.length();
    175   if (cpf.patt[l-1] != '*')
    176     {
    177     if (strcmp(key.c_str()+(lk-l+1), cpf.patt.c_str()+1) == 0)   mtch = true;
    178     }
    179   else if  (key.find(cpf.patt.substr(1,l-2)) < lk)  mtch = true;
    180   }
    181 else if (cpf.patt[l-1] == '*')
    182   {
    183   if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0)  mtch = true;
    184   }
    185 else if (key == cpf.patt)  mtch = true;
    186 
    187 // Si oui, on appelle la fonction correspondante
    188 if (mtch)  rc = cpf.pf(key, toks);
    189 
    190 return(rc);
    191 }
    192 
    193 
    194 int
    195 DataCards::ApplyPFL(string const& key, string const& toks)
    196 {
    197 int rc = 0;
    198 CrdPFList::iterator icf;
    199 for(icf = cpfs.begin(); icf != cpfs.end(); icf++)
    200   rc += ApplyPF((*icf), key, toks);
    201 return(rc);
    202 }
    203 
    204 void 
    205 DataCards::RemoveCard(string const& key)
    206 {
    207 CardList::iterator i;
    208 for(i=cards.begin(); i != cards.end(); i++)
    209   if ((*i).kw == key) { cards.erase(i);  break;  }
    210 }
    211 
    212 DataCards::Card *
    213 DataCards::FindKey(string const& key)
    214 {
    215 /*
    216   CardList::iterator i = find_if(cards.begin(), cards.end(), bind2nd(KeyEq(),key));
    217   if (i == cards.end() ) return NULL;
    218 */
    219   CardList::iterator i;
    220   for(i=cards.begin(); i != cards.end(); i++)
    221     if ((*i).kw == key) return &*i;
    222 
    223   return NULL;
    224 }
    225167
    226168//++
     
    246188
    247189
     190/*! Returns true if \b key is present in the list */
    248191bool
    249192DataCards::HasKey(string const& key)
     
    252195}
    253196
     197//! Returns the total number of cards
    254198int
    255199DataCards::NbCards()
     
    258202}
    259203
     204/*! Returns the number of parameters for card \b key. (-1) if \b key not present  */
    260205int
    261206DataCards::NbParam(string const& key)
     
    266211}
    267212
     213/*! Returns the parameter number \b numero for card \b key */
    268214string
    269215DataCards::SParam(string const& key, int numero, string def)
     
    275221}
    276222
     223/*! Returns the parameter number \b numero for card \b key, converted to type long */
    277224long
    278225DataCards::IParam(string const& key, int numero, long def)
     
    286233}
    287234
     235/*! Returns the parameter number \b numero for card \b key, converted to type double */
    288236double
    289237DataCards::DParam(string const& key, int numero, double def)
     
    298246
    299247   
    300 ostream& operator << (ostream& s, DataCards c)
    301 {
    302   for (DataCards::CardList::iterator i = c.cards.begin(); i != c.cards.end(); i++) {
     248/*! Prints the list of cards on the output stream \b s */
     249void
     250DataCards::Print(ostream& s) const
     251{
     252  for (CardList::const_iterator i = cards.begin(); i != cards.end(); i++) {
    303253    s << setw(10) << (*i).kw << " ";
    304     for (vector<string>::iterator j = (*i).tokens.begin(); j != (*i).tokens.end(); j++)
     254    for (vector<string>::const_iterator j = (*i).tokens.begin(); j != (*i).tokens.end(); j++)
    305255      s << (*j) << " ";
    306256    s << endl;
    307257  }
    308   return s;
    309 }
    310 
    311 
    312 
     258}
     259
     260/*! Reads the file named \b fn. */
     261void
     262DataCards::DoReadFile(string const& fn)
     263{
     264char line_buff[512];
     265FILE *fip;
     266
     267if ( (fip = fopen(fn.c_str(),"r")) == NULL ) 
     268  throw IOExc("DataCards::DoReadFile() fopen Error ") ;
     269while (fgets(line_buff,511,fip) != NULL)
     270  {
     271  line_buff[strlen(line_buff)-1] = '\0';   /*  LF/CR de la fin */
     272  string line(line_buff);
     273  AppendCard(line);
     274  }
     275}
     276
     277int
     278DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks)
     279{
     280int l,lk;
     281int rc = 0;
     282// On verifie si le "pattern" correspond
     283bool mtch = false;
     284l = cpf.patt.length();
     285if (cpf.patt == "*")  mtch = true;
     286else if (cpf.patt[0] == '*')   
     287  {
     288  lk = key.length();
     289  if (cpf.patt[l-1] != '*')
     290    {
     291    if (strcmp(key.c_str()+(lk-l+1), cpf.patt.c_str()+1) == 0)   mtch = true;
     292    }
     293  else if  (key.find(cpf.patt.substr(1,l-2)) < lk)  mtch = true;
     294  }
     295else if (cpf.patt[l-1] == '*')
     296  {
     297  if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0)  mtch = true;
     298  }
     299else if (key == cpf.patt)  mtch = true;
     300
     301// Si oui, on appelle la fonction correspondante
     302if (mtch)  rc = cpf.pf(key, toks);
     303
     304return(rc);
     305}
     306
     307
     308int
     309DataCards::ApplyPFL(string const& key, string const& toks)
     310{
     311int rc = 0;
     312CrdPFList::iterator icf;
     313for(icf = cpfs.begin(); icf != cpfs.end(); icf++)
     314  rc += ApplyPF((*icf), key, toks);
     315return(rc);
     316}
     317
     318void 
     319DataCards::RemoveCard(string const& key)
     320{
     321CardList::iterator i;
     322for(i=cards.begin(); i != cards.end(); i++)
     323  if ((*i).kw == key) { cards.erase(i);  break;  }
     324}
     325
     326DataCards::Card *
     327DataCards::FindKey(string const& key)
     328{
     329/*
     330  CardList::iterator i = find_if(cards.begin(), cards.end(), bind2nd(KeyEq(),key));
     331  if (i == cards.end() ) return NULL;
     332*/
     333  CardList::iterator i;
     334  for(i=cards.begin(); i != cards.end(); i++)
     335    if ((*i).kw == key) return &*i;
     336
     337  return NULL;
     338}
     339
     340
     341
Note: See TracChangeset for help on using the changeset viewer.