2 * Copyright (C) 2013-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
10 * \file platform\Environment.cpp
11 * \brief Implements CEnvironment class functions.
13 * Some ideas were inspired by PostgreSQL's pgwin32_putenv function.
14 * Refined, updated, enhanced and modified for XBMC by Karlson2k.
17 #include "Environment.h"
22 // --------------------- Main Functions ---------------------
24 int CEnvironment::setenv(const std::string
&name
, const std::string
&value
, int overwrite
/*= 1*/)
27 return (win_setenv(name
, value
, overwrite
? autoDetect
: addOnly
) == 0) ? 0 : -1;
29 if (value
.empty() && overwrite
!= 0)
30 return ::unsetenv(name
.c_str());
31 return ::setenv(name
.c_str(), value
.c_str(), overwrite
);
35 std::string
CEnvironment::getenv(const std::string
&name
)
38 return win_getenv(name
);
40 char * str
= ::getenv(name
.c_str());
47 int CEnvironment::unsetenv(const std::string
&name
)
50 return (win_setenv(name
, "", deleteVariable
)) == 0 ? 0 : -1;
52 return ::unsetenv(name
.c_str());
56 int CEnvironment::putenv(const std::string
&envstring
)
58 if (envstring
.empty())
60 size_t pos
= envstring
.find('=');
61 if (pos
== 0) // '=' is the first character
63 if (pos
== std::string::npos
)
64 return unsetenv(envstring
);
65 if (pos
== envstring
.length()-1) // '=' is in last position
67 std::string
name(envstring
);
68 name
.erase(name
.length()-1, 1);
69 return unsetenv(name
);
71 std::string
name(envstring
, 0, pos
), value(envstring
, pos
+1);
73 return setenv(name
, value
);