changeset 1480:5ac102155409 plugin-path-config

Environment variable lookup fixes, & use this implementation throughout
author Chris Cannam
date Fri, 08 Jun 2018 11:27:40 +0100
parents ba27edcd6102
children 75fe1c1e003f
files base/ResourceFinder.cpp plugin/DSSIPluginFactory.cpp plugin/LADSPAPluginFactory.cpp plugin/PluginPathSetter.cpp system/System.cpp system/test/TestEnv.h
diffstat 6 files changed, 70 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/base/ResourceFinder.cpp	Thu Jun 07 16:49:09 2018 +0100
+++ b/base/ResourceFinder.cpp	Fri Jun 08 11:27:40 2018 +0100
@@ -35,6 +35,8 @@
 #include <iostream>
 #include <stdexcept>
 
+#include "system/System.h"
+
 /**
    Resource files may be found in three places:
 
@@ -67,10 +69,11 @@
     QStringList list;
 
 #ifdef Q_OS_WIN32
-    char *programFiles = getenv("ProgramFiles");
-    if (programFiles && programFiles[0]) {
+    std::string programFiles;
+    (void)getEnvUtf8("ProgramFiles", programFiles);
+    if (programFiles != "") {
         list << QString("%1/%2/%3")
-            .arg(programFiles)
+            .arg(QString::fromStdString(programFiles))
             .arg(qApp->organizationName())
             .arg(qApp->applicationName());
     } else {
--- a/plugin/DSSIPluginFactory.cpp	Thu Jun 07 16:49:09 2018 +0100
+++ b/plugin/DSSIPluginFactory.cpp	Fri Jun 08 11:27:40 2018 +0100
@@ -39,6 +39,7 @@
 #include "lrdf.h"
 #endif // HAVE_LRDF
 
+using std::string;
 
 DSSIPluginFactory::DSSIPluginFactory() :
     LADSPAPluginFactory()
@@ -206,38 +207,38 @@
 DSSIPluginFactory::getPluginPath()
 {
     std::vector<QString> pathList;
-    std::string path;
+    string path;
 
-    char *cpath = getenv("DSSI_PATH");
-    if (cpath) path = cpath;
+    (void)getEnvUtf8("DSSI_PATH", path);
 
     if (path == "") {
 
         path = DEFAULT_DSSI_PATH;
 
-        char *home = getenv("HOME");
-        if (home) {
-            std::string::size_type f;
-            while ((f = path.find("$HOME")) != std::string::npos &&
+        string home;
+        if (getEnvUtf8("HOME", home)) {
+            string::size_type f;
+            while ((f = path.find("$HOME")) != string::npos &&
                    f < path.length()) {
                 path.replace(f, 5, home);
             }
         }
 
 #ifdef _WIN32
-        const char *pfiles = getenv("ProgramFiles");
-        if (!pfiles) pfiles = "C:\\Program Files";
-        {
-        std::string::size_type f;
-        while ((f = path.find("%ProgramFiles%")) != std::string::npos &&
+        string pfiles;
+        if (!getEnvUtf8("ProgramFiles", pfiles)) {
+            pfiles = "C:\\Program Files";
+        }
+
+        string::size_type f;
+        while ((f = path.find("%ProgramFiles%")) != string::npos &&
                f < path.length()) {
             path.replace(f, 14, pfiles);
         }
-        }
 #endif
     }
 
-    std::string::size_type index = 0, newindex = 0;
+    string::size_type index = 0, newindex = 0;
 
     while ((newindex = path.find(PATH_SEPARATOR, index)) < path.size()) {
         pathList.push_back(path.substr(index, newindex - index).c_str());
@@ -347,7 +348,7 @@
         }
 
         if (category == "") {
-            std::string name = rtd->name;
+            string name = rtd->name;
             if (name.length() > 4 &&
                 name.substr(name.length() - 4) == " VST") {
                 if (descriptor->run_synth || descriptor->run_multiple_synths) {
--- a/plugin/LADSPAPluginFactory.cpp	Thu Jun 07 16:49:09 2018 +0100
+++ b/plugin/LADSPAPluginFactory.cpp	Fri Jun 08 11:27:40 2018 +0100
@@ -40,6 +40,7 @@
 #include "lrdf.h"
 #endif // HAVE_LRDF
 
+using std::string;
 
 LADSPAPluginFactory::LADSPAPluginFactory()
 {
@@ -566,38 +567,38 @@
 LADSPAPluginFactory::getPluginPath()
 {
     std::vector<QString> pathList;
-    std::string path;
+    string path;
 
-    char *cpath = getenv("LADSPA_PATH");
-    if (cpath) path = cpath;
+    (void)getEnvUtf8("DSSI_PATH", path);
 
     if (path == "") {
 
         path = DEFAULT_LADSPA_PATH;
 
-        char *home = getenv("HOME");
-        if (home) {
-            std::string::size_type f;
-            while ((f = path.find("$HOME")) != std::string::npos &&
+        string home;
+        if (getEnvUtf8("HOME", home)) {
+            string::size_type f;
+            while ((f = path.find("$HOME")) != string::npos &&
                    f < path.length()) {
                 path.replace(f, 5, home);
             }
         }
 
 #ifdef _WIN32
-        const char *pfiles = getenv("ProgramFiles");
-        if (!pfiles) pfiles = "C:\\Program Files";
-        {
-        std::string::size_type f;
-        while ((f = path.find("%ProgramFiles%")) != std::string::npos &&
+        string pfiles;
+        if (!getEnvUtf8("ProgramFiles", pfiles)) {
+            pfiles = "C:\\Program Files";
+        }
+
+        string::size_type f;
+        while ((f = path.find("%ProgramFiles%")) != string::npos &&
                f < path.length()) {
             path.replace(f, 14, pfiles);
         }
-        }
 #endif
     }
 
-    std::string::size_type index = 0, newindex = 0;
+    string::size_type index = 0, newindex = 0;
 
     while ((newindex = path.find(PATH_SEPARATOR, index)) < path.size()) {
         pathList.push_back(path.substr(index, newindex - index).c_str());
@@ -734,7 +735,7 @@
         QString category = m_taxonomy[identifier];
         
         if (category == "") {
-            std::string name = rtd->name;
+            string name = rtd->name;
             if (name.length() > 4 &&
                 name.substr(name.length() - 4) == " VST") {
                 category = "VST effects";
--- a/plugin/PluginPathSetter.cpp	Thu Jun 07 16:49:09 2018 +0100
+++ b/plugin/PluginPathSetter.cpp	Fri Jun 08 11:27:40 2018 +0100
@@ -23,6 +23,8 @@
 #include <QSettings>
 #include <QMutexLocker>
 
+#include "system/System.h"
+
 QMutex
 PluginPathSetter::m_mutex;
 
@@ -35,6 +37,8 @@
 std::map<QString, QString>
 PluginPathSetter::m_originalEnvValues;
 
+using std::string;
+
 PluginPathSetter::Paths
 PluginPathSetter::getEnvironmentPathsUncached()
 {
@@ -75,20 +79,21 @@
     if (!m_defaultPaths.empty()) {
         return m_defaultPaths;
     }
-        
-    QString savedPathVamp = qEnvironmentVariable("VAMP_PATH");
-    QString savedPathDssi = qEnvironmentVariable("DSSI_PATH");
-    QString savedPathLadspa = qEnvironmentVariable("LADSPA_PATH");
 
-    qunsetenv("VAMP_PATH");
-    qunsetenv("DSSI_PATH");
-    qunsetenv("LADSPA_PATH");
+    string savedPathVamp, savedPathDssi, savedPathLadspa;
+    (void)getEnvUtf8("VAMP_PATH", savedPathVamp);
+    (void)getEnvUtf8("DSSI_PATH", savedPathDssi);
+    (void)getEnvUtf8("LADSPA_PATH", savedPathLadspa);
+
+    putEnvUtf8("VAMP_PATH", "");
+    putEnvUtf8("DSSI_PATH", "");
+    putEnvUtf8("LADSPA_PATH", "");
 
     Paths paths = getEnvironmentPathsUncached();
 
-    qputenv("VAMP_PATH", savedPathVamp.toUtf8());
-    qputenv("DSSI_PATH", savedPathDssi.toUtf8());
-    qputenv("LADSPA_PATH", savedPathLadspa.toUtf8());
+    putEnvUtf8("VAMP_PATH", savedPathVamp);
+    putEnvUtf8("DSSI_PATH", savedPathDssi);
+    putEnvUtf8("LADSPA_PATH", savedPathLadspa);
 
     m_defaultPaths = paths;
     return m_defaultPaths;
@@ -131,11 +136,13 @@
             settings.value(QString("use-env-variable-%1").arg(tag),
                            p.second.useEnvVariable)
             .toBool();
-        std::string envVarStr = envVariable.toStdString();
-        
-        QString currentValue = qEnvironmentVariable(envVarStr.c_str());
-        if (currentValue != QString() && useEnvVariable) {
-            directories = currentValue.split(
+
+        string envVarStr = envVariable.toStdString();
+        string currentValue;
+        (void)getEnvUtf8(envVarStr, currentValue);
+
+        if (currentValue != "" && useEnvVariable) {
+            directories = QString::fromStdString(currentValue).split(
 #ifdef Q_OS_WIN
                ";"
 #else
@@ -198,7 +205,7 @@
 
     for (auto p: paths) {
         QString envVariable = p.second.envVariable;
-        std::string envVarStr = envVariable.toStdString();
+        string envVarStr = envVariable.toStdString();
         QString currentValue = qEnvironmentVariable(envVarStr.c_str());
         m_originalEnvValues[envVariable] = currentValue;
         if (currentValue != QString() && p.second.useEnvVariable) {
@@ -213,7 +220,7 @@
 #endif
             ;
         QString proposedValue = p.second.directories.join(separator);
-        qputenv(envVarStr.c_str(), proposedValue.toUtf8());
+        putEnvUtf8(envVarStr, proposedValue.toStdString());
     }
 }
 
--- a/system/System.cpp	Thu Jun 07 16:49:09 2018 +0100
+++ b/system/System.cpp	Fri Jun 08 11:27:40 2018 +0100
@@ -357,9 +357,9 @@
         return false;
     }
 
-    int wvallen = int(wcslen(wvarbuf));
+    int wvallen = int(wcslen(wvalue));
     int vallen = WideCharToMultiByte(CP_UTF8, 0,
-                                     wvarbuf, wvallen,
+                                     wvalue, wvallen,
                                      0, 0, 0, 0);
     if (vallen < 0) {
         SVCERR << "WARNING: Unable to convert environment value to UTF-8"
@@ -369,7 +369,7 @@
 
     char *val = new char[vallen + 1];
     (void)WideCharToMultiByte(CP_UTF8, 0,
-                              wvarbuf, wvallen,
+                              wvalue, wvallen,
                               val, vallen, 0, 0);
     val[vallen] = '\0';
 
--- a/system/test/TestEnv.h	Thu Jun 07 16:49:09 2018 +0100
+++ b/system/test/TestEnv.h	Fri Jun 08 11:27:40 2018 +0100
@@ -47,17 +47,11 @@
         bool rv = getEnvUtf8("PATH", value);
         QCOMPARE(rv, true);
         QVERIFY(value != "");
-    }
-    
-    void roundTripShort()
-    {
-        bool rv = false;
-        rv = putEnvUtf8("XYZABC", "woo");
-        QCOMPARE(rv, true);
-        string value;
-        rv = getEnvUtf8("XYZABC", value);
-        QCOMPARE(rv, true);
-        QCOMPARE(value, "woo");
+        QVERIFY(value.size() > 5); // Not quite but nearly certain,
+                                   // and weeds out an unfortunate
+                                   // case where we accidentally
+                                   // returned the variable's name
+                                   // instead of its value!
     }
     
     void roundTripAsciiAscii()