From bf9eb67a065e9c58f752c8aef3585d8e9b641c56 Mon Sep 17 00:00:00 2001 From: acetone Date: Mon, 24 May 2021 14:50:45 +0300 Subject: [PATCH] sorted search --- main.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index d255b1b..a2db940 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include "tcpsyncclient.h" @@ -22,10 +24,13 @@ std::map conf = std::string search(std::string text) { + constexpr int maxSize = 10; std::string values; + std::vector matches; std::regex regex(".*" + text + ".*", std::regex_constants::extended | std::regex_constants::icase); boost::filesystem::recursive_directory_iterator dir(conf["logpath"]), end; + uint64_t success = 0; for (; dir != end; ++dir) { if (! boost::filesystem::is_directory(dir->path())) @@ -37,17 +42,37 @@ std::string search(std::string text) { if (std::regex_match(buffer, regex)) { + ++success; std::string date = buffer.substr(0, buffer.find(' ')); - if (values.find(date) == std::string::npos) + + bool first = true; + for (auto entry: matches) { - if (values != "") values += ", "; - values += date; + if (entry.find(date) != std::string::npos) first = false; } + if (first) matches.push_back(date); } } + log.close(); + } + } + if (matches.size() > 0) + { + std::reverse(matches.begin(), matches.end()); + values += "(" + std::to_string(success) + ") "; + + for (int i = matches.size()-1, count = 0; i >= 0 && count < maxSize ; --i, ++count) + { // Компоновка выходной строки + if (values.find('-') != std::string::npos) values += ", "; + values += matches[i]; + } + if (values != "") values += "."; + + for (auto it = values.begin(), end = values.end(); it != end; ++it) + { // Замена тире на слеш + if (*it == '-') *it = '/'; } } - if (values != "") values += "."; return values; }