Namespace the env_vars.h functions

This commit is contained in:
Marek Roszko 2021-06-21 20:35:11 -04:00
parent 0f27618125
commit 7802037495
5 changed files with 76 additions and 87 deletions

View File

@ -185,7 +185,7 @@ void DIALOG_CONFIGURE_PATHS::AppendEnvVar( const wxString& aName, const wxString
wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
nameTextEditor->SetValidator( ENV_VAR_NAME_VALIDATOR() );
nameCellAttr->SetEditor( nameTextEditor );
nameCellAttr->SetReadOnly( IsEnvVarImmutable( aName ) );
nameCellAttr->SetReadOnly( ENV_VAR::IsEnvVarImmutable( aName ) );
nameCellAttr->DecRef();
m_EnvVars->SetCellValue( i, TV_VALUE_COL, aPath );
@ -405,7 +405,7 @@ void DIALOG_CONFIGURE_PATHS::OnRemoveEnvVar( wxCommandEvent& event )
if( curRow < 0 || m_EnvVars->GetNumberRows() <= curRow )
return;
else if( IsEnvVarImmutable( m_EnvVars->GetCellValue( curRow, TV_NAME_COL ) ) )
else if( ENV_VAR::IsEnvVarImmutable( m_EnvVars->GetCellValue( curRow, TV_NAME_COL ) ) )
{
wxBell();
return;
@ -586,11 +586,11 @@ void DIALOG_CONFIGURE_PATHS::OnHelp( wxCommandEvent& event )
"will only accept upper case letters, digits, and the underscore characters." );
msg << "</b>";
for( const auto& var: GetPredefinedEnvVars() )
for( const auto& var : ENV_VAR::GetPredefinedEnvVars() )
{
msg << "<br><br><b>" << var << "</b>";
const auto desc = LookUpEnvVarHelp( var );
const auto desc = ENV_VAR::LookUpEnvVarHelp( var );
if( desc.size() > 0 )
msg << ": " << desc;

View File

@ -33,7 +33,7 @@ using STRING_MAP = std::map<wxString, wxString>;
* extract them from elsewhere in the program
* (where they are originally defined)
*/
static const ENV_VAR_LIST predefined_env_vars = {
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = {
"KIPRJMOD",
"KICAD6_SYMBOL_DIR",
"KICAD6_3DMODEL_DIR",
@ -44,9 +44,9 @@ static const ENV_VAR_LIST predefined_env_vars = {
};
bool IsEnvVarImmutable( const wxString& aEnvVar )
bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
{
for( const auto& s: predefined_env_vars )
for( const auto& s : predefinedEnvVars )
{
if( s == aEnvVar )
return true;
@ -56,13 +56,13 @@ bool IsEnvVarImmutable( const wxString& aEnvVar )
}
const ENV_VAR_LIST& GetPredefinedEnvVars()
const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
{
return predefined_env_vars;
return predefinedEnvVars;
}
void initialiseEnvVarHelp( STRING_MAP& aMap )
static void initialiseEnvVarHelp( STRING_MAP& aMap )
{
// Set up dynamically, as we want to be able to use _() translations,
// which can't be done statically
@ -101,21 +101,21 @@ void initialiseEnvVarHelp( STRING_MAP& aMap )
}
wxString LookUpEnvVarHelp( const wxString& aEnvVar )
wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
{
static STRING_MAP env_var_help_text;
static STRING_MAP envVarHelpText;
if( env_var_help_text.size() == 0 )
initialiseEnvVarHelp( env_var_help_text );
if( envVarHelpText.size() == 0 )
initialiseEnvVarHelp( envVarHelpText );
return env_var_help_text[aEnvVar];
return envVarHelpText[ aEnvVar ];
}
template<>
OPT<double> GetEnvVar( const wxString& aEnvVarName )
OPT<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
{
OPT<double> opt_value;
OPT<double> optValue;
wxString env;
if( wxGetEnv( aEnvVarName, &env ) )
@ -123,23 +123,23 @@ OPT<double> GetEnvVar( const wxString& aEnvVarName )
double value;
if( env.ToDouble( &value ) )
{
opt_value = value;
optValue = value;
}
}
return opt_value;
return optValue;
}
template<>
OPT<wxString> GetEnvVar( const wxString& aEnvVarName )
OPT<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
{
OPT<wxString> opt_value;
OPT<wxString> optValue;
wxString env;
if( wxGetEnv( aEnvVarName, &env ) )
{
opt_value = env;
optValue = env;
}
return opt_value;
return optValue;
}

View File

@ -80,7 +80,7 @@ static OPT<double> getEnvironmentScale()
if( port_id == wxPORT_GTK )
{
// Under GTK, the user can use GDK_SCALE to force the scaling
scale = GetEnvVar<double>( "GDK_SCALE" );
scale = ENV_VAR::GetEnvVar<double>( "GDK_SCALE" );
}
if( scale )

View File

@ -68,15 +68,4 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
wxString ResolveFile( const wxString& aFileName, const ENV_VAR_MAP* aEnvVars,
const PROJECT* aProject );
/**
* Check if a given filename is within a given project directory (not whether it exists!)
*
* @param aFileName is the absolute path to check
* @param aProject is the project to test against
* @param aSubPath will be filled with the relative path to the file inside the project (if any)
* @return true if aFileName's path is inside aProject's path
*/
bool PathIsInsideProject( const wxString& aFileName, const PROJECT* aProject,
wxFileName* aSubPath = nullptr );
#endif /* ENV_PATHS_H */

View File

@ -26,66 +26,66 @@
#define ENV_VARS_H
#include <wx/string.h>
#include <vector>
#include <core/optional.h>
using ENV_VAR_LIST = std::vector<wxString>;
namespace ENV_VAR
{
using ENV_VAR_LIST = std::vector<wxString>;
/**
* Determine if an environment variable is "predefined", i.e. if the
* name of the variable is special to KiCad, and isn't just a user-specified
* substitution name.
* @param aEnvVar the variable to check
* @return true if predefined
*/
bool IsEnvVarImmutable( const wxString& aEnvVar );
/**
* Determine if an environment variable is "predefined", i.e. if the
* name of the variable is special to KiCad, and isn't just a user-specified
* substitution name.
* @param aEnvVar the variable to check
* @return true if predefined
*/
bool IsEnvVarImmutable( const wxString& aEnvVar );
/**
* Get the list of pre-defined environment variables.
*/
const ENV_VAR_LIST& GetPredefinedEnvVars();
/**
* Get the list of pre-defined environment variables.
*/
const ENV_VAR_LIST& GetPredefinedEnvVars();
/**
* Look up long-form help text for a given environment variable.
*
* This is intended for use in more verbose help resources (as opposed to
* tooltip text)
*
* @param aEnvVar The variable to look up
* @return A string with help for that variable. Empty if
* no help available for this variable.
*/
wxString LookUpEnvVarHelp( const wxString& aEnvVar );
/**
* Look up long-form help text for a given environment variable.
*
* This is intended for use in more verbose help resources (as opposed to
* tooltip text)
*
* @param aEnvVar The variable to look up
* @return A string with help for that variable. Empty if
* no help available for this variable.
*/
wxString LookUpEnvVarHelp( const wxString& aEnvVar );
/**
* Get an environment variable as a specific type, if set correctly
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set and parseable, otherwise empty.
*/
template <typename VAL_TYPE>
OPT<VAL_TYPE> GetEnvVar( const wxString& aEnvVarName );
/**
* Get an environment variable as a specific type, if set correctly
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set and parseable, otherwise empty.
*/
template <typename VAL_TYPE>
OPT<VAL_TYPE> GetEnvVar( const wxString& aEnvVarName );
/**
* Get a string environment variable, if it is set.
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set, otherwise empty.
*/
template<>
OPT<wxString> GetEnvVar( const wxString& aEnvVarName );
/**
* Get a double from an environment variable, if set
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set and parseable as a double,
* otherwise empty.
*/
template <>
OPT<double> GetEnvVar( const wxString& aEnvVarName );
/**
* Get a string environment variable, if it is set.
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set, otherwise empty.
*/
template<>
OPT<wxString> GetEnvVar( const wxString& aEnvVarName );
/**
* Get a double from an environment variable, if set
*
* @param aEnvVarName the name of the environment variable
* @return an OPT containing the value, if set and parseable as a double,
* otherwise empty.
*/
template <>
OPT<double> GetEnvVar( const wxString& aEnvVarName );
};
#endif /* ENV_VARS_H */