File: env.hh

package info (click to toggle)
sdformat 12.3.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,980 kB
  • sloc: cpp: 54,706; python: 3,729; javascript: 704; ruby: 366; sh: 97; ansic: 30; makefile: 16
file content (139 lines) | stat: -rw-r--r-- 3,557 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2021 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Note: this is a direct copy from ignition::common,
// and can be removed when sdformat has common as a dependency

#ifndef SDF_TEST_ENV_HH
#define SDF_TEST_ENV_HH

#include <cstdlib>
#include <string>

#ifdef _WIN32
#include <windows.h>
#endif

namespace sdf
{
  namespace testing
  {

    /// \brief Find the environment variable '_name' and return its value.
    ///
    /// Note: the intention is to put this in ign-util, and remove it
    /// from sdformat once the depcnency is in place
    ///
    /// \param[in] _name Name of the environment variable.
    /// \param[out] _value Value if the variable was found.
    /// \param[in] _allowEmpty Allow set-but-empty variables.
    ///           (Unsupported on Windows)
    /// \return True if the variable was found or false otherwise.
    bool env(const std::string &_name,
             std::string &_value,
             bool _allowEmpty)
    {
      std::string v;
      bool valid = false;
#ifdef _WIN32
      // Unused on Windows, suppress warning
      (void) _allowEmpty;
      const DWORD buffSize = 32767;
      static char buffer[buffSize];
      if (GetEnvironmentVariable(_name.c_str(), buffer, buffSize))
      {
        v = buffer;
      }

      if (!v.empty())
      {
        valid = true;
      }

#else
      const char *cvar = std::getenv(_name.c_str());
      if (cvar != nullptr)
      {
        v = cvar;
        valid = true;

        if (v[0] == '\0' && !_allowEmpty)
        {
          valid = false;
        }
      }
#endif
      if (valid)
      {
        _value = v;
        return true;
      }
      return false;
    }

    bool env(const std::string &_name, std::string &_value)
    {
      return env(_name, _value, false);
    }

    /// \brief Set the environment variable '_name'.
    ///
    /// Note that on Windows setting an empty string (_value=="")
    /// is the equivalent of unsetting the variable.
    ///
    /// \param[in] _name Name of the environment variable.
    /// \param[in] _value Value of the variable to be set.
    /// \return True if the variable was set or false otherwise.
    bool setenv(const std::string &_name,
                const std::string &_value)
    {
#ifdef _WIN32
      if (0 != _putenv_s(_name.c_str(), _value.c_str()))
      {
        return false;
      }
#else
      if (0 != ::setenv(_name.c_str(), _value.c_str(), true))
      {
        return false;
      }
#endif
      return true;
    }

    /// \brief Unset the environment variable '_name'.
    /// \param[in] _name Name of the environment variable.
    /// \return True if the variable was unset or false otherwise.
    bool unsetenv(const std::string &_name)
    {
#ifdef _WIN32
      if (0 != _putenv_s(_name.c_str(), ""))
      {
        return false;
      }
#else
      if (0 != ::unsetenv(_name.c_str()))
      {
        return false;
      }
#endif
      return true;
    }
  }  // namespace testing
}  // namespace sdf

#endif