ROBOOP, A Robotics Object Oriented Package in C++
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 #ifdef _MSC_VER // Microsoft
61 #pragma warning (disable:4786) // Disable decorated name truncation warnings
62 #pragma warning (disable:4503) // Disable decorated name truncation warnings
63 #endif
64 #include <iostream>
65 #include <string>
66 #include <iomanip>
67 #include <fstream>
68 
69 #include <boost/lexical_cast.hpp>
70 
71 #include <sstream>
72 #include <vector>
73 
74 
76 #define CAN_NOT_OPEN_FILE -1
77 
79 #define CAN_NOT_CREATE_FILE -2
80 
81 
83 typedef struct Data{
84  std::string section;
85  std::string parameter;
86  std::string value;
87 } Data;
88 
90 typedef std::vector< Data > Conf_data;
91 
93 class Config {
94 
95 public:
96  Config(const bool bPrintErrorMessages = true);
97  short read_conf(std::ifstream & inconffile);
98  void clear();
99  void print();
100 
101  bool section_exists(const std::string & section) const;
102  bool parameter_exists(const std::string & section, const std::string & parameter) const;
103 
104 
105  template<typename T> bool select(const std::string & section, const std::string & parameter,
106  T & value) const
111  {
112  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
113  {
114  if( (iter->section == section) && (iter->parameter == parameter) )
115  {
116  try
117  {
118  value = boost::lexical_cast<T>(iter->value);
119  }
120  catch (boost::bad_lexical_cast & e)
121  {
122  return false;
123  }
124  return true;
125  }
126  }
127  return false;
128  }
129 
130  short write_conf(std::ofstream & outconffile, const std::string & file_title,
131  const int space_between_column);
132 
133  template <typename T> bool add(const std::string & section, const std::string & parameter,
134  const T & value)
140  {
141  Data dataSet;
142  dataSet.section = section;
143  dataSet.parameter = parameter;
144  try
145  {
146  dataSet.value = boost::lexical_cast<std::string>(value);
147  }
148  catch (boost::bad_lexical_cast & e)
149  {
150  return false;
151  }
152 
153  for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
154  {
155  if(section == iterConf->section) // section already exist
156  {
157  if(parameter == iterConf->parameter) // parameter already exist
158  {
159  try
160  {
161  iterConf->value = boost::lexical_cast<std::string>(value);
162  }
163  catch (boost::bad_lexical_cast & e)
164  {
165  return false;
166  }
167  }
168  // parameter does not exist
169  for(Conf_data::iterator iterConf2 = iterConf; iterConf2 != conf.end(); ++iterConf2)
170  {
171  if(section != iterConf2->section)
172  {
173  conf.insert(iterConf2, dataSet);
174  return true;
175  }
176  }
177  }
178  }
179  // section and parameter does not exist.
180  conf.push_back(dataSet);
181  return true;
182  }
183 
184 private:
187 };
188 
189 #endif
190