config.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2003-2004 Etienne Lachance
3 
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8 
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 Report problems and direct all questions to:
20 
21 email: etienne.lachance@polytml.ca or richard.gourdeau@polymtl.ca
22 -------------------------------------------------------------------------------
23 Revision_history:
24 
25 2004/07/01: Etienne Lachance
26  -Added doxygen documentation.
27 
28 2004/07/01: Ethan Tira-Thompson
29  -Added support for newmat's use_namespace #define, using ROBOOP namespace
30  -Added dependance on utils.h because we need to get the use_namespace setting
31 
32 2004/07/13: Ethan Tira-Thompson
33  -Added a select_real and add_real function for type indepence of Real
34  -Added functions to test for sections and parameters existance
35 
36 2004/07/29: Etienne Lachance
37  -Added clear function. Suggested by Sylvain Marleau.
38 
39 2004/08/10: Etienne Lachance
40  -Removed select_real and add_real functions in order to make config.h/cpp
41  independent of ROBOOP.
42  -Removed using ROBOOP namespace
43 
44 2004/08/14: Etienne Lachance
45  -Merge all select_* and add_* functions into select() and add() functions.
46 
47 2006/02/04: Etienne Lachance
48  -Member functions add and select are now in template form.
49 -------------------------------------------------------------------------------
50 */
51 
52 #ifndef CONFIG_H
53 #define CONFIG_H
54 
60 static const char header_config_rcsid[] = "$Id: config.h,v 1.18 2006/05/16 19:24:26 gourdeau Exp $";
62 
63 
64 #ifdef _MSC_VER // Microsoft
65 #pragma warning (disable:4786) // Disable decorated name truncation warnings
66 #pragma warning (disable:4503) // Disable decorated name truncation warnings
67 #endif
68 #include <iostream>
69 #include <string>
70 #include <iomanip>
71 #include <fstream>
72 
73 #include <boost/lexical_cast.hpp>
74 
75 #include <sstream>
76 #include <vector>
77 
78 
80 #define CAN_NOT_OPEN_FILE -1
81 
83 #define CAN_NOT_CREATE_FILE -2
84 
85 
87 typedef struct Data{
88  std::string section;
89  std::string parameter;
90  std::string value;
91 } Data;
92 
94 typedef std::vector< Data > Conf_data;
95 
97 class Config {
98 
99 public:
100  Config(const bool bPrintErrorMessages = true);
101  short read_conf(std::ifstream & inconffile);
102  void clear();
103  void print();
104 
105  bool section_exists(const std::string & section) const;
106  bool parameter_exists(const std::string & section, const std::string & parameter) const;
107 
108 
109  template<typename T> bool select(const std::string & section, const std::string & parameter,
110  T & value) const
115  {
116  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
117  {
118  if( (iter->section == section) && (iter->parameter == parameter) )
119  {
120  try
121  {
122  value = boost::lexical_cast<T>(iter->value);
123  }
124  catch (boost::bad_lexical_cast & e)
125  {
126  return false;
127  }
128  return true;
129  }
130  }
131  return false;
132  }
133 
134  short write_conf(std::ofstream & outconffile, const std::string & file_title,
135  const int space_between_column);
136 
137  template <typename T> bool add(const std::string & section, const std::string & parameter,
138  const T & value)
144  {
145  Data dataSet;
146  dataSet.section = section;
147  dataSet.parameter = parameter;
148  try
149  {
150  dataSet.value = boost::lexical_cast<std::string>(value);
151  }
152  catch (boost::bad_lexical_cast & e)
153  {
154  return false;
155  }
156 
157  for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
158  {
159  if(section == iterConf->section) // section already exist
160  {
161  if(parameter == iterConf->parameter) // parameter already exist
162  {
163  try
164  {
165  iterConf->value = boost::lexical_cast<std::string>(value);
166  }
167  catch (boost::bad_lexical_cast & e)
168  {
169  return false;
170  }
171  }
172  // parameter does not exist
173  for(Conf_data::iterator iterConf2 = iterConf; iterConf2 != conf.end(); ++iterConf2)
174  {
175  if(section != iterConf2->section)
176  {
177  conf.insert(iterConf2, dataSet);
178  return true;
179  }
180  }
181  }
182  }
183  // section and parameter does not exist.
184  conf.push_back(dataSet);
185  return true;
186  }
187 
188 private:
191 };
192 
193 #endif
194 
Conf_data conf
Data store from/to configuration file.
Definition: config.h:189
std::vector< Data > Conf_data
Configuration data type.
Definition: config.h:94
Basic data element used in Config class.
Definition: config.h:87
std::string parameter
Definition: config.h:89
static const char header_config_rcsid[]
RCS/CVS version.
Definition: config.h:61
bool select(const std::string &section, const std::string &parameter, T &value) const
Get a parameter data, of a certain section, into the string value.
Definition: config.h:109
Handle configuration files.
Definition: config.h:97
bool bPrintErrorMessages
Print error messages on stderr.
Definition: config.h:190
std::string section
Definition: config.h:88
bool add(const std::string &section, const std::string &parameter, const T &value)
Added the value(string) of the parameter in the section in the configuration data. The functions will added the parameter and the section if it does not already exist.
Definition: config.h:137
struct Data Data
Basic data element used in Config class.
std::string value
Definition: config.h:90


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:16