KiCad PCB EDA Suite
Loading...
Searching...
No Matches
env_var_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/utils.h>
23
24#include <pgm_base.h>
25
26
28 const std::optional<wxString>& aValue ) :
29 m_name( aName )
30{
31 // Store the old value if it exists
32 wxString oldValue;
33 if( wxGetEnv( aName, &oldValue ) )
34 m_oldValue = oldValue;
35
36 // Set/remove the variable for the lifetime of this object
37 if( aValue )
38 wxSetEnv( aName, *aValue );
39 else
40 wxUnsetEnv( aName );
41}
42
43
45{
46 if( m_oldValue )
47 wxSetEnv( m_name, *m_oldValue );
48 else
49 wxUnsetEnv( m_name );
50}
51
52
53void KI_TEST::SCOPED_PROCESS_ENV_VAR::SetValue( const wxString& aValue )
54{
55 wxSetEnv( m_name, aValue );
56}
57
58
59std::optional<wxString> KI_TEST::SCOPED_PROCESS_ENV_VAR::GetValue() const
60{
61 wxString value;
62 if( wxGetEnv( m_name, &value ) )
63 return value;
64 else
65 return std::nullopt;
66}
67
68
70{
71 wxUnsetEnv( m_name );
72}
73
74
75KI_TEST::SCOPED_PGM_ENV_VAR::SCOPED_PGM_ENV_VAR( const wxString& aName, const std::optional<wxString>& aValue ) :
76 m_pgmEnvVars( Pgm().GetLocalEnvVariables() ),
77 m_name( aName )
78{
79 const auto it = m_pgmEnvVars.find( aName );
80
81 // Store the old value if it exists
82 if( it != m_pgmEnvVars.end() )
83 m_oldValue = it->second;
84
85 // Set/remove the variable for the lifetime of this object
86 if( aValue )
87 m_pgmEnvVars[aName] = ENV_VAR_ITEM( aName, *aValue );
88 else
89 m_pgmEnvVars.erase( aName );
90}
91
92
100
101
106
107
109{
110 return m_pgmEnvVars.at( m_name );
111}
KiCad uses environment variables internally for determining the base paths for libraries,...
std::optional< ENV_VAR_ITEM > m_oldValue
ENV_VAR_ITEM & GetItem()
Get the wrapped environment variable item.
SCOPED_PGM_ENV_VAR(const wxString &aName, const std::optional< wxString > &aValue)
std::optional< wxString > m_oldValue
std::optional< wxString > GetValue() const
Get the current value of the environment variable, if it exists.
void ClearValue()
Clear the environment variable for the lifetime of this object.
void SetValue(const wxString &aValue)
Set a new value for the environment variable (which will still only be in effect for the lifetime of ...
SCOPED_PROCESS_ENV_VAR(const wxString &aName, const std::optional< wxString > &aValue)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE