Enhanced API for environment variable handling

This commit is contained in:
Matthias Koefferlein 2023-12-07 19:32:19 +01:00
parent 04ba7d3040
commit 293074c2af
5 changed files with 97 additions and 10 deletions

View File

@ -32,19 +32,12 @@
#include "tlUnitTest.h"
#include "tlStream.h"
#include "tlEnv.h"
int run_pymodtest (tl::TestBase *_this, const std::string &fn)
{
static std::string pypath;
if (pypath.empty ()) {
pypath = "PYTHONPATH=";
pypath += STRINGIFY (PYTHONPATH);
}
#if defined(_WIN32)
_putenv (const_cast<char *> (pypath.c_str ()));
#else
putenv (const_cast<char *> (pypath.c_str ()));
#endif
std::string pypath = STRINGIFY (PYTHONPATH);
tl::set_env ("PYTHONPATH", pypath);
tl::info << pypath;
std::string fp (tl::testdata ());

View File

@ -23,6 +23,7 @@
#include "tlEnv.h"
#include "tlString.h"
#include "tlThreads.h"
#include <string>
@ -38,8 +39,13 @@
namespace tl
{
static tl::Mutex s_env_lock;
static std::map<std::string, std::string> s_env_map;
std::string get_env (const std::string &name, const std::string &def_value)
{
tl::MutexLocker env_locker (&s_env_lock);
#ifdef _WIN32
std::wstring wname = tl::to_wstring (name);
wchar_t *env = _wgetenv (wname.c_str ());
@ -58,6 +64,38 @@ std::string get_env (const std::string &name, const std::string &def_value)
#endif
}
void set_env (const std::string &name, const std::string &value)
{
tl::MutexLocker env_locker (&s_env_lock);
s_env_map [name] = name + "=" + value;
const std::string &s = s_env_map [name];
#if defined(_WIN32)
_putenv (const_cast<char *> (s.c_str ()));
#else
putenv (const_cast<char *> (s.c_str ()));
#endif
}
void unset_env (const std::string &name)
{
tl::MutexLocker env_locker (&s_env_lock);
#if defined(_WIN32)
s_env_map [name] = name + "=";
#else
s_env_map [name] = name;
#endif
const std::string &s = s_env_map [name];
#if defined(_WIN32)
_putenv (const_cast<char *> (s.c_str ()));
#else
putenv (const_cast<char *> (s.c_str ()));
#endif
}
bool has_env (const std::string &name)
{
#ifdef _WIN32

View File

@ -38,6 +38,16 @@ namespace tl
*/
std::string TL_PUBLIC get_env (const std::string &name, const std::string &def_value = std::string ());
/**
* @brief Sets the value for the given environment variable
*/
void TL_PUBLIC set_env (const std::string &name, const std::string &value);
/**
* @brief Removes the given environment variable
*/
void TL_PUBLIC unset_env (const std::string &name);
/**
* @brief Gets the value if the given environment variable is set
*/

View File

@ -0,0 +1,45 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2023 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tlEnv.h"
#include "tlUnitTest.h"
const char *dne_name = "__DOES_NOT_EXIST__";
TEST(1)
{
EXPECT_EQ (tl::has_env (dne_name), false);
tl::set_env (dne_name, "123");
EXPECT_EQ (tl::has_env (dne_name), true);
EXPECT_EQ (tl::get_env (dne_name), "123");
tl::set_env (dne_name, "42");
EXPECT_EQ (tl::has_env (dne_name), true);
EXPECT_EQ (tl::get_env (dne_name), "42");
tl::unset_env (dne_name);
EXPECT_EQ (tl::get_env (dne_name, "bla"), "bla");
EXPECT_EQ (tl::has_env (dne_name), false);
}

View File

@ -16,6 +16,7 @@ SOURCES = \
tlDataMappingTests.cc \
tlDeferredExecutionTests.cc \
tlDeflateTests.cc \
tlEnvTests.cc \
tlEventsTests.cc \
tlExpressionTests.cc \
tlFileSystemWatcherTests.cc \