| 3003 | namespace platform { |
| 3004 | |
| 3005 | bool getEnvironmentVar(const char* name, std::string& value) { |
| 3006 | #if defined(__unixish__) |
| 3007 | char* val = getenv(name); |
| 3008 | if (val) { |
| 3009 | value = std::string(val); |
| 3010 | return true; |
| 3011 | } |
| 3012 | return false; |
| 3013 | #elif defined(_WIN32) |
| 3014 | int len = GetEnvironmentVariable(name, nullptr, 0); |
| 3015 | if (len == 0) { |
| 3016 | if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
| 3017 | return false; |
| 3018 | } |
| 3019 | TraceEvent(SevError, "GetEnvironmentVariable").detail("Name", name).GetLastError(); |
| 3020 | throw platform_error(); |
| 3021 | } |
| 3022 | value.resize(len); |
| 3023 | int rc = GetEnvironmentVariable(name, &value[0], len); |
| 3024 | if (rc + 1 != len) { |
| 3025 | TraceEvent(SevError, "WrongEnvVarLength").detail("ExpectedLength", len).detail("ReceivedLength", rc + 1); |
| 3026 | throw platform_error(); |
| 3027 | } |
| 3028 | value.resize(len - 1); |
| 3029 | return true; |
| 3030 | #else |
| 3031 | #error Port me! |
| 3032 | #endif |
| 3033 | } |
| 3034 | |
| 3035 | int setEnvironmentVar(const char* name, const char* value, int overwrite) { |
| 3036 | #if defined(_WIN32) |
no test coverage detected