Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor paths to use rcpputils filesystem helper #46

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ament_index_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ unless the library was explicitly added as a static library."
ON)

find_package(ament_cmake REQUIRED)
find_package(rcpputils REQUIRED)

add_library(${PROJECT_NAME}
src/get_package_prefix.cpp
Expand All @@ -32,6 +33,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "AMENT_INDEX_CPP_BUILDING_DLL
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")
ament_target_dependencies(${PROJECT_NAME} rcpputils)

ament_export_include_directories(include)
ament_export_interfaces(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
Expand Down
2 changes: 2 additions & 0 deletions ament_index_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rcpputils</depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
Expand Down
6 changes: 5 additions & 1 deletion ament_index_cpp/src/get_package_share_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <string>

#include "rcpputils/filesystem_helper.hpp"
piraka9011 marked this conversation as resolved.
Show resolved Hide resolved

#include "ament_index_cpp/get_package_prefix.hpp"

namespace ament_index_cpp
Expand All @@ -24,7 +26,9 @@ namespace ament_index_cpp
std::string
get_package_share_directory(const std::string & package_name)
{
return get_package_prefix(package_name) + "/share/" + package_name;
auto share_directory =
rcpputils::fs::path{get_package_prefix(package_name)} / "share" / package_name;
return share_directory.string();
}

} // namespace ament_index_cpp
8 changes: 5 additions & 3 deletions ament_index_cpp/src/get_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <stdexcept>
#include <string>

#include "rcpputils/filesystem_helper.hpp"
piraka9011 marked this conversation as resolved.
Show resolved Hide resolved

#include "ament_index_cpp/get_search_paths.hpp"

namespace ament_index_cpp
Expand All @@ -39,9 +41,9 @@ get_resource(
}
auto paths = get_search_paths();
for (auto path : paths) {
auto resource_path = path + "/share/ament_index/resource_index/" +
resource_type + "/" + resource_name;
std::ifstream s(resource_path);
auto resource_path = rcpputils::fs::path{path} / "share" / "ament_index" / "resource_index" /
resource_type / resource_name;
std::ifstream s(resource_path.string());
if (s.is_open()) {
std::stringstream buffer;
buffer << s.rdbuf();
Expand Down
11 changes: 7 additions & 4 deletions ament_index_cpp/src/get_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <stdexcept>
#include <string>

#include "rcpputils/filesystem_helper.hpp"
piraka9011 marked this conversation as resolved.
Show resolved Hide resolved

#include "ament_index_cpp/get_search_paths.hpp"

namespace ament_index_cpp
Expand All @@ -38,17 +40,18 @@ get_resources(const std::string & resource_type)
std::map<std::string, std::string> resources;
auto paths = get_search_paths();
for (auto base_path : paths) {
auto path = base_path + "/share/ament_index/resource_index/" + resource_type;
auto path =
rcpputils::fs::path{base_path} / "share" / "ament_index" / "resource_index" / resource_type;

#ifndef _WIN32
auto dir = opendir(path.c_str());
auto dir = opendir(path.string().c_str());
if (!dir) {
continue;
}
dirent * entry;
while ((entry = readdir(dir)) != NULL) {
// ignore directories
auto subdir = opendir((path + "/" + entry->d_name).c_str());
auto subdir = opendir((path / entry->d_name).string().c_str());
if (subdir) {
closedir(subdir);
continue;
Expand All @@ -69,7 +72,7 @@ get_resources(const std::string & resource_type)
closedir(dir);

#else
std::string pattern = path + "/*";
std::string pattern = rcpputils::fs::path{path} / "*";
piraka9011 marked this conversation as resolved.
Show resolved Hide resolved
WIN32_FIND_DATA find_data;
HANDLE find_handle = FindFirstFile(pattern.c_str(), &find_data);
if (find_handle == INVALID_HANDLE_VALUE) {
Expand Down
8 changes: 5 additions & 3 deletions ament_index_cpp/src/has_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <stdexcept>
#include <string>

#include "rcpputils/filesystem_helper.hpp"

#include "ament_index_cpp/get_search_paths.hpp"

namespace ament_index_cpp
Expand All @@ -37,9 +39,9 @@ has_resource(
}
auto paths = get_search_paths();
for (auto path : paths) {
auto resource_path = path + "/share/ament_index/resource_index/" +
resource_type + "/" + resource_name;
std::ifstream s(resource_path);
auto resource_path = rcpputils::fs::path{path} / "share" / "ament_index" / "resource_index" /
resource_type / resource_name;
std::ifstream s(resource_path.string());
if (s.is_open()) {
if (prefix_path) {
*prefix_path = path;
Expand Down