Skip to content

Commit

Permalink
WIP Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Cousineau <eric.cousineau@tri.global>
  • Loading branch information
EricCousineau-TRI committed Apr 2, 2019
1 parent 6cbba4d commit c020e56
Showing 1 changed file with 28 additions and 45 deletions.
73 changes: 28 additions & 45 deletions src/find_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "rcpputils/find_library.hpp"

#include <cassert>
#include <cstddef>

#include <list>
Expand All @@ -26,24 +27,27 @@ namespace rcpputils
namespace
{

std::string get_env_var(const char * env_var)
{
char * value = nullptr;
#ifndef _WIN32
value = getenv(env_var);
#else
size_t value_size;
_dupenv_s(&value, &value_size, env_var);
#endif
std::string value_str = "";
if (value) {
value_str = value;
#ifdef _WIN32
free(value);
static constexpr char kPathVar[] = "PATH";
static constexpr char kPathSeparator = ';';
static constexpr char kSolibPrefix[] = "";
static constexpr char kSolibExtension[] = ".dll";
#elif __APPLE__
static constexpr char kPathVar[] = "DYLD_LIBRARY_PATH";
static constexpr char kPathSeparator = ':';
static constexpr char kSolibPrefix[] = "lib";
static constexpr char kSolibExtension[] = ".dylib";
#else
static constexpr char kPathVar[] = "LD_LIBRARY_PATH";
static constexpr char kPathSeparator = ':';
static constexpr char kSolibPrefix[] = "lib";
static constexpr char kSolibExtension[] = ".so";
#endif
}
// printf("get_env_var(%s) = %s\n", env_var, value_str.c_str());
return value_str;

std::string get_env_var(const char * kPathVar)
{
char * value = getenv(kPathVar);
return value ? value : "";
}

std::list<std::string> split(const std::string & value, const char delimiter)
Expand All @@ -54,50 +58,29 @@ std::list<std::string> split(const std::string & value, const char delimiter)
while (std::getline(ss, s, delimiter)) {
list.push_back(s);
}
// printf("split(%s) = %zu\n", value.c_str(), list.size());
return list;
}

bool is_file_exist(const char * filename)
{
std::ifstream h(filename);
// printf("is_file_exist(%s) = %s\n", filename, h.good() ? "true" : "false");
return h.good();
}

} // namespace

std::string find_library_path(const std::string & library_name)
{
// TODO(eric.cousineau): Does Poco provide this functionality?
const char * env_var;
char separator;
const char * filename_prefix;
const char * filename_extension;
#ifdef _WIN32
env_var = "PATH";
separator = ';';
filename_prefix = "";
filename_extension = ".dll";
#elif __APPLE__
env_var = "DYLD_LIBRARY_PATH";
separator = ':';
filename_prefix = "lib";
filename_extension = ".dylib";
#else
env_var = "LD_LIBRARY_PATH";
separator = ':';
filename_prefix = "lib";
filename_extension = ".so";
#endif
std::string search_path = get_env_var(env_var);
std::list<std::string> search_paths = split(search_path, separator);
// TODO: Does Poco provide this functionality (ros2/rcpputils#7)?
// TODO: Have this return a library pointer instead (ros2/rcpputils#8).
std::string search_path = get_env_var(kPathVar);
std::list<std::string> search_paths = split(search_path, kPathSeparator);

std::string filename = filename_prefix;
filename += library_name + filename_extension;
std::string filename = kSolibPrefix;
filename += library_name + kSolibExtension;

for (auto it : search_paths) {
std::string path = it + "/" + filename;
for (const auto& search_path : search_paths) {
std::string path = search_path + "/" + filename;
if (is_file_exist(path.c_str())) {
return path;
}
Expand Down

0 comments on commit c020e56

Please sign in to comment.