source: Sophya/trunk/Poubelle/DPC:FitsIOServer/NTools/datacards.cc

Last change on this file was 658, checked in by ansari, 26 years ago

no message

File size: 6.8 KB
Line 
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
37DataCards::DataCards()
38{
39}
40
41DataCards::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
65void
66DataCards::AddProcF(ProcCard f, string const& mtch)
67{
68CrdPF mpf;
69if (f == NULL) return;
70mpf.pf = f;
71if (mtch.length() <= 0) mpf.patt = "*";
72else mpf.patt = mtch;
73cpfs.push_back(mpf);
74
75// On applique cette nouvelle fonction aux cartes existantes
76CardList::iterator ic;
77for(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
87void
88DataCards::Clear()
89{
90cards.erase(cards.begin(), cards.end());
91}
92
93void
94DataCards::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
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 )
154 throw IOExc("DataCards::DoReadFile() fopen Error ") ;
155while (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
163int
164DataCards::ApplyPF(CrdPF & cpf, string const& key, string const& toks)
165{
166int l,lk;
167int rc = 0;
168// On verifie si le "pattern" correspond
169bool mtch = false;
170l = cpf.patt.length();
171if (cpf.patt == "*") mtch = true;
172else 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 }
181else if (cpf.patt[l-1] == '*')
182 {
183 if ( strncmp(key.c_str(), cpf.patt.c_str(),l-1) == 0) mtch = true;
184 }
185else if (key == cpf.patt) mtch = true;
186
187// Si oui, on appelle la fonction correspondante
188if (mtch) rc = cpf.pf(key, toks);
189
190return(rc);
191}
192
193
194int
195DataCards::ApplyPFL(string const& key, string const& toks)
196{
197int rc = 0;
198CrdPFList::iterator icf;
199for(icf = cpfs.begin(); icf != cpfs.end(); icf++)
200 rc += ApplyPF((*icf), key, toks);
201return(rc);
202}
203
204void
205DataCards::RemoveCard(string const& key)
206{
207CardList::iterator i;
208for(i=cards.begin(); i != cards.end(); i++)
209 if ((*i).kw == key) { cards.erase(i); break; }
210}
211
212DataCards::Card *
213DataCards::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
248bool
249DataCards::HasKey(string const& key)
250{
251 return FindKey(key) != NULL;
252}
253
254int
255DataCards::NbCards()
256{
257return(cards.size());
258}
259
260int
261DataCards::NbParam(string const& key)
262{
263 DataCards::Card * p = FindKey(key);
264 if (!p) return(-1);
265 else return(p->tokens.size());
266}
267
268string
269DataCards::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
277long
278DataCards::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
288double
289DataCards::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
300ostream& 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
Note: See TracBrowser for help on using the repository browser.