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

Allow adding to message set without recursively opening includes #48

Merged
merged 1 commit into from
Jun 28, 2024
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
41 changes: 23 additions & 18 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) {
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) {
// 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 Expand Up @@ -291,14 +296,14 @@ namespace mav {
addFromXML(xml_path);
}

void addFromXML(const std::string &file_path) {
XMLParser parser = XMLParser::forFile(file_path);
void addFromXML(const std::string &file_path, bool recursive_open_includes = true) {
XMLParser parser = XMLParser::forFile(file_path, recursive_open_includes);
parser.parse(_enums, _messages, _message_ids);
}
#endif // _NO_STD_FILESYSTEM

void addFromXMLString(const std::string &xml_string) {
XMLParser parser = XMLParser::forXMLString(xml_string);
void addFromXMLString(const std::string &xml_string, bool recursive_open_includes = false) {
XMLParser parser = XMLParser::forXMLString(xml_string, recursive_open_includes);
parser.parse(_enums, _messages,_message_ids);
}

Expand Down
Loading