config.h
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2004  Etienne Lachance
00003 
00004 This library is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as
00006 published by the Free Software Foundation; either version 2.1 of the
00007 License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 Report problems and direct all questions to:
00020 
00021 email: etienne.lachance@polytml.ca or richard.gourdeau@polymtl.ca
00022 -------------------------------------------------------------------------------
00023 Revision_history:
00024 
00025 2004/07/01: Etienne Lachance
00026    -Added doxygen documentation.
00027 
00028 2004/07/01: Ethan Tira-Thompson
00029     -Added support for newmat's use_namespace #define, using ROBOOP namespace
00030     -Added dependance on utils.h because we need to get the use_namespace setting
00031 
00032 2004/07/13: Ethan Tira-Thompson
00033     -Added a select_real and add_real function for type indepence of Real
00034     -Added functions to test for sections and parameters existance
00035 
00036 2004/07/29: Etienne Lachance
00037    -Added clear function. Suggested by Sylvain Marleau.
00038 
00039 2004/08/10: Etienne Lachance
00040    -Removed select_real and add_real functions in order to make config.h/cpp 
00041     independent of ROBOOP.
00042    -Removed using ROBOOP namespace
00043 
00044 2004/08/14: Etienne Lachance
00045    -Merge all select_* and add_* functions into select() and add() functions.
00046 
00047 2006/02/04: Etienne Lachance
00048    -Member functions add and select are now in template form.
00049 -------------------------------------------------------------------------------
00050 */
00051 
00052 #ifndef CONFIG_H
00053 #define CONFIG_H
00054 
00060 
00061 static const char header_config_rcsid[] = "$Id: config.h,v 1.18 2006/05/16 19:24:26 gourdeau Exp $";
00062 
00063 
00064 #ifdef _MSC_VER                  // Microsoft
00065 #pragma warning (disable:4786)  // Disable decorated name truncation warnings 
00066 #pragma warning (disable:4503)  // Disable decorated name truncation warnings 
00067 #endif
00068 #include <iostream>
00069 #include <string>
00070 #include <iomanip>
00071 #include <fstream>
00072 
00073 #include <boost/lexical_cast.hpp>
00074 
00075 #include <sstream>
00076 #include <vector>
00077 
00078 
00080 #define CAN_NOT_OPEN_FILE                     -1
00081 
00083 #define CAN_NOT_CREATE_FILE                   -2
00084 
00085 
00087 typedef struct Data{
00088    std::string section;
00089    std::string parameter;
00090    std::string value;
00091 } Data;
00092 
00094 typedef std::vector< Data > Conf_data;
00095 
00097 class Config {
00098 
00099 public:
00100     Config(const bool bPrintErrorMessages = true);
00101     short read_conf(std::ifstream & inconffile);
00102     void clear();
00103     void print();
00104 
00105     bool section_exists(const std::string & section) const;
00106     bool parameter_exists(const std::string & section, const std::string & parameter) const;
00107 
00108 
00109     template<typename T> bool select(const std::string & section, const std::string & parameter,
00110                                      T & value) const
00115     {
00116         for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00117         {
00118             if( (iter->section == section) && (iter->parameter == parameter) )
00119             {
00120                 try
00121                 {
00122                     value = boost::lexical_cast<T>(iter->value);
00123                 }
00124                 catch (boost::bad_lexical_cast & e)
00125                 {
00126                     return false;
00127                 }
00128                 return true;
00129             }
00130         }
00131         return false;
00132     }
00133 
00134     short write_conf(std::ofstream & outconffile, const std::string & file_title,
00135                      const int space_between_column);
00136 
00137     template <typename T> bool add(const std::string & section, const std::string & parameter, 
00138                                    const T & value)
00144     {
00145         Data dataSet;
00146         dataSet.section = section;
00147         dataSet.parameter = parameter;
00148         try
00149         {
00150             dataSet.value = boost::lexical_cast<std::string>(value);
00151         }
00152         catch (boost::bad_lexical_cast & e)
00153         {
00154             return false;
00155         }
00156 
00157         for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
00158         {
00159             if(section == iterConf->section) // section already exist
00160             {
00161                 if(parameter == iterConf->parameter) // parameter already exist
00162                 {
00163                     try
00164                     {
00165                         iterConf->value = boost::lexical_cast<std::string>(value);
00166                     }
00167                     catch (boost::bad_lexical_cast & e)
00168                     {
00169                         return false;
00170                     }
00171                 }
00172                 // parameter does not exist
00173                 for(Conf_data::iterator iterConf2 = iterConf; iterConf2 != conf.end(); ++iterConf2)
00174                 {
00175                     if(section != iterConf2->section)
00176                     {
00177                         conf.insert(iterConf2, dataSet);
00178                         return true;
00179                     }
00180                 }
00181             }
00182         }
00183         // section and parameter does not exist.
00184         conf.push_back(dataSet);
00185         return true;
00186     }
00187 
00188 private:
00189    Conf_data conf;            
00190    bool bPrintErrorMessages;  
00191 };
00192 
00193 #endif 
00194 


kni
Author(s): Martin Günther
autogenerated on Thu Aug 27 2015 13:40:06