szaszm commented on code in PR #1895:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1895#discussion_r1981486375


##########
utils/src/utils/Environment.cpp:
##########
@@ -123,4 +130,49 @@ bool Environment::isRunningAsService() {
   return runningAsService;
 }
 
+std::unordered_map<std::string, std::string> 
Environment::getEnvironmentVariables() {
+  std::unordered_map<std::string, std::string> env_var_map;
+
+#ifdef WIN32
+  LPWCH env_strings = GetEnvironmentStringsW();
+  if (!env_strings) {
+    return env_var_map;
+  }
+
+  LPWCH env = env_strings;
+
+  while (*env) {
+    std::wstring wstring_variable_key_value_pair(env);
+
+    int size_needed = WideCharToMultiByte(CP_UTF8, 0, 
wstring_variable_key_value_pair.c_str(), -1, nullptr, 0, nullptr, nullptr);
+    std::vector<char> buffer(size_needed);
+    WideCharToMultiByte(CP_UTF8, 0, wstring_variable_key_value_pair.c_str(), 
-1, buffer.data(), size_needed, nullptr, nullptr);
+
+    std::string variable_key_value_pair(buffer.data(), buffer.size());
+    size_t pos = variable_key_value_pair.find('=');
+    if (pos != std::string::npos) {
+      std::string key = variable_key_value_pair.substr(0, pos);
+      std::string value = variable_key_value_pair.substr(pos + 1);
+      env_var_map[key] = value;
+    }
+
+    env += wcslen(env) + 1;
+  }
+
+  FreeEnvironmentStringsW(env_strings);
+#else
+  for (char **env = environ; *env != nullptr; ++env) {

Review Comment:
   Accessing the environment is not thread safe. This class has a mutex that 
could maybe work if all reads and write use it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to