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

[ros2]: Improve finding of message file #100

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Changes from all 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
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>
achim-k marked this conversation as resolved.
Show resolved Hide resolved
#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