Mercurial > hg > vamp-plugin-sdk
changeset 512:54277cb679ff
Wide-char environment variable lookup
author | Chris Cannam |
---|---|
date | Fri, 08 Jun 2018 11:25:19 +0100 |
parents | 328cb056da44 |
children | 4b619c5871ee |
files | src/vamp-hostsdk/Files.cpp src/vamp-hostsdk/Files.h src/vamp-hostsdk/PluginHostAdapter.cpp |
diffstat | 3 files changed, 74 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/src/vamp-hostsdk/Files.cpp Fri May 11 17:02:06 2018 +0100 +++ b/src/vamp-hostsdk/Files.cpp Fri Jun 08 11:25:19 2018 +0100 @@ -322,3 +322,66 @@ return files; } + +bool +Files::getEnvUtf8(std::string variable, std::string &value) +{ + value = ""; + +#ifdef _WIN32 + int wvarlen = MultiByteToWideChar(CP_UTF8, 0, + variable.c_str(), int(variable.length()), + 0, 0); + if (wvarlen < 0) { + cerr << "Vamp::HostExt: Unable to convert environment variable name " + << variable << " to wide characters" << endl; + return false; + } + + wchar_t *wvarbuf = new wchar_t[wvarlen + 1]; + (void)MultiByteToWideChar(CP_UTF8, 0, + variable.c_str(), int(variable.length()), + wvarbuf, wvarlen); + wvarbuf[wvarlen] = L'\0'; + + wchar_t *wvalue = _wgetenv(wvarbuf); + + delete[] wvarbuf; + + if (!wvalue) { + return false; + } + + int wvallen = int(wcslen(wvalue)); + int vallen = WideCharToMultiByte(CP_UTF8, 0, + wvalue, wvallen, + 0, 0, 0, 0); + if (vallen < 0) { + cerr << "Vamp::HostExt: Unable to convert environment value to UTF-8" + << endl; + return false; + } + + char *val = new char[vallen + 1]; + (void)WideCharToMultiByte(CP_UTF8, 0, + wvalue, wvallen, + val, vallen, 0, 0); + val[vallen] = '\0'; + + value = val; + + delete[] val; + return true; + +#else + + char *val = getenv(variable.c_str()); + if (!val) { + return false; + } + + value = val; + return true; + +#endif +}
--- a/src/vamp-hostsdk/Files.h Fri May 11 17:02:06 2018 +0100 +++ b/src/vamp-hostsdk/Files.h Fri Jun 08 11:25:19 2018 +0100 @@ -62,6 +62,8 @@ static std::string lcBasename(std::string path); static std::string splicePath(std::string a, std::string b); static std::vector<std::string> listFiles(std::string dir, std::string ext); + + static bool getEnvUtf8(std::string variable, std::string &value); }; #endif
--- a/src/vamp-hostsdk/PluginHostAdapter.cpp Fri May 11 17:02:06 2018 +0100 +++ b/src/vamp-hostsdk/PluginHostAdapter.cpp Fri Jun 08 11:25:19 2018 +0100 @@ -37,6 +37,8 @@ #include <vamp-hostsdk/PluginHostAdapter.h> #include <cstdlib> +#include "Files.h" + #if ( VAMP_SDK_MAJOR_VERSION != 2 || VAMP_SDK_MINOR_VERSION != 7 ) #error Unexpected version of Vamp SDK header included #endif @@ -70,8 +72,7 @@ std::vector<std::string> path; std::string envPath; - char *cpath = getenv("VAMP_PATH"); - if (cpath) envPath = cpath; + (void)Files::getEnvUtf8("VAMP_PATH", envPath); #ifdef _WIN32 #define PATH_SEPARATOR ';' @@ -87,9 +88,8 @@ if (envPath == "") { envPath = DEFAULT_VAMP_PATH; - char *chome = getenv("HOME"); - if (chome) { - std::string home(chome); + std::string home; + if (Files::getEnvUtf8("HOME", home)) { std::string::size_type f; while ((f = envPath.find("$HOME")) != std::string::npos && f < envPath.length()) { @@ -97,9 +97,10 @@ } } #ifdef _WIN32 - char *cpfiles = getenv("ProgramFiles"); - if (!cpfiles) cpfiles = (char *)"C:\\Program Files"; - std::string pfiles(cpfiles); + std::string pfiles; + if (!Files::getEnvUtf8("ProgramFiles", pfiles)) { + pfiles = "C:\\Program Files"; + } std::string::size_type f; while ((f = envPath.find("%ProgramFiles%")) != std::string::npos && f < envPath.length()) {