diff options
-rw-r--r-- | src/corelib/global/qglobal.cpp | 56 | ||||
-rw-r--r-- | src/corelib/global/qglobal.h | 3 | ||||
-rw-r--r-- | tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp | 17 |
3 files changed, 75 insertions, 1 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 17cb9db86f..b60e7495c6 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -2085,6 +2085,62 @@ QByteArray qgetenv(const char *varName) /*! \relates <QtGlobal> + \internal + + This function checks whether the environment variable \a varName + is empty. + + Equivalent to + \code + qgetenv(varName).isEmpty() + \endcode + except that it's potentially much faster, and can't throw exceptions. + + \sa qgetenv(), qEnvironmentVariableIsSet() +*/ +bool qEnvironmentVariableIsEmpty(const char *varName) Q_DECL_NOEXCEPT +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + // we provide a buffer that can only hold the empty string, so + // when the env.var isn't empty, we'll get an ERANGE error (buffer + // too small): + size_t dummy; + char buffer = '\0'; + return getenv_s(&dummy, &buffer, 1, varName) != ERANGE; +#else + const char * const value = ::getenv(varName); + return !value || !*value; +#endif +} + +/*! + \relates <QtGlobal> + \internal + + This function checks whether the environment variable \a varName + is set. + + Equivalent to + \code + !qgetenv(varName).isNull() + \endcode + except that it's potentially much faster, and can't throw exceptions. + + \sa qgetenv(), qEnvironmentVariableIsEmpty() +*/ +bool qEnvironmentVariableIsSet(const char *varName) Q_DECL_NOEXCEPT +{ +#if defined(_MSC_VER) && _MSC_VER >= 1400 + size_t requiredSize = 0; + (void)getenv_s(&requiredSize, 0, 0, varName); + return requiredSize != 0; +#else + return ::getenv(varName) != 0; +#endif +} + +/*! + \relates <QtGlobal> This function sets the \a value of the environment variable named \a varName. It will create the variable if it does not exist. It diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 5e58f5a4b0..546c6cf575 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1055,6 +1055,9 @@ class QByteArray; Q_CORE_EXPORT QByteArray qgetenv(const char *varName); Q_CORE_EXPORT bool qputenv(const char *varName, const QByteArray& value); +Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) Q_DECL_NOEXCEPT; +Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) Q_DECL_NOEXCEPT; + inline int qIntCast(double f) { return int(f); } inline int qIntCast(float f) { return int(f); } diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp index e074aeb3e9..d7733bad02 100644 --- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp +++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp @@ -53,12 +53,27 @@ private slots: void tst_QGetPutEnv::getSetCheck() { - const char* varName = "should_not_exist"; + const char varName[] = "should_not_exist"; + + QVERIFY(!qEnvironmentVariableIsSet(varName)); + QVERIFY(qEnvironmentVariableIsEmpty(varName)); QByteArray result = qgetenv(varName); QCOMPARE(result, QByteArray()); + +#ifndef Q_OS_WIN + QVERIFY(qputenv(varName, "")); // deletes varName instead of making it empty, on Windows + + QVERIFY(qEnvironmentVariableIsSet(varName)); + QVERIFY(qEnvironmentVariableIsEmpty(varName)); +#endif + QVERIFY(qputenv(varName, QByteArray("supervalue"))); + + QVERIFY(qEnvironmentVariableIsSet(varName)); + QVERIFY(!qEnvironmentVariableIsEmpty(varName)); result = qgetenv(varName); QVERIFY(result == "supervalue"); + qputenv(varName,QByteArray()); } |