Utils: Consolidate port parsing methods

Change-Id: I137e9faa2c5f18e9ade0e2c59d73811d682abf13
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-05-12 08:42:51 +02:00
parent f8d637dc71
commit 6ea8a76e44
6 changed files with 46 additions and 59 deletions

View File

@@ -25,8 +25,46 @@
#include "port.h"
#include "stringutils.h"
/*! \class Utils::Port
\brief The Port class implements a wrapper around a 16 bit port number
to be used in conjunction with IP addresses.
*/
namespace Utils {
QList<Port> Port::parseFromSedOutput(const QByteArray &output)
{
QList<Port> ports;
const QList<QByteArray> portStrings = output.split('\n');
for (const QByteArray &portString : portStrings) {
if (portString.size() != 4)
continue;
bool ok;
const Utils::Port port(portString.toInt(&ok, 16));
if (ok) {
if (!ports.contains(port))
ports << port;
} else {
qWarning("%s: Unexpected string '%s' is not a port.",
Q_FUNC_INFO, portString.data());
}
}
return ports;
}
QList<Port> Port::parseFromNetstatOutput(const QByteArray &output)
{
QList<Port> ports;
const QList<QByteArray> lines = output.split('\n');
for (const QByteArray &line : lines) {
const Port port(Utils::parseUsedPortFromNetstatOutput(line));
if (port.isValid() && !ports.contains(port))
ports.append(port);
}
return ports;
}
} // Utils