2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2013-08-15 16:08:16 +02:00
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
2018-04-23 12:46:37 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2013-08-15 16:08:16 +02:00
|
|
|
#define QTC_WIN_EXE_SUFFIX ".exe"
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
|
|
// Add more as needed.
|
|
|
|
|
enum OsType { OsTypeWindows, OsTypeLinux, OsTypeMac, OsTypeOtherUnix, OsTypeOther };
|
|
|
|
|
|
2023-03-15 09:30:14 +01:00
|
|
|
inline QString osTypeToString(OsType osType)
|
|
|
|
|
{
|
|
|
|
|
switch (osType) {
|
|
|
|
|
case OsTypeWindows:
|
|
|
|
|
return "Windows";
|
|
|
|
|
case OsTypeLinux:
|
|
|
|
|
return "Linux";
|
|
|
|
|
case OsTypeMac:
|
|
|
|
|
return "Mac";
|
|
|
|
|
case OsTypeOtherUnix:
|
|
|
|
|
return "Other Unix";
|
|
|
|
|
case OsTypeOther:
|
|
|
|
|
default:
|
|
|
|
|
return "Other";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline OsType osTypeFromString(const QString &string)
|
|
|
|
|
{
|
|
|
|
|
if (string == "Windows")
|
|
|
|
|
return OsTypeWindows;
|
|
|
|
|
if (string == "Linux")
|
|
|
|
|
return OsTypeLinux;
|
|
|
|
|
if (string == "Mac")
|
|
|
|
|
return OsTypeMac;
|
|
|
|
|
if (string == "Other Unix")
|
|
|
|
|
return OsTypeOtherUnix;
|
|
|
|
|
return OsTypeOther;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 13:34:11 +02:00
|
|
|
namespace OsSpecificAspects {
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2018-09-03 14:05:57 +02:00
|
|
|
inline QString withExecutableSuffix(OsType osType, const QString &executable)
|
2018-04-23 13:34:11 +02:00
|
|
|
{
|
|
|
|
|
QString finalName = executable;
|
2022-02-14 16:51:15 +01:00
|
|
|
if (osType == OsTypeWindows && !finalName.endsWith(QTC_WIN_EXE_SUFFIX))
|
2018-04-23 13:34:11 +02:00
|
|
|
finalName += QLatin1String(QTC_WIN_EXE_SUFFIX);
|
|
|
|
|
return finalName;
|
|
|
|
|
}
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2022-09-28 12:37:57 +02:00
|
|
|
constexpr Qt::CaseSensitivity fileNameCaseSensitivity(OsType osType)
|
2018-04-23 13:34:11 +02:00
|
|
|
{
|
|
|
|
|
return osType == OsTypeWindows || osType == OsTypeMac ? Qt::CaseInsensitive : Qt::CaseSensitive;
|
|
|
|
|
}
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2022-09-28 12:37:57 +02:00
|
|
|
constexpr Qt::CaseSensitivity envVarCaseSensitivity(OsType osType)
|
2019-08-19 12:04:05 +02:00
|
|
|
{
|
|
|
|
|
return fileNameCaseSensitivity(osType);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 12:37:57 +02:00
|
|
|
constexpr QChar pathListSeparator(OsType osType)
|
2018-04-23 13:34:11 +02:00
|
|
|
{
|
|
|
|
|
return QLatin1Char(osType == OsTypeWindows ? ';' : ':');
|
|
|
|
|
}
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2022-09-28 12:37:57 +02:00
|
|
|
constexpr Qt::KeyboardModifier controlModifier(OsType osType)
|
2018-04-23 13:34:11 +02:00
|
|
|
{
|
|
|
|
|
return osType == OsTypeMac ? Qt::MetaModifier : Qt::ControlModifier;
|
|
|
|
|
}
|
2013-08-15 16:08:16 +02:00
|
|
|
|
2018-09-03 14:05:57 +02:00
|
|
|
inline QString pathWithNativeSeparators(OsType osType, const QString &pathName)
|
2018-04-23 13:34:11 +02:00
|
|
|
{
|
|
|
|
|
if (osType == OsTypeWindows) {
|
|
|
|
|
const int pos = pathName.indexOf('/');
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
|
QString n = pathName;
|
|
|
|
|
std::replace(std::begin(n) + pos, std::end(n), '/', '\\');
|
|
|
|
|
return n;
|
2018-04-23 12:46:37 +02:00
|
|
|
}
|
2021-05-17 16:11:19 +02:00
|
|
|
} else {
|
|
|
|
|
const int pos = pathName.indexOf('\\');
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
|
QString n = pathName;
|
|
|
|
|
std::replace(std::begin(n) + pos, std::end(n), '\\', '/');
|
|
|
|
|
return n;
|
|
|
|
|
}
|
2018-04-23 12:46:37 +02:00
|
|
|
}
|
2018-04-23 13:34:11 +02:00
|
|
|
return pathName;
|
|
|
|
|
}
|
2018-04-23 12:46:37 +02:00
|
|
|
|
2018-04-23 13:34:11 +02:00
|
|
|
} // namespace OsSpecificAspects
|
2013-08-15 16:08:16 +02:00
|
|
|
} // namespace Utils
|