mirror of https://github.com/KLayout/klayout.git
Enhanced API for environment variable handling
This commit is contained in:
parent
04ba7d3040
commit
293074c2af
|
|
@ -32,19 +32,12 @@
|
||||||
|
|
||||||
#include "tlUnitTest.h"
|
#include "tlUnitTest.h"
|
||||||
#include "tlStream.h"
|
#include "tlStream.h"
|
||||||
|
#include "tlEnv.h"
|
||||||
|
|
||||||
int run_pymodtest (tl::TestBase *_this, const std::string &fn)
|
int run_pymodtest (tl::TestBase *_this, const std::string &fn)
|
||||||
{
|
{
|
||||||
static std::string pypath;
|
std::string pypath = STRINGIFY (PYTHONPATH);
|
||||||
if (pypath.empty ()) {
|
tl::set_env ("PYTHONPATH", pypath);
|
||||||
pypath = "PYTHONPATH=";
|
|
||||||
pypath += STRINGIFY (PYTHONPATH);
|
|
||||||
}
|
|
||||||
#if defined(_WIN32)
|
|
||||||
_putenv (const_cast<char *> (pypath.c_str ()));
|
|
||||||
#else
|
|
||||||
putenv (const_cast<char *> (pypath.c_str ()));
|
|
||||||
#endif
|
|
||||||
tl::info << pypath;
|
tl::info << pypath;
|
||||||
|
|
||||||
std::string fp (tl::testdata ());
|
std::string fp (tl::testdata ());
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include "tlEnv.h"
|
#include "tlEnv.h"
|
||||||
#include "tlString.h"
|
#include "tlString.h"
|
||||||
|
#include "tlThreads.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -38,8 +39,13 @@
|
||||||
namespace tl
|
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)
|
std::string get_env (const std::string &name, const std::string &def_value)
|
||||||
{
|
{
|
||||||
|
tl::MutexLocker env_locker (&s_env_lock);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::wstring wname = tl::to_wstring (name);
|
std::wstring wname = tl::to_wstring (name);
|
||||||
wchar_t *env = _wgetenv (wname.c_str ());
|
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
|
#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)
|
bool has_env (const std::string &name)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,16 @@ namespace tl
|
||||||
*/
|
*/
|
||||||
std::string TL_PUBLIC get_env (const std::string &name, const std::string &def_value = std::string ());
|
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
|
* @brief Gets the value if the given environment variable is set
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,7 @@ SOURCES = \
|
||||||
tlDataMappingTests.cc \
|
tlDataMappingTests.cc \
|
||||||
tlDeferredExecutionTests.cc \
|
tlDeferredExecutionTests.cc \
|
||||||
tlDeflateTests.cc \
|
tlDeflateTests.cc \
|
||||||
|
tlEnvTests.cc \
|
||||||
tlEventsTests.cc \
|
tlEventsTests.cc \
|
||||||
tlExpressionTests.cc \
|
tlExpressionTests.cc \
|
||||||
tlFileSystemWatcherTests.cc \
|
tlFileSystemWatcherTests.cc \
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue