summaryrefslogtreecommitdiffstats
path: root/src/linguist/shared/ioutils.cpp
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@qt.io>2022-07-13 15:55:33 +0200
committerJoerg Bornemann <joerg.bornemann@qt.io>2022-09-12 07:28:44 +0200
commitb1606e955548c620f2c810a9a34cd6fc91e245ae (patch)
tree653588f70d581e296a62940333ce8c83bec20bc6 /src/linguist/shared/ioutils.cpp
parent65cb9a7c5444669d498aad2e2707b2a47b56f002 (diff)
Linguist: Sync code shared with qmake
Copied from qtbase/qmake/library/. Change-Id: Id46f949c927a92ff0249d665c6302eb250ccf252 Reviewed-by: Kai Koehne <kai.koehne@qt.io> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/linguist/shared/ioutils.cpp')
-rw-r--r--src/linguist/shared/ioutils.cpp51
1 files changed, 45 insertions, 6 deletions
diff --git a/src/linguist/shared/ioutils.cpp b/src/linguist/shared/ioutils.cpp
index 633b76cea..5a5c45526 100644
--- a/src/linguist/shared/ioutils.cpp
+++ b/src/linguist/shared/ioutils.cpp
@@ -8,7 +8,7 @@
#include <qregularexpression.h>
#ifdef Q_OS_WIN
-# include <windows.h>
+# include <qt_windows.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
@@ -24,6 +24,40 @@ QT_BEGIN_NAMESPACE
using namespace QMakeInternal;
+QString IoUtils::binaryAbsLocation(const QString &argv0)
+{
+ QString ret;
+ if (!argv0.isEmpty() && isAbsolutePath(argv0)) {
+ ret = argv0;
+ } else if (argv0.contains(QLatin1Char('/'))
+#ifdef Q_OS_WIN
+ || argv0.contains(QLatin1Char('\\'))
+#endif
+ ) { // relative PWD
+ ret = QDir::current().absoluteFilePath(argv0);
+ } else { // in the PATH
+ QByteArray pEnv = qgetenv("PATH");
+ QDir currentDir = QDir::current();
+#ifdef Q_OS_WIN
+ QStringList paths = QString::fromLocal8Bit(pEnv).split(QLatin1String(";"));
+ paths.prepend(QLatin1String("."));
+#else
+ QStringList paths = QString::fromLocal8Bit(pEnv).split(QLatin1String(":"));
+#endif
+ for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
+ if ((*p).isEmpty())
+ continue;
+ QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0);
+ if (QFile::exists(candidate)) {
+ ret = candidate;
+ break;
+ }
+ }
+ }
+
+ return QDir::cleanPath(ret);
+}
+
IoUtils::FileType IoUtils::fileType(const QString &fileName)
{
Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName));
@@ -53,7 +87,12 @@ bool IoUtils::isRelativePath(const QString &path)
&& (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\'))) {
return false;
}
- // (... unless, of course, they're UNC, which qmake fails on anyway)
+ // ... unless, of course, they're UNC:
+ if (path.length() >= 2
+ && (path.at(0).unicode() == '\\' || path.at(0).unicode() == '/')
+ && path.at(1) == path.at(0)) {
+ return false;
+ }
#else
if (path.startsWith(QLatin1Char('/')))
return false;
@@ -61,14 +100,14 @@ bool IoUtils::isRelativePath(const QString &path)
return true;
}
-QStringView IoUtils::pathName(QStringView fileName)
+QStringView IoUtils::pathName(const QString &fileName)
{
- return fileName.left(fileName.lastIndexOf(QLatin1Char('/')) + 1);
+ return QStringView{fileName}.left(fileName.lastIndexOf(QLatin1Char('/')) + 1);
}
-QStringView IoUtils::fileName(QStringView fileName)
+QStringView IoUtils::fileName(const QString &fileName)
{
- return fileName.mid(fileName.lastIndexOf(QLatin1Char('/')) + 1);
+ return QStringView(fileName).mid(fileName.lastIndexOf(QLatin1Char('/')) + 1);
}
QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)