resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmCLocaleEnvironmentScope.cxx
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
5 #include <sstream>
6 #include <utility>
7
8 #include "cmSystemTools.h"
9
10 cmCLocaleEnvironmentScope::cmCLocaleEnvironmentScope()
11 {
12   this->SetEnv("LANGUAGE", "");
13   this->SetEnv("LC_MESSAGES", "C");
14
15   std::string lcAll = this->GetEnv("LC_ALL");
16
17   if (!lcAll.empty()) {
18     this->SetEnv("LC_ALL", "");
19     this->SetEnv("LC_CTYPE", lcAll);
20   }
21 }
22
23 std::string cmCLocaleEnvironmentScope::GetEnv(std::string const& key)
24 {
25   std::string value;
26   cmSystemTools::GetEnv(key, value);
27   return value;
28 }
29
30 void cmCLocaleEnvironmentScope::SetEnv(std::string const& key,
31                                        std::string const& value)
32 {
33   std::string oldValue = this->GetEnv(key);
34
35   this->EnvironmentBackup.insert(std::make_pair(key, oldValue));
36
37   if (value.empty()) {
38     cmSystemTools::UnsetEnv(key.c_str());
39   } else {
40     std::ostringstream tmp;
41     tmp << key << "=" << value;
42     cmSystemTools::PutEnv(tmp.str());
43   }
44 }
45
46 cmCLocaleEnvironmentScope::~cmCLocaleEnvironmentScope()
47 {
48   for (auto const& envb : this->EnvironmentBackup) {
49     std::ostringstream tmp;
50     tmp << envb.first << "=" << envb.second;
51     cmSystemTools::PutEnv(tmp.str());
52   }
53 }