7b51fa1d751666f908422f7e63441407e0bb2bdb
[anna.git] / source / core / util / Environment.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/core/util/Environment.hpp>
38
39 #include <anna/config/defines.hpp>
40 #include <anna/core/tracing/Logger.hpp>
41 #include <anna/core/tracing/TraceMethod.hpp>
42
43 #include <boost/program_options.hpp>
44 #include <boost/algorithm/string.hpp>
45
46
47 #include <stdlib.h>
48
49
50
51 using namespace std;
52 using namespace anna;
53
54
55
56 void Environment::initialize() throw() {
57   LOGMETHOD(TraceMethod tm("Environment", "initialize", ANNA_FILE_LOCATION));
58
59   // clear data
60   a_managedVars.clear();
61
62   // Register:
63   namespace po = boost::program_options;
64   po::options_description desc("Options");
65
66   FILE *fp;
67   char c_var[256];
68   std::string var;
69
70   /* Open the command for reading. */
71   fp = popen("env | cut -d'=' -f1", "r");
72   if (fp == NULL) {
73     Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION);
74     return;
75   }
76
77   /* Read the output a line at a time - output it. */
78   while (fgets(c_var, sizeof(c_var)-1, fp) != NULL) {
79     var = c_var;
80     boost::trim(var);
81     desc.add_options()(var.c_str(), var.c_str());
82     a_managedVars[var] = ""; // temporary
83   }
84
85   /* close */
86   pclose(fp);
87
88   // Parsing:
89   po::variables_map vm;
90   try {
91     po::store(po::parse_environment(desc, [](const std::string& variable) { return variable; }), vm); // can throw
92
93     std::map<std::string, std::string>::const_iterator it;
94     std::string var, val;
95     for (it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
96       var = (*it).first;
97       if (vm.count(var.c_str())) { // protection
98         val = vm[var.c_str()].as<std::string>();
99         a_managedVars[var] = val;
100       }
101     }
102
103     po::notify(vm);
104
105   } catch (po::error& e) {
106     Logger::error(e.what(), ANNA_FILE_LOCATION);
107   }
108 }
109
110 std::string Environment::getValue (const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
111   std::string result = "";
112
113   if (!variableName)
114     throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION);
115
116   std::map<std::string, std::string>::const_iterator it = a_managedVars.find(variableName);
117   if (it == a_managedVars.end()) {
118     std::string msg = "The variable '";
119     msg += variableName;
120     msg += "' is not defined in the environment.";
121     LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION));
122     if (exceptionIfMissing)
123       throw RuntimeException(msg, ANNA_FILE_LOCATION);
124     return "";
125   }
126
127   return it->second;
128 }
129