38#define SREPLACE(s) REPLACE(s)
40#define RESPLOTDIR SREPLACE(RESPLOTDIR_TMP)
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() ) {
65 for (
unsigned i=0 ; i<
m_Path.size() ; i++ ) {
66 std::string tmp_filename =
m_Path[i]+filename;
74 cerr <<
"ReadCards::Construct() cannot open file " << filename << endl;
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 );
87 std::sprintf( cmd,
"cpp -I. -P %s > %s",
m_FileName.c_str(), tfile );
91 std::cout <<
"pid: " << pid <<
" " << tfile << std::endl;
96 cout <<
"ReadCards::Construct() opening " <<
m_FileName << endl;
102 std::sprintf( cmd,
"rm %s", tfile );
109int count(
const std::string& s,
const std::string& p ) {
110 if ( s.find(p)==std::string::npos )
return 0;
112 for (
size_t i=0 ; i<s.size() ; i++ ) {
113 if ( s.substr(i,p.size()) == p)
count++;
124 ostringstream os(ostringstream::out);
134 getline(
m_File,line,
'\n');
139 if ( (pos=line.find(
"//")) != std::string::npos ) {
143 for (
unsigned iq=0 ; iq<pos ; iq++ ) {
144 if ( line[iq]==
'\"' ) quotecount++;
145 else if ( line[iq]==
'"' ) quotecount++;
150 if ( quotecount%2==0 ) {
151 std::string tmpline =
chop(line,
"//");
155 line = std::move(tmpline);
161 if ( (pos=line.find(
'#')) != std::string::npos ) {
165 for (
unsigned iq=0 ; iq<pos ; iq++ ) {
166 if ( line[iq]==
'\"' ) quotecount++;
167 else if ( line[iq]==
'"' ) quotecount++;
172 if ( quotecount%2==0 ) {
173 std::string tmpline =
chop(line,
"#");
184 if ( line.size() ) os << line;
215 while ( pos != std::string::npos ) {
217 std::string duff =
m_String.substr(0,pos);
220 size_t n =
count( duff,
"\"" );
237 if (
m_String.find(
'|')==std::string::npos ) {
238 error(
"syntax error, missing semicolon at end of input " +
m_String );
245 line =
chop(line,
"|");
252 for (
size_t iq=0 ; iq<line.size() ; iq++ ) {
254 if ( line[iq]==
'\"' ) quotecount++;
255 else if ( line[iq]==
'"' ) quotecount++;
256 if ( line[iq]==
'=' )
break;
260 if ( found && quotecount>0 )
error(
"syntax error in tag name : " + input +
" quotes" );
262 string sline =
chop(line,
"=");
282 if ( sline.size() )
error(
"syntax error in tag name : " + input );
295 vector<std::string> values;
302 for (
size_t iq=0 ; iq<line.size() ; iq++ ) {
304 if ( line[iq]==
'\"' ) quotecount++;
305 else if ( line[iq]==
'"' ) quotecount++;
306 if ( line[iq]==
'{' && ( quotecount==0 || quotecount%2==0 ) )
break;
309 if ( found && quotecount>0 )
error(
"syntax error in tag name : " + input +
" quotes" );
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);
329 while ( line.size() ) {
344 if ( qo.size()>1 )
error(
"syntax error before quote : " + input);
349 if ( qo.size()==1 ) {
354 if ( token.size()== 0 )
error(
"sytax error, missing quote : " + input);
366 if (
chop(line,
",").size() )
error(
"syntax error after quote : " + input);
369 token =
chop(line,
",");
376 if ( token.find(
' ')!=string::npos )
error(
"space not within quotes : " + input);
380 if ( qo.empty() && token.empty() )
error(
"missing token : " + input);
383 size_t pos = token.find(
"####");
384 while ( pos !=std::string::npos ) {
385 token.replace( pos, 4,
";" );
386 pos = token.find(
"####");
389 values.push_back(std::move(token));
395 if ( bra.empty() && values.size()>1 )
error(
"missing braces : " + input);
398 if ( values.empty() ) {
401 error(
"tag with no value : " + input);
421 if ( s.size() )
error(
"syntax error in tag name : " + input );
431 vector<std::string> sv;
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);
444 string token =
chop(s,
",");
452 if ( qo.size()>1 ||
qc.size()>1 )
error(
"syntax error before \" : " + input);
455 if (
qc.size()!=qo.size() )
error(
"mismatched \" : " + input);
459 if ( token.find(
' ')!=string::npos )
error(
"space not wintin \"\" : " + input);
462 if ( token.empty() )
error(
"missing token : " + input);
464 sv.push_back(std::move(token));
467 if ( sv.empty() )
error(
"tag with no value " + input);
486 for (
unsigned i=0 ; i<
m_Values.size() ; i++ ) {
490 printf(
"read tag %s\t = ",
m_Values[i].Tag().c_str() );
492 for (
unsigned j=0 ; j<
m_Values[i].Val().size() ; j++ ) {
496 const std::vector<std::string>& vals =
m_Values[i].Val();
498 if ( vals[j].size()>10 || ( vals.size()>5 && !(j%5) ) ) printf(
"\n\t\t\t");
499 printf(
"%s ", vals[j].c_str() );
#define RESPLOTDIR
set up the search path for the config files
int count(const std::string &s, const std::string &p)
static const Attributes_t empty
vector< string > parseright(string &s)
bool AddTag(const string &tag, const vector< string > &values)
static std::vector< std::string > m_Path
string parseleft(string &s)
void parse()
parse the remaining cleaned input string
void Construct(const std::string &filename)
check for file in cwd or if not, check in the RESPLOTDIR, then read the file
void clean()
remove comments and whitespace
std::vector< Value > m_Values
std::string choptoken(std::string &s1, const std::string &s2)
std::string chopends(std::string &s1, const std::string &s2)
std::string chomp(std::string &s1, const std::string &s2)
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
std::string chopfirst(std::string &s1, const std::string &s2)
std::string choplast(std::string &s1, const std::string &s2)
std::string chomptoken(std::string &s1, const std::string &s2)
std::string chop(std::string &s1, const std::string &s2)
bool canopen(const std::string &s)