Replace qgetenv() calls converted to QString with qEnvironmentVariable()

It's slightly more efficient.

Pick-to: 6.9
Change-Id: Id5ac04fc27eee108c8e5fffd786c3d5f793a0a9d
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2024-12-31 16:26:39 -03:00
parent ce95c26034
commit db34e27f7f
23 changed files with 75 additions and 74 deletions

View File

@ -1186,7 +1186,7 @@ QMessagePattern::QMessagePattern()
#ifndef QT_BOOTSTRAPPED
timer.start();
#endif
const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN"));
const QString envPattern = qEnvironmentVariable("QT_MESSAGE_PATTERN");
if (envPattern.isEmpty()) {
setPattern(QLatin1StringView(defaultPattern));
fromEnvironment = false;

View File

@ -1783,7 +1783,7 @@ bool QFileSystemEngine::setFileTime(int fd, const QDateTime &newDate, QFile::Fil
QString QFileSystemEngine::homePath()
{
QString home = QFile::decodeName(qgetenv("HOME"));
QString home = qEnvironmentVariable("HOME");
if (home.isEmpty())
home = rootPath();
return QDir::cleanPath(home);
@ -1799,7 +1799,7 @@ QString QFileSystemEngine::tempPath()
#ifdef QT_UNIX_TEMP_PATH_OVERRIDE
return QT_UNIX_TEMP_PATH_OVERRIDE ""_L1;
#else
QString temp = QFile::decodeName(qgetenv("TMPDIR"));
QString temp = qEnvironmentVariable("TMPDIR");
if (temp.isEmpty()) {
if (false) {
#if defined(Q_OS_DARWIN) && !defined(QT_BOOTSTRAPPED)

View File

@ -282,9 +282,8 @@ void QLoggingRegistry::initializeRules()
}
QList<QLoggingRule> er, qr, cr;
// get rules from environment
const QByteArray rulesFilePath = qgetenv("QT_LOGGING_CONF");
if (!rulesFilePath.isEmpty())
er = loadRulesFromFile(QFile::decodeName(rulesFilePath));
if (QString rulesFilePath = qEnvironmentVariable("QT_LOGGING_CONF"); !rulesFilePath.isEmpty())
er = loadRulesFromFile(rulesFilePath);
if (qtLoggingDebug())
debugMsg("Checking %s environment variable", "QT_LOGGING_RULES");

View File

@ -6,6 +6,7 @@
#include <qdir.h>
#include <qfileinfo.h>
#include <qvarlengtharray.h>
#ifndef QT_BOOTSTRAPPED
#include <qobject.h>
@ -424,6 +425,28 @@ QStringList QStandardPaths::locateAll(StandardLocation type, const QString &file
return result;
}
static Q_DECL_COLD_FUNCTION QString fallbackPathVariable()
{
#if defined(_PATH_DEFPATH)
// BSD API.
return QString::fromLocal8Bit(_PATH_DEFPATH);
#endif
#if defined(_CS_PATH)
// POSIX API.
size_t n = confstr(_CS_PATH, nullptr, 0);
if (n) {
// n includes the terminating null
QVarLengthArray<char, 1024> rawpath(n);
confstr(_CS_PATH, rawpath.data(), n);
return QString::fromLocal8Bit(QByteArrayView(rawpath.data(), n - 1));
}
#else
// Windows SDK's execvpe() does not have a fallback, so we won't
// apply one either.
#endif
return {};
}
#ifdef Q_OS_WIN
static QStringList executableExtensions()
{
@ -490,30 +513,16 @@ QString QStandardPaths::findExecutable(const QString &executableName, const QStr
QStringList searchPaths = paths;
if (paths.isEmpty()) {
QByteArray pEnv = qgetenv("PATH");
QString pEnv = qEnvironmentVariable("PATH");
if (Q_UNLIKELY(pEnv.isNull())) {
// Get a default path. POSIX.1 does not actually require this, but
// most Unix libc fall back to confstr(_CS_PATH) if the PATH
// environment variable isn't set. Let's try to do the same.
#if defined(_PATH_DEFPATH)
// BSD API.
pEnv = _PATH_DEFPATH;
#elif defined(_CS_PATH)
// POSIX API.
size_t n = confstr(_CS_PATH, nullptr, 0);
if (n) {
pEnv.resize(n);
// size()+1 is ok because QByteArray always has an extra NUL-terminator
confstr(_CS_PATH, pEnv.data(), pEnv.size() + 1);
}
#else
// Windows SDK's execvpe() does not have a fallback, so we won't
// apply one either.
#endif
pEnv = fallbackPathVariable();
}
// Remove trailing slashes, which occur on Windows.
const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(
const QStringList rawPaths = pEnv.split(
QDir::listSeparator(), Qt::SkipEmptyParts);
searchPaths.reserve(rawPaths.size());
for (const QString &rawPath : rawPaths) {

View File

@ -185,7 +185,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
xdgCacheHome = QDir::homePath() + "/.qttest/cache"_L1;
} else {
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
xdgCacheHome = qEnvironmentVariable("XDG_CACHE_HOME");
if (!xdgCacheHome.startsWith(u'/'))
xdgCacheHome.clear(); // spec says relative paths should be ignored
@ -204,7 +204,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
xdgStateHome = QDir::homePath() + "/.qttest/state"_L1;
} else {
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
xdgStateHome = QFile::decodeName(qgetenv("XDG_STATE_HOME"));
xdgStateHome = qEnvironmentVariable("XDG_STATE_HOME");
if (!xdgStateHome.startsWith(u'/'))
xdgStateHome.clear(); // spec says relative paths should be ignored
@ -223,7 +223,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
if (isTestModeEnabled()) {
xdgDataHome = QDir::homePath() + "/.qttest/share"_L1;
} else {
xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
xdgDataHome = qEnvironmentVariable("XDG_DATA_HOME");
if (!xdgDataHome.startsWith(u'/'))
xdgDataHome.clear(); // spec says relative paths should be ignored
@ -243,7 +243,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
xdgConfigHome = QDir::homePath() + "/.qttest/config"_L1;
} else {
// http://standards.freedesktop.org/basedir-spec/latest/
xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
xdgConfigHome = qEnvironmentVariable("XDG_CONFIG_HOME");
if (!xdgConfigHome.startsWith(u'/'))
xdgConfigHome.clear(); // spec says relative paths should be ignored
@ -256,7 +256,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
}
case RuntimeLocation:
{
QString xdgRuntimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
QString xdgRuntimeDir = qEnvironmentVariable("XDG_RUNTIME_DIR");
if (!xdgRuntimeDir.startsWith(u'/'))
xdgRuntimeDir.clear(); // spec says relative paths should be ignored
@ -285,7 +285,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
#if QT_CONFIG(regularexpression)
// http://www.freedesktop.org/wiki/Software/xdg-user-dirs
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
QString xdgConfigHome = qEnvironmentVariable("XDG_CONFIG_HOME");
if (!xdgConfigHome.startsWith(u'/'))
xdgConfigHome.clear(); // spec says relative paths should be ignored
@ -390,7 +390,7 @@ static QStringList dirsList(const QString &xdgEnvVar)
static QStringList xdgDataDirs()
{
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
QString xdgDataDirsEnv = qEnvironmentVariable("XDG_DATA_DIRS");
QStringList dirs = dirsList(xdgDataDirsEnv);
if (dirs.isEmpty())
@ -402,7 +402,7 @@ static QStringList xdgDataDirs()
static QStringList xdgConfigDirs()
{
// http://standards.freedesktop.org/basedir-spec/latest/
const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS"));
const QString xdgConfigDirs = qEnvironmentVariable("XDG_CONFIG_DIRS");
QStringList dirs = dirsList(xdgConfigDirs);
if (dirs.isEmpty())

View File

@ -50,9 +50,8 @@ QIconLoader::QIconLoader() :
static inline QString systemThemeName()
{
const auto override = qgetenv("QT_QPA_SYSTEM_ICON_THEME");
if (!override.isEmpty())
return QString::fromLocal8Bit(override);
if (QString override = qEnvironmentVariable("QT_QPA_SYSTEM_ICON_THEME"); !override.isEmpty())
return override;
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName);
if (themeHint.isValid())

View File

@ -1499,7 +1499,7 @@ void QGuiApplicationPrivate::createPlatformIntegration()
QHighDpiScaling::initHighDpiScaling();
// Load the platform integration
QString platformPluginPath = QString::fromLocal8Bit(qgetenv("QT_QPA_PLATFORM_PLUGIN_PATH"));
QString platformPluginPath = qEnvironmentVariable("QT_QPA_PLATFORM_PLUGIN_PATH");
QByteArray platformName;

View File

@ -105,10 +105,10 @@ static inline bool detectWebBrowser(const QByteArray &desktop,
return true;
if (checkBrowserVariable) {
QByteArray browserVariable = qgetenv("DEFAULT_BROWSER");
QString browserVariable = qEnvironmentVariable("DEFAULT_BROWSER");
if (browserVariable.isEmpty())
browserVariable = qgetenv("BROWSER");
if (!browserVariable.isEmpty() && checkExecutable(QString::fromLocal8Bit(browserVariable), browser))
browserVariable = qEnvironmentVariable("BROWSER");
if (!browserVariable.isEmpty() && checkExecutable(browserVariable, browser))
return true;
}

View File

@ -1204,11 +1204,11 @@ QPlatformTheme *QKdeTheme::createKdeTheme()
// - fallback to /etc/kde<version>
QStringList kdeDirs;
const QString kdeHomePathVar = QFile::decodeName(qgetenv("KDEHOME"));
const QString kdeHomePathVar = qEnvironmentVariable("KDEHOME");
if (!kdeHomePathVar.isEmpty())
kdeDirs += kdeHomePathVar;
const QString kdeDirsVar = QFile::decodeName(qgetenv("KDEDIRS"));
const QString kdeDirsVar = qEnvironmentVariable("KDEDIRS");
if (!kdeDirsVar.isEmpty())
kdeDirs += kdeDirsVar.split(u':', Qt::SkipEmptyParts);

View File

@ -360,7 +360,7 @@ void QPlatformFontDatabase::releaseHandle(void *handle)
*/
QString QPlatformFontDatabase::fontDir() const
{
QString fontpath = QString::fromLocal8Bit(qgetenv("QT_QPA_FONTDIR"));
QString fontpath = qEnvironmentVariable("QT_QPA_FONTDIR");
if (fontpath.isEmpty())
fontpath = QLibraryInfo::path(QLibraryInfo::LibrariesPath) + "/fonts"_L1;

View File

@ -51,7 +51,7 @@ void QBasicPlatformVulkanInstance::loadVulkanLibrary(const QString &defaultLibra
// embedded systems without a Vulkan loader and possibly with custom vendor
// library names.
if (qEnvironmentVariableIsSet("QT_VULKAN_LIB"))
loadList.append({ QString::fromUtf8(qgetenv("QT_VULKAN_LIB")), -1 });
loadList.append({ qEnvironmentVariable("QT_VULKAN_LIB"), -1 });
// Then what the platform specified. On Linux the version is likely 1, thus
// preferring libvulkan.so.1 over libvulkan.so.

View File

@ -22,7 +22,7 @@ QEvdevKeyboardManager::QEvdevKeyboardManager(const QString &key, const QString &
Q_UNUSED(key);
QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_KEYBOARD_PARAMETERS"));
QString spec = qEnvironmentVariable("QT_QPA_EVDEV_KEYBOARD_PARAMETERS");
if (spec.isEmpty())
spec = specification;

View File

@ -24,7 +24,7 @@ QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specif
{
Q_UNUSED(key);
QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_MOUSE_PARAMETERS"));
QString spec = qEnvironmentVariable("QT_QPA_EVDEV_MOUSE_PARAMETERS");
if (spec.isEmpty())
spec = specification;

View File

@ -23,7 +23,7 @@ QEvdevTabletManager::QEvdevTabletManager(const QString &key, const QString &spec
if (qEnvironmentVariableIsSet("QT_QPA_EVDEV_DEBUG"))
const_cast<QLoggingCategory &>(qLcEvdevTablet()).setEnabled(QtDebugMsg, true);
QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_TABLET_PARAMETERS"));
QString spec = qEnvironmentVariable("QT_QPA_EVDEV_TABLET_PARAMETERS");
if (spec.isEmpty())
spec = specification;

View File

@ -23,7 +23,7 @@ QEvdevTouchManager::QEvdevTouchManager(const QString &key, const QString &specif
if (qEnvironmentVariableIsSet("QT_QPA_EVDEV_DEBUG"))
const_cast<QLoggingCategory &>(qLcEvdevTouch()).setEnabled(QtDebugMsg, true);
QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS"));
QString spec = qEnvironmentVariable("QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS");
if (spec.isEmpty())
spec = specification;

View File

@ -52,19 +52,19 @@ void QOutputMapping::set(QOutputMapping *mapping)
bool QDefaultOutputMapping::load()
{
static QByteArray configFile = qgetenv("QT_QPA_EGLFS_KMS_CONFIG");
static QString configFile = qEnvironmentVariable("QT_QPA_EGLFS_KMS_CONFIG");
if (configFile.isEmpty())
return false;
QFile file(QString::fromUtf8(configFile));
QFile file(configFile);
if (!file.open(QFile::ReadOnly)) {
qWarning("touch input support: Failed to open %s", configFile.constData());
qWarning("touch input support: Failed to open %ls", qUtf16Printable(configFile));
return false;
}
const QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
if (!doc.isObject()) {
qWarning("touch input support: Failed to parse %s", configFile.constData());
qWarning("touch input support: Failed to parse %ls", qUtf16Printable(configFile));
return false;
}

View File

@ -731,11 +731,9 @@ QString QIBusPlatformInputContextPrivate::getSocketPath()
QByteArray displayNumber = "0";
bool isWayland = false;
if (qEnvironmentVariableIsSet("IBUS_ADDRESS_FILE")) {
QByteArray path = qgetenv("IBUS_ADDRESS_FILE");
return QString::fromLocal8Bit(path);
} else if (qEnvironmentVariableIsSet("WAYLAND_DISPLAY")) {
display = qgetenv("WAYLAND_DISPLAY");
if (QString path = qEnvironmentVariable("IBUS_ADDRESS_FILE"); !path.isNull()) {
return path;
} else if (display = qgetenv("WAYLAND_DISPLAY"); !display.isEmpty()) {
isWayland = true;
} else {
display = qgetenv("DISPLAY");

View File

@ -50,29 +50,25 @@ DeviceIntegration::DeviceIntegration()
}
}
QByteArray requested;
// The environment variable can override everything.
if (qEnvironmentVariableIsSet("QT_QPA_EGLFS_INTEGRATION")) {
requested = qgetenv("QT_QPA_EGLFS_INTEGRATION");
} else {
QString requested = qEnvironmentVariable("QT_QPA_EGLFS_INTEGRATION");
if (requested.isNull()) {
// Device-specific makespecs may define a preferred plugin.
#ifdef EGLFS_PREFERRED_PLUGIN
#define DEFAULT_PLUGIN EGLFS_PREFERRED_PLUGIN
#define STR(s) #s
#define STRQ(s) STR(s)
requested = STRQ(DEFAULT_PLUGIN);
requested = QStringLiteral(STRQ(DEFAULT_PLUGIN));
#endif
}
// Treat "none" as special. There has to be a way to indicate
// that plugins must be ignored when the device is known to be
// functional with the default, non-specialized integration.
if (requested != QByteArrayLiteral("none")) {
if (requested != QStringLiteral("none")) {
if (!requested.isEmpty()) {
QString reqStr = QString::fromLocal8Bit(requested);
pluginKeys.removeOne(reqStr);
pluginKeys.prepend(reqStr);
pluginKeys.removeOne(requested);
pluginKeys.prepend(requested);
}
qCDebug(qLcEglDevDebug) << "EGL device integration plugin keys (sorted):" << pluginKeys;
while (!m_integration && !pluginKeys.isEmpty()) {

View File

@ -531,7 +531,7 @@ QByteArray QXcbIntegration::wmClass() const
if (m_instanceName)
name = QString::fromLocal8Bit(m_instanceName);
if (name.isEmpty() && qEnvironmentVariableIsSet(resourceNameVar))
name = QString::fromLocal8Bit(qgetenv(resourceNameVar));
name = qEnvironmentVariable(resourceNameVar);
if (name.isEmpty())
name = argv0BaseName();

View File

@ -54,7 +54,7 @@ QPlatformPrinterSupport *QPlatformPrinterSupportPlugin::get()
const QMultiMap<int, QString> keyMap = loader()->keyMap();
QMultiMap<int, QString>::const_iterator it = keyMap.cbegin();
if (!qEnvironmentVariableIsEmpty("QT_PRINTER_MODULE")) {
QString module = QString::fromLocal8Bit(qgetenv("QT_PRINTER_MODULE"));
QString module = qEnvironmentVariable("QT_PRINTER_MODULE");
QMultiMap<int, QString>::const_iterator it2 = std::find_if(keyMap.cbegin(), keyMap.cend(), [module](const QString &value){ return value == module; });
if (it2 == keyMap.cend())
qWarning() << "Unable to load printer plugin" << module;

View File

@ -187,7 +187,7 @@ int runUic(int argc, char *argv[])
if (parser.isSet(pythonPathOption))
pythonPaths = parser.value(pythonPathOption);
else if (qEnvironmentVariableIsSet(pythonPathVar))
pythonPaths = QString::fromUtf8(qgetenv(pythonPathVar));
pythonPaths = qEnvironmentVariable(pythonPathVar);
driver.option().pythonRoot = pythonRoot(pythonPaths, inputFile);
}

View File

@ -3995,11 +3995,11 @@ QString QFileDialogPrivate::getEnvironmentVariable(const QString &string)
{
#ifdef Q_OS_UNIX
if (string.size() > 1 && string.startsWith(u'$')) {
return QString::fromLocal8Bit(qgetenv(QStringView{string}.mid(1).toLatin1().constData()));
return qEnvironmentVariable(QStringView{string}.mid(1).toLatin1().constData());
}
#else
if (string.size() > 2 && string.startsWith(u'%') && string.endsWith(u'%')) {
return QString::fromLocal8Bit(qgetenv(QStringView{string}.mid(1, string.size() - 2).toLatin1().constData()));
return qEnvironmentVariable(QStringView{string}.mid(1, string.size() - 2).toLatin1().constData());
}
#endif
return string;

View File

@ -363,8 +363,8 @@ QWidget *qt_desktopWidget = nullptr; // root window widgets
*/
void QApplicationPrivate::process_cmdline()
{
if (styleOverride.isEmpty() && qEnvironmentVariableIsSet("QT_STYLE_OVERRIDE"))
styleOverride = QString::fromLocal8Bit(qgetenv("QT_STYLE_OVERRIDE"));
if (styleOverride.isEmpty())
styleOverride = qEnvironmentVariable("QT_STYLE_OVERRIDE");
// process platform-indep command line
if (qt_is_tty_app || !argc)
@ -1411,8 +1411,8 @@ QString QApplicationPrivate::desktopStyleKey()
{
#if defined(QT_BUILD_INTERNAL)
// Allow auto-tests to override the desktop style
if (qEnvironmentVariableIsSet("QT_DESKTOP_STYLE_KEY"))
return QString::fromLocal8Bit(qgetenv("QT_DESKTOP_STYLE_KEY"));
if (QString env = qEnvironmentVariable("QT_DESKTOP_STYLE_KEY"); !env.isNull())
return env;
#endif
// The platform theme might return a style that is not available, find