ROBOOP, A Robotics Object Oriented Package in C++
config.cpp
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 -------------------------------------------------------------------------------
24 Revision_history:
25 
26 2004/06/10: Etienne Lachance
27  -Added doxygen documentation.
28 
29 2004/07/01: Ethan Tira-Thompson
30  -Added support for newmat's use_namespace #define, using ROBOOP namespace
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 2004/08/20: Etienne Lachance
48  -Parameter value can now contain space.
49  -Fix print member function.
50 -------------------------------------------------------------------------------
51 */
52 
58 #include "config.h"
59 
60 using namespace std;
61 
62 Config::Config(const bool bPrintErrorMessages_)
63 
64 {
65  bPrintErrorMessages = bPrintErrorMessages_;
66 }
67 
68 short Config::read_conf(ifstream & inconffile)
85 {
86  //std::ifstream inconffile(filename.c_str(), std::ios::in);
87 
88  if(inconffile)
89  {
90  string temp;
91  int tmpPos;
92  Data data;
93  getline(inconffile, temp);
94 
95  while( !inconffile.eof() )
96  {
97  // Is-it comment line?
98  if(temp.substr(0,1) != "#")
99  {
100  // Is-it a section name?
101  if(temp.substr(0,1) == "[") // [section]
102  {
103  // Search for the end of the section name and ignore the rest of the line.
104  tmpPos = temp.find("]");
105  if (tmpPos != string::npos)
106  {
107  data.section = temp.substr(1, tmpPos-1); // remove []
108  // Read the next line, it's should be a parameter.
109  getline(inconffile, temp);
110  // Be sure that is not another section name.
111  while( (temp.substr(0,1) != "[") &&
112  (!inconffile.eof()) )
113  {
114  if(temp.substr(0,1) != "#") // ignore comments
115  {
116  if(temp.find(":") != string::npos)
117  {
118  istringstream inputString(temp);
119  inputString >> data.parameter >> data.value;
120  string tmp_value;
121  while(inputString >> tmp_value)
122  {
123  data.value.append(" ");
124  data.value.append(tmp_value);
125  }
126  // Find ":" in parameter.
127  tmpPos = data.parameter.find(":");
128  if (tmpPos != string::npos)
129  // remove ":" a the end of parameter
130  data.parameter = data.parameter.substr(0, tmpPos);
131  else
132  {
133  inputString >> data.value;
134  string tmp_value;
135  while(inputString >> tmp_value)
136  {
137  data.value.append(" ");
138  data.value.append(tmp_value);
139  }
140  }
141 
142  // Add data to the config vector
143  conf.push_back(data);
144  }
145 
146  // Read the next line.
147  getline(inconffile, temp);
148  }
149  else
150  {
151  // Ignore comments and read the next line.
152  getline(inconffile, temp);
153  }
154  }
155  }
156  }
157  }
158  if(temp.substr(0,1) != "[") {
159  getline(inconffile, temp);
160  }
161  }
162  }
163  else
164  {
165  if (bPrintErrorMessages)
166  {
167  cerr << "Config::read_conf: invalid input ifstream " << endl;
168  }
169  return CAN_NOT_OPEN_FILE;
170  }
171  return 0;
172 }
173 
176 {
177  conf.clear();
178 }
179 
182 {
183  string tmpSection;
184  for(Conf_data::iterator iter = conf.begin(); iter != conf.end(); ++iter)
185  {
186  if (tmpSection != iter->section)
187  {
188  //Beginning of a section
189  tmpSection = iter->section;
190  cout << "\n[" << tmpSection << "]" << endl;
191  cout << iter->parameter+":" << " " << iter->value << endl;
192  }
193  else
194  cout << iter->parameter+":" << " " << iter->value << endl;
195  }
196 }
197 
198 bool Config::section_exists(const string& section) const
203 {
204  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
205  if(iter->section == section)
206  return true;
207  return false;
208 }
209 
210 bool Config::parameter_exists(const string& section, const string& parameter) const
215 {
216  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
217  if( (iter->section == section) && (iter->parameter == parameter) )
218  return true;
219  return false;
220 }
221 
222 short Config::write_conf(ofstream & outconffile, const string & file_title,
223  const int space_between_column)
230 {
231  if(outconffile)
232  { // file header
233  outconffile << "# ---------------------------------------------------" << endl;
234  outconffile << "# " << file_title << endl;
235  outconffile << "# ---------------------------------------------------" << endl;
236  outconffile << endl;
237 
238  string section = "";
239 
240  for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
241  {
242  if(section != iterConf->section)
243  {
244  section = iterConf->section;
245  outconffile << "\n[" << section << "]\n" << endl;
246  }
247  outconffile << setw(space_between_column-iterConf->parameter.size())
248  << iterConf->parameter + ":" << " " << iterConf->value << endl;
249  }
250  return 0;
251  }
252  else
253  {
254  if (bPrintErrorMessages)
255  {
256  cerr << "Config::write_conf: invalid input ofstream " << endl;
257  }
258  return CAN_NOT_CREATE_FILE;
259  }
260 }
261