source: Sophya/trunk/SophyaLib/NTools/datacards.cc@ 445

Last change on this file since 445 was 244, checked in by ansari, 26 years ago

beaucoup modifs rz+cmv 22/4/99

File size: 6.7 KB
Line 
1// $Id: datacards.cc,v 1.2 1999-04-22 16:18:22 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 <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
36DataCards::DataCards()
37{
38}
39
40DataCards::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
64void
65DataCards::AddProcF(ProcCard f, string const& mtch)
66{
67CrdPF mpf;
68if (f == NULL) return;
69mpf.pf = f;
70if (mtch.length() <= 0) mpf.patt = "*";
71else mpf.patt = mtch;
72cpfs.push_back(mpf);
73
74// On applique cette nouvelle fonction aux cartes existantes
75CardList::iterator ic;
76for(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
86void
87DataCards::Clear()
88{
89cards.erase(cards.begin(), cards.end());
90}
91
92void
93DataCards::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
117void
118DataCards::AppendCard(string const& crd)
119{
120Card c;
121size_t p = 1;
122size_t q = crd.find_first_of(" \t");
123size_t l = crd.length();
124
125string toks;
126if (l < 2) return;
127if (crd[0] != '@') return;
128
129if (q < l)
130 { c.kw = crd.substr(p,q-p); toks = crd.substr(q, l-q); }
131else { c.kw = crd.substr(p,l-p); toks = ""; }
132// On applique les ProcFunc's
133ApplyPFL(c.kw, toks);
134while (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 ...
143RemoveCard(c.kw);
144cards.push_back(c);
145}
146
147void
148DataCards::DoReadFile(string const& fn)
149{
150char line_buff[512];
151FILE *fip;
152
153if ( (fip = fopen(fn.c_str(),"r")) == NULL ) THROW(fileErr);
154while (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
162int
163DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks)
164{
165int l,lk;
166int rc = 0;
167// On verifie si le "pattern" correspond
168bool mtch = false;
169l = cpf.patt.length();
170if (cpf.patt == "*") mtch = true;
171else 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 }
180else if (cpf.patt[l-1] == '*')
181 {
182 if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0) mtch = true;
183 }
184else if (key == cpf.patt) mtch = true;
185
186// Si oui, on appelle la fonction correspondante
187if (mtch) rc = cpf.pf(key, toks);
188
189return(rc);
190}
191
192
193int
194DataCards::ApplyPFL(string const& key, string const& toks)
195{
196int rc = 0;
197CrdPFList::iterator icf;
198for(icf = cpfs.begin(); icf != cpfs.end(); icf++)
199 rc += ApplyPF((*icf), key, toks);
200return(rc);
201}
202
203void
204DataCards::RemoveCard(string const& key)
205{
206CardList::iterator i;
207for(i=cards.begin(); i != cards.end(); i++)
208 if ((*i).kw == key) { cards.erase(i); break; }
209}
210
211DataCards::Card *
212DataCards::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
247bool
248DataCards::HasKey(string const& key)
249{
250 return FindKey(key) != NULL;
251}
252
253int
254DataCards::NbCards()
255{
256return(cards.size());
257}
258
259int
260DataCards::NbParam(string const& key)
261{
262 DataCards::Card * p = FindKey(key);
263 if (!p) return(-1);
264 else return(p->tokens.size());
265}
266
267string
268DataCards::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
276long
277DataCards::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
287double
288DataCards::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
299ostream& 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
Note: See TracBrowser for help on using the repository browser.