42 std::filesystem::path& relative_filepath)
44 std::filesystem::path sub_path = path;
45 if (std::filesystem::is_regular_file(sub_path))
47 relative_filepath = sub_path.filename();
48 sub_path = sub_path.parent_path();
52 relative_filepath = sub_path;
57 while (!sub_path.empty() && sub_path != sub_path.root_path())
59 if (std::filesystem::is_regular_file(sub_path /
"package.xml"))
65 tinyxml2::XMLDocument package_xml_file;
66 auto is_open = package_xml_file.LoadFile((sub_path /
"package.xml").c_str());
67 if (is_open == tinyxml2::XML_SUCCESS)
70 package_xml_file.FirstChildElement(
"package")->FirstChildElement(
"name")->FirstChild()->ToText()->Value();
79 relative_filepath = sub_path.filename() / relative_filepath;
80 sub_path = sub_path.parent_path();
88 for (
const auto& attr : attributes)
92 const char* value = e.Attribute(attr.name);
93 if (value && strcmp(attr.value, value) == 0)
105 tinyxml2::XMLElement*
uniqueInsert(tinyxml2::XMLDocument& doc, tinyxml2::XMLElement& element,
const char* tag,
106 const std::vector<XMLAttribute>& attributes,
const char*
text)
109 tinyxml2::XMLElement* result = element.FirstChildElement(tag);
111 result = result->NextSiblingElement(tag);
115 result = doc.NewElement(tag);
116 element.InsertEndChild(result);
120 for (
const auto& attr : attributes)
122 if (!result->Attribute(attr.name))
123 result->SetAttribute(attr.name, attr.value);
127 if (
text && !result->GetText())
129 tinyxml2::XMLText* text_el = doc.NewText(
text);
130 result->InsertEndChild(text_el);
bool hasRequiredAttributes(const tinyxml2::XMLElement &e, const std::vector< XMLAttribute > &attributes)
tinyxml2::XMLElement * uniqueInsert(tinyxml2::XMLDocument &doc, tinyxml2::XMLElement &element, const char *tag, const std::vector< XMLAttribute > &attributes={}, const char *text=nullptr)
Insert a new XML element with a given tag, attributes, and text.
bool extractPackageNameFromPath(const std::filesystem::path &path, std::string &package_name, std::filesystem::path &relative_filepath)