Skip to content

Commit

Permalink
[ros2]: Improve finding of message file (#100)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
achim-k authored Dec 1, 2022
1 parent cf7ce69 commit 80aa1bb
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions ros2_foxglove_bridge/src/message_definition_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "foxglove_bridge/message_definition_cache.hpp"

#include <filesystem>
#include <fstream>
#include <optional>
#include <regex>
Expand All @@ -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_]+)$)"};

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 80aa1bb

Please sign in to comment.