From 2be6bdae5cb6ba3e3c0641ff4888a2a49568d886 Mon Sep 17 00:00:00 2001 From: acetone Date: Mon, 24 May 2021 15:38:08 +0300 Subject: [PATCH] sorted search --- main.cpp | 44 ++++++++++++++++++++++++++------------------ tcpsyncclient.cpp | 3 ++- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index b9da4c6..4b0515e 100644 --- a/main.cpp +++ b/main.cpp @@ -25,53 +25,61 @@ std::map conf = std::string search(std::string text) { constexpr int maxSize = 10; + uint64_t success = 0; + uint64_t found = 0; 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())) + if (boost::filesystem::is_directory(dir->path())) continue; + + std::string buffer; + std::ifstream log(dir->path().c_str()); + while(getline(log, buffer)) { - std::string buffer; - std::ifstream log(dir->path().c_str()); - - while(getline(log, buffer)) + if (std::regex_match(buffer, regex)) { - if (std::regex_match(buffer, regex)) + std::string date = buffer.substr(0, buffer.find(' ')); + bool first = true; + for (auto entry: matches) { - ++success; - std::string date = buffer.substr(0, buffer.find(' ')); - - bool first = true; - for (auto entry: matches) + if (entry.find(date) != std::string::npos) { - if (entry.find(date) != std::string::npos) first = false; + ++found; + first = false; } - if (first) matches.push_back(date); + } + if (first) + { + matches.push_back(date); + ++success; } } - log.close(); } + log.close(); + } if (matches.size() > 0) { std::sort(matches.begin(), matches.end()); - values += "(" + std::to_string(success) + ") "; + values += "(" + std::to_string(found) + ") "; 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 (success > maxSize) values += "..."; + else values += "."; } return values; } @@ -127,7 +135,7 @@ int write_log(std::string msg) } else return 2; - out << year << "-" << month << "-" << day << " " << msg; + out << year << "-" << month << "-" << day << " " << msg << std::endl; out.close(); return 0; } diff --git a/tcpsyncclient.cpp b/tcpsyncclient.cpp index 4e3182e..90022ef 100644 --- a/tcpsyncclient.cpp +++ b/tcpsyncclient.cpp @@ -176,7 +176,8 @@ void TcpSyncClient::process_msg() m_raw_nickname = msg.substr(1, msg.find('!') - 1); while (m_raw[0] == ' ') m_raw = m_raw.substr(1); - while (m_raw[m_raw.size() - 1] == '\n') m_raw.pop_back(); + while (m_raw[m_raw.size() - 1] == '\n'|| + m_raw[m_raw.size() - 1] == '\r') m_raw.pop_back(); if (m_raw.find("ACTION") == 0) { m_raw = "-" + m_raw.substr(7);