You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.1 KiB

  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 modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @file env_vars.h
  21. *
  22. * Functions related to environment variables, including help functions.
  23. */
  24. #ifndef ENV_VARS_H
  25. #define ENV_VARS_H
  26. #include <kicommon.h>
  27. #include <wx/string.h>
  28. #include <map>
  29. #include <vector>
  30. #include <optional>
  31. class ENV_VAR_ITEM;
  32. namespace ENV_VAR
  33. {
  34. using ENV_VAR_LIST = std::vector<wxString>;
  35. /**
  36. * Determine if an environment variable is "predefined", i.e. if the
  37. * name of the variable is special to KiCad, and isn't just a user-specified
  38. * substitution name.
  39. *
  40. * @param aEnvVar the variable to check.
  41. * @return true if predefined.
  42. */
  43. KICOMMON_API bool IsEnvVarImmutable( const wxString& aEnvVar );
  44. /**
  45. * Get the list of pre-defined environment variables.
  46. */
  47. KICOMMON_API const ENV_VAR_LIST& GetPredefinedEnvVars();
  48. /**
  49. * Construct a versioned environment variable based on this KiCad major version.
  50. *
  51. * @param aBaseName is the suffix, like TEMPLATE_DIR.
  52. * @return an environment variable name, like KICAD8_TEMPLATE_DIR.
  53. */
  54. KICOMMON_API wxString GetVersionedEnvVarName( const wxString& aBaseName );
  55. /**
  56. * Attempt to retrieve the value of a versioned environment variable, such as
  57. * KICAD8_TEMPLATE_DIR.
  58. *
  59. * If this value exists in the map, it will be returned. If not, the map will be searched
  60. * for keys matching KICAD*_<aBaseName>, and the first match's value will be returned. If
  61. * there are no matches, std::nullopt will be returned.
  62. *
  63. * @param aMap is an #ENV_VAR_MAP (@see environment.h).
  64. * @param aBaseName is the suffix for the environment variable (@see GetVersionedEnvVarName).
  65. */
  66. KICOMMON_API std::optional<wxString>
  67. GetVersionedEnvVarValue( const std::map<wxString, ENV_VAR_ITEM>& aMap,
  68. const wxString& aBaseName );
  69. /**
  70. * Look up long-form help text for a given environment variable.
  71. *
  72. * This is intended for use in more verbose help resources (as opposed to tooltip text).
  73. *
  74. * @param aEnvVar The variable to look up.
  75. * @return A string with help for that variable. Empty if
  76. * no help available for this variable.
  77. */
  78. KICOMMON_API wxString LookUpEnvVarHelp( const wxString& aEnvVar );
  79. /**
  80. * Get an environment variable as a specific type, if set correctly.
  81. *
  82. * @param aEnvVarName the name of the environment variable.
  83. * @return an std::optional containing the value, if set and parseable, otherwise empty.
  84. */
  85. template <typename VAL_TYPE>
  86. KICOMMON_API std::optional<VAL_TYPE> GetEnvVar( const wxString& aEnvVarName );
  87. /**
  88. * Get a string environment variable, if it is set.
  89. *
  90. * @param aEnvVarName the name of the environment variable
  91. * @return an std::optional containing the value, if set, otherwise empty.
  92. */
  93. template<>
  94. KICOMMON_API std::optional<wxString> GetEnvVar( const wxString& aEnvVarName );
  95. /**
  96. * Get a double from an environment variable, if set.
  97. *
  98. * @param aEnvVarName the name of the environment variable
  99. * @return an std::optional containing the value, if set and parseable as a double,
  100. * otherwise empty.
  101. */
  102. template <>
  103. KICOMMON_API std::optional<double> GetEnvVar( const wxString& aEnvVarName );
  104. };
  105. #endif /* ENV_VARS_H */