comparison src/vamp-hostsdk/Files.cpp @ 472:79a219ba6178

Correct use of multibyte-widechar conversion routines
author Chris Cannam
date Fri, 28 Oct 2016 14:30:26 +0100
parents 35fa4733bc5d
children 0545cd3f1738
comparison
equal deleted inserted replaced
471:59f7c7ae2f64 472:79a219ba6178
123 Files::loadLibrary(string path) 123 Files::loadLibrary(string path)
124 { 124 {
125 void *handle = 0; 125 void *handle = 0;
126 #ifdef _WIN32 126 #ifdef _WIN32
127 #ifdef UNICODE 127 #ifdef UNICODE
128 int len = path.length() + 1; // cannot be more wchars than length in bytes of utf8 string 128 int wlen = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), path.length(), 0, 0);
129 wchar_t *buffer = new wchar_t[len]; 129 if (wlen < 0) {
130 int rv = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), len, buffer, len);
131 if (rv <= 0) {
132 cerr << "Vamp::HostExt: Unable to convert library path \"" 130 cerr << "Vamp::HostExt: Unable to convert library path \""
133 << path << "\" to wide characters " << endl; 131 << path << "\" to wide characters " << endl;
134 delete[] buffer;
135 return handle; 132 return handle;
136 } 133 }
134 wchar_t *buffer = new wchar_t[wlen+1];
135 (void)MultiByteToWideChar(CP_UTF8, 0, path.c_str(), path.length(), buffer, wlen);
136 buffer[wlen] = L'\0';
137 handle = LoadLibrary(buffer); 137 handle = LoadLibrary(buffer);
138 delete[] buffer; 138 delete[] buffer;
139 #else 139 #else
140 handle = LoadLibrary(path.c_str()); 140 handle = LoadLibrary(path.c_str());
141 #endif 141 #endif
216 vector<string> files; 216 vector<string> files;
217 217
218 #ifdef _WIN32 218 #ifdef _WIN32
219 string expression = dir + "\\*." + extension; 219 string expression = dir + "\\*." + extension;
220 #ifdef UNICODE 220 #ifdef UNICODE
221 int len = expression.length() + 1; // cannot be more wchars than length in bytes of utf8 string 221 int wlen = MultiByteToWideChar(CP_UTF8, 0, expression.c_str(), expression.length(), 0, 0);
222 wchar_t *buffer = new wchar_t[len]; 222 if (wlen < 0) {
223 int rv = MultiByteToWideChar(CP_UTF8, 0, expression.c_str(), len, buffer, len);
224 if (rv <= 0) {
225 cerr << "Vamp::HostExt: Unable to convert wildcard path \"" 223 cerr << "Vamp::HostExt: Unable to convert wildcard path \""
226 << expression << "\" to wide characters" << endl; 224 << expression << "\" to wide characters" << endl;
227 delete[] buffer;
228 return files; 225 return files;
229 } 226 }
227 wchar_t *buffer = new wchar_t[wlen+1];
228 (void)MultiByteToWideChar(CP_UTF8, 0, expression.c_str(), expression.length(), buffer, wlen);
229 buffer[wlen] = L'\0';
230 WIN32_FIND_DATA data; 230 WIN32_FIND_DATA data;
231 HANDLE fh = FindFirstFile(buffer, &data); 231 HANDLE fh = FindFirstFile(buffer, &data);
232 if (fh == INVALID_HANDLE_VALUE) { 232 if (fh == INVALID_HANDLE_VALUE) {
233 delete[] buffer; 233 delete[] buffer;
234 return files; 234 return files;
235 } 235 }
236 236
237 bool ok = true; 237 bool ok = true;
238 while (ok) { 238 while (ok) {
239 wchar_t *fn = data.cFileName; 239 wchar_t *fn = data.cFileName;
240 int wlen = wcslen(fn) + 1; 240 int wlen = wcslen(fn);
241 int maxlen = wlen * 6; 241 int len = WideCharToMultiByte(CP_UTF8, 0, fn, wlen, 0, 0, 0, 0);
242 char *conv = new char[maxlen]; 242 if (len < 0) {
243 int rv = WideCharToMultiByte(CP_UTF8, 0, fn, wlen, conv, maxlen, 0, 0); 243 cerr << "Vamp::HostExt: Unable to convert wide char filename to utf-8" << endl;
244 if (rv > 0) { 244 break;
245 }
246 char *conv = new char[len+1];
247 (void)WideCharToMultiByte(CP_UTF8, 0, fn, wlen, conv, len, 0, 0);
248 conv[len] = '\0';
249 if (len > 0) {
245 files.push_back(conv); 250 files.push_back(conv);
246 } 251 }
247 delete[] conv; 252 delete[] conv;
248 ok = FindNextFile(fh, &data); 253 ok = FindNextFile(fh, &data);
249 } 254 }