83ed2f8815476961956eea5b3ac54079ffd3ca0a
[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   // clear data
59   a_managedVars.clear();
60   // Register:
61   namespace po = boost::program_options;
62   po::options_description desc("Options");
63   FILE *fp;
64   char c_var[256];
65   std::string var;
66   /* Open the command for reading. */
67   fp = popen("env | cut -d'=' -f1", "r");
68
69   if(fp == NULL) {
70     Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION);
71     return;
72   }
73
74   /* Read the output a line at a time - output it. */
75   while(fgets(c_var, sizeof(c_var) - 1, fp) != NULL) {
76     var = c_var;
77     boost::trim(var);
78     desc.add_options()(var.c_str(), var.c_str());
79     a_managedVars[var] = ""; // temporary
80   }
81
82   /* close */
83   pclose(fp);
84   // Parsing:
85   po::variables_map vm;
86
87   try {
88     po::store(po::parse_environment(desc, [](const std::string & variable) { return variable; }), vm); // can throw
89     std::map<std::string, std::string>::const_iterator it;
90     std::string var, val;
91
92     for(it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
93       var = (*it).first;
94
95       if(vm.count(var.c_str())) { // protection
96         val = vm[var.c_str()].as<std::string>();
97         a_managedVars[var] = val;
98       }
99     }
100
101     po::notify(vm);
102   } catch(po::error& e) {
103     Logger::error(e.what(), ANNA_FILE_LOCATION);
104   }
105 }
106
107 std::string Environment::getValue(const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
108   std::string result = "";
109
110   if(!variableName)
111     throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION);
112
113   std::map<std::string, std::string>::const_iterator it = a_managedVars.find(variableName);
114
115   if(it == a_managedVars.end()) {
116     std::string msg = "The variable '";
117     msg += variableName;
118     msg += "' is not defined in the environment.";
119     LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION));
120
121     if(exceptionIfMissing) throw RuntimeException(msg, ANNA_FILE_LOCATION);
122
123     return "";
124   }
125
126   return it->second;
127 }
128