Skip to content

Commit

Permalink
feat: [common/util] add os util class
Browse files Browse the repository at this point in the history
Provides an interface for system judgment

Log:
Change-Id: Ieb72050fb0bb820c0e1d3da32629b4838a26b7fb
  • Loading branch information
deepin-mozart committed Jul 9, 2024
1 parent d23d223 commit 5812640
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/common/util/hostsysteminfo.h
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
50 changes: 50 additions & 0 deletions src/common/util/oshelper.h
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

0 comments on commit 5812640

Please sign in to comment.