forked from linuxdeepin/deepin-unioncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [common/util] add os util class
Provides an interface for system judgment Log: Change-Id: Ieb72050fb0bb820c0e1d3da32629b4838a26b7fb
- Loading branch information
1 parent
d23d223
commit 5812640
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
#ifndef HOSTSYSTEMINFO_H | ||
#define HOSTSYSTEMINFO_H | ||
|
||
#include "common/common_global.h" | ||
#include "util/oshelper.h" | ||
|
||
#include <QString> | ||
|
||
namespace Utils { | ||
|
||
class COMMON_EXPORT HostSystemInfo | ||
{ | ||
public: | ||
static constexpr OsType hostOs() | ||
{ | ||
#if defined(Q_OS_WIN) | ||
return OsTypeWindows; | ||
#elif defined(Q_OS_LINUX) | ||
return OsTypeLinux; | ||
#elif defined(Q_OS_UNIX) | ||
return OsTypeOtherUnix; | ||
#else | ||
return OsTypeOther; | ||
#endif | ||
} | ||
|
||
static constexpr bool isWindowsHost() { return hostOs() == OsTypeWindows; } | ||
static constexpr bool isLinuxHost() { return hostOs() == OsTypeLinux; } | ||
static constexpr bool isAnyUnixHost() | ||
{ | ||
#ifdef Q_OS_UNIX | ||
return true; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
static QString withExecutableSuffix(const QString &executable) | ||
{ | ||
return OsHelper::withExecutableSuffix(hostOs(), executable); | ||
} | ||
|
||
static QChar pathListSeparator() | ||
{ | ||
return OsHelper::pathListSeparator(hostOs()); | ||
} | ||
}; | ||
|
||
} // namespace Utils | ||
|
||
#endif // HOSTSYSTEMINFO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
#ifndef OSHELPER_H | ||
#define OSHELPER_H | ||
|
||
#include <QString> | ||
|
||
#include <algorithm> | ||
|
||
#define WIN_EXE_SUFFIX ".exe" | ||
|
||
namespace Utils { | ||
|
||
// Add more as needed. | ||
enum OsType { OsTypeWindows, OsTypeLinux, OsTypeOtherUnix, OsTypeOther }; | ||
|
||
namespace OsHelper { | ||
|
||
inline QString withExecutableSuffix(OsType osType, const QString &executable) | ||
{ | ||
QString finalName = executable; | ||
if (osType == OsTypeWindows) | ||
finalName += QLatin1String(WIN_EXE_SUFFIX); | ||
return finalName; | ||
} | ||
|
||
inline QChar pathListSeparator(OsType osType) | ||
{ | ||
return QLatin1Char(osType == OsTypeWindows ? ';' : ':'); | ||
} | ||
|
||
inline QString pathWithNativeSeparators(OsType osType, const QString &pathName) | ||
{ | ||
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; | ||
} | ||
} | ||
return pathName; | ||
} | ||
|
||
} // namespace OSHELPER_H | ||
} // namespace Utils | ||
|
||
#endif |