Mercurial > hg > svcore
comparison system/System.cpp @ 1475:bd1a2cacd1e7 plugin-path-config
Add provisional utf-8 getenv/putenv implementations
author | Chris Cannam |
---|---|
date | Thu, 07 Jun 2018 15:35:04 +0100 |
parents | c7e68755c7ec |
children | b60cb1637634 |
comparison
equal
deleted
inserted
replaced
1474:dcff44a76573 | 1475:bd1a2cacd1e7 |
---|---|
2 | 2 |
3 /* | 3 /* |
4 Sonic Visualiser | 4 Sonic Visualiser |
5 An audio file viewer and annotation editor. | 5 An audio file viewer and annotation editor. |
6 Centre for Digital Music, Queen Mary, University of London. | 6 Centre for Digital Music, Queen Mary, University of London. |
7 This file copyright 2006 Chris Cannam and QMUL. | 7 This file copyright 2006-2018 Chris Cannam and QMUL. |
8 | 8 |
9 This program is free software; you can redistribute it and/or | 9 This program is free software; you can redistribute it and/or |
10 modify it under the terms of the GNU General Public License as | 10 modify it under the terms of the GNU General Public License as |
11 published by the Free Software Foundation; either version 2 of the | 11 published by the Free Software Foundation; either version 2 of the |
12 License, or (at your option) any later version. See the file | 12 License, or (at your option) any later version. See the file |
61 { | 61 { |
62 ::Sleep(usec / 1000); | 62 ::Sleep(usec / 1000); |
63 } | 63 } |
64 #endif | 64 #endif |
65 | 65 |
66 int gettimeofday(struct timeval *tv, void *tz) | 66 int gettimeofday(struct timeval *tv, void * /* tz */) |
67 { | 67 { |
68 union { | 68 union { |
69 long long ns100; | 69 long long ns100; |
70 FILETIME ft; | 70 FILETIME ft; |
71 } now; | 71 } now; |
326 float modf(float x, float y) { return x - (y * floorf(x / y)); } | 326 float modf(float x, float y) { return x - (y * floorf(x / y)); } |
327 | 327 |
328 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; } | 328 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; } |
329 float princargf(float a) { return float(princarg(a)); } | 329 float princargf(float a) { return float(princarg(a)); } |
330 | 330 |
331 | 331 bool |
332 getEnvUtf8(std::string variable, std::string &value) | |
333 { | |
334 value = ""; | |
335 | |
336 #ifdef _WIN32 | |
337 int wvarlen = MultiByteToWideChar(CP_UTF8, 0, | |
338 variable.c_str(), int(variable.length()), | |
339 0, 0); | |
340 if (wvarlen < 0) { | |
341 SVCERR << "WARNING: Unable to convert environment variable name " | |
342 << variable << " to wide characters" << endl; | |
343 return false; | |
344 } | |
345 | |
346 wchar_t *wvarbuf = new wchar_t[wvarlen + 1]; | |
347 (void)MultiByteToWideChar(CP_UTF8, 0, | |
348 variable.c_str(), int(variable.length()), | |
349 wvarbuf, wvarlen); | |
350 wvarbuf[wvarlen] = L'\0'; | |
351 | |
352 wchar_t *wvalue = _wgetenv(wvarbuf); | |
353 | |
354 delete[] wvarbuf; | |
355 | |
356 if (!wvalue) { | |
357 return false; | |
358 } | |
359 | |
360 int wvallen = int(wcslen(wvarbuf)); | |
361 int vallen = WideCharToMultiByte(CP_UTF8, 0, | |
362 wvarbuf, wvallen, | |
363 0, 0, 0, 0); | |
364 if (vallen < 0) { | |
365 SVCERR << "WARNING: Unable to convert environment value to UTF-8" | |
366 << endl; | |
367 return false; | |
368 } | |
369 | |
370 char *val = new char[vallen + 1]; | |
371 (void)WideCharToMultiByte(CP_UTF8, 0, | |
372 wvarbuf, wvallen, | |
373 val, vallen, 0, 0); | |
374 val[vallen] = '\0'; | |
375 | |
376 value = val; | |
377 | |
378 delete[] val; | |
379 return true; | |
380 | |
381 #else | |
382 | |
383 char *val = getenv(variable.c_str()); | |
384 if (!val) { | |
385 return false; | |
386 } | |
387 | |
388 value = val; | |
389 return true; | |
390 | |
391 #endif | |
392 } | |
393 | |
394 bool | |
395 putEnvUtf8(std::string variable, std::string value) | |
396 { | |
397 std::string entry = variable + "=" + value; | |
398 | |
399 #ifdef _WIN32 | |
400 int wentlen = MultiByteToWideChar(CP_UTF8, 0, | |
401 entry.c_str(), int(entry.length()), | |
402 0, 0); | |
403 if (wentlen < 0) { | |
404 SVCERR << "WARNING: Unable to convert environment entry to " | |
405 << "wide characters" << endl; | |
406 return false; | |
407 } | |
408 | |
409 wchar_t *wentbuf = new wchar_t[wentlen + 1]; | |
410 (void)MultiByteToWideChar(CP_UTF8, 0, | |
411 entry.c_str(), int(entry.length()), | |
412 wentbuf, wentlen); | |
413 wentbuf[wentlen] = L'\0'; | |
414 | |
415 int rv = _wputenv(wentbuf); | |
416 | |
417 delete[] wentbuf; | |
418 | |
419 if (rv != 0) { | |
420 SVCERR << "WARNING: Failed to set environment entry" << endl; | |
421 return false; | |
422 } | |
423 return true; | |
424 | |
425 #else | |
426 | |
427 int rv = putenv(entry.c_str()); | |
428 | |
429 if (rv != 0) { | |
430 SVCERR << "WARNING: Failed to set environment entry" << endl; | |
431 return false; | |
432 } | |
433 return true; | |
434 | |
435 #endif | |
436 } | |
437 |