From d5663e583b728a364eda8fa5ff8f84e168a332a4 Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 15 May 2018 08:46:11 +0200 Subject: [PATCH] add clean for remove -1 results --- main.cpp | 18 +++++++++++++++++- mayu.cpp | 41 ++++++++++++++++++++++++++++++++--------- mayu.h | 5 ++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 26927ad..9b9bb6e 100644 --- a/main.cpp +++ b/main.cpp @@ -31,6 +31,7 @@ int main(int argc, char *argv[]) QStringList arguments = a.arguments(); arguments.removeAt(0); + bool a_clean = false; mayuMode a_mode = mayuMode::Ping; for (int i = arguments.length(); i > 0; i--) { const QString &argument = arguments.at(i-1); @@ -42,16 +43,31 @@ int main(int argc, char *argv[]) a_mode = mayuMode::Resolve; arguments.removeAt(i-1); } + else if (argument == "-c" || argument == "--clean") { + a_clean = true; + arguments.removeAt(i-1); + } + else if (argument == "-pc" || argument == "-cp") { + a_mode = mayuMode::Ping; + a_clean = true; + arguments.removeAt(i-1); + } + else if (argument == "-rc" || argument == "-cr") { + a_mode = mayuMode::Resolve; + a_clean = true; + arguments.removeAt(i-1); + } } if (arguments.length() >= 2) { mayu a_mayu(arguments.at(0), arguments.at(1)); a_mayu.setMayuMode(a_mode); + a_mayu.setCleanUp(a_clean); a_mayu.work(); return a_mayu.getResult(); } else { - QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " input.txt" << " output.json" << endl; + QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " [-c clean]" << " input.txt" << " output.json" << endl; } return 0; diff --git a/mayu.cpp b/mayu.cpp index 96de91e..489ef09 100644 --- a/mayu.cpp +++ b/mayu.cpp @@ -37,6 +37,7 @@ mayu::mayu(const QString &hostsFile, const QString &jsonFile, QObject *parent) : p_return = -1; p_timeout = 2.5; p_tries = 4; + p_clean = false; p_mayuMode = mayuMode::Ping; if (!hostsFile.isEmpty()) setHostsFile(hostsFile); @@ -76,6 +77,11 @@ void mayu::setPingTries(int tries) p_tries = tries; } +void mayu::setCleanUp(bool clean) +{ + p_clean = clean; +} + mayuMode mayu::getMayuMode() { return p_mayuMode; @@ -106,6 +112,11 @@ int mayu::getPingTries() return p_tries; } +bool mayu::getCleanUp() +{ + return p_clean; +} + int mayu::getResult() { return p_return; @@ -184,7 +195,14 @@ double mayu::ping(const QString &host, int tries, double timeout) char hostname[100]; len = 100; ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len); - QTextStream(stdout) << "Host: " << hostname << " Ping: " << latency << "ms" << " Status: " << (pingSuccess ? "true" : "false") << endl; + QString latencyString; + if (latency != -1) { + latencyString = QString::number(latency) + "ms"; + } + else { + latencyString = QString::number(latency); + } + QTextStream(stdout) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl; #endif } if (pingSuccess) { @@ -199,7 +217,7 @@ double mayu::ping(const QString &host, int tries, double timeout) } #endif -const QList mayu::resolve(const QString &host) +const QList mayu::resolve(const QString &host, bool emptyWhenError) { QList resultList; QList hostAddresses = QHostInfo::fromName(host).addresses(); @@ -218,10 +236,12 @@ const QList mayu::resolve(const QString &host) #ifdef E_DEBUG qDebug() << "Hostname" << host << "not found"; #endif - mayuResult m_result; - m_result.host = host; - m_result.result = "-1"; - resultList += m_result; + if (!emptyWhenError) { + mayuResult m_result; + m_result.host = host; + m_result.result = "-1"; + resultList += m_result; + } } return resultList; } @@ -326,7 +346,9 @@ void mayu::p_workPing() const QStringList hostsList = getHosts(); for (const QString &host : hostsList) { double result = ping(host, p_tries, p_timeout); - jsonObject[host] = result; + if (!(result == -1 && p_clean)) { + jsonObject[host] = result; + } } p_saveWork(jsonObject); } @@ -339,12 +361,13 @@ void mayu::p_workResolve() QJsonObject jsonObject; const QStringList hostsList = getHosts(); for (const QString &host : hostsList) { - const QList resultList = resolve(host); + const QList resultList = resolve(host, p_clean); QJsonArray arrayList; for (const mayuResult &result : resultList) { arrayList += result.result; } - jsonObject[host] = arrayList; + if (!arrayList.isEmpty()) + jsonObject[host] = arrayList; } p_saveWork(jsonObject); } diff --git a/mayu.h b/mayu.h index 9ac3c27..62081d5 100644 --- a/mayu.h +++ b/mayu.h @@ -42,17 +42,19 @@ public: void setJsonFile(const QString &fileName); void setPingTimeout(double timeout); void setPingTries(int tries); + void setCleanUp(bool clean); mayuMode getMayuMode(); const QString getHostsFile(); const QStringList getHosts(); const QString getJsonFile(); double getPingTimeout(); int getPingTries(); + bool getCleanUp(); int getResult(); #ifdef MAYU_UNIX static double ping(const QString &host, int tries, double timeout = 2.5); #endif - static const QList resolve(const QString &host); + static const QList resolve(const QString &host, bool emptyWhenError = false); public slots: void parse_hosts(); @@ -76,6 +78,7 @@ private: double p_timeout; int p_return; int p_tries; + bool p_clean; #ifdef PRIVILEGE_DROP_REQUIRED uid_t p_uid; #endif