1 | // $Id: datacards.cc,v 1.1.1.1 1999-04-09 17:57:55 ansari Exp $
|
---|
2 | //
|
---|
3 | // Datacards, acquisition EROS II
|
---|
4 | //
|
---|
5 | //
|
---|
6 | // Eric Aubourg, Decembre 95
|
---|
7 | //
|
---|
8 | // DAPNIA/SPP (Saclay) / CEA
|
---|
9 |
|
---|
10 | #include "defs.h"
|
---|
11 | #include "datacards.h"
|
---|
12 | #include <algorithm>
|
---|
13 | #include <iostream.h>
|
---|
14 | #include <iomanip.h>
|
---|
15 | #include <stdio.h>
|
---|
16 |
|
---|
17 | //++
|
---|
18 | // Class DataCards
|
---|
19 | // Lib Outils++
|
---|
20 | // include datacards.h
|
---|
21 | //
|
---|
22 | // Cette classe permet la gestion des parametres d'un programme a partir
|
---|
23 | // de mot-cle (lecture d'un fichier par exemple)
|
---|
24 | //--
|
---|
25 |
|
---|
26 | //++
|
---|
27 | // Titre Constructeurs
|
---|
28 | //--
|
---|
29 | //++
|
---|
30 | //
|
---|
31 | // DataCards()
|
---|
32 | // DataCards(string const& fn)
|
---|
33 | // Createur avec lecture des parametres ds le fichier de nom "fn"
|
---|
34 | //--
|
---|
35 |
|
---|
36 | DataCards::DataCards()
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 | DataCards::DataCards(string const& fn)
|
---|
41 | {
|
---|
42 | ReadFile(fn);
|
---|
43 | }
|
---|
44 |
|
---|
45 | //++
|
---|
46 | // Titre Methodes
|
---|
47 | //--
|
---|
48 | //++
|
---|
49 | // AddProcF(ProcCard f, string const& mtch="*")
|
---|
50 | // Ajoute une fonction de traitement a la liste pour les mots cle
|
---|
51 | // compatibles avec la chaine "mtch" ("mtch" peut contenir "*" en debut
|
---|
52 | // fin de mot)
|
---|
53 | //
|
---|
54 | // Clear()
|
---|
55 | // Supprime les cartes existantes
|
---|
56 | //
|
---|
57 | // ReadFile(string const& fn)
|
---|
58 | // Lit le contenu du fichiers "fn" et ajoute les cartes a la liste
|
---|
59 | //
|
---|
60 | // AppendCard(string const& line)
|
---|
61 | // Rajoute la carte "line" a la liste
|
---|
62 | //--
|
---|
63 |
|
---|
64 | void
|
---|
65 | DataCards::AddProcF(ProcCard f, string const& mtch)
|
---|
66 | {
|
---|
67 | CrdPF mpf;
|
---|
68 | if (f == NULL) return;
|
---|
69 | mpf.pf = f;
|
---|
70 | if (mtch.length() <= 0) mpf.patt = "*";
|
---|
71 | else mpf.patt = mtch;
|
---|
72 | cpfs.push_back(mpf);
|
---|
73 |
|
---|
74 | // On applique cette nouvelle fonction aux cartes existantes
|
---|
75 | CardList::iterator ic;
|
---|
76 | for(ic = cards.begin(); ic != cards.end(); ic ++)
|
---|
77 | {
|
---|
78 | vector<string>::iterator ik;
|
---|
79 | string tks;
|
---|
80 | for(ik = (*ic).tokens.begin(); ik != (*ic).tokens.end(); ik++)
|
---|
81 | tks = tks + " " + (*ik);
|
---|
82 | ApplyPF(mpf, (*ic).kw, tks);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | void
|
---|
87 | DataCards::Clear()
|
---|
88 | {
|
---|
89 | cards.erase(cards.begin(), cards.end());
|
---|
90 | }
|
---|
91 |
|
---|
92 | void
|
---|
93 | DataCards::ReadFile(string const& fn)
|
---|
94 | {
|
---|
95 | string file = fn;
|
---|
96 | if (file == "") {
|
---|
97 | char * efn = getenv("PEIDA_DATACARDS");
|
---|
98 | if (efn != NULL) file = efn;
|
---|
99 | }
|
---|
100 | if (file == "") return;
|
---|
101 |
|
---|
102 | TRY {
|
---|
103 | DoReadFile(file);
|
---|
104 | } CATCHALL {
|
---|
105 | char* wdp = getenv("PEIDA_WORK");
|
---|
106 | if (wdp) {
|
---|
107 | cerr << "DataCards::ReadFile() Error reading file " << fn << " (Trying with PEIDA_WORK) \n";
|
---|
108 | file = wdp + file;
|
---|
109 | DoReadFile(file);
|
---|
110 | }
|
---|
111 | else cerr << "DataCards::ReadFile() Error reading file " << fn << "\n";
|
---|
112 | THROW(fileErr);
|
---|
113 |
|
---|
114 | } ENDTRY
|
---|
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 ) THROW(fileErr);
|
---|
154 | while (fgets(line_buff,511,fip) != NULL)
|
---|
155 | {
|
---|
156 | line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
|
---|
157 | string line(line_buff);
|
---|
158 | AppendCard(line);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | int
|
---|
163 | DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks)
|
---|
164 | {
|
---|
165 | int l,lk;
|
---|
166 | int rc = 0;
|
---|
167 | // On verifie si le "pattern" correspond
|
---|
168 | bool mtch = false;
|
---|
169 | l = cpf.patt.length();
|
---|
170 | if (cpf.patt == "*") mtch = true;
|
---|
171 | else if (cpf.patt[0] == '*')
|
---|
172 | {
|
---|
173 | lk = key.length();
|
---|
174 | if (cpf.patt[l-1] != '*')
|
---|
175 | {
|
---|
176 | if (strcmp(key.c_str()+(lk-l+1), cpf.patt.c_str()+1) == 0) mtch = true;
|
---|
177 | }
|
---|
178 | else if (key.find(cpf.patt.substr(1,l-2)) < lk) mtch = true;
|
---|
179 | }
|
---|
180 | else if (cpf.patt[l-1] == '*')
|
---|
181 | {
|
---|
182 | if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0) mtch = true;
|
---|
183 | }
|
---|
184 | else if (key == cpf.patt) mtch = true;
|
---|
185 |
|
---|
186 | // Si oui, on appelle la fonction correspondante
|
---|
187 | if (mtch) rc = cpf.pf(key, toks);
|
---|
188 |
|
---|
189 | return(rc);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | int
|
---|
194 | DataCards::ApplyPFL(string const& key, string const& toks)
|
---|
195 | {
|
---|
196 | int rc = 0;
|
---|
197 | CrdPFList::iterator icf;
|
---|
198 | for(icf = cpfs.begin(); icf != cpfs.end(); icf++)
|
---|
199 | rc += ApplyPF((*icf), key, toks);
|
---|
200 | return(rc);
|
---|
201 | }
|
---|
202 |
|
---|
203 | void
|
---|
204 | DataCards::RemoveCard(string const& key)
|
---|
205 | {
|
---|
206 | CardList::iterator i;
|
---|
207 | for(i=cards.begin(); i != cards.end(); i++)
|
---|
208 | if ((*i).kw == key) { cards.erase(i); break; }
|
---|
209 | }
|
---|
210 |
|
---|
211 | DataCards::Card *
|
---|
212 | DataCards::FindKey(string const& key)
|
---|
213 | {
|
---|
214 | /*
|
---|
215 | CardList::iterator i = find_if(cards.begin(), cards.end(), bind2nd(KeyEq(),key));
|
---|
216 | if (i == cards.end() ) return NULL;
|
---|
217 | */
|
---|
218 | CardList::iterator i;
|
---|
219 | for(i=cards.begin(); i != cards.end(); i++)
|
---|
220 | if ((*i).kw == key) return &*i;
|
---|
221 |
|
---|
222 | return NULL;
|
---|
223 | }
|
---|
224 |
|
---|
225 | //++
|
---|
226 | // Titre Acces aux parametres
|
---|
227 | //--
|
---|
228 | //++
|
---|
229 | // int NbCards()
|
---|
230 | // Renvoie le nombre de cartes data
|
---|
231 | // bool HasKey(string const& key)
|
---|
232 | // Indique l'existence d'une carte avec la cle "key"
|
---|
233 | // int NbParam(string const& key)
|
---|
234 | // Indique le nombre de parametre (separes par des espaces) pour la cle "key"
|
---|
235 | // string SParam(string const& key, int num = 0, string def="")
|
---|
236 | // Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) sous forme de
|
---|
237 | // chaine de caracteres ("string")
|
---|
238 | // long IParam(string const& key, int numero = 0, long def = 0)
|
---|
239 | // Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) convertie
|
---|
240 | // en entier ("long")
|
---|
241 | // double DParam(string const& key, int numero = 0, double def = 0)
|
---|
242 | // Renvoie la valeur du parametre numero "num" ( 0..(NbParam()-1) ) convertie
|
---|
243 | // en flottant ("double")
|
---|
244 | //--
|
---|
245 |
|
---|
246 |
|
---|
247 | bool
|
---|
248 | DataCards::HasKey(string const& key)
|
---|
249 | {
|
---|
250 | return FindKey(key) != NULL;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int
|
---|
254 | DataCards::NbCards()
|
---|
255 | {
|
---|
256 | return(cards.size());
|
---|
257 | }
|
---|
258 |
|
---|
259 | int
|
---|
260 | DataCards::NbParam(string const& key)
|
---|
261 | {
|
---|
262 | DataCards::Card * p = FindKey(key);
|
---|
263 | if (!p) return(-1);
|
---|
264 | else return(p->tokens.size());
|
---|
265 | }
|
---|
266 |
|
---|
267 | string
|
---|
268 | DataCards::SParam(string const& key, int numero, string def)
|
---|
269 | {
|
---|
270 | DataCards::Card * p = FindKey(key);
|
---|
271 | if (!p) return def;
|
---|
272 | if ( (numero < 0) || (numero >= p->tokens.size()) ) return def;
|
---|
273 | return p->tokens[numero];
|
---|
274 | }
|
---|
275 |
|
---|
276 | long
|
---|
277 | DataCards::IParam(string const& key, int numero, long def)
|
---|
278 | {
|
---|
279 | string p = SParam(key, numero, "");
|
---|
280 | if (p == "") return def;
|
---|
281 | long i;
|
---|
282 | //istrstream(p.c_str(), p.length()) >> i;
|
---|
283 | sscanf(p.c_str(),"%ld",&i);
|
---|
284 | return i;
|
---|
285 | }
|
---|
286 |
|
---|
287 | double
|
---|
288 | DataCards::DParam(string const& key, int numero, double def)
|
---|
289 | {
|
---|
290 | string p = SParam(key, numero, "");
|
---|
291 | if (p == "") return def;
|
---|
292 | double i;
|
---|
293 | //istrstream(p.c_str(), p.length()) >> i;
|
---|
294 | sscanf(p.c_str(),"%lg",&i);
|
---|
295 | return i;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | ostream& operator << (ostream& s, DataCards c)
|
---|
300 | {
|
---|
301 | for (DataCards::CardList::iterator i = c.cards.begin(); i != c.cards.end(); i++) {
|
---|
302 | s << setw(10) << (*i).kw << " ";
|
---|
303 | for (vector<string>::iterator j = (*i).tokens.begin(); j != (*i).tokens.end(); j++)
|
---|
304 | s << (*j) << " ";
|
---|
305 | s << endl;
|
---|
306 | }
|
---|
307 | return s;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 |
|
---|