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 static const char rcsid[] = "$Id: config.cpp,v 1.20 2006/05/16 19:24:26 gourdeau Exp $";
60 
61 
62 #include "config.h"
63 
64 using namespace std;
65 
66 Config::Config(const bool bPrintErrorMessages_)
67 {
69  bPrintErrorMessages = bPrintErrorMessages_;
70 }
71 
72 short Config::read_conf(ifstream & inconffile)
89 {
90  //std::ifstream inconffile(filename.c_str(), std::ios::in);
91 
92  if(inconffile)
93  {
94  string temp;
95  unsigned int tmpPos;
96  Data data;
97  getline(inconffile, temp);
98 
99  while( !inconffile.eof() )
100  {
101  // Is-it comment line?
102  if(temp.substr(0,1) != "#")
103  {
104  // Is-it a section name?
105  if(temp.substr(0,1) == "[") // [section]
106  {
107  // Search for the end of the section name and ignore the rest of the line.
108  tmpPos = temp.find("]");
109  if (tmpPos != string::npos)
110  {
111  data.section = temp.substr(1, tmpPos-1); // remove []
112  // Read the next line, it's should be a parameter.
113  getline(inconffile, temp);
114  // Be sure that is not another section name.
115  while( (temp.substr(0,1) != "[") &&
116  (!inconffile.eof()) )
117  {
118  if(temp.substr(0,1) != "#") // ignore comments
119  {
120  if(temp.find(":") != string::npos)
121  {
122  istringstream inputString(temp);
123  inputString >> data.parameter >> data.value;
124  string tmp_value;
125  while(inputString >> tmp_value)
126  {
127  data.value.append(" ");
128  data.value.append(tmp_value);
129  }
130  // Find ":" in parameter.
131  tmpPos = data.parameter.find(":");
132  if (tmpPos != string::npos)
133  // remove ":" a the end of parameter
134  data.parameter = data.parameter.substr(0, tmpPos);
135  else
136  {
137  inputString >> data.value;
138  string tmp_value;
139  while(inputString >> tmp_value)
140  {
141  data.value.append(" ");
142  data.value.append(tmp_value);
143  }
144  }
145 
146  // Add data to the config vector
147  conf.push_back(data);
148  }
149 
150  // Read the next line.
151  getline(inconffile, temp);
152  }
153  else
154  {
155  // Ignore comments and read the next line.
156  getline(inconffile, temp);
157  }
158  }
159  }
160  }
161  }
162  if(temp.substr(0,1) != "[") {
163  getline(inconffile, temp);
164  }
165  }
166  }
167  else
168  {
169  if (bPrintErrorMessages)
170  {
171  cerr << "Config::read_conf: invalid input ifstream " << endl;
172  }
173  return CAN_NOT_OPEN_FILE;
174  }
175  return 0;
176 }
177 
180 {
181  conf.clear();
182 }
183 
186 {
187  string tmpSection;
188  for(Conf_data::iterator iter = conf.begin(); iter != conf.end(); ++iter)
189  {
190  if (tmpSection != iter->section)
191  {
192  //Beginning of a section
193  tmpSection = iter->section;
194  cout << "\n[" << tmpSection << "]" << endl;
195  cout << iter->parameter+":" << " " << iter->value << endl;
196  }
197  else
198  cout << iter->parameter+":" << " " << iter->value << endl;
199  }
200 }
201 
202 bool Config::section_exists(const string& section) const
207 {
208  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
209  if(iter->section == section)
210  return true;
211  return false;
212 }
213 
214 bool Config::parameter_exists(const string& section, const string& parameter) const
219 {
220  for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
221  if( (iter->section == section) && (iter->parameter == parameter) )
222  return true;
223  return false;
224 }
225 
226 short Config::write_conf(ofstream & outconffile, const string & file_title,
227  const int space_between_column)
234 {
235  if(outconffile)
236  { // file header
237  outconffile << "# ---------------------------------------------------" << endl;
238  outconffile << "# " << file_title << endl;
239  outconffile << "# ---------------------------------------------------" << endl;
240  outconffile << endl;
241 
242  string section = "";
243 
244  for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
245  {
246  if(section != iterConf->section)
247  {
248  section = iterConf->section;
249  outconffile << "\n[" << section << "]\n" << endl;
250  }
251  outconffile << setw(space_between_column-iterConf->parameter.size())
252  << iterConf->parameter + ":" << " " << iterConf->value << endl;
253  }
254  return 0;
255  }
256  else
257  {
258  if (bPrintErrorMessages)
259  {
260  cerr << "Config::write_conf: invalid input ofstream " << endl;
261  }
262  return CAN_NOT_CREATE_FILE;
263  }
264 }
265 
bool section_exists(const std::string &section) const
Test to see if a section exists.
Definition: config.cpp:202
short read_conf(std::ifstream &inconffile)
Read a configuration file.
Definition: config.cpp:72
Basic data element used in Config class.
Definition: config.h:87
void print()
Print the configuration data.
Definition: config.cpp:184
Header file for Config class definitions.
#define CAN_NOT_OPEN_FILE
Return when can not open file.
Definition: config.h:80
std::string parameter
Definition: config.h:89
Config(const bool bPrintErrorMessages=true)
Constructor.
Definition: config.cpp:66
std::string section
Definition: config.h:88
static const char rcsid[]
RCS/CVS version.
Definition: config.cpp:59
short write_conf(std::ofstream &outconffile, const std::string &file_title, const int space_between_column)
Write the configuration information, contained in conf, on disk.
Definition: config.cpp:226
void clear()
Clear the data buffer.
Definition: config.cpp:178
#define CAN_NOT_CREATE_FILE
Return when can not create a file.
Definition: config.h:83
bool parameter_exists(const std::string &section, const std::string &parameter) const
Test to see if a parameter exists within a section.
Definition: config.cpp:214
std::string value
Definition: config.h:90


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