MCPcopy Create free account
hub / github.com/arun11299/cpp-subprocess / MapFromWindowsEnvironment

Function MapFromWindowsEnvironment

cpp-subprocess/subprocess.hpp:441–484  ·  view source on GitHub ↗

env_map_t MapFromWindowsEnvironment() Imports current Environment in a C-string table Parses the strings by splitting on the first "=" per line Creates a map of the variables Returns the map

Source from the content-addressed store, hash-verified

439 // * Creates a map of the variables
440 // * Returns the map
441 inline env_map_t MapFromWindowsEnvironment(){
442 wchar_t *variable_strings_ptr;
443 wchar_t *environment_strings_ptr;
444 std::wstring delimiter(L"=");
445 int del_len = delimiter.length();
446 env_map_t mapped_environment;
447
448 // Get a pointer to the environment block.
449 environment_strings_ptr = GetEnvironmentStringsW();
450 // If the returned pointer is NULL, exit.
451 if (environment_strings_ptr == NULL)
452 {
453 throw OSError("GetEnvironmentStringsW", 0);
454 }
455
456 // Variable strings are separated by NULL byte, and the block is
457 // terminated by a NULL byte.
458
459 variable_strings_ptr = (wchar_t *) environment_strings_ptr;
460
461 //Since the environment map ends with a null, we can loop until we find it.
462 while (*variable_strings_ptr)
463 {
464 // Create a string from Variable String
465 platform_str_t current_line(variable_strings_ptr);
466 // Find the first "equals" sign.
467 auto pos = current_line.find(delimiter);
468 // Assuming it's not missing ...
469 if(pos!=std::wstring::npos){
470 // ... parse the key and value.
471 platform_str_t key = current_line.substr(0, pos);
472 platform_str_t value = current_line.substr(pos + del_len);
473 // Map the entry.
474 mapped_environment[key] = value;
475 }
476 // Jump to next line in the environment map.
477 variable_strings_ptr += std::wcslen(variable_strings_ptr) + 1;
478 }
479 // We're done with the old environment map buffer.
480 FreeEnvironmentStringsW(environment_strings_ptr);
481
482 // Return the map.
483 return mapped_environment;
484 }
485
486 // env_vector_t WindowsEnvironmentVectorFromMap(const env_map_t &source_map)
487 // * Creates a vector buffer for the new environment string table

Callers 1

Calls 1

OSErrorClass · 0.85

Tested by

no test coverage detected