Kodi Documentation 22.0
Kodi is an open source media player and entertainment hub.
Loading...
Searching...
No Matches
CEnvironment Class Reference

Platform-independent environment variables manipulations. More...

#include <Environment.h>

Static Public Member Functions

static int setenv (const std::string &name, const std::string &value, int overwrite=1)
 Sets or unsets environment variable.
 
static int unsetenv (const std::string &name)
 Deletes environment variable.
 
static int putenv (const std::string &envstring)
 Adds/modifies/deletes environment variable.
 
static std::string getenv (const std::string &name)
 Gets value of environment variable in UTF-8 encoding.
 

Detailed Description

Platform-independent environment variables manipulations.

Provide analog for POSIX functions:

  • setenv
  • unsetenv
  • putenv
  • getenv

You can generally use the functions as you would normally in POSIX-style. The differences below are just to make things more convenient through use of std::string (2,3), and to also allow the Win32-style of unsetting variables (4,5) if wanted.

  1. CEnvironment::setenv parameter 'overwrite' is optional, set by default to 1 (allow overwrite).
  2. CEnvironment::putenv uses copy of provided string (rather than string itself) to change environment, so you can free parameter variable right after call of function.
  3. CEnvironment::getenv returns a copy of environment variable value instead of pointer to value.
  4. CEnvironment::setenv can be used to unset variables. Just pass empty string for 'value' parameter.
  5. CEnvironment::putenv can be used to unset variables. Set parameter to 'var=' (Windows style) or just 'var' (POSIX style), and 'var' will be unset.

All 'std::string' types are supposed to be in UTF-8 encoding. All functions work on all platforms. Special care is taken on Windows platform where Environment is changed for process itself, for process runtime library and for all runtime libraries (MSVCRT) loaded by third-party modules. Functions internally make all necessary UTF-8 <-> wide conversions.*

Member Function Documentation

◆ getenv()

static std::string CEnvironment::getenv ( const std::string & name)
static

Gets value of environment variable in UTF-8 encoding.

Parameters
nameThe name of environment variable.
Returns
Copy of of environment variable value or empty string if variable in not present in environment.
See also
xbmc_getenvUtf8, xbmc_getenvW

◆ putenv()

static int CEnvironment::putenv ( const std::string & envstring)
static

Adds/modifies/deletes environment variable.

Parameters
envstringThe variable-value string in form 'var=value'. If set to 'var=' or 'var', then variable will be deleted from the environment.
Returns
Zero on success, non-zero on error.

◆ setenv()

static int CEnvironment::setenv ( const std::string & name,
const std::string & value,
int overwrite = 1 )
static

Sets or unsets environment variable.

Parameters
nameThe environment variable name to add/modify/delete.
valueThe environment variable new value. If set to empty string, variable will be deleted from the environment.
overwrite(optional) If set to non-zero, existing variable will be overwritten. If set to zero and variable is already present, then variable will be unchanged and function returns success.
Returns
Zero on success, non-zero on error.

◆ unsetenv()

static int CEnvironment::unsetenv ( const std::string & name)
static

Deletes environment variable.

Parameters
nameThe environment variable name to delete.
Returns
Zero on success, non-zero on error.

The documentation for this class was generated from the following files: