forked from qt-creator/qt-creator
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:
@@ -25,8 +25,46 @@
|
|||||||
|
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
|
|
||||||
|
#include "stringutils.h"
|
||||||
|
|
||||||
/*! \class Utils::Port
|
/*! \class Utils::Port
|
||||||
|
|
||||||
\brief The Port class implements a wrapper around a 16 bit port number
|
\brief The Port class implements a wrapper around a 16 bit port number
|
||||||
to be used in conjunction with IP addresses.
|
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
|
||||||
|
@@ -29,6 +29,7 @@
|
|||||||
#include "qtcassert.h"
|
#include "qtcassert.h"
|
||||||
|
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
@@ -55,6 +56,9 @@ public:
|
|||||||
|
|
||||||
QString toString() const { return QString::number(m_port); }
|
QString toString() const { return QString::number(m_port); }
|
||||||
|
|
||||||
|
static QList<Port> parseFromSedOutput(const QByteArray &output);
|
||||||
|
static QList<Port> parseFromNetstatOutput(const QByteArray &output);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_port = -1;
|
int m_port = -1;
|
||||||
};
|
};
|
||||||
|
@@ -62,7 +62,6 @@
|
|||||||
#include <utils/processinterface.h>
|
#include <utils/processinterface.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/qtcprocess.h>
|
#include <utils/qtcprocess.h>
|
||||||
#include <utils/stringutils.h>
|
|
||||||
#include <utils/temporaryfile.h>
|
#include <utils/temporaryfile.h>
|
||||||
#include <utils/treemodel.h>
|
#include <utils/treemodel.h>
|
||||||
#include <utils/utilsicons.h>
|
#include <utils/utilsicons.h>
|
||||||
@@ -587,24 +586,7 @@ PortsGatheringMethod DockerDevice::portsGatheringMethod() const
|
|||||||
CommandLine::Raw};
|
CommandLine::Raw};
|
||||||
},
|
},
|
||||||
|
|
||||||
[](const QByteArray &output) {
|
&Port::parseFromSedOutput
|
||||||
QList<Utils::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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -39,7 +39,6 @@
|
|||||||
#include <utils/hostosinfo.h>
|
#include <utils/hostosinfo.h>
|
||||||
#include <utils/portlist.h>
|
#include <utils/portlist.h>
|
||||||
#include <utils/qtcprocess.h>
|
#include <utils/qtcprocess.h>
|
||||||
#include <utils/stringutils.h>
|
|
||||||
#include <utils/url.h>
|
#include <utils/url.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
@@ -139,16 +138,7 @@ PortsGatheringMethod DesktopDevice::portsGatheringMethod() const
|
|||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
|
||||||
[](const QByteArray &output) {
|
&Port::parseFromNetstatOutput
|
||||||
QList<Utils::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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,7 +37,6 @@
|
|||||||
#include <utils/port.h>
|
#include <utils/port.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/qtcprocess.h>
|
#include <utils/qtcprocess.h>
|
||||||
#include <utils/stringutils.h>
|
|
||||||
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
@@ -168,16 +167,7 @@ PortsGatheringMethod QnxDevice::portsGatheringMethod() const
|
|||||||
return {filePath("netstat"), {"-na"}};
|
return {filePath("netstat"), {"-na"}};
|
||||||
},
|
},
|
||||||
|
|
||||||
[](const QByteArray &output) {
|
&Port::parseFromNetstatOutput
|
||||||
QList<Utils::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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1035,24 +1035,7 @@ PortsGatheringMethod LinuxDevice::portsGatheringMethod() const
|
|||||||
CommandLine::Raw};
|
CommandLine::Raw};
|
||||||
},
|
},
|
||||||
|
|
||||||
[](const QByteArray &output) {
|
&Port::parseFromSedOutput
|
||||||
QList<Utils::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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user