Marlin  1.17.1
 All Classes Namespaces Functions Variables Enumerations Friends Pages
Parser.h
1 #ifndef Parser_h
2 #define Parser_h 1
3 
4 //#include "lcio.h"
5 #include "IParser.h"
6 #include "StringParameters.h"
7 #include "marlin/Exceptions.h"
8 
9 #include <fstream>
10 #include <string>
11 #include <iostream>
12 
13 namespace marlin{
14 
15 class LCTokenizer ;
16 
17 
18 typedef std::map< std::string , std::shared_ptr<StringParameters> > StringParametersMap;
19 
20 
36 class Parser : public IParser {
37 
38 
39 public:
40 
41 
42  // Parser(const std::string[] & namespaces ) ;
43  // void parse( const std::string& fileName )
44 
45  Parser( const std::string& fileName ) ;
46  virtual ~Parser() ;
47 
48  std::shared_ptr<StringParameters> getParameters( const std::string& sectionName ) const ;
49 
51  void setCmdLineParameters( const CommandLineParametersMap & ){
52  throw ParseException( "dynamic command line options only supported for xml steering files" ) ;
53  }
54 
55 
58  void parse() ;
59 
61  void write(const std::string& fname) const ;
62 
63 
64 protected:
65 
69  int readNextValidLine( std::string& str , std::istream& stream) ;
70 
71 
72  mutable StringParametersMap _map{};
73  StringParameters* _current ;
74 
75  std::string _fileName ;
76 
77 private:
78  Parser() = delete;
79  Parser(const marlin::Parser&) = delete;
80  Parser& operator=(const marlin::Parser&) = delete;
81 
82 };
83 
84 
85 
86 
87 
91 
92  std::vector< std::string >& _tokens ;
93  char _del ;
94  char _last ;
95  size_t _max ;
96  public:
97 
98  LCTokenizer( std::vector< std::string >& tokens, char del, size_t max=-1 ) : _tokens(tokens) , _del(del), _last(del), _max(max) {
99  }
100 
101 
102  void operator()(const char& c) {
103 
104  if( c != _del or _tokens.size() >= _max ) {
105 
106  if( _last == _del and _tokens.size() < _max ) {
107  _tokens.push_back("") ;
108  }
109  _tokens.back() += c ;
110  result() ;
111  }
112  _last = c ;
113 
114  }
115 
116  ~LCTokenizer(){
117  }
118 
119  std::vector<std::string> & result() {
120 
121  return _tokens ;
122 
123  }
124 };
125 
126 
127 } // end namespace marlin
128 #endif
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
ParseException used for parse errors, e.g.
Definition: Exceptions.h:16
Interface for a parser of a steering file to be used with marlin.
Definition: IParser.h:19
Simple parser class for Marlin.
Definition: Parser.h:36
void setCmdLineParameters(const CommandLineParametersMap &)
set command line parameters
Definition: Parser.h:51
Helper class for Parser.
Definition: Parser.h:90
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
void parse()
Parse the input file.
Definition: Parser.cc:16
void write(const std::string &fname) const
Write down the parsed file in a new file.
Definition: Parser.cc:163