RSC  0.16.0
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, 2012 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 <iterator>
30 
31 #include <boost/format.hpp>
32 #include <boost/tokenizer.hpp>
33 
34 #include "../runtime/ContainerIO.h"
35 
36 using namespace std;
37 
38 using namespace boost;
39 
40 using namespace rsc::logging;
41 
42 namespace rsc {
43 namespace config {
44 
45 // Taken from Boost.ProgramOptions
46 string trim(const string& s) {
47  string::size_type n, n2;
48  n = s.find_first_not_of(" \t\r\n");
49  if (n == string::npos) {
50  return string();
51  } else {
52  n2 = s.find_last_not_of(" \t\r\n");
53  return s.substr(n, n2 - n + 1);
54  }
55 }
56 
57 ConfigFileSource::ConfigFileSource(istream& stream) :
58  logger(Logger::getLogger("rsc.config.ConfigFileSource")), stream(stream) {
59 
60  // Parse option keys and values and store them in an internal map.
61  // Option keys are of the form
62  //
63  // COMPONENT1.COMPONENT2.COMPONENT3 ...
64  //
65  // Components of option keys can be quoted to allow embedded '.'
66  // characters. For example:
67  //
68  // logging."system.subsystem"
69  //
70  // would be parsed as option key
71  //
72  // (logging system.subsystem) instead of
73  // (logging system subsystem)
74  string name;
75  string value;
76  while (getOption(name, value)) {
77  vector<string> key;
78  splitKeyAtDots(name, key);
79 
80  RSCTRACE(logger, "Option " << key << " -> " << value);
81  options[key] = value;
82  }
83 }
84 
86  for (map<vector<string>, string>::const_iterator it = options.begin();
87  it != options.end(); ++it) {
88  handler.handleOption(it->first, it->second);
89  }
90 }
91 
92 // Based on Boost.ProgramOptions
93 bool ConfigFileSource::getOption(string& name, string& value) {
94  string line;
95  while (getline(this->stream, line)) {
96 
97  // check for files encoded with the old Mac EOL style.
98  size_t rPos = line.find('\r');
99  if (rPos != line.npos && rPos != 0 && rPos != line.size() - 1) {
100  throw invalid_argument("Old Mac EOL style '\\r' is not supported.");
101  }
102 
103  // Strip '#' comments and whitespace
104  string::size_type n;
105  if ((n = line.find('#')) != string::npos)
106  line = line.substr(0, n);
107  line = trim(line);
108 
109  if (!line.empty()) {
110  // Handle section name
111  if (*line.begin() == '[' && *line.rbegin() == ']') {
112  currentSection = line.substr(1, line.size() - 2);
113  if (*currentSection.rbegin() != '.') {
114  currentSection += '.';
115  }
116  } else if ((n = line.find('=')) != string::npos) {
117  name = currentSection + trim(line.substr(0, n));
118  value = trim(line.substr(n + 1));
119  return true;
120  } else {
121  throw invalid_argument(
122  str(format("Syntax error in line `%1%'") % line));
123  }
124  }
125  }
126  return false;
127 }
128 
129 }
130 }
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 splitKeyAtDots(const std::string &input, std::vector< std::string > &output)
Creates a key vector structure used in the options backend from a dot-separated string.
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
STL namespace.
LoggerPtr getLogger()
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
std::map< std::vector< std::string >, std::string > options
Provides a hierarchical logging system with the possibility to install different backends, which are instances of LoggingSystem.
void handler(int signal)
bool getOption(std::string &name, std::string &value)
Interface for logging adapters that can be used with RSC.
Definition: Logger.h:54