Skip to content

Commit

Permalink
MessageSet: Allow opening of message definitions without recursive op…
Browse files Browse the repository at this point in the history
…ening of sub-files.

This is especially not wanted when loading messages from inline strings.
  • Loading branch information
ThomasDebrunner committed Jun 26, 2024
1 parent 7d82dca commit c46307a
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions include/mav/MessageSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ namespace mav {
std::shared_ptr<rapidxml::file<>> _source_file;
std::shared_ptr<rapidxml::xml_document<>> _document;
std::string _root_xml_folder_path;
bool _recursive_open_files;

XMLParser(
std::shared_ptr<rapidxml::file<>> source_file,
std::shared_ptr<rapidxml::xml_document<>> document,
const std::string &root_xml_folder_path) :
const std::string &root_xml_folder_path,
bool recursive_open_files) :
_source_file(std::move(source_file)),
_document(std::move(document)),
_root_xml_folder_path(root_xml_folder_path) {}
_root_xml_folder_path(root_xml_folder_path),
_recursive_open_files(recursive_open_files) {}


static inline uint64_t _strict_stoul(const std::string &str, int base=10) {
Expand Down Expand Up @@ -172,16 +175,16 @@ namespace mav {

public:
#ifndef _NO_STD_FILESYSTEM
static XMLParser forFile(const std::string &file_name) {
static XMLParser forFile(const std::string &file_name, bool recursive_open_includes = true) {
auto file = std::make_shared<rapidxml::file<>>(file_name.c_str());
auto doc = std::make_shared<rapidxml::xml_document<>>();
doc->parse<0>(file->data());

return {file, doc, std::filesystem::path{file_name}.parent_path().string()};
return {file, doc, std::filesystem::path{file_name}.parent_path().string(), recursive_open_includes};
}
#endif // _NO_STD_FILESYSTEM

static XMLParser forXMLString(const std::string &xml_string) {
static XMLParser forXMLString(const std::string &xml_string, bool recursive_open_includes = false) {
// pass by value on purpose, rapidxml mutates the string on parse
auto istream = std::istringstream(xml_string);
auto file = std::make_shared<rapidxml::file<>>(istream);
Expand All @@ -191,7 +194,7 @@ namespace mav {
} catch (const rapidxml::parse_error &e) {
throw ParseError(e.what());
}
return {file, doc, ""};
return {file, doc, "", recursive_open_includes};
}


Expand All @@ -204,14 +207,16 @@ namespace mav {
throw ParseError("Root node \"mavlink\" not found");
}
#ifndef _NO_STD_FILESYSTEM
for (auto include_element = root_node->first_node("include");
include_element != nullptr;
include_element = include_element->next_sibling("include")) {

const std::string include_name = include_element->value();
auto sub_parser = XMLParser::forFile(
(std::filesystem::path{_root_xml_folder_path} / include_name).string());
sub_parser.parse(out_enum, out_messages, out_message_ids);
if (_recursive_open_files) {
for (auto include_element = root_node->first_node("include");
include_element != nullptr;
include_element = include_element->next_sibling("include")) {

const std::string include_name = include_element->value();
auto sub_parser = XMLParser::forFile(
(std::filesystem::path{_root_xml_folder_path} / include_name).string(), true);
sub_parser.parse(out_enum, out_messages, out_message_ids);
}
}
#endif // _NO_STD_FILESYSTEM

Expand Down

0 comments on commit c46307a

Please sign in to comment.