From 59498f7fc14ce2f4e9f67d725834ff348e7d289b Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sun, 1 Dec 2013 14:45:32 +0100 Subject: [PATCH] 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. --- src/common/ebml.cpp | 25 +++++++++++++++++++++++++ src/common/ebml.h | 1 + 2 files changed, 26 insertions(+) diff --git a/src/common/ebml.cpp b/src/common/ebml.cpp index 2cb291d9c..5fe9ba265 100644 --- a/src/common/ebml.cpp +++ b/src/common/ebml.cpp @@ -494,6 +494,31 @@ find_ebml_element_by_id(EbmlMaster *master, return nullptr; } +std::pair +find_element_in_master(EbmlMaster *master, + EbmlElement *element_to_find) { + if (!master || !element_to_find) + return std::make_pair(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(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(nullptr, 0); +} + void fix_mandatory_elements(EbmlElement *master) { if (dynamic_cast(master)) diff --git a/src/common/ebml.h b/src/common/ebml.h index 31e20c1c1..73544f391 100644 --- a/src/common/ebml.h +++ b/src/common/ebml.h @@ -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 find_element_in_master(EbmlMaster *master, EbmlElement *element_to_find); void fix_mandatory_elements(EbmlElement *master);