Skip to content

Commit

Permalink
Add environment variable public API function to GeneralResourceLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwason committed Jan 9, 2025
1 parent 4b9793e commit 914f528
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 9 additions & 2 deletions tesseract_common/include/tesseract_common/resource_locator.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ class GeneralResourceLocator : public ResourceLocator
*/
bool addPath(const tesseract_common::fs::path& path);

/**
* @brief Load paths from an environment variable
*
* @param environment_variable The environment variable to load paths from
* @return true Success
* @return false Environment variable does not exist
*/
bool loadEnvironmentVariable(const std::string& environment_variable);

private:
friend class boost::serialization::access;
template <class Archive>
Expand All @@ -129,8 +138,6 @@ class GeneralResourceLocator : public ResourceLocator
std::unordered_map<std::string, std::string> package_paths_;

void processToken(const std::string& token);

void initEnv(const std::string& env_variable);
};

/** @brief Represents resource data available from a file or url */
Expand Down
11 changes: 7 additions & 4 deletions tesseract_common/src/resource_locator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ GeneralResourceLocator::GeneralResourceLocator(const std::vector<std::string>& e
{
for (const auto& env_variable : environment_variables)
{
initEnv(env_variable);
loadEnvironmentVariable(env_variable);
}
}

Expand All @@ -69,13 +69,13 @@ GeneralResourceLocator::GeneralResourceLocator(const std::vector<tesseract_commo

for (const auto& env_variable : environment_variables)
{
initEnv(env_variable);
loadEnvironmentVariable(env_variable);
}
}

void GeneralResourceLocator::initEnv(const std::string& env_variable)
bool GeneralResourceLocator::loadEnvironmentVariable(const std::string& environment_variable)
{
char* ros_package_paths = std::getenv(env_variable.c_str());
char* ros_package_paths = std::getenv(environment_variable.c_str());
if (ros_package_paths != nullptr)
{
std::vector<std::string> tokens;
Expand All @@ -86,7 +86,10 @@ void GeneralResourceLocator::initEnv(const std::string& env_variable)
#endif
for (const auto& token : tokens)
processToken(token);

return true;
}
return false;
}

bool GeneralResourceLocator::addPath(const tesseract_common::fs::path& path)
Expand Down

0 comments on commit 914f528

Please sign in to comment.