void Environment::initialize() throw() {
LOGMETHOD(TraceMethod tm("Environment", "initialize", ANNA_FILE_LOCATION));
-
// clear data
a_managedVars.clear();
-
// Register:
namespace po = boost::program_options;
po::options_description desc("Options");
-
FILE *fp;
char c_var[256];
std::string var;
-
/* Open the command for reading. */
fp = popen("env | cut -d'=' -f1", "r");
- if (fp == NULL) {
+
+ if(fp == NULL) {
Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION);
return;
}
/* Read the output a line at a time - output it. */
- while (fgets(c_var, sizeof(c_var)-1, fp) != NULL) {
+ while(fgets(c_var, sizeof(c_var) - 1, fp) != NULL) {
var = c_var;
boost::trim(var);
desc.add_options()(var.c_str(), var.c_str());
/* close */
pclose(fp);
-
// Parsing:
po::variables_map vm;
- try {
- po::store(po::parse_environment(desc, [](const std::string& variable) { return variable; }), vm); // can throw
+ try {
+ po::store(po::parse_environment(desc, [](const std::string & variable) { return variable; }), vm); // can throw
std::map<std::string, std::string>::const_iterator it;
std::string var, val;
- for (it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
+
+ for(it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
var = (*it).first;
- if (vm.count(var.c_str())) { // protection
+
+ if(vm.count(var.c_str())) { // protection
val = vm[var.c_str()].as<std::string>();
a_managedVars[var] = val;
}
}
po::notify(vm);
-
- } catch (po::error& e) {
+ } catch(po::error& e) {
Logger::error(e.what(), ANNA_FILE_LOCATION);
}
}
-std::string Environment::getValue (const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
+std::string Environment::getValue(const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
std::string result = "";
- if (!variableName)
+ if(!variableName)
throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION);
std::map<std::string, std::string>::const_iterator it = a_managedVars.find(variableName);
- if (it == a_managedVars.end()) {
+
+ if(it == a_managedVars.end()) {
std::string msg = "The variable '";
msg += variableName;
msg += "' is not defined in the environment.";
LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION));
- if (exceptionIfMissing)
- throw RuntimeException(msg, ANNA_FILE_LOCATION);
+
+ if(exceptionIfMissing) throw RuntimeException(msg, ANNA_FILE_LOCATION);
+
return "";
}
// http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
BOOST_AUTO_TEST_CASE(tokenizer) {
-
anna::Tokenizer lst;
lst.apply("In three words", " ");
//BOOST_REQUIRE(lst.size() == 3);
BOOST_REQUIRE_EQUAL(lst.size(), 3);
-
anna::Tokenizer::const_iterator tok_iter(lst.begin());
BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In");
BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three");
BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words");
- BOOST_REQUIRE_EQUAL(tok_iter, lst.end());
+ BOOST_REQUIRE_EQUAL(tok_iter, lst.end());
BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException);
-
lst.apply("In three words", ",");
BOOST_REQUIRE_EQUAL(lst.size(), 1);
-
lst.apply("", ",");
BOOST_REQUIRE_EQUAL(lst.size(), 0);
BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
-
// std::string str = "";
// for (int k = 0; k < 100; k++)
// str += "x ";
}
BOOST_AUTO_TEST_CASE(functions_asString) {
-
std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
-
char cad_aux[128];
- sprintf (cad_aux, "%d", 123);
+ sprintf(cad_aux, "%d", 123);
BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
-
unsigned long ul = 43200111UL;
- sprintf (cad_aux, "%lu", ul);
+ sprintf(cad_aux, "%lu", ul);
BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
-
unsigned long long ull = 4321000111ULL;
- sprintf (cad_aux, "%llu", ull);
+ sprintf(cad_aux, "%llu", ull);
BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
-
Unsigned64 u64 = ull;
BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
-
unsigned int ui = 1234567890U;
- sprintf (cad_aux, "%u", ui);
+ sprintf(cad_aux, "%u", ui);
BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
-
float f = 123.34;
double d = 3.1415;
- sprintf (cad_aux, "%4.2f", f);
+ sprintf(cad_aux, "%4.2f", f);
BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
- sprintf (cad_aux, "%4.2f", d);
+ sprintf(cad_aux, "%4.2f", d);
BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
}
BOOST_AUTO_TEST_CASE(configuration) {
-
anna::Configuration conf;
-
BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException);
BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf"));
-
//[ property ]
//thing = tshirt
//size = 1
//[ owner ]
//name = edu
//address = madrid
-
BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
-
std::string value;
-
BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt");
BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1);
BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue");
BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu");
BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid");
-
conf.setDefaultValue("owner", "phone", "555 55 55");
BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55");
BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */);
}
BOOST_AUTO_TEST_CASE(environment) {
-
anna::Environment &env = anna::Environment::instantiate();
-
BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true /* exception if missing */), anna::RuntimeException);
BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException);
BOOST_REQUIRE_EQUAL(env.getValue("HOME"), std::string("/home/eramos"));
std::string ntpStr = current2.asString();
current2.storeUnix(unixtime);
std::string unixStr = current2.asString();
-
BOOST_REQUIRE_EQUAL(ntpStr, unixStr);
-
anna::time::Date myBirth("CET"); // 19 December 1974, 10:00 (GMT+1 = UTC + 1 = CET)
//anna::time::Date myBirth; // if Context TZ = shell TZ = "CET"
myBirth.store("19741219101500"); // yyyymmddHHmmss
-
anna::time::Date same_moment_canary_island("GMT");
same_moment_canary_island.store(myBirth.getUnixTimestamp());
myBirth.setTzContext("GMT");
BOOST_REQUIRE_EQUAL(same_moment_canary_island.yyyymmddHHmmss(), myBirth.yyyymmddHHmmss());
-
anna::time::Date birthday("EET");
birthday.store(same_moment_canary_island.getTm(), "GMT"); // TZ origin = "GMT"
//BOOST_REQUIRE_EQUAL(birthday, myBirth);
BOOST_REQUIRE_EQUAL(birthday.getUnixTimestamp(), myBirth.getUnixTimestamp());
-
myBirth.setTzContext(anna::time::functions::getLocalTz().getValue().c_str());
birthday.setTzContext(anna::time::functions::getLocalTz().getValue().c_str()); // Go from "EET" to "CET"
BOOST_REQUIRE_EQUAL(myBirth.yyyymmddHHmmss(), birthday.yyyymmddHHmmss());
-
// Adding 18 years to 'myBirth':
struct tm Tm = myBirth.getTm();
Tm.tm_year += 18;