]> granicus.if.org Git - icinga2/commitdiff
Improve check argument parsing
authorJean Flach <jean-marcel.flach@netways.de>
Thu, 13 Nov 2014 14:57:10 +0000 (15:57 +0100)
committerJean Flach <jean-marcel.flach@netways.de>
Thu, 13 Nov 2014 14:57:10 +0000 (15:57 +0100)
plugins/check_disk.cpp
plugins/check_uptime.cpp
plugins/thresholds.cpp
plugins/thresholds.h

index 1eaaa5a0168b94dfff59771bd0e644c21496b3b3..f423bc372d0d50bf81f98ebdca9cea7fea2c84cd 100644 (file)
@@ -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<wstring>().c_str());
+                       printInfo.unit = parseBUnit(vm["unit"].as<wstring>());
                } catch (std::invalid_argument) {
                        wcout << L"Unknown unit Type " << vm["unit"].as<wstring>() << endl;
                        return 3;
index bcd8fbf343ed6f410ed39c75cf29b7c1f0035751..9d23b77f5aae257f92c9e805fe554a04cebd6d61 100644 (file)
@@ -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<wstring>().c_str());
+                       printInfo.unit = parseTUnit(vm["unit"].as<wstring>());
                } catch (std::invalid_argument) {
 
                } wcout << L"Unknown unit type " << vm["unit"].as<wstring>() << endl;
index aa4c4d957a51d9809b990313c9dffa6b00e4d7ad..86e1582e94b95b7d64f6b682159b02aedca01a81 100644 (file)
@@ -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";
index e47b2f068da59a53d3da7db7c653c947a0ea8f82..d55ed38c419f9b8cc82b4e79c310d8bd9e7af29a 100644 (file)
@@ -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