From 80aa1bb55a3610221f5f0ab6155e9de13f630cc8 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Krauch Date: Thu, 1 Dec 2022 13:14:10 -0300 Subject: [PATCH] [ros2]: Improve finding of message file (#100) **Public-Facing Changes** None **Description** Can now handle cases where a message file's name is a substring of another message file's name, e.g. `Foo.msg` and `AAAAFoo.msg` Fixes #97 --- .../src/message_definition_cache.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ros2_foxglove_bridge/src/message_definition_cache.cpp b/ros2_foxglove_bridge/src/message_definition_cache.cpp index 99acd47..8b52674 100644 --- a/ros2_foxglove_bridge/src/message_definition_cache.cpp +++ b/ros2_foxglove_bridge/src/message_definition_cache.cpp @@ -1,5 +1,6 @@ #include "foxglove_bridge/message_definition_cache.hpp" +#include #include #include #include @@ -15,12 +16,6 @@ namespace foxglove { -#ifdef _WIN32 -static const std::string SEPARATOR = "\\"; -#else -static const std::string SEPARATOR = "/"; -#endif - // Match datatype names (foo_msgs/Bar or foo_msgs/msg/Bar) static const std::regex PACKAGE_TYPENAME_REGEX{R"(^([a-zA-Z0-9_]+)/(?:msg/)?([a-zA-Z0-9_]+)$)"}; @@ -155,15 +150,15 @@ const MessageSpec& MessageDefinitionCache::load_message_spec( // Find the first line that ends with the filename we're looking for const auto lines = split_string(index_contents); const auto it = std::find_if(lines.begin(), lines.end(), [&filename](const std::string& line) { - return line.size() >= filename.size() && - line.compare(line.size() - filename.size(), filename.size(), filename) == 0; + std::filesystem::path filePath(line); + return filePath.filename() == filename; }); if (it == lines.end()) { throw DefinitionNotFoundError(definition_identifier.package_resource_name); } // Read the file - const std::string full_path = share_dir + SEPARATOR + *it; + const std::string full_path = share_dir + std::filesystem::path::preferred_separator + *it; std::ifstream file{full_path}; if (!file.good()) { throw DefinitionNotFoundError(definition_identifier.package_resource_name);