comparison src/knownplugins.cpp @ 36:ae74d39e9e4e plugin-path-config

Wide-char environment variable lookup
author Chris Cannam
date Fri, 08 Jun 2018 11:28:51 +0100
parents 4154894d638c
children
comparison
equal deleted inserted replaced
35:4154894d638c 36:ae74d39e9e4e
27 dealings in this Software without prior written authorization. 27 dealings in this Software without prior written authorization.
28 */ 28 */
29 29
30 #include "knownplugins.h" 30 #include "knownplugins.h"
31 31
32 #include <iostream>
33
32 using namespace std; 34 using namespace std;
33 35
34 #if defined(_WIN32) 36 #if defined(_WIN32)
37 #include <windows.h>
35 #define PATH_SEPARATOR ';' 38 #define PATH_SEPARATOR ';'
36 #else 39 #else
37 #define PATH_SEPARATOR ':' 40 #define PATH_SEPARATOR ':'
38 #endif 41 #endif
42
43 static bool
44 getEnvUtf8(std::string variable, std::string &value)
45 {
46 value = "";
47
48 #ifdef _WIN32
49 int wvarlen = MultiByteToWideChar(CP_UTF8, 0,
50 variable.c_str(), int(variable.length()),
51 0, 0);
52 if (wvarlen < 0) {
53 cerr << "WARNING: Unable to convert environment variable name "
54 << variable << " to wide characters" << endl;
55 return false;
56 }
57
58 wchar_t *wvarbuf = new wchar_t[wvarlen + 1];
59 (void)MultiByteToWideChar(CP_UTF8, 0,
60 variable.c_str(), int(variable.length()),
61 wvarbuf, wvarlen);
62 wvarbuf[wvarlen] = L'\0';
63
64 wchar_t *wvalue = _wgetenv(wvarbuf);
65
66 delete[] wvarbuf;
67
68 if (!wvalue) {
69 return false;
70 }
71
72 int wvallen = int(wcslen(wvalue));
73 int vallen = WideCharToMultiByte(CP_UTF8, 0,
74 wvalue, wvallen,
75 0, 0, 0, 0);
76 if (vallen < 0) {
77 cerr << "WARNING: Unable to convert environment value to UTF-8" << endl;
78 return false;
79 }
80
81 char *val = new char[vallen + 1];
82 (void)WideCharToMultiByte(CP_UTF8, 0,
83 wvalue, wvallen,
84 val, vallen, 0, 0);
85 val[vallen] = '\0';
86
87 value = val;
88
89 delete[] val;
90 return true;
91
92 #else
93
94 char *val = getenv(variable.c_str());
95 if (!val) {
96 return false;
97 }
98
99 value = val;
100 return true;
101
102 #endif
103 }
39 104
40 KnownPlugins::KnownPlugins(BinaryFormat format) : 105 KnownPlugins::KnownPlugins(BinaryFormat format) :
41 m_format(format) 106 m_format(format)
42 { 107 {
43 string variableSuffix = ""; 108 string variableSuffix = "";
128 193
129 if (path == "") { 194 if (path == "") {
130 return path; 195 return path;
131 } 196 }
132 197
133 char *home = getenv("HOME"); 198 string home;
134 if (home) { 199 if (getEnvUtf8("HOME", home)) {
135 string::size_type f; 200 string::size_type f;
136 while ((f = path.find("$HOME")) != string::npos && 201 while ((f = path.find("$HOME")) != string::npos &&
137 f < path.length()) { 202 f < path.length()) {
138 path.replace(f, 5, home); 203 path.replace(f, 5, home);
139 } 204 }
140 } 205 }
141 206
142 #ifdef _WIN32 207 #ifdef _WIN32
143 const char *pfiles = 0; 208 string pfiles, pfiles32;
144 const char *pfiles32 = 0; 209 if (!getEnvUtf8("ProgramFiles", pfiles)) {
145
146 pfiles = getenv("ProgramFiles");
147 if (!pfiles) {
148 pfiles = "C:\\Program Files"; 210 pfiles = "C:\\Program Files";
149 } 211 }
150 212 if (!getEnvUtf8("ProgramFiles(x86)", pfiles32)) {
151 pfiles32 = getenv("ProgramFiles(x86)");
152 if (!pfiles32) {
153 pfiles32 = "C:\\Program Files (x86)"; 213 pfiles32 = "C:\\Program Files (x86)";
154 } 214 }
155 215
156 string::size_type f; 216 string::size_type f;
157 while ((f = path.find("%ProgramFiles%")) != string::npos && 217 while ((f = path.find("%ProgramFiles%")) != string::npos &&
187 vector<string> 247 vector<string>
188 KnownPlugins::expandConventionalPath(PluginType type, string var) 248 KnownPlugins::expandConventionalPath(PluginType type, string var)
189 { 249 {
190 string path; 250 string path;
191 251
192 char *cpath = getenv(var.c_str()); 252 if (!getEnvUtf8(var, path)) {
193 if (cpath) path = cpath;
194 if (path == "") {
195 path = getDefaultPathString(type); 253 path = getDefaultPathString(type);
196 } 254 }
197 255
198 return expandPathString(path); 256 return expandPathString(path);
199 } 257 }