Async  1.3.1
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 <memory>
54 #include <sstream>
55 
56 
57 /****************************************************************************
58  *
59  * Project Includes
60  *
61  ****************************************************************************/
62 
63 
64 
65 /****************************************************************************
66  *
67  * Local Includes
68  *
69  ****************************************************************************/
70 
71 
72 
73 /****************************************************************************
74  *
75  * Forward declarations
76  *
77  ****************************************************************************/
78 
79 
80 
81 /****************************************************************************
82  *
83  * Namespace
84  *
85  ****************************************************************************/
86 
87 namespace Async
88 {
89 
90 
91 /****************************************************************************
92  *
93  * Forward declarations of classes inside of the declared namespace
94  *
95  ****************************************************************************/
96 
97 
98 
99 /****************************************************************************
100  *
101  * Defines & typedefs
102  *
103  ****************************************************************************/
104 
105 
106 
107 /****************************************************************************
108  *
109  * Exported Global Variables
110  *
111  ****************************************************************************/
112 
113 
114 
115 /****************************************************************************
116  *
117  * Class definitions
118  *
119  ****************************************************************************/
120 
134 class Config
135 {
136  public:
140  Config(void) : file(NULL) {}
141 
145  ~Config(void);
146 
152  bool open(const std::string& name);
153 
166  const std::string &getValue(const std::string& section,
167  const std::string& tag) const;
168 
181  bool getValue(const std::string& section, const std::string& tag,
182  std::string& value) const;
183 
204  template <typename Rsp>
205  bool getValue(const std::string& section, const std::string& tag,
206  Rsp &rsp, bool missing_ok = false) const
207  {
208  std::string str_val;
209  if (!getValue(section, tag, str_val))
210  {
211  return missing_ok;
212  }
213  std::stringstream ssval(str_val);
214  Rsp tmp;
215  ssval >> tmp;
216  if(!ssval.eof())
217  {
218  ssval >> std::ws;
219  }
220  if (ssval.fail() || !ssval.eof())
221  {
222  return false;
223  }
224  rsp = tmp;
225  return true;
226  } /* Config::getValue */
227 
248  template <template <typename, typename> class Container,
249  typename Value>
250  bool getValue(const std::string& section, const std::string& tag,
251  Container<Value, std::allocator<Value> > &c,
252  bool missing_ok = false) const
253  {
254  std::string str_val;
255  if (!getValue(section, tag, str_val))
256  {
257  return missing_ok;
258  }
259  if (str_val.empty())
260  {
261  c.clear();
262  return true;
263  }
264  std::stringstream ssval(str_val);
265  while (!ssval.eof())
266  {
267  Value tmp;
268  ssval >> tmp;
269  if(!ssval.eof())
270  {
271  ssval >> std::ws;
272  }
273  if (ssval.fail())
274  {
275  return false;
276  }
277  c.push_back(tmp);
278  }
279  return true;
280  } /* Config::getValue */
281 
303  template <typename Rsp>
304  bool getValue(const std::string& section, const std::string& tag,
305  const Rsp& min, const Rsp& max, Rsp &rsp,
306  bool missing_ok = false) const
307  {
308  std::string str_val;
309  if (!getValue(section, tag, str_val))
310  {
311  return missing_ok;
312  }
313  std::stringstream ssval(str_val);
314  Rsp tmp;
315  ssval >> tmp;
316  if(!ssval.eof())
317  {
318  ssval >> std::ws;
319  }
320  if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
321  {
322  return false;
323  }
324  rsp = tmp;
325  return true;
326  } /* Config::getValue */
327 
334  std::list<std::string> listSection(const std::string& section);
335 
349  void setValue(const std::string& section, const std::string& tag,
350  const std::string& value);
351 
352  private:
353  typedef std::map<std::string, std::string> Values;
354  typedef std::map<std::string, Values> Sections;
355 
356  FILE *file;
357  Sections sections;
358 
359  bool parseCfgFile(void);
360  char *trimSpaces(char *line);
361  char *parseSection(char *line);
362  char *parseDelimitedString(char *str, char begin_tok, char end_tok);
363  bool parseValueLine(char *line, std::string& tag, std::string& value);
364  char *parseValue(char *value);
365  char *translateEscapedChars(char *val);
366 
367 }; /* class Config */
368 
369 
370 } /* namespace */
371 
372 #endif /* ASYNC_CONFIG_INCLUDED */
373 
374 
375 
376 /*
377  * This file has not been truncated
378  */
379 
Config(void)
Default constuctor.
Definition: AsyncConfig.h:140
void setValue(const std::string &section, const std::string &tag, const std::string &value)
Set the value of a configuration variable.
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:304
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:205
bool getValue(const std::string &section, const std::string &tag, Container< Value, std::allocator< Value > > &c, bool missing_ok=false) const
Get the value of the given config variable into container.
Definition: AsyncConfig.h:250
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:134
Namespace for the asynchronous programming classes.