cmCLocaleEnvironmentScope.cxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCLocaleEnvironmentScope.h"
  4. #include "cmSystemTools.h"
  5. #include <sstream>
  6. #include <utility>
  7. cmCLocaleEnvironmentScope::cmCLocaleEnvironmentScope()
  8. {
  9. this->SetEnv("LANGUAGE", "");
  10. this->SetEnv("LC_MESSAGES", "C");
  11. std::string lcAll = this->GetEnv("LC_ALL");
  12. if (!lcAll.empty()) {
  13. this->SetEnv("LC_ALL", "");
  14. this->SetEnv("LC_CTYPE", lcAll);
  15. }
  16. }
  17. std::string cmCLocaleEnvironmentScope::GetEnv(std::string const& key)
  18. {
  19. std::string value;
  20. cmSystemTools::GetEnv(key, value);
  21. return value;
  22. }
  23. void cmCLocaleEnvironmentScope::SetEnv(std::string const& key,
  24. std::string const& value)
  25. {
  26. std::string oldValue = this->GetEnv(key);
  27. this->EnvironmentBackup.insert(std::make_pair(key, oldValue));
  28. if (value.empty()) {
  29. cmSystemTools::UnsetEnv(key.c_str());
  30. } else {
  31. std::ostringstream tmp;
  32. tmp << key << "=" << value;
  33. cmSystemTools::PutEnv(tmp.str());
  34. }
  35. }
  36. cmCLocaleEnvironmentScope::~cmCLocaleEnvironmentScope()
  37. {
  38. for (auto const& envb : this->EnvironmentBackup) {
  39. std::ostringstream tmp;
  40. tmp << envb.first << "=" << envb.second;
  41. cmSystemTools::PutEnv(tmp.str());
  42. }
  43. }