Async  0.18.0
AsyncConfig.h
Go to the documentation of this file.
00001 
00038 #ifndef ASYNC_CONFIG_INCLUDED
00039 #define ASYNC_CONFIG_INCLUDED
00040 
00041 
00042 /****************************************************************************
00043  *
00044  * System Includes
00045  *
00046  ****************************************************************************/
00047 
00048 #include <stdio.h>
00049 
00050 #include <string>
00051 #include <map>
00052 #include <list>
00053 #include <sstream>
00054 
00055 
00056 /****************************************************************************
00057  *
00058  * Project Includes
00059  *
00060  ****************************************************************************/
00061 
00062 
00063 
00064 /****************************************************************************
00065  *
00066  * Local Includes
00067  *
00068  ****************************************************************************/
00069 
00070 
00071 
00072 /****************************************************************************
00073  *
00074  * Forward declarations
00075  *
00076  ****************************************************************************/
00077 
00078 
00079 
00080 /****************************************************************************
00081  *
00082  * Namespace
00083  *
00084  ****************************************************************************/
00085 
00086 namespace Async
00087 {
00088 
00089 
00090 /****************************************************************************
00091  *
00092  * Forward declarations of classes inside of the declared namespace
00093  *
00094  ****************************************************************************/
00095 
00096   
00097 
00098 /****************************************************************************
00099  *
00100  * Defines & typedefs
00101  *
00102  ****************************************************************************/
00103 
00104 
00105 
00106 /****************************************************************************
00107  *
00108  * Exported Global Variables
00109  *
00110  ****************************************************************************/
00111 
00112 
00113 
00114 /****************************************************************************
00115  *
00116  * Class definitions
00117  *
00118  ****************************************************************************/
00119 
00133 class Config
00134 {
00135   public:
00139     Config(void) : file(NULL) {}
00140   
00144     ~Config(void);
00145   
00151     bool open(const std::string& name);
00152     
00165     const std::string &getValue(const std::string& section,
00166                                  const std::string& tag) const;
00167 
00180     bool getValue(const std::string& section, const std::string& tag,
00181                   std::string& value) const;
00182 
00203     template <typename Rsp>
00204     bool getValue(const std::string& section, const std::string& tag,
00205                   Rsp &rsp, bool missing_ok = false) const
00206     {
00207       std::string str_val;
00208       if (!getValue(section, tag, str_val) && missing_ok)
00209       {
00210         return true;
00211       }
00212       std::stringstream ssval(str_val);
00213       Rsp tmp;
00214       ssval >> tmp >> std::ws;
00215       if (ssval.fail() || !ssval.eof())
00216       {
00217         return false;
00218       }
00219       rsp = tmp;
00220       return true;
00221     } /* Config::getValue */
00222 
00244     template <typename Rsp>
00245     bool getValue(const std::string& section, const std::string& tag,
00246                   const Rsp& min, const Rsp& max, Rsp &rsp,
00247                   bool missing_ok = false) const
00248     {
00249       std::string str_val;
00250       if (!getValue(section, tag, str_val) && missing_ok)
00251       {
00252         return true;
00253       }
00254       std::stringstream ssval(str_val);
00255       Rsp tmp;
00256       ssval >> tmp >> std::ws;
00257       if (ssval.fail() || !ssval.eof() || (tmp < min) || (tmp > max))
00258       {
00259         return false;
00260       }
00261       rsp = tmp;
00262       return true;
00263     } /* Config::getValue */
00264 
00271     std::list<std::string> listSection(const std::string& section);
00272     
00273   private:
00274     typedef std::map<std::string, std::string>  Values;
00275     typedef std::map<std::string, Values>       Sections;
00276     
00277     FILE      *file;
00278     Sections  sections;
00279     
00280     bool parseCfgFile(void);
00281     char *trimSpaces(char *line);
00282     char *parseSection(char *line);
00283     char *parseDelimitedString(char *str, char begin_tok, char end_tok);
00284     bool parseValueLine(char *line, std::string& tag, std::string& value);
00285     char *parseValue(char *value);
00286     char *translateEscapedChars(char *val);
00287 
00288 };  /* class Config */
00289 
00290 
00291 } /* namespace */
00292 
00293 #endif /* ASYNC_CONFIG_INCLUDED */
00294 
00295 
00296 
00297 /*
00298  * This file has not been truncated
00299  */
00300