changeset 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 dcff44a76573
children 5afbac960a30
files system/System.cpp system/System.h
diffstat 2 files changed, 123 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/system/System.cpp	Wed Jun 06 15:55:34 2018 +0100
+++ b/system/System.cpp	Thu Jun 07 15:35:04 2018 +0100
@@ -4,7 +4,7 @@
   Sonic Visualiser
   An audio file viewer and annotation editor.
   Centre for Digital Music, Queen Mary, University of London.
-  This file copyright 2006 Chris Cannam and QMUL.
+  This file copyright 2006-2018 Chris Cannam and QMUL.
     
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
@@ -63,7 +63,7 @@
     }
 #endif
 
-    int gettimeofday(struct timeval *tv, void *tz)
+    int gettimeofday(struct timeval *tv, void * /* tz */)
     {
         union { 
             long long ns100;  
@@ -328,4 +328,110 @@
 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
 float princargf(float a) { return float(princarg(a)); }
 
+bool
+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) {
+        SVCERR << "WARNING: 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(wvarbuf));
+    int vallen = WideCharToMultiByte(CP_UTF8, 0,
+                                     wvarbuf, wvallen,
+                                     0, 0, 0, 0);
+    if (vallen < 0) {
+        SVCERR << "WARNING: Unable to convert environment value to UTF-8"
+               << endl;
+        return false;
+    }
+
+    char *val = new char[vallen + 1];
+    (void)WideCharToMultiByte(CP_UTF8, 0,
+                              wvarbuf, 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
+}
+
+bool
+putEnvUtf8(std::string variable, std::string value)
+{
+    std::string entry = variable + "=" + value;
+    
+#ifdef _WIN32
+    int wentlen = MultiByteToWideChar(CP_UTF8, 0,
+                                      entry.c_str(), int(entry.length()),
+                                      0, 0);
+    if (wentlen < 0) {
+        SVCERR << "WARNING: Unable to convert environment entry to "
+               << "wide characters" << endl;
+        return false;
+    }
+    
+    wchar_t *wentbuf = new wchar_t[wentlen + 1];
+    (void)MultiByteToWideChar(CP_UTF8, 0,
+                              entry.c_str(), int(entry.length()),
+                              wentbuf, wentlen);
+    wentbuf[wentlen] = L'\0';
+
+    int rv = _wputenv(wentbuf);
+
+    delete[] wentbuf;
+
+    if (rv != 0) {
+        SVCERR << "WARNING: Failed to set environment entry" << endl;
+        return false;
+    }
+    return true;
+
+#else
+
+    int rv = putenv(entry.c_str());
+
+    if (rv != 0) {
+        SVCERR << "WARNING: Failed to set environment entry" << endl;
+        return false;
+    }
+    return true;
+    
+#endif
+}
+
--- a/system/System.h	Wed Jun 06 15:55:34 2018 +0100
+++ b/system/System.h	Thu Jun 07 15:35:04 2018 +0100
@@ -4,7 +4,7 @@
     Sonic Visualiser
     An audio file viewer and annotation editor.
     Centre for Digital Music, Queen Mary, University of London.
-    This file copyright 2006 Chris Cannam and QMUL.
+    This file copyright 2006-2018 Chris Cannam and QMUL.
     
     This program is free software; you can redistribute it and/or
     modify it under the terms of the GNU General Public License as
@@ -13,8 +13,8 @@
     COPYING included with this distribution for more information.
 */
 
-#ifndef _SYSTEM_H_
-#define _SYSTEM_H_
+#ifndef SV_SYSTEM_H
+#define SV_SYSTEM_H
 
 #include "base/Debug.h"
 
@@ -182,6 +182,18 @@
 #define powf pow
 #endif
 
+/** Return the value of the given environment variable by reference.
+    Return true if successfully retrieved, false if unset or on error.
+    Both the variable name and the returned value are UTF-8 encoded.
+*/
+extern bool getEnvUtf8(std::string variable, std::string &value);
+
+/** Set the value of the given environment variable.
+    Return true if successfully set, false on error.
+    Both the variable name and the value must be UTF-8 encoded.
+*/
+extern bool putEnvUtf8(std::string variable, std::string value);
+
 #endif /* ! _SYSTEM_H_ */