ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCards.cxx
Go to the documentation of this file.
1
17
18
19#include <string>
20#include <algorithm>
21
22#include <stdio.h>
23
24#include <sys/types.h>
25#include <unistd.h>
26
27#include "utils.h"
28#include "ReadCards.h"
29
30
31
32std::vector<std::string> ReadCards::m_Path;
33
36
37#ifdef RESPLOTDIR_TMP
38#define SREPLACE(s) REPLACE(s)
39#define REPLACE(s) #s
40#define RESPLOTDIR SREPLACE(RESPLOTDIR_TMP)
41#else
42#define RESPLOTDIR ""
43#endif
44
46 if ( getenv("CONFIG_PATH") ) {
47 std::string config_path = getenv("CONFIG_PATH");
48 std::cout << "ReadCards::CONFIG_PATH " << config_path << std::endl;
49 while ( config_path.size() ) {
50 m_Path.push_back(chop(config_path, ":")+'/');
51 }
52 }
53 m_Path.push_back("");
54 m_Path.push_back(std::string( RESPLOTDIR )+'/');
55 // for ( int i=0 ; i<m_Path.size() ; i++ ) cout << "ReadCards>>CreatePath() m_Path[" << i << "]=" << m_Path[i] << endl;
56}
57
58
61
62void ReadCards::Construct(const std::string& filename) {
63
64 if ( m_Path.size()==0 ) CreatePath();
65 for ( unsigned i=0 ; i<m_Path.size() ; i++ ) {
66 std::string tmp_filename = m_Path[i]+filename;
67 if ( canopen(tmp_filename) ) {
68 m_FileName = std::move(tmp_filename);
69 break;
70 }
71 }
72
73 if ( m_FileName.size()==0 ) {
74 cerr << "ReadCards::Construct() cannot open file " << filename << endl;
75 exit(0);
76 }
77
78
79 int pid = getpid();
80
81 char tfile[1024];
82
83 if ( m_FileName.find('/')==std::string::npos ) std::sprintf( tfile, ".readcards-%s-%d", m_FileName.c_str(), pid );
84 else std::sprintf( tfile, ".readcards-%d", pid );
85
86 char cmd[2056];
87 std::sprintf( cmd, "cpp -I. -P %s > %s", m_FileName.c_str(), tfile );
88
89 std::system( cmd );
90
91 std::cout << "pid: " << pid << " " << tfile << std::endl;
92
93 // ReadCards inputdata(datafile);
94 // m_File.open(m_FileName.c_str());
95 m_File.open(tfile);
96 cout << "ReadCards::Construct() opening " << m_FileName << endl;
97 ReadParam();
98 // cout << "ReadCards::Construct() read "
99 // << m_Values.size() << " entries" << endl;
100 m_File.close();
101
102 std::sprintf( cmd, "rm %s", tfile );
103 std::system( cmd );
104
105 // print();
106}
107
108
109int count( const std::string& s, const std::string& p ) {
110 if ( s.find(p)==std::string::npos ) return 0;
111 int count = 0;
112 for ( size_t i=0 ; i<s.size() ; i++ ) {
113 if ( s.substr(i,p.size()) == p) count++;
114 }
115 return count;
116}
117
118
121
123
124 ostringstream os(ostringstream::out);
125 std::string line;
126
127 // cout << "ReadCards reading from file " << m_FileName << endl;
128
129
130 while ( !m_File.eof() ) {
131 size_t pos;
132
133
134 getline(m_File,line,'\n');
135
136 // cout << ">> " << line << endl;
137
138
139 if ( (pos=line.find("//")) != std::string::npos ) {
140
141 int quotecount = 0;
142
143 for ( unsigned iq=0 ; iq<pos ; iq++ ) {
144 if ( line[iq]=='\"' ) quotecount++;
145 else if ( line[iq]=='"' ) quotecount++;
146 }
147
148 // std::cout << "quotecount " << quotecount << std::endl;
149
150 if ( quotecount%2==0 ) {
151 std::string tmpline = chop(line,"//");
152 // size_t n = std::count(tmpline.begin(), tmpline.end(), "\"");
153 // size_t n = count( tmpline, "\"");
154 // if ( n%2==0 )
155 line = std::move(tmpline);
156 }
157 }
158
159#if 0
161 if ( (pos=line.find('#')) != std::string::npos ) {
162
163 int quotecount = 0;
164
165 for ( unsigned iq=0 ; iq<pos ; iq++ ) {
166 if ( line[iq]=='\"' ) quotecount++;
167 else if ( line[iq]=='"' ) quotecount++;
168 }
169
170 // std::cout << "quotecount " << quotecount << std::endl;
171
172 if ( quotecount%2==0 ) {
173 std::string tmpline = chop(line,"#");
174 // size_t n = count( tmpline, "\"");
175 // size_t n = std::count(tmpline.begin(), tmpline.end(), "\"");
176 // if ( n%2==0 )
177 line = tmpline;
178 }
179 }
180#endif
181
182 // removespace(line);
183
184 if ( line.size() ) os << line;
185 }
186
187 // cout << "ReadCards read " << i << " lines from file " << m_FileName << endl;
188
189 m_String = os.str();
190}
191
192
193
196
197
199{
200
201 // std::string shafted;
202
204
205 while ( m_String.size() ) {
206
207 // cout << "m_String.size() " << m_String.size() << endl;
208
209 // if ( m_String.size()<3 ) cout << "m_String >" << m_String << "<" << endl;
210
211 // break at semi colons that are not within "" pairs
212
213 size_t pos = m_String.find(';');
214
215 while ( pos != std::string::npos ) {
216
217 std::string duff = m_String.substr(0,pos);
218
219 // size_t n = std::count(duff.begin(), duff.end(), "\"");
220 size_t n = count( duff, "\"" );
221
222 if ( n%2==0 ) {
223 /* Make the replacement. */
224 m_String.replace( pos, 1, "|");
225 }
226 else {
227 m_String.replace( pos, 1, "####");
228 }
229
230 // std::cout << "duff: " << duff << " : " << n << " " << std::endl;
231
232
233 pos = m_String.find(';');
234
235 }
236
237 if ( m_String.find('|')==std::string::npos ) {
238 error("syntax error, missing semicolon at end of input " + m_String );
239 }
240
241 string input = choptoken(m_String,"|"); // shafted = input;
242 string line = input;
243
244 // copy the unparsed line
245 line = chop(line,"|");
246
248
249 int quotecount = 0;
250 bool found = true;
251
252 for ( size_t iq=0 ; iq<line.size() ; iq++ ) {
253 found = true;
254 if ( line[iq]=='\"' ) quotecount++;
255 else if ( line[iq]=='"' ) quotecount++;
256 if ( line[iq]=='=' ) break;
257 found = false;
258 }
259
260 if ( found && quotecount>0 ) error("syntax error in tag name : " + input + " quotes" );
261
262 string sline = chop(line, "="); // split at =
263
264 // parse the line
265
266 // nice way to do it ...
267 // string tagname = parseleft(sline); // parse the token name
268 // vector<string> values = parseright(line); // parse the vector of values
269
270
271 // and the nasty way, but we do get access to the whole line for the
272 // error reporting
273
274 // parse the tag name
275
276 // cout << "input >" << input << "<" << endl;
277 // cout << "line (val)>" << line << "<" << endl;
278 // cout << "sline (tag)>" << sline << "<" << endl;
279
280 chopends(sline);
281 string tagname = chopfirst(sline);
282 if ( sline.size() ) error("syntax error in tag name : " + input );
283
284 // cout << "tagname >" << tagname << "<" << endl;
285
286
287 // now parse the right hand side string vector
288
289 // cout << "line >" << line << "<" << endl;
290
291 chopends(line);
292
293 // cout << "line (chopped) >" << line << "<" << endl;
294
295 vector<std::string> values;
296
297 // string bra = chopfirst(s,"{");
298 // string ket = choplast(s,"}");
299
300 quotecount = 0;
301
302 for ( size_t iq=0 ; iq<line.size() ; iq++ ) {
303 found = true;
304 if ( line[iq]=='\"' ) quotecount++;
305 else if ( line[iq]=='"' ) quotecount++;
306 if ( line[iq]=='{' && ( quotecount==0 || quotecount%2==0 ) ) break;
307 found = false;
308 }
309 if ( found && quotecount>0 ) error("syntax error in tag name : " + input + " quotes" );
310
311 string bra = "";
312 string ket = "";
313
314
315 if ( found ) {
316 bra = choptoken(line,"{");
317 ket = chomptoken(line,"}");
318
319 if ( bra.size()>1 ) error("syntax error before brace : " + input);
320 if ( ket.size()>1 ) error("syntax error after brace : " + input);
321 if ( bra.size()!=ket.size() ) error("mismatched braces :" + input);
322 }
323
324 //Note: nargs is unused unless the debug code below is uncommented
325 //int nargs = 0;
326
327 bool empty = true;
328
329 while ( line.size() ) {
330
331 empty = false;
332
333 // get rid of spaces at either end of line
334 chopends(line);
335 //nargs++;
336
337 // cout << nargs << " line >" << line << "< " << endl;
338
339 // check if open quote
340 string qo = choptoken(line, "\"");
341
342 // cout << "qo >" << qo << "<" << endl;
343
344 if ( qo.size()>1 ) error("syntax error before quote : " + input);
345
346 // split the comma seperated list but...
347 // if a quoted string, don't parse until after closing quote
348 string token;
349 if ( qo.size()==1 ) { // ie if a quoted string
350 token = choptoken(line, "\""); // chop to closing quote
351
352 // cout << "token >" << token << "<" << endl;
353
354 if ( token.size()== 0 ) error("sytax error, missing quote : " + input);
355 // cout << "SIZE " << token.size() << " " << token << endl;
356
357 chopends(line); // get rid spaces around remaining line
358
359 // cout << "chopped line >" << line << "<" << endl;
360
361 choplast(token, " "); // get rid of extra spaces after closing quote
362 chomp(token, "\""); // get rid of closing quote
363
364 // cout << "final token >" << token << "<" << endl;
365
366 if ( chop(line, ",").size() ) error("syntax error after quote : " + input);
367 }
368 else {
369 token = chop(line, ",");
370
371 // cout << "token >" << token << "< line >" << line << "<" << endl;
372 }
373
374 // check no spaces in values not enclosed in quotes
375 if ( qo.empty()) {
376 if ( token.find(' ')!=string::npos ) error("space not within quotes : " + input);
377 }
378
379 // missing value
380 if ( qo.empty() && token.empty() ) error("missing token : " + input);
381
382
383 size_t pos = token.find("####");
384 while ( pos !=std::string::npos ) {
385 token.replace( pos, 4, ";" );
386 pos = token.find("####");
387 }
388
389 values.push_back(std::move(token));
390 }
391
392 if ( !empty ) {
393
394 // check the vector had braces
395 if ( bra.empty() && values.size()>1 ) error("missing braces : " + input);
396
397 // missing value
398 if ( values.empty() ) {
399 // std::cout << "shafted :" << shafted << ":" << std::endl;
400 // std::cout << "\nm_String " << m_String << std::endl;
401 error("tag with no value : " + input);
402 }
403
404 // add the tag, value pairing
405
406 AddTag(tagname,values);
407 }
408
410 }
411}
412
413
414
415
416
417string ReadCards::parseleft(string& s) {
418 chopends(s);
419 string input = s;
420 string token = chopfirst(s);
421 if ( s.size() ) error("syntax error in tag name : " + input );
422 return token;
423}
424
425
426
427vector<string> ReadCards::parseright(std::string& s) {
428 chopends(s);
429
430 string input = s;
431 vector<std::string> sv;
432
433 // string bra = chopfirst(s,"{");
434 // string ket = choplast(s,"}");
435 string bra = choptoken(s,"{");
436 string ket = chomptoken(s,"}");
437
438 if ( bra.size()>1 ) error("syntax error before { : "+input);
439 if ( ket.size()>1 ) error("syntax error after } : "+input);
440 if ( bra.size()!=ket.size() ) error("mismatched braces :" + input);
441
442 while ( s.size() ) {
443 // split the comma seperated list
444 string token = chop(s, ",");
445
446 // get rid ofr spaces at either end
447 chopends(token);
448
449 // check open and closing quotes
450 string qo = chopfirst(token, "\"");
451 string qc = choplast(token, "\"");
452 if ( qo.size()>1 || qc.size()>1 ) error("syntax error before \" : " + input);
453
454 // check quotes balance
455 if ( qc.size()!=qo.size() ) error("mismatched \" : " + input);
456
457 // check no spaces in values not enclosed in quotes
458 if ( qo.empty() ) {
459 if ( token.find(' ')!=string::npos ) error("space not wintin \"\" : " + input);
460 }
461
462 if ( token.empty() ) error("missing token : " + input);
463
464 sv.push_back(std::move(token));
465 }
466
467 if ( sv.empty() ) error("tag with no value " + input);
468
469 return sv;
470}
471
472
473
475{
476 clean();
477 parse();
478 // print();
479}
480
481
482
483
485 // cout << "ReadCards::print() read " << m_Values.size() << " cards from file " << m_FileName << endl;
486 for ( unsigned i=0 ; i<m_Values.size() ; i++ ) {
487 // cout << " ReadCards::print() read " << m_Values[i].Tag() << " =";
488 // cout << " read " << m_Values[i].Tag() << " =";
489
490 printf("read tag %s\t = ", m_Values[i].Tag().c_str() );
491
492 for ( unsigned j=0 ; j<m_Values[i].Val().size() ; j++ ) {
493 // if ( m_Values[i].Val().size()>5 ) if (!(j%5)) cout << endl << " ";
494 // cout << " " << (m_Values[i].Val())[j];
495
496 const std::vector<std::string>& vals = m_Values[i].Val();
497
498 if ( vals[j].size()>10 || ( vals.size()>5 && !(j%5) ) ) printf("\n\t\t\t");
499 printf("%s ", vals[j].c_str() );
500 }
501 cout << endl;
502 }
503}
504
505
506
507
508
#define RESPLOTDIR
set up the search path for the config files
Definition ReadCards.cxx:42
int count(const std::string &s, const std::string &p)
static const int qc[]
static const Attributes_t empty
vector< string > parseright(string &s)
std::string m_FileName
Definition ReadCards.h:217
bool AddTag(const string &tag, const vector< string > &values)
Definition ReadCards.h:203
std::string m_String
Definition ReadCards.h:221
ifstream m_File
Definition ReadCards.h:219
static std::vector< std::string > m_Path
Definition ReadCards.h:226
string parseleft(string &s)
void parse()
parse the remaining cleaned input string
void CreatePath()
Definition ReadCards.cxx:45
void ReadParam()
void Construct(const std::string &filename)
check for file in cwd or if not, check in the RESPLOTDIR, then read the file
Definition ReadCards.cxx:62
void print()
void clean()
remove comments and whitespace
std::vector< Value > m_Values
Definition ReadCards.h:223
std::string choptoken(std::string &s1, const std::string &s2)
Definition hcg.cxx:233
std::string chopends(std::string &s1, const std::string &s2)
Definition hcg.cxx:290
std::string chomp(std::string &s1, const std::string &s2)
Definition hcg.cxx:214
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
std::string chopfirst(std::string &s1, const std::string &s2)
Definition hcg.cxx:260
std::string choplast(std::string &s1, const std::string &s2)
Definition hcg.cxx:276
std::string chomptoken(std::string &s1, const std::string &s2)
Definition hcg.cxx:247
std::string chop(std::string &s1, const std::string &s2)
Definition hcg.cxx:161
bool canopen(const std::string &s)
Definition utils.cxx:178