Async  0.18.0
AsyncConfig.h
Go to the documentation of this file.
1 
38 #ifndef ASYNC_CONFIG_INCLUDED
39 #define ASYNC_CONFIG_INCLUDED
40 
41 
42 /****************************************************************************
43  *
44  * System Includes
45  *
46  ****************************************************************************/
47 
48 #include <stdio.h>
49 
50 #include <string>
51 #include <map>
52 #include <list>
53 #include <sstream>
54 
55 
56 /****************************************************************************
57  *
58  * Project Includes
59  *
60  ****************************************************************************/
61 
62 
63 
64 /****************************************************************************
65  *
66  * Local Includes
67  *
68  ****************************************************************************/
69 
70 
71 
72 /****************************************************************************
73  *
74  * Forward declarations
75  *
76  ****************************************************************************/
77 
78 
79 
80 /****************************************************************************
81  *
82  * Namespace
83  *
84  ****************************************************************************/
85 
86 namespace Async
87 {
88 
89 
90 /****************************************************************************
91  *
92  * Forward declarations of classes inside of the declared namespace
93  *
94  ****************************************************************************/
95 
96 
97 
98 /****************************************************************************
99  *
100  * Defines & typedefs
101  *
102  ****************************************************************************/
103 
104 
105 
106 /****************************************************************************
107  *
108  * Exported Global Variables
109  *
110  ****************************************************************************/
111 
112 
113 
114 /****************************************************************************
115  *
116  * Class definitions
117  *
118  ****************************************************************************/
119 
133 class Config
134 {
135  public:
139  Config(void) : file(NULL) {}
140 
144  ~Config(void);
145 
151  bool open(const std::string& name);
152 
165  const std::string &getValue(const std::string& section,
166  const std::string& tag) const;
167 
180  bool getValue(const std::string& section, const std::string& tag,
181  std::string& value) const;
182 
203  template <typename Rsp>
204  bool getValue(const std::string& section, const std::string& tag,
205  Rsp &rsp, bool missing_ok = false) const
206  {
207  std::string str_val;
208  if (!getValue(section, tag, str_val) && missing_ok)
209  {
210  return true;
211  }
212  std::stringstream ssval(str_val);
213  Rsp tmp;
214  ssval >> tmp >> std::ws;
215  if (ssval.fail() || !ssval.eof())
216  {
217  return false;
218  }
219  rsp = tmp;
220  return true;
221  } /* Config::getValue */
222 
244  template <typename Rsp>
245  bool getValue(const std::string& section, const std::string& tag,
246  const Rsp& min, const Rsp& max, Rsp &rsp,
247  bool missing_ok = false) const
248  {
249  std::string str_val;
250  if (!getValue(section, tag, str_val) && missing_ok)
251  {
252  return true;
253  }
254  std::stringstream ssval(str_val);
255  Rsp tmp;
256  ssval >> tmp >> std::ws;
257  if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
258  {
259  return false;
260  }
261  rsp = tmp;
262  return true;
263  } /* Config::getValue */
264 
271  std::list<std::string> listSection(const std::string& section);
272 
273  private:
274  typedef std::map<std::string, std::string> Values;
275  typedef std::map<std::string, Values> Sections;
276 
277  FILE *file;
278  Sections sections;
279 
280  bool parseCfgFile(void);
281  char *trimSpaces(char *line);
282  char *parseSection(char *line);
283  char *parseDelimitedString(char *str, char begin_tok, char end_tok);
284  bool parseValueLine(char *line, std::string& tag, std::string& value);
285  char *parseValue(char *value);
286  char *translateEscapedChars(char *val);
287 
288 }; /* class Config */
289 
290 
291 } /* namespace */
292 
293 #endif /* ASYNC_CONFIG_INCLUDED */
294 
295 
296 
297 /*
298  * This file has not been truncated
299  */
300 
Config(void)
Default constuctor.
Definition: AsyncConfig.h:139
bool getValue(const std::string &section, const std::string &tag, const Rsp &min, const Rsp &max, Rsp &rsp, bool missing_ok=false) const
Get a range checked variable value.
Definition: AsyncConfig.h:245
const std::string & getValue(const std::string &section, const std::string &tag) const
Return the string value of the given configuration variable.
bool getValue(const std::string &section, const std::string &tag, Rsp &rsp, bool missing_ok=false) const
Get the value of the given configuration variable.
Definition: AsyncConfig.h:204
bool open(const std::string &name)
Open the given config file.
~Config(void)
Destructor.
std::list< std::string > listSection(const std::string &section)
Return the name of all the tags in the given section.
A class for reading INI-formatted configuration files.
Definition: AsyncConfig.h:133