| 1 | // $Id: datacards.cc,v 1.1.1.1 1999-11-26 16:37:09 ansari Exp $ | 
|---|
| 2 | // | 
|---|
| 3 | // Datacards, acquisition EROS II | 
|---|
| 4 | // | 
|---|
| 5 | // | 
|---|
| 6 | // Eric Aubourg, Decembre 95 | 
|---|
| 7 | // | 
|---|
| 8 | // DAPNIA/SPP (Saclay) / CEA | 
|---|
| 9 |  | 
|---|
| 10 | #include "machdefs.h" | 
|---|
| 11 | #include "datacards.h" | 
|---|
| 12 | #include "pexceptions.h" | 
|---|
| 13 | #include <algorithm> | 
|---|
| 14 | #include <iostream.h> | 
|---|
| 15 | #include <iomanip.h> | 
|---|
| 16 | #include <stdio.h> | 
|---|
| 17 |  | 
|---|
| 18 | //++ | 
|---|
| 19 | // Class        DataCards | 
|---|
| 20 | // Lib          Outils++ | 
|---|
| 21 | // include      datacards.h | 
|---|
| 22 | // | 
|---|
| 23 | //      Cette classe permet la gestion des parametres d'un programme a partir | 
|---|
| 24 | //      de mot-cle (lecture d'un fichier par exemple) | 
|---|
| 25 | //-- | 
|---|
| 26 |  | 
|---|
| 27 | //++ | 
|---|
| 28 | // Titre        Constructeurs | 
|---|
| 29 | //-- | 
|---|
| 30 | //++ | 
|---|
| 31 | // | 
|---|
| 32 | // DataCards() | 
|---|
| 33 | // DataCards(string const& fn) | 
|---|
| 34 | //    Createur avec lecture des parametres ds le fichier de nom "fn" | 
|---|
| 35 | //-- | 
|---|
| 36 |  | 
|---|
| 37 | DataCards::DataCards() | 
|---|
| 38 | { | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | DataCards::DataCards(string const& fn) | 
|---|
| 42 | { | 
|---|
| 43 | ReadFile(fn); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | //++ | 
|---|
| 47 | // Titre        Methodes | 
|---|
| 48 | //-- | 
|---|
| 49 | //++ | 
|---|
| 50 | // AddProcF(ProcCard f, string const& mtch="*") | 
|---|
| 51 | //      Ajoute une fonction de traitement a la liste pour les mots cle | 
|---|
| 52 | //      compatibles avec la chaine "mtch" ("mtch" peut contenir "*" en debut | 
|---|
| 53 | //      fin de mot) | 
|---|
| 54 | // | 
|---|
| 55 | // Clear() | 
|---|
| 56 | //      Supprime les cartes existantes | 
|---|
| 57 | // | 
|---|
| 58 | // ReadFile(string const& fn) | 
|---|
| 59 | //      Lit le contenu du fichiers "fn" et ajoute les cartes a la liste | 
|---|
| 60 | // | 
|---|
| 61 | // AppendCard(string const& line) | 
|---|
| 62 | //      Rajoute la carte "line" a la liste | 
|---|
| 63 | //-- | 
|---|
| 64 |  | 
|---|
| 65 | void | 
|---|
| 66 | DataCards::AddProcF(ProcCard f, string const& mtch) | 
|---|
| 67 | { | 
|---|
| 68 | CrdPF mpf; | 
|---|
| 69 | if (f == NULL)  return; | 
|---|
| 70 | mpf.pf = f; | 
|---|
| 71 | if (mtch.length() <= 0)  mpf.patt = "*"; | 
|---|
| 72 | else  mpf.patt = mtch; | 
|---|
| 73 | cpfs.push_back(mpf); | 
|---|
| 74 |  | 
|---|
| 75 | // On applique cette nouvelle fonction aux cartes existantes | 
|---|
| 76 | CardList::iterator  ic; | 
|---|
| 77 | for(ic = cards.begin(); ic != cards.end(); ic ++) | 
|---|
| 78 | { | 
|---|
| 79 | vector<string>::iterator ik; | 
|---|
| 80 | string tks; | 
|---|
| 81 | for(ik = (*ic).tokens.begin(); ik != (*ic).tokens.end(); ik++) | 
|---|
| 82 | tks = tks + " " + (*ik); | 
|---|
| 83 | ApplyPF(mpf, (*ic).kw, tks); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void | 
|---|
| 88 | DataCards::Clear() | 
|---|
| 89 | { | 
|---|
| 90 | cards.erase(cards.begin(), cards.end()); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void | 
|---|
| 94 | DataCards::ReadFile(string const& fn) | 
|---|
| 95 | { | 
|---|
| 96 | string file = fn; | 
|---|
| 97 | if (file == "") { | 
|---|
| 98 | char * efn = getenv("PEIDA_DATACARDS"); | 
|---|
| 99 | if (efn != NULL)    file = efn; | 
|---|
| 100 | } | 
|---|
| 101 | if (file == "")  return; | 
|---|
| 102 |  | 
|---|
| 103 | try { | 
|---|
| 104 | DoReadFile(file); | 
|---|
| 105 | } catch(...) { | 
|---|
| 106 | char* wdp = getenv("PEIDA_WORK"); | 
|---|
| 107 | if (wdp) { | 
|---|
| 108 | cerr << "DataCards::ReadFile() Error reading file " << fn << " (Trying with PEIDA_WORK) \n"; | 
|---|
| 109 | file = wdp + file; | 
|---|
| 110 | DoReadFile(file); | 
|---|
| 111 | } | 
|---|
| 112 | else cerr << "DataCards::ReadFile() Error reading file " << fn << "\n"; | 
|---|
| 113 | throw IOExc("DataCards::ReadFile() Error"); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void | 
|---|
| 118 | DataCards::AppendCard(string const& crd) | 
|---|
| 119 | { | 
|---|
| 120 | Card c; | 
|---|
| 121 | size_t p = 1; | 
|---|
| 122 | size_t q = crd.find_first_of(" \t"); | 
|---|
| 123 | size_t l = crd.length(); | 
|---|
| 124 |  | 
|---|
| 125 | string toks; | 
|---|
| 126 | if (l < 2)  return; | 
|---|
| 127 | if (crd[0] != '@')  return; | 
|---|
| 128 |  | 
|---|
| 129 | if (q < l) | 
|---|
| 130 | {  c.kw = crd.substr(p,q-p);  toks = crd.substr(q, l-q); } | 
|---|
| 131 | else { c.kw = crd.substr(p,l-p);  toks = ""; } | 
|---|
| 132 | //  On applique les ProcFunc's | 
|---|
| 133 | ApplyPFL(c.kw, toks); | 
|---|
| 134 | while (q < l) | 
|---|
| 135 | { | 
|---|
| 136 | p = crd.find_first_not_of(" \t",q+1); // au debut d'un token | 
|---|
| 137 | if (p>=l) break; | 
|---|
| 138 | q = crd.find_first_of(" \t",p); // la fin du token; | 
|---|
| 139 | string token = crd.substr(p,q-p); | 
|---|
| 140 | c.tokens.push_back(token); | 
|---|
| 141 | } | 
|---|
| 142 | // On supprime la carte de la liste, si elle existe deja ... | 
|---|
| 143 | RemoveCard(c.kw); | 
|---|
| 144 | cards.push_back(c); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 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 | } | 
|---|
| 225 |  | 
|---|
| 226 | //++ | 
|---|
| 227 | // Titre        Acces aux parametres | 
|---|
| 228 | //-- | 
|---|
| 229 | //++ | 
|---|
| 230 | // int   NbCards() | 
|---|
| 231 | //      Renvoie le nombre de cartes data | 
|---|
| 232 | // bool  HasKey(string const& key) | 
|---|
| 233 | //      Indique l'existence d'une carte avec la cle "key" | 
|---|
| 234 | // int   NbParam(string const& key) | 
|---|
| 235 | //      Indique le nombre de parametre (separes par des espaces) pour la cle "key" | 
|---|
| 236 | // string  SParam(string const& key, int num = 0, string def="") | 
|---|
| 237 | //      Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) sous forme de | 
|---|
| 238 | //      chaine de caracteres ("string") | 
|---|
| 239 | // long    IParam(string const& key, int numero = 0, long def = 0) | 
|---|
| 240 | //      Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) convertie | 
|---|
| 241 | //      en entier ("long") | 
|---|
| 242 | // double  DParam(string const& key, int numero = 0, double def = 0) | 
|---|
| 243 | //      Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) convertie | 
|---|
| 244 | //      en flottant ("double") | 
|---|
| 245 | //-- | 
|---|
| 246 |  | 
|---|
| 247 |  | 
|---|
| 248 | bool | 
|---|
| 249 | DataCards::HasKey(string const& key) | 
|---|
| 250 | { | 
|---|
| 251 | return FindKey(key) != NULL; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | int | 
|---|
| 255 | DataCards::NbCards() | 
|---|
| 256 | { | 
|---|
| 257 | return(cards.size()); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | int | 
|---|
| 261 | DataCards::NbParam(string const& key) | 
|---|
| 262 | { | 
|---|
| 263 | DataCards::Card * p = FindKey(key); | 
|---|
| 264 | if (!p) return(-1); | 
|---|
| 265 | else return(p->tokens.size()); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | string | 
|---|
| 269 | DataCards::SParam(string const& key, int numero, string def) | 
|---|
| 270 | { | 
|---|
| 271 | DataCards::Card * p = FindKey(key); | 
|---|
| 272 | if (!p) return def; | 
|---|
| 273 | if ( (numero < 0) || (numero >= p->tokens.size()) )  return def; | 
|---|
| 274 | return p->tokens[numero]; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | long | 
|---|
| 278 | DataCards::IParam(string const& key, int numero, long def) | 
|---|
| 279 | { | 
|---|
| 280 | string p = SParam(key, numero, ""); | 
|---|
| 281 | if (p == "") return def; | 
|---|
| 282 | long i; | 
|---|
| 283 | //istrstream(p.c_str(), p.length()) >> i; | 
|---|
| 284 | sscanf(p.c_str(),"%ld",&i); | 
|---|
| 285 | return i; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | double | 
|---|
| 289 | DataCards::DParam(string const& key, int numero, double def) | 
|---|
| 290 | { | 
|---|
| 291 | string p = SParam(key, numero, ""); | 
|---|
| 292 | if (p == "") return def; | 
|---|
| 293 | double i; | 
|---|
| 294 | //istrstream(p.c_str(), p.length()) >> i; | 
|---|
| 295 | sscanf(p.c_str(),"%lg",&i); | 
|---|
| 296 | return i; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 |  | 
|---|
| 300 | ostream& operator << (ostream& s, DataCards c) | 
|---|
| 301 | { | 
|---|
| 302 | for (DataCards::CardList::iterator i = c.cards.begin(); i != c.cards.end(); i++) { | 
|---|
| 303 | s << setw(10) << (*i).kw << " "; | 
|---|
| 304 | for (vector<string>::iterator j = (*i).tokens.begin(); j != (*i).tokens.end(); j++) | 
|---|
| 305 | s << (*j) << " "; | 
|---|
| 306 | s << endl; | 
|---|
| 307 | } | 
|---|
| 308 | return s; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 |  | 
|---|
| 312 |  | 
|---|