Module: check_mk
Branch: master
Commit: c51e0e29f6058e38b5f523bce326c0ecd72f5d51
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c51e0e29f6058e…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Thu Mar 8 09:51:08 2018 +0100
Win-agent: sort eventlogs case insensitive
---
agents/windows/build_version | 2 +-
agents/windows/sections/SectionEventlog.cc | 8 +++---
agents/windows/stringutil.cc | 13 ++++++++++
agents/windows/stringutil.h | 5 +++-
agents/windows/test/stringutilTest.cc | 40 ++++++++++++++++++++++++++----
5 files changed, 58 insertions(+), 10 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index ac68758..a4c2c8d 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3162
+3164
diff --git a/agents/windows/sections/SectionEventlog.cc
b/agents/windows/sections/SectionEventlog.cc
index e96613e..c1f88c5 100644
--- a/agents/windows/sections/SectionEventlog.cc
+++ b/agents/windows/sections/SectionEventlog.cc
@@ -102,9 +102,11 @@ eventlog::States loadEventlogOffsets(const
std::vector<std::string> &statefiles,
}
}
- std::sort(
- states.begin(), states.end(),
- [](const auto &s1, const auto &s2) { return s1.name < s2.name;
});
+ std::sort(states.begin(), states.end(),
+ [](const auto &s1, const auto &s2) {
+ return ci_compare(s1.name, s2.name);
+ });
+
if (!states.empty()) {
return states;
}
diff --git a/agents/windows/stringutil.cc b/agents/windows/stringutil.cc
index 096178e..534a97d 100644
--- a/agents/windows/stringutil.cc
+++ b/agents/windows/stringutil.cc
@@ -71,6 +71,19 @@ bool ci_equal(const std::string &lhs, const std::string &rhs)
{
ci_compare_pred);
}
+bool ci_compare(const std::string &lhs, const std::string &rhs) {
+ for (auto ri = lhs.cbegin(), li = rhs.cbegin();
+ ri != lhs.cend() && li != rhs.cend(); ++ri, ++li) {
+ auto l1 = std::tolower(*ri), l2 = std::tolower(*li);
+ if (l1 != l2) {
+ return l1 < l2;
+ }
+ }
+
+ // If equal until here, lhs < rhs iff lhs shorter than rhs.
+ return lhs.size() < rhs.size();
+}
+
// clang-format off
template <> char sol<char>() { return '^'; }
template <> wchar_t sol<wchar_t>() { return L'^'; }
diff --git a/agents/windows/stringutil.h b/agents/windows/stringutil.h
index 2286262..011f21e 100644
--- a/agents/windows/stringutil.h
+++ b/agents/windows/stringutil.h
@@ -135,9 +135,12 @@ inline std::ostream &operator<<(std::ostream &os, const
Utf8 &u) {
return os << to_utf8(u._value);
}
-// case insensitive compare
+// case insensitive equality check
bool ci_equal(const std::string &lhs, const std::string &rhs);
+// case insensitive compare function (e. g. for std::sort)
+bool ci_compare(const std::string &lhs, const std::string &rhs);
+
// clang-format off
// Start-of-line char: '^' or L'^'
template <class CharT> inline CharT sol();
diff --git a/agents/windows/test/stringutilTest.cc
b/agents/windows/test/stringutilTest.cc
index 240c78c..80b0db7 100644
--- a/agents/windows/test/stringutilTest.cc
+++ b/agents/windows/test/stringutilTest.cc
@@ -193,31 +193,61 @@ TEST(wa_stringutilTest,
isPathRelative_relative_with_whitespace_quotes_unix) {
TEST(wa_stringutilTest, ci_equal__equal_cases_equal) {
const std::string s1{"asdfgh"};
const std::string s2{s1};
- ASSERT_TRUE(ci_equal(s1, s2));
+ ASSERT_TRUE(ci_equal(s1, s2)) << "Expected " << s1 <<
" == " << s2;
}
TEST(wa_stringutilTest, ci_equal__equal_cases_unequal) {
const std::string s1{"asdfgh§$"};
const std::string s2{"aSdFgH§$"};
- ASSERT_TRUE(ci_equal(s1, s2));
+ ASSERT_TRUE(ci_equal(s1, s2)) << "Expected " << s1 <<
" == " << s2;
}
TEST(wa_stringutilTest, ci_equal__unequal_first_shorter) {
const std::string s1{"asdfgh"};
const std::string s2{"aSdFgH§$"};
- ASSERT_FALSE(ci_equal(s1, s2));
+ ASSERT_FALSE(ci_equal(s1, s2)) << "Expected " << s1 <<
" != " << s2;
}
TEST(wa_stringutilTest, ci_equal__unequal_first_longer) {
const std::string s1{"asdfgh§$"};
const std::string s2{"aSdFgH"};
- ASSERT_FALSE(ci_equal(s1, s2));
+ ASSERT_FALSE(ci_equal(s1, s2)) << "Expected " << s1 <<
" != " << s2;
}
TEST(wa_stringutilTest, ci_equal__unequal) {
const std::string s1{"asdfg$"};
const std::string s2{"aSdFgH"};
- ASSERT_FALSE(ci_equal(s1, s2));
+ ASSERT_FALSE(ci_equal(s1, s2)) << "Expected " << s1 <<
" != " << s2;
+}
+
+TEST(wa_stringutilTest, ci_compare__equal) {
+ const std::string s1{"asdfgh"};
+ const std::string s2{"aSdFgH"};
+ ASSERT_FALSE(ci_compare(s1, s2)) << "Expected " << s1 <<
" == " << s2;
+}
+
+TEST(wa_stringutilTest, ci_compare__true) {
+ const std::string s1{"asdfgg"};
+ const std::string s2{"aSdFgH"};
+ ASSERT_TRUE(ci_compare(s1, s2)) << "Expected " << s1 <<
" < " << s2;
+}
+
+TEST(wa_stringutilTest, ci_compare__false) {
+ const std::string s1{"asdfgH"};
+ const std::string s2{"aSdFgg"};
+ ASSERT_FALSE(ci_compare(s1, s2)) << "Expected " << s1 <<
" > " << s2;
+}
+
+TEST(wa_stringutilTest, ci_compare__first_shorter) {
+ const std::string s1{"asdfg"};
+ const std::string s2{"aSdFgg"};
+ ASSERT_TRUE(ci_compare(s1, s2)) << "Expected " << s1 <<
" < " << s2;
+}
+
+TEST(wa_stringutilTest, ci_compare__second_shorter) {
+ const std::string s1{"aSdfgh"};
+ const std::string s2{"asdFg"};
+ ASSERT_FALSE(ci_compare(s1, s2)) << "Expected " << s1 <<
" > " << s2;
}
// Tests for char version of globmatch: