Skip to content

Commit

Permalink
refactor(core-clp): Remove unused functions from src/clp/Utils.*. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden-D-Zhang authored Jan 8, 2025
1 parent e1f70e7 commit 818be9e
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 113 deletions.
51 changes: 0 additions & 51 deletions components/core/src/clp/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,57 +88,6 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) {
return ErrorCode_Success;
}

string get_parent_directory_path(string const& path) {
string dirname = get_unambiguous_path(path);

size_t last_slash_pos = dirname.find_last_of('/');
if (0 == last_slash_pos) {
dirname = "/";
} else if (string::npos == last_slash_pos) {
dirname = ".";
} else {
dirname.resize(last_slash_pos);
}

return dirname;
}

string get_unambiguous_path(string const& path) {
string unambiguous_path;
if (path.empty()) {
return unambiguous_path;
}

// Break path into components
vector<string> path_components;
boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on);

// Remove ambiguous components
list<string> unambiguous_components;
size_t num_components_to_ignore = 0;
for (size_t i = path_components.size(); i-- > 0;) {
if (".." == path_components[i]) {
++num_components_to_ignore;
} else if ("." == path_components[i] || path_components[i].empty()) {
// Do nothing
} else if (num_components_to_ignore > 0) {
--num_components_to_ignore;
} else {
unambiguous_components.emplace_front(path_components[i]);
}
}

// Assemble unambiguous path from leading slash (if any) and the unambiguous components
if ('/' == path[0]) {
unambiguous_path += '/';
}
if (!unambiguous_components.empty()) {
unambiguous_path += boost::join(unambiguous_components, "/");
}

return unambiguous_path;
}

ErrorCode read_list_of_paths(string const& list_path, vector<string>& paths) {
unique_ptr<FileReader> file_reader;
try {
Expand Down
22 changes: 0 additions & 22 deletions components/core/src/clp/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok);
*/
ErrorCode create_directory_structure(std::string const& path, mode_t mode);

/**
* Gets the parent directory path for a given path
* Corner cases:
* - get_dirname("abc") = "."
* - get_dirname(".") = "."
* - get_dirname("..") = "."
* - get_dirname("/") = "/"
* - get_dirname("/.") = "/"
* - get_dirname("/..") = "/"
* - get_dirname("/abc") = "/"
* @param path
* @return Parent directory path
*/
std::string get_parent_directory_path(std::string const& path);

/**
* Removes ".", "..", and consecutive "/" from a given path and returns the result
* @param path The given path
* @return The unambiguous path
*/
std::string get_unambiguous_path(std::string const& path);

/**
* Read a list of paths from a file
* @param list_path
Expand Down
40 changes: 0 additions & 40 deletions components/core/tests/test-Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

using clp::create_directory_structure;
using clp::ErrorCode_Success;
using clp::get_parent_directory_path;
using clp::get_unambiguous_path;
using std::string;

TEST_CASE("create_directory_structure", "[create_directory_structure]") {
Expand Down Expand Up @@ -44,41 +42,3 @@ TEST_CASE("create_directory_structure", "[create_directory_structure]") {

REQUIRE(0 == rmdir("/tmp/5807"));
}

TEST_CASE("get_parent_directory_path", "[get_parent_directory_path]") {
// Corner cases
// Anything without a slash should return "."
REQUIRE(get_parent_directory_path(".") == ".");
REQUIRE(get_parent_directory_path("..") == ".");
REQUIRE(get_parent_directory_path("abc") == ".");
// A single slash, at the beginning, should always return "/"
REQUIRE(get_parent_directory_path("/") == "/");
REQUIRE(get_parent_directory_path("/.") == "/");
REQUIRE(get_parent_directory_path("/..") == "/");
REQUIRE(get_parent_directory_path("/abc") == "/");

// Normal cases
REQUIRE(get_parent_directory_path("//abc/./def//../def/.///") == "/abc");
}

TEST_CASE("get_unambiguous_path", "[get_unambiguous_path]") {
// Base cases (should not modify anything)
REQUIRE(get_unambiguous_path("/") == "/");
REQUIRE(get_unambiguous_path("abc") == "abc");
REQUIRE(get_unambiguous_path("/abc") == "/abc");
REQUIRE(get_unambiguous_path("/abc/def") == "/abc/def");

// Corner cases
REQUIRE(get_unambiguous_path(".").empty());
REQUIRE(get_unambiguous_path("..").empty());
REQUIRE(get_unambiguous_path("////") == "/");
REQUIRE(get_unambiguous_path("/./.././//../") == "/");
REQUIRE(get_unambiguous_path("./.././//../").empty());
REQUIRE(get_unambiguous_path("/abc/def/.././../") == "/");
REQUIRE(get_unambiguous_path("abc/def/.././../").empty());

// Normal cases
REQUIRE(get_unambiguous_path("/abc///def/../ghi/./") == "/abc/ghi");
REQUIRE(get_unambiguous_path("abc///def/../ghi/./") == "abc/ghi");
REQUIRE(get_unambiguous_path("../abc///def/../ghi/./") == "abc/ghi");
}

0 comments on commit 818be9e

Please sign in to comment.