// -*- C++ -*- #include #include #include #include #include #include #include "optionUtils.h" #include "dout.h" using namespace std; // Variable definitions OptionUtils::OptionMap OptionUtils::ns_intMap; OptionUtils::OptionMap OptionUtils::ns_doubleMap; OptionUtils::OptionMap OptionUtils::ns_stringMap; OptionUtils::OptionMap OptionUtils::ns_boolMap; void OptionUtils::parseArguments (int argc, char** argv) { bool printOptions = false; bool exitAfterPrinting = false; for (int loop = 1; loop < argc; ++loop) { string arg = argv[loop]; unsigned int where = arg.find_first_of("="); if (string::npos != where) { if (! _setVariableFromString (arg) ) { cerr << "Don't understand: " << arg << endl; exit(0); } continue; } // tag=value strings else if (arg.at(0) == '-') { lowercaseString (arg); char first = arg.at(1); // Print the values if ('p' == first) { printOptions = true; continue; } // Exit after printing values if ('e' == first) { printOptions = true; exitAfterPrinting = true; continue; } } // -arg strings cerr << "Don't understand: " << arg << endl; exit(0); } // for loop if (printOptions) { printOptionValues(); if (exitAfterPrinting) { exit(0); } } // if printOptions } bool OptionUtils::_setVariableFromString (const string &arg) { unsigned int where = arg.find_first_of("="); string varname = arg.substr (0, where); string value = arg.substr (where + 1); lowercaseString (varname); OptionMapIter iter = ns_doubleMap.find(varname); if (ns_doubleMap.end() != iter) { // we found it double *doublePtr = (double*) iter->second; *doublePtr = atof(value.c_str()); return true; } iter = ns_intMap.find(varname); if (ns_intMap.end() != iter) { // we found it int *intPtr = (int*) iter->second; // use 'atof' instead of 'atoi' to get scientific notation *intPtr = (int) atof( value.c_str() ); return true; } iter = ns_stringMap.find(varname); if (ns_stringMap.end() != iter) { // we found it string *stringPtr = (string*) iter->second; *stringPtr = value; return true; } iter = ns_boolMap.find(varname); if (ns_boolMap.end() != iter) { // we found it bool *boolPtr = (bool*) iter->second; int val = atoi(value.c_str()); if (val) { *boolPtr = true; } else { *boolPtr = false; } return true; } return false; } string OptionUtils::removeEnding (const string &input, const string &ending) { unsigned int position = input.rfind(ending); if (input.length() - ending.length() == position) { // we've got it return input.substr(0, position); } // If we're still here, it wasn't there return input; } void OptionUtils::printOptionValues() { cout << "------------------------------------------------------------------" << endl << "Option Values:" << endl; // Print the bools first if (ns_boolMap.size()) { cout << " Bool options:" << endl; } for (OptionMapIter iter = ns_boolMap.begin(); ns_boolMap.end() != iter; ++iter) { bool *valuePtr = (bool *) iter->second; cout << " " << iter->first << " = "; if (*valuePtr) { cout << "true"; } else { cout << "false"; } cout << endl; } // for iter // Print the ints next if (ns_intMap.size()) { cout << " Int options:" << endl; } for (OptionMapIter iter = ns_intMap.begin(); ns_intMap.end() != iter; ++iter) { int *valuePtr = (int *)iter->second; cout << " " << iter->first << " = " << *valuePtr << endl; } // for iter // Print the doubles next if (ns_doubleMap.size()) { cout << " Double options:" << endl; } for (OptionMapIter iter = ns_doubleMap.begin(); ns_doubleMap.end() != iter; ++iter) { double *valuePtr = (double *)iter->second; cout << " " << iter->first << " = " << *valuePtr << endl; } // for iter // Print the strings next if (ns_stringMap.size()) { cout << " String options:" << endl; } for (OptionMapIter iter = ns_stringMap.begin(); ns_stringMap.end() != iter; ++iter) { string *valuePtr = (string *)iter->second; cout << " " << iter->first << " = '" << *valuePtr << "'" << endl; } // for iter cout << "------------------------------------------------------------------" << endl; } void OptionUtils::lowercaseString(string& arg) { // assumes 'toLower(ch)' modifies ch std::for_each (arg.begin(), arg.end(), OptionUtils::toLower); // // assumes 'toLower(ch)' returns the lower case char // std::transform (arg.begin(), arg.end(), arg.begin(), // OptionUtils::toLower); } char OptionUtils::toLower (char& ch) { ch = tolower (ch); return ch; } void OptionUtils::addOptionKey (string key, int &variable) { lowercaseString (key); ns_intMap[key] = (void*) &variable; } void OptionUtils::addOptionKey (string key, double &variable) { lowercaseString (key); ns_doubleMap[key] = (void*) &variable; } void OptionUtils::addOptionKey (string key, bool &variable) { lowercaseString (key); ns_boolMap[key] = (void*) &variable; } void OptionUtils::addOptionKey (string key, string &variable) { lowercaseString (key); ns_stringMap[key] = (void*) &variable; }