Changeset 895 in Sophya for trunk/SophyaLib/SUtils
- Timestamp:
- Apr 12, 2000, 7:49:54 PM (25 years ago)
- Location:
- trunk/SophyaLib/SUtils
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SUtils/datacards.cc
r754 r895 1 // $Id: datacards.cc,v 1. 1 2000-03-02 16:10:58ansari Exp $1 // $Id: datacards.cc,v 1.2 2000-04-12 17:49:42 ansari Exp $ 2 2 // 3 3 // Datacards, acquisition EROS II … … 18 18 //++ 19 19 // Class DataCards 20 // Lib Outils++20 // Lib SysTools 21 21 // include datacards.h 22 22 // … … 24 24 // de mot-cle (lecture d'un fichier par exemple) 25 25 //-- 26 26 27 27 28 //++ … … 35 36 //-- 36 37 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 */ 37 49 DataCards::DataCards() 38 50 { 39 51 } 40 52 53 /*! constructor with the specification of a parameter file */ 41 54 DataCards::DataCards(string const& fn) 42 55 { … … 63 76 //-- 64 77 78 /*! 79 Adds a new processing function for all keywords matching the 80 specified pattern. 81 \param mtch : The pattern - can contain * 82 */ 65 83 void 66 84 DataCards::AddProcF(ProcCard f, string const& mtch) … … 85 103 } 86 104 87 void 88 DataCards::Clear()105 /*! Resets the objects - Suppresses all cards */ 106 void DataCards::Clear() 89 107 { 90 108 cards.erase(cards.begin(), cards.end()); 91 109 } 92 110 111 /*! Reads the file named \b fn. if fn=="" the value of \b SOPHYA_DATACARDS is used */ 93 112 void 94 113 DataCards::ReadFile(string const& fn) … … 96 115 string file = fn; 97 116 if (file == "") { 98 char * efn = getenv(" PEIDA_DATACARDS");117 char * efn = getenv("SOPHYA_DATACARDS"); 99 118 if (efn != NULL) file = efn; 100 119 } … … 104 123 DoReadFile(file); 105 124 } catch(...) { 106 char* wdp = getenv(" PEIDA_WORK");125 char* wdp = getenv("SOPHYA_WORK"); 107 126 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"; 109 128 file = wdp + file; 110 129 DoReadFile(file); … … 115 134 } 116 135 136 /*! Appends a card, represented by the string \b crd to the list of cards */ 117 137 void 118 138 DataCards::AppendCard(string const& crd) … … 145 165 } 146 166 147 void148 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 int164 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" correspond169 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 correspondante188 if (mtch) rc = cpf.pf(key, toks);189 190 return(rc);191 }192 193 194 int195 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 void205 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 }225 167 226 168 //++ … … 246 188 247 189 190 /*! Returns true if \b key is present in the list */ 248 191 bool 249 192 DataCards::HasKey(string const& key) … … 252 195 } 253 196 197 //! Returns the total number of cards 254 198 int 255 199 DataCards::NbCards() … … 258 202 } 259 203 204 /*! Returns the number of parameters for card \b key. (-1) if \b key not present */ 260 205 int 261 206 DataCards::NbParam(string const& key) … … 266 211 } 267 212 213 /*! Returns the parameter number \b numero for card \b key */ 268 214 string 269 215 DataCards::SParam(string const& key, int numero, string def) … … 275 221 } 276 222 223 /*! Returns the parameter number \b numero for card \b key, converted to type long */ 277 224 long 278 225 DataCards::IParam(string const& key, int numero, long def) … … 286 233 } 287 234 235 /*! Returns the parameter number \b numero for card \b key, converted to type double */ 288 236 double 289 237 DataCards::DParam(string const& key, int numero, double def) … … 298 246 299 247 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 */ 249 void 250 DataCards::Print(ostream& s) const 251 { 252 for (CardList::const_iterator i = cards.begin(); i != cards.end(); i++) { 303 253 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++) 305 255 s << (*j) << " "; 306 256 s << endl; 307 257 } 308 return s; 309 } 310 311 312 258 } 259 260 /*! Reads the file named \b fn. */ 261 void 262 DataCards::DoReadFile(string const& fn) 263 { 264 char line_buff[512]; 265 FILE *fip; 266 267 if ( (fip = fopen(fn.c_str(),"r")) == NULL ) 268 throw IOExc("DataCards::DoReadFile() fopen Error ") ; 269 while (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 277 int 278 DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks) 279 { 280 int l,lk; 281 int rc = 0; 282 // On verifie si le "pattern" correspond 283 bool mtch = false; 284 l = cpf.patt.length(); 285 if (cpf.patt == "*") mtch = true; 286 else 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 } 295 else if (cpf.patt[l-1] == '*') 296 { 297 if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0) mtch = true; 298 } 299 else if (key == cpf.patt) mtch = true; 300 301 // Si oui, on appelle la fonction correspondante 302 if (mtch) rc = cpf.pf(key, toks); 303 304 return(rc); 305 } 306 307 308 int 309 DataCards::ApplyPFL(string const& key, string const& toks) 310 { 311 int rc = 0; 312 CrdPFList::iterator icf; 313 for(icf = cpfs.begin(); icf != cpfs.end(); icf++) 314 rc += ApplyPF((*icf), key, toks); 315 return(rc); 316 } 317 318 void 319 DataCards::RemoveCard(string const& key) 320 { 321 CardList::iterator i; 322 for(i=cards.begin(); i != cards.end(); i++) 323 if ((*i).kw == key) { cards.erase(i); break; } 324 } 325 326 DataCards::Card * 327 DataCards::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 -
trunk/SophyaLib/SUtils/datacards.h
r754 r895 1 1 // This may look like C code, but it is really -*- C++ -*- 2 2 // 3 // $Id: datacards.h,v 1. 1 2000-03-02 16:10:59ansari Exp $3 // $Id: datacards.h,v 1.2 2000-04-12 17:49:43 ansari Exp $ 4 4 // 5 5 // Datacards, acquisition EROS II … … 14 14 #define DATACARDS_SEEN 15 15 16 #include "machdefs.h" 16 17 #include <string.h> 17 18 #include <string> … … 19 20 #include <list> 20 21 #include <vector> 21 #include <string>22 22 23 #include "peida.h" 23 24 namespace SOPHYA { 24 25 25 26 typedef int (*ProcCard)(string const& key, string const& toks); 26 27 27 class DataCards EXC_AWARE { 28 //! Class for decoding parameters from an ascii file. 29 30 class DataCards { 28 31 public: 32 29 33 DataCards(); 30 34 DataCards(string const& fn); 31 35 32 // nom dans variable d'environnement PEIDA_DATACARDS33 // par defaut, peida.datacards36 // nom dans variable d'environnement SOPHYA_DATACARDS 37 // par defaut, sophya.datacards 34 38 // Si pas chemin complet, on tente dans repertoire 35 // en cours, puis dans PEIDA_WORK39 // en cours, puis dans SOPHYA_WORK 36 40 37 41 virtual ~DataCards() {} … … 50 54 double DParam(string const& key, int numero = 0, double def = 0); 51 55 52 friend ostream& operator << (ostream& s, DataCards c);56 void Print(ostream& s) const; 53 57 54 p ublic:58 protected: 55 59 struct Card { 56 60 string kw; 57 61 vector<string> tokens; 58 STRUCTCOMPF(Card,kw) 62 bool operator == (Card const & b) const { return(kw == b.kw); } 63 bool operator < (Card const & b) const { return(kw < b.kw); } 59 64 }; 60 typedef list<Card> CardList;61 65 struct CrdPF { 62 66 ProcCard pf; 63 67 string patt; 64 STRUCTCOMPF(CrdPF,pf) 68 bool operator == (CrdPF const & b) const { return(pf == b.pf); } 69 bool operator < (CrdPF const & b) const { return(pf == b.pf); } 65 70 }; 66 typedef list<CrdPF> CrdPFList;67 protected:68 CardList cards;69 CrdPFList cpfs;70 71 71 72 void DoReadFile(string const& fn); … … 77 78 78 79 Card* FindKey(string const& key); 80 81 typedef list<Card> CardList; 82 typedef list<CrdPF> CrdPFList; 83 CardList cards; 84 CrdPFList cpfs; 85 79 86 struct KeyEq : binary_function<Card, string, bool> { 80 87 bool operator()(const Card& x, const string& y) const { return x.kw == y; } 81 88 }; 82 89 }; 90 91 //! operator << overloading - calls Print() 92 inline ostream& operator << (ostream& s, DataCards c) 93 { c.Print(s); return s; } 94 95 } // namespace SOPHYA 96 83 97 #endif
Note:
See TracChangeset
for help on using the changeset viewer.