Async  1.5.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 <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 
156  bool open(const std::string& name);
157 
170  const std::string &getValue(const std::string& section,
171  const std::string& tag) const;
172 
185  bool getValue(const std::string& section, const std::string& tag,
186  std::string& value) const;
187 
208  template <typename Rsp>
209  bool getValue(const std::string& section, const std::string& tag,
210  Rsp &rsp, bool missing_ok = false) const
211  {
212  std::string str_val;
213  if (!getValue(section, tag, str_val))
214  {
215  return missing_ok;
216  }
217  std::stringstream ssval(str_val);
218  Rsp tmp;
219  ssval >> tmp;
220  if(!ssval.eof())
221  {
222  ssval >> std::ws;
223  }
224  if (ssval.fail() || !ssval.eof())
225  {
226  return false;
227  }
228  rsp = tmp;
229  return true;
230  } /* Config::getValue */
231 
252  template <template <typename, typename> class Container,
253  typename Value>
254  bool getValue(const std::string& section, const std::string& tag,
255  Container<Value, std::allocator<Value> > &c,
256  bool missing_ok = false) const
257  {
258  std::string str_val;
259  if (!getValue(section, tag, str_val))
260  {
261  return missing_ok;
262  }
263  if (str_val.empty())
264  {
265  c.clear();
266  return true;
267  }
268  std::stringstream ssval(str_val);
269  while (!ssval.eof())
270  {
271  Value tmp;
272  ssval >> tmp;
273  if(!ssval.eof())
274  {
275  ssval >> std::ws;
276  }
277  if (ssval.fail())
278  {
279  return false;
280  }
281  c.push_back(tmp);
282  }
283  return true;
284  } /* Config::getValue */
285 
307  template <typename Rsp>
308  bool getValue(const std::string& section, const std::string& tag,
309  const Rsp& min, const Rsp& max, Rsp &rsp,
310  bool missing_ok = false) const
311  {
312  std::string str_val;
313  if (!getValue(section, tag, str_val))
314  {
315  return missing_ok;
316  }
317  std::stringstream ssval(str_val);
318  Rsp tmp;
319  ssval >> tmp;
320  if(!ssval.eof())
321  {
322  ssval >> std::ws;
323  }
324  if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
325  {
326  return false;
327  }
328  rsp = tmp;
329  return true;
330  } /* Config::getValue */
331 
338  std::list<std::string> listSection(const std::string& section);
339 
353  void setValue(const std::string& section, const std::string& tag,
354  const std::string& value);
355 
356  private:
357  typedef std::map<std::string, std::string> Values;
358  typedef std::map<std::string, Values> Sections;
359 
360  FILE *file;
361  Sections sections;
362 
363  bool parseCfgFile(void);
364  char *trimSpaces(char *line);
365  char *parseSection(char *line);
366  char *parseDelimitedString(char *str, char begin_tok, char end_tok);
367  bool parseValueLine(char *line, std::string& tag, std::string& value);
368  char *parseValue(char *value);
369  char *translateEscapedChars(char *val);
370 
371 }; /* class Config */
372 
373 
374 } /* namespace */
375 
376 #endif /* ASYNC_CONFIG_INCLUDED */
377 
378 
379 
380 /*
381  * This file has not been truncated
382  */
383 
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:254
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:209
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.
const std::string & getValue(const std::string &section, const std::string &tag) const
Return the string value of the given configuration variable.
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.
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:308