| 46 | { |
| 47 | |
| 48 | bool Getenv(const char * name, std::string & value) |
| 49 | { |
| 50 | if (!name || !*name) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | #if defined(_WIN32) |
| 56 | // Define working strings, converting to UTF-16 if necessary |
| 57 | #ifdef UNICODE |
| 58 | std::wstring name_str = Utf8ToUtf16(name); |
| 59 | std::wstring value_str; |
| 60 | #else |
| 61 | std::string name_str = name; |
| 62 | std::string value_str; |
| 63 | #endif |
| 64 | |
| 65 | if(uint32_t size = GetEnvironmentVariable(name_str.c_str(), nullptr, 0)) |
| 66 | { |
| 67 | value_str.resize(size); |
| 68 | |
| 69 | GetEnvironmentVariable(name_str.c_str(), &value_str[0], size); |
| 70 | |
| 71 | // GetEnvironmentVariable is designed for raw pointer strings and therefore requires that |
| 72 | // the destination buffer be long enough to place a null terminator at the end of it. Since |
| 73 | // we're using std::wstrings here, the null terminator is unnecessary (and causes false |
| 74 | // negatives in unit tests since the extra character makes it "non-equal" to normally |
| 75 | // defined std::wstrings). Therefore, we pop the last character off (the null terminator) |
| 76 | // to ensure that the string conforms to expectations. |
| 77 | value_str.pop_back(); |
| 78 | |
| 79 | // Return value, converting to UTF-8 if necessary |
| 80 | #ifdef UNICODE |
| 81 | value = Utf16ToUtf8(value_str); |
| 82 | #else |
| 83 | value = value_str; |
| 84 | #endif |
| 85 | return true; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | value.clear(); |
| 90 | return false; |
| 91 | } |
| 92 | #else |
| 93 | const char * val = ::getenv(name); |
| 94 | value = (val && *val) ? val : ""; |
| 95 | return val; // Returns true if the env. variable exists but empty. |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | void Setenv(const char * name, const std::string & value) |
| 100 | { |