RSC  0.7.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Environment.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 "Environment.h"
28 
29 #include <algorithm>
30 #include <iterator>
31 
32 // For dynamic libs the environ variable is not available
33 // on MacOS, hence a workaround is needed. See also:
34 // http://article.gmane.org/gmane.comp.lib.boost.devel/103843
35 #if defined(__APPLE__) && defined(__DYNAMIC__)
36 #include <crt_externs.h>
37 #define environ (*_NSGetEnviron())
38 #endif
39 
40 #include <boost/algorithm/string.hpp>
41 #include <boost/program_options.hpp>
42 #include <boost/program_options/environment_iterator.hpp>
43 
45 
46 using namespace std;
47 
48 using namespace boost;
49 using namespace boost::filesystem;
50 
51 using namespace rsc::logging;
52 
53 namespace rsc {
54 namespace config {
55 
57 #ifndef WIN32
58  return "/etc/";
59 #else
60  return "c:\\";
61 #endif
62 }
63 
65 #ifndef WIN32
66  char* rawHome = getenv("HOME");
67  if (!rawHome) {
68  throw runtime_error("Home directory not defined in HOME variable.");
69  }
70  string home = string(rawHome) + string("/");
71 #else
72  char* rawHomeDrive = getenv("HOMEDRIVE");
73  if (!rawHomeDrive) {
74  throw runtime_error("HOMEDRIVE variable not set.");
75  }
76  char* rawHomePath = getenv("HOMEPATH");
77  if (!rawHomePath) {
78  throw runtime_error("HOMEPATH variable not set.");
79  }
80  string home = string(rawHomeDrive) + string(rawHomePath) + string("\\");
81 #endif
82  return home;
83 }
84 
86  return userHomeDirectory() / ".config";
87 }
88 
89 string transformName(const string& name, const string& prefix) {
90  if (starts_with(name, prefix)) {
91  string result;
92  transform(name.begin() + prefix.size(), name.end(), back_inserter(
93  result), &::tolower);
94  return result;
95  } else {
96  return "";
97  }
98 }
99 
100 EnvironmentVariableSource::EnvironmentVariableSource(const string& prefix) :
101  logger(Logger::getLogger("rsc.config.EnvironmentVariableSource")), prefix(
102  prefix) {
103 }
104 
106  for (environment_iterator it = environment_iterator(environ); it
107  != environment_iterator(); ++it) {
108  string name = transformName(it->first, this->prefix);
109  if (name.empty()) {
110  continue;
111  }
112 
113  vector<string> key;
114  split(key, name, is_any_of("_"));
115  string value = it->second;
116 
117  RSCTRACE(logger, "Option " << key << " -> " << value);
118 
119  handler.handleOption(key, value);
120  }
121 }
122 
123 }
124 }
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.
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.
path userConfigDirectory()
Return the canonical directory for configuration files of the user associated with the current proces...
Definition: Environment.cpp:85
string transformName(const string &name, const string &prefix)
Definition: Environment.cpp:89
Implementations of this interface receive options from ConfigSource objects.
Definition: OptionHandler.h:43
path systemConfigDirectory()
Return the directory in which system-wide configuration files are located.
Definition: Environment.cpp:56
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
rsc::logging::LoggerPtr logger
Definition: Environment.h:99
path userHomeDirectory()
Return the home directory of the user associated with the current process.
Definition: Environment.cpp:64
Interface for logging adapters that can be used with RSC.
Definition: Logger.h:54