From: Jean Flach Date: Thu, 13 Nov 2014 14:57:10 +0000 (+0100) Subject: Improve check argument parsing X-Git-Tag: v2.2.0~33 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f85be08480a00b377ef515c0f760971f710fc34f;p=icinga2 Improve check argument parsing --- diff --git a/plugins/check_disk.cpp b/plugins/check_disk.cpp index 1eaaa5a01..f423bc372 100644 --- a/plugins/check_disk.cpp +++ b/plugins/check_disk.cpp @@ -192,7 +192,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& if (vm.count("unit")) { try { - printInfo.unit = parseBUnit(vm["unit"].as().c_str()); + printInfo.unit = parseBUnit(vm["unit"].as()); } catch (std::invalid_argument) { wcout << L"Unknown unit Type " << vm["unit"].as() << endl; return 3; diff --git a/plugins/check_uptime.cpp b/plugins/check_uptime.cpp index bcd8fbf34..9d23b77f5 100644 --- a/plugins/check_uptime.cpp +++ b/plugins/check_uptime.cpp @@ -164,7 +164,7 @@ int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& if (vm.count("unit")) { try{ - printInfo.unit = parseTUnit(vm["unit"].as().c_str()); + printInfo.unit = parseTUnit(vm["unit"].as()); } catch (std::invalid_argument) { } wcout << L"Unknown unit type " << vm["unit"].as() << endl; diff --git a/plugins/thresholds.cpp b/plugins/thresholds.cpp index aa4c4d957..86e1582e9 100644 --- a/plugins/thresholds.cpp +++ b/plugins/thresholds.cpp @@ -22,6 +22,8 @@ #include "boost\algorithm\string.hpp" #include "boost\lexical_cast.hpp" + +using namespace boost::algorithm; using std::wstring; @@ -32,6 +34,9 @@ threshold parse(const wstring& stri) wstring str = stri; + //kill whitespace + str.erase((std::remove(str.begin(), str.end(), L" "), str.end())); + bool low = (str.at(0) == L'!'); if (low) str = wstring(str.begin() + 1, str.end()); @@ -74,23 +79,26 @@ threshold parse(const wstring& stri) } } -Bunit parseBUnit(const wchar_t *str) +Bunit parseBUnit(const wstring& str) { - if (!wcscmp(str, L"B")) + wstring wstr = to_upper_copy(str); + + if (wstr == L"B") return BunitB; - if (!wcscmp(str, L"kB")) + if (wstr == L"kB") return BunitkB; - if (!wcscmp(str, L"MB")) + if (wstr == L"MB") return BunitMB; - if (!wcscmp(str, L"GB")) + if (wstr == L"GB") return BunitGB; - if (!wcscmp(str, L"TB")) + if (wstr == L"TB") return BunitTB; throw std::invalid_argument("Unknown unit type"); } -wstring BunitStr(const Bunit& unit) { +wstring BunitStr(const Bunit& unit) +{ switch (unit) { case BunitB: return L"B"; @@ -106,20 +114,23 @@ wstring BunitStr(const Bunit& unit) { return NULL; } -Tunit parseTUnit(const wchar_t *str) { - if (!wcscmp(str, L"ms")) +Tunit parseTUnit(const wstring& str) { + wstring wstr = to_lower_copy(str); + + if (wstr == L"ms") return TunitMS; - if (!wcscmp(str, L"s")) + if (wstr == L"s") return TunitS; - if (!wcscmp(str, L"m")) + if (wstr == L"m") return TunitM; - if (!wcscmp(str, L"h")) + if (wstr == L"h") return TunitH; throw std::invalid_argument("Unknown unit type"); } -wstring TunitStr(const Tunit& unit) { +wstring TunitStr(const Tunit& unit) +{ switch (unit) { case TunitMS: return L"ms"; diff --git a/plugins/thresholds.h b/plugins/thresholds.h index e47b2f068..d55ed38c4 100644 --- a/plugins/thresholds.h +++ b/plugins/thresholds.h @@ -76,8 +76,8 @@ public: }; threshold parse(const std::wstring&); -Bunit parseBUnit(const wchar_t *); +Bunit parseBUnit(const std::wstring&); std::wstring BunitStr(const Bunit&); -Tunit parseTUnit(const wchar_t *); +Tunit parseTUnit(const std::wstring&); std::wstring TunitStr(const Tunit&); #endif \ No newline at end of file