StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
inputParser.h
1 //
3 // Copyright 2011
4 //
5 // This file is part of starlight.
6 //
7 // starlight is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // starlight is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with starlight. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //
22 // File and Version Information:
23 // $Rev:: $: revision of last commit
24 // $Author: jwebb $: author of last commit
25 // $Date: 2012/11/27 22:27:32 $: date of last commit
26 //
27 // Description:
28 //
29 //
30 //
32 
33 
34 #ifndef INPUTPARSER_H
35 #define INPUTPARSER_H
36 
37 #include <string>
38 #include <typeinfo>
39 #include <iostream>
40 #include <fstream>
41 #include <map>
42 
44 {
45 public:
46 
48  inputParser();
49 
51  ~inputParser();
52 
54  int parseFile(std::string filename);
56  int parseFile(std::ifstream &in );
57 
59  void addIntParameter(std::string name, int *var, bool required = true);
60 
62  void addUintParameter(std::string name, unsigned int *var, bool required = true);
63 
65  void addFloatParameter(std::string name, float *var, bool required = true);
66 
68  void addDoubleParameter(std::string name, double *var, bool required = true);
69 
71  void addBoolParameter(std::string name, bool *var, bool required = true);
72 
74  void printParameterInfo(std::ostream &out = std::cout);
75 
77  bool validateParameters(std::ostream &warnOut = std::cout, std::ostream &errOut = std::cerr);
78 
79 private:
80 
81  template <class T>
82  class _parameter
83  {
84  public:
85  _parameter(std::string name, T *val, bool required = true, bool found = false) : _name(name), _val(val), _required(required), _found(found){}
86 
87  bool operator==(const _parameter &rhs) const { return _name == rhs._name; }
88 
89  bool operator<(const _parameter &rhs) const { return _name.c_str()[0] < rhs._name.c_str()[0]; }
90 
91  void printParameterInfo(std::ostream &out = std::cout)
92  {
93  out << std::boolalpha << _name << "\t\t";
94  if(_found)
95  {
96  out << *_val << std::endl;
97  }
98  else
99  {
100  out << "NOT FOUND" << std::endl;
101  }
102  out << std::noboolalpha;
103  }
104 
105 
106  std::string _name;
107  T *_val;
108  bool _required;
109  bool _found;
110  };
111 
112  std::map<std::string, _parameter<int> > _intParameters;
113  std::map<std::string, _parameter<unsigned int> > _uintParameters;
114  std::map<std::string, _parameter<float> > _floatParameters;
115  std::map<std::string, _parameter<double> > _doubleParameters;
116  std::map<std::string, _parameter<bool> > _boolParameters;
117 
118 };
119 
120 #endif // INPUTPARSER_H
void addUintParameter(std::string name, unsigned int *var, bool required=true)
void addDoubleParameter(std::string name, double *var, bool required=true)
void addBoolParameter(std::string name, bool *var, bool required=true)
int parseFile(std::string filename)
void addIntParameter(std::string name, int *var, bool required=true)
bool validateParameters(std::ostream &warnOut=std::cout, std::ostream &errOut=std::cerr)
void addFloatParameter(std::string name, float *var, bool required=true)
void printParameterInfo(std::ostream &out=std::cout)