Commit b5d8d023 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'search-env-path' into 'master'

Adding search_env_path in radixcore/system.

See merge request !32
parents acb3f12d e51ffdb1
Pipeline #12780 passed with stages
in 9 minutes and 16 seconds
...@@ -366,6 +366,33 @@ bool remove_file(const std::string &path) ...@@ -366,6 +366,33 @@ bool remove_file(const std::string &path)
{ {
if (dir_exists(path)) return false; if (dir_exists(path)) return false;
return remove(path.c_str()) == 0; return remove(path.c_str()) == 0;
} // remove_file }
std::string search_env_path(const std::string &filename)
{
std::string path_env = env("PATH");
std::vector<std::string> list = split_string(";", path_env);
if (list.size() == 1) // we didn't split anything
{
// guess it was ':' separator, try ':'
list = split_string(":", path_env);
}
for (size_t i = 0; i < list.size(); ++i)
{
std::string item = list[i];
item = item + separator_char();
radix_line("Searching " << item << " for " << filename);
std::string path = item + filename;
if (file_exists(path))
{
radix_line("Found " << path);
return path;
}
}
radix_line("Did not find " << filename << " on the PATH");
return std::string();
}
// remove_file
} // namespace radix } // namespace radix
...@@ -192,6 +192,13 @@ RADIX_PUBLIC bool remove_dir(const std::string &path); ...@@ -192,6 +192,13 @@ RADIX_PUBLIC bool remove_dir(const std::string &path);
*/ */
RADIX_PUBLIC bool remove_file(const std::string &path); RADIX_PUBLIC bool remove_file(const std::string &path);
/**
* @brief search_env_path Search the environment variable PATH for the filename
* @param filename filename to search for
* @return The PATH prefix if the filename is found to exist or empty.
*/
RADIX_PUBLIC std::string search_env_path(const std::string &filename);
} // namespace radix } // namespace radix
#endif /** RADIX_RADIXCORE_SYSTEM_HH_ */ #endif /** RADIX_RADIXCORE_SYSTEM_HH_ */
...@@ -205,3 +205,22 @@ TEST(GetPaths, Basic) ...@@ -205,3 +205,22 @@ TEST(GetPaths, Basic)
EXPECT_EQ("a", paths[0]); EXPECT_EQ("a", paths[0]);
} }
} }
TEST(System, SearchEnvPath)
{
std::string app;
std::string blessed;
#ifdef _WIN32
app = "where.exe";
blessed = "C:\\WINDOWS\\system32\\where.exe";
#elif __APPLE__
app = "ls";
blessed = "/bin/ls";
#else
app = "ls";
blessed = "/usr/bin/ls";
#endif
std::string path = search_env_path(app);
EXPECT_EQ(blessed, path);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment