common: add function for finding an EbmlElement in an EbmlMaster recursively

Returns both the master and the index at which it was found inside
that master.
This commit is contained in:
Moritz Bunkus 2013-12-01 14:45:32 +01:00
parent b06cb5eb2e
commit 59498f7fc1
2 changed files with 26 additions and 0 deletions

View File

@ -494,6 +494,31 @@ find_ebml_element_by_id(EbmlMaster *master,
return nullptr;
}
std::pair<EbmlMaster *, size_t>
find_element_in_master(EbmlMaster *master,
EbmlElement *element_to_find) {
if (!master || !element_to_find)
return std::make_pair<EbmlMaster *, size_t>(nullptr, 0);
auto &elements = master->GetElementList();
auto itr = brng::find(elements, element_to_find);
if (itr != elements.end())
return std::make_pair(master, std::distance(elements.begin(), itr));
for (auto &sub_element : elements) {
auto sub_master = dynamic_cast<EbmlMaster *>(sub_element);
if (!sub_master)
continue;
auto result = find_element_in_master(sub_master, element_to_find);
if (result.first)
return result;
}
return std::make_pair<EbmlMaster *, size_t>(nullptr, 0);
}
void
fix_mandatory_elements(EbmlElement *master) {
if (dynamic_cast<KaxInfo *>(master))

View File

@ -322,6 +322,7 @@ const EbmlCallbacks *find_ebml_parent_callbacks(const EbmlCallbacks &base, const
const EbmlSemantic *find_ebml_semantic(const EbmlCallbacks &base, const EbmlId &id);
EbmlElement *find_ebml_element_by_id(EbmlMaster *master, const EbmlId &id);
std::pair<EbmlMaster *, size_t> find_element_in_master(EbmlMaster *master, EbmlElement *element_to_find);
void fix_mandatory_elements(EbmlElement *master);