Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser.cc
Go to the documentation of this file.
1 #include "marlin/Parser.h"
2 
3 #include <algorithm>
4 
5 namespace marlin{
6 
7  // open steering file with processor names
8  Parser::Parser( const std::string& fileName) :
9  _current(0) , _fileName( fileName ) {
10  }
11 
13  }
14 
15 
16  void Parser::parse(){
17 
18  std::ifstream inFile( _fileName.c_str() ) ;
19 
20  if( ! inFile ){
21  std::cerr << "Parser::Parser: couldn't open file: " << _fileName << std::endl ;
22  return ;
23  }
24 
25 
26  std::string inputLine ;
27  StringParameters params ;
28 
29  while( readNextValidLine( inputLine , inFile) ){
30 
31 
33  LCTokenizer t( tokens ,' ') ;
34 
35 
36  std::for_each( inputLine.begin(),inputLine.end(), t ) ;
37 
38  // std::cout << " tokens " ;
39  // for(int i=0;i<tokens.size();i++){
40  // std::cout << tokens[i] << "| " ;
41  // }
42  // std::cout << std::endl ;
43 
44  // ignore empty lines (containing whitespace and tabs only )
45  if( tokens.size() < 1 ) continue ;
46 
47  // check whether a new section starts:
48  if( tokens[0] == ".begin" ) {
49 
50  if( tokens.size() < 2 ) {
51  std::cerr << " Parser::parse : section has to have a name: .begin sectionName " << std::endl ;
52  exit(1) ;
53  }
54  _map[ tokens[1] ] = std::make_shared<StringParameters>();
55  _current = _map[ tokens[1] ].get() ;
56  // std::cout << " Parser::parse: >>> creating new map entry : " << tokens[1] << std::endl ;
57 
58  } else if ( tokens[0] == ".end" ) {
59 
60  _current = 0 ;
61 
62  } else if ( _current != 0 ) {
63 
64 
65  _current->add( tokens ) ;
66 
67 
68  // std::cout << " Parser::parse: >>> "
69  // << tokens[0] << " = "
70  // << _current->getIntVal( tokens[0] ) << " | "
71  // << _current->getFloatVal( tokens[0] ) << " | "
72  // << _current->getStringVal( tokens[0] )
73  // << std::endl ;
74 
75  }
76  }
77 
78 
79  // do some postprocessing ( create global entry with available processors )
80 
81  std::vector<std::string> availableProcs ;
82  availableProcs.push_back("AvailableProcessors") ;
83 
84  StringParameters* global = 0 ;
85 
86  for( StringParametersMap::const_iterator iter = _map.begin() ; iter != _map.end() ; iter++){
87 
88  std::string name = iter->first ;
89 
90  StringParameters* p = iter->second.get() ;
91 
92  std::string type = p->getStringVal("ProcessorType") ;
93 
94  if( type.size() > 0 ) {
95 
96  availableProcs.push_back( name ) ;
97  }
98 
99  if( name == "Global" )
100  global = p ;
101 
102  // std::cout << " parameter section " << iter->first
103  // << std::endl
104  // << *iter->second
105  // << std::endl ;
106 
107  }
108  if( global != 0 )
109  global->add( availableProcs ) ;
110  }
111 
112 
114 
115  for( StringParametersMap::const_iterator iter = _map.begin() ; iter != _map.end() ; iter++){
116  // std::cout << " parameter section " << iter->first
117  // << std::endl
118  // << *iter->second
119  // << std::endl ;
120  }
121 
122  return _map[ sectionName ] ;
123 
124 // StringParametersMap::const_iterator iter ;
125 // if( ( iter = _map.find( sectionName ) ) != _map.end() )
126 // return iter->second ;
127 // else
128 // return 0 ;
129  }
130 
131 
133 
134  while( ! stream.eof() ) {
135 
136  getline( stream , str ) ;
137 
138 
139  char firstChar = ' ' ;
140  bool haveFirst = false ;
141 
142  for(unsigned int i=0; i < str.length() ; i ++){
143 
144  // replace tabs and cariage returns with whitespace
145  if( (str[i] == '\t') || (str[i] == '\r') ) str[i] = ' ' ;
146 
147  // get first non whitespace character
148  if( ! haveFirst && str[i] != ' ' ){
149  firstChar = str[i] ;
150  haveFirst = true ;
151  }
152  }
153  // if( str.length() != 0 && str[0] != '#' )
154  if( str.length() != 0 && firstChar != '#' )
155  return str.length() ;
156  }
157 
158  return 0 ;
159  }
160 
161 
162 
163  void Parser::write(const std::string& fname) const{
164  std::ifstream inFile( _fileName.c_str() ) ;
165 
166  if( ! inFile ){
167  std::cerr << "Parser::write: couldn't open input file: " << _fileName << std::endl ;
168  return ;
169  }
170 
171  std::ofstream outFile( fname.c_str() ) ;
172 
173  if( ! outFile ){
174  std::cerr << "Parser::write: couldn't open output file: " << fname << std::endl ;
175  return ;
176  }
177 
178  outFile << inFile.rdbuf() ;
179  inFile.close() ;
180  outFile.close() ;
181  }
182 
183 
184 
185 
186 } // namespace marlin
std::shared_ptr< StringParameters > getParameters(const std::string &sectionName) const
Return the StringParameters defined for this section of the steering file.
Definition: Parser.cc:113
std::string _fileName
Definition: Parser.h:75
T endl(T...args)
T end(T...args)
void add(const std::string &key, const std::vector< std::string > &values)
Parser()=delete
STL class.
virtual ~Parser()
Definition: Parser.cc:12
STL class.
T eof(T...args)
T push_back(T...args)
STL class.
Helper class for Parser.
Definition: Parser.h:90
const std::string & getStringVal(const std::string &key)
int readNextValidLine(std::string &str, std::istream &stream)
Helper method that reads the next line from a stream that is not empty or starts with a &#39;#&#39;...
Definition: Parser.cc:132
StringParametersMap _map
Definition: Parser.h:72
T size(T...args)
void parse()
Parse the input file.
Definition: Parser.cc:16
T begin(T...args)
void write(const std::string &fname) const
Write down the parsed file in a new file.
Definition: Parser.cc:163
T c_str(T...args)
StringParameters * _current
Definition: Parser.h:73
T for_each(T...args)
Simple parameters class for Marlin.
STL class.