lbm_reference
ConfParser.h
Go to the documentation of this file.
1 //! \file ConfParser.h
2 //! Declaration of the ConfParser class
3 
4 //! \date Jan 17, 2009
5 //! \author Florian Rathgeber
6 
7 #ifndef CONFPARSER_H_
8 #define CONFPARSER_H_
9 
10 #include <cstring>
11 #include <stdexcept>
12 #include <boost/lexical_cast.hpp>
13 
14 #include "ConfBlock.h"
15 
16 //! Common namespace for all classes related to parsing of configuration files
17 
18 namespace confparser {
19 
20  //! Exception class for syntax errors
21 
22  //! Is capable of providing information about the file and the line where
23  //! the syntax error occured as well as a description of the error.
24 
25  class BadSyntax : public std::logic_error {
26 
27  public:
28 
29  // ============================ //
30  // Constructors and destructors //
31  // ============================ //
32 
33  //! Default constructor
34 
35  //! Sets no specific information.
36 
37  BadSyntax() throw()
38  : std::logic_error( "Syntax error occured" ),
39  file_( "" ),
40  line_( -1 ),
41  error_( "" ) {}
42 
43  //! Constructor
44 
45  //! Sets information about file and line of error occurrence as well as an
46  //! error description.
47 
48  BadSyntax( const std::string file, const int line, const std::string error ) throw()
49  : std::logic_error( "Syntax error occured in file " + file + " on line "
50  + boost::lexical_cast<std::string>( line ) + ": " + error ),
51  file_( file ),
52  line_( line ),
53  error_( error ) {}
54 
55  //! Destructor
56 
57  virtual ~BadSyntax() throw() {}
58 
59  // ======= //
60  // Getters //
61  // ======= //
62 
63  //! Returns the file name
64 
65  //! \return Name of the file the syntax error occurred
66 
67  const char* getFileName() const throw() { return file_.c_str(); }
68 
69  //! Returns the line of the error
70 
71  //! \return Line where the syntax error occurred
72 
73  int getLine() const throw() { return line_; }
74 
75  //! Returns error description
76 
77  //! \return Description of the error
78 
79  const char* getErrorMsg() const throw() { return error_.c_str(); }
80 
81  private:
82 
83  // ============ //
84  // Data members //
85  // ============ //
86 
87  //! File with erroneous syntax
88 
89  const std::string file_;
90 
91  //! Line of the syntax error
92 
93  const int line_;
94 
95  //! Description of the syntax error
96 
97  const std::string error_;
98 
99  };
100 
101  //! Parser for hierarchical configuration files
102 
103  //! Parses hierarchical configuration files of the following structure:
104  //! \code
105  //! key0 value0;
106  //! ...
107  //! block0 {
108  //! key1 value1;
109  //! ...
110  //! subblock0 {
111  //! key2 value2;
112  //! ...
113  //! }
114  //! subblock1 {
115  //! key3 value3;
116  //! ...
117  //! }
118  //! ...
119  //! }
120  //! block1 {
121  //! ...
122  //! }
123  //! ...
124  //! \endcode
125  //! The parsing result is a tree structure of ConfBlock objects and the actual
126  //! key-value pairs are stored in a std::map.
127 
128  class ConfParser {
129 
130  public:
131 
132  // ============================ //
133  // Constructors and destructors //
134  // ============================ //
135 
136  //! Constructor
137 
138  //! Initializes root as empty block.
139 
141 
142  //! Destructor
143 
144  //! Does nothing
145 
146  virtual ~ConfParser() {}
147 
148  // ============= //
149  // Functionality //
150  // ============= //
151 
152  //! Parse a given configuration file
153 
154  //! \param[in] configFileName Path to the configuration file to parse
155  //! \throw BadSyntax
156 
157  ConfBlock& parse( std::string configFileName ) throw( BadSyntax );
158 
159  protected:
160 
161  // ========================== //
162  // Protected helper functions //
163  // ========================== //
164 
165  //! Recursively parse a specific block
166 
167  //! \param[in] currBlock Pointer to current block
168  //! \param[in] level Nesting depth of current block
169  //! \param[in] nLine Current line in the configuration file
170  //! \param[in] configFile Stream to write out to
171  //! \param[in] configFileName Name of the configuration file parsed
172 
173  int parse_rec( ConfBlock* currBlock,
174  int level,
175  int nLine,
176  std::ifstream& configFile,
177  const std::string& configFileName );
178 
179  //! Root block of the parse tree that is built, will be returned by parse
180 
182 
183  };
184 
185 } // namespace confparser
186 
187 #endif /* CONFPARSER_H_ */