RSC  0.7.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigFileSource.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2011 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "ConfigFileSource.h"
28 
29 #include <boost/format.hpp>
30 #include <boost/algorithm/string.hpp>
31 
32 #include "../runtime/ContainerIO.h"
33 
34 using namespace std;
35 
36 using namespace boost;
37 
38 using namespace rsc::logging;
39 
40 namespace rsc {
41 namespace config {
42 
43 // Taken from Boost.ProgramOptions
44 string trim(const string& s) {
45  string::size_type n, n2;
46  n = s.find_first_not_of(" \t\r\n");
47  if (n == string::npos) {
48  return string();
49  } else {
50  n2 = s.find_last_not_of(" \t\r\n");
51  return s.substr(n, n2 - n + 1);
52  }
53 }
54 
55 ConfigFileSource::ConfigFileSource(istream& stream) :
56  logger(Logger::getLogger("rsc.config.ConfigFileSource")), stream(stream) {
57 
58  // parse options and store them in an internal map
59  string name;
60  string value;
61  while (getOption(name, value)) {
62 
63  vector<string> key;
64  split(key, name, is_any_of("."));
65 
66  RSCTRACE(logger, "Option " << key << " -> " << value);
67 
68  options[key] = value;
69 
70  }
71 
72 }
73 
75  for (map<vector<string>, string>::const_iterator it = options.begin();
76  it != options.end(); ++it) {
77  handler.handleOption(it->first, it->second);
78  }
79 }
80 
81 // Based on Boost.ProgramOptions
82 bool ConfigFileSource::getOption(string& name, string& value) {
83  string line;
84  while (getline(this->stream, line)) {
85 
86  // check for files encoded with the old Mac EOL style.
87  size_t rPos = line.find('\r');
88  if (rPos != line.npos && rPos != 0 && rPos != line.size() - 1) {
89  throw invalid_argument("Old Mac EOL style '\\r' is not supported.");
90  }
91 
92  // strip '#' comments and whitespace
93  string::size_type n;
94  if ((n = line.find('#')) != string::npos)
95  line = line.substr(0, n);
96  line = trim(line);
97 
98  if (!line.empty()) {
99  // Handle section name
100  if (*line.begin() == '[' && *line.rbegin() == ']') {
101  currentSection = line.substr(1, line.size() - 2);
102  if (*currentSection.rbegin() != '.') {
103  currentSection += '.';
104  }
105  } else if ((n = line.find('=')) != string::npos) {
106  name = currentSection + trim(line.substr(0, n));
107  value = trim(line.substr(n + 1));
108  return true;
109  } else {
110  throw invalid_argument(
111  str(format("Syntax error in line `%1%'") % line));
112  }
113  }
114  }
115  return false;
116 }
117 
118 }
119 }
string trim(const string &s)
virtual void handleOption(const std::vector< std::string > &key, const std::string &value)=0
This method is called once for each individual option available from a given ConfigSource.
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.
Implementations of this interface receive options from ConfigSource objects.
Definition: OptionHandler.h:43
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
std::map< std::vector< std::string >, std::string > options
bool getOption(std::string &name, std::string &value)
Interface for logging adapters that can be used with RSC.
Definition: Logger.h:54