diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index f1e93e9298..d2e6d45b5c 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -140,16 +140,17 @@ CiffDirectory::~CiffDirectory() { } } -void CiffComponent::add(UniquePtr component) { - doAdd(std::move(component)); +CiffComponent* CiffComponent::add(UniquePtr component) { + return doAdd(std::move(component)); } -void CiffEntry::doAdd(UniquePtr /*component*/) { +CiffComponent* CiffEntry::doAdd(UniquePtr /*component*/) { throw Error(ErrorCode::kerFunctionNotSupported, "CiffEntry::add"); } // CiffEntry::doAdd -void CiffDirectory::doAdd(UniquePtr component) { +CiffComponent* CiffDirectory::doAdd(UniquePtr component) { components_.push_back(component.release()); + return components_.back(); } // CiffDirectory::doAdd void CiffHeader::read(const byte* pData, size_t size) { @@ -542,39 +543,30 @@ CiffComponent* CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) { if not found, create it set value */ - CiffComponent* cc = nullptr; if (!crwDirs.empty()) { auto dir = crwDirs.top(); crwDirs.pop(); // Find the directory for (const auto& c : components_) if (c->tag() == dir.dir) { - cc = c; - break; + // Recursive call to next lower level directory + return c->add(crwDirs, crwTagId); } - if (!cc) { - // Directory doesn't exist yet, add it - auto m = std::make_unique(dir.dir, dir.parent); - add(std::move(m)); - cc = components_.back(); - } - // Recursive call to next lower level directory - return cc->add(crwDirs, crwTagId); + + // Directory doesn't exist yet, add it + auto m = std::make_unique(dir.dir, dir.parent); + return add(std::move(m)); } // Find the tag for (const auto& c : components_) if (c->tagId() == crwTagId) { - cc = c; - break; + return c; } - if (!cc) { - // Tag doesn't exist yet, add it - auto m = std::make_unique(crwTagId, tag()); - add(std::move(m)); - cc = components_.back(); - } - return cc; + + // Tag doesn't exist yet, add it + auto m = std::make_unique(crwTagId, tag()); + return add(std::move(m)); } // CiffDirectory::doAdd void CiffHeader::remove(uint16_t crwTagId, uint16_t crwDir) const { diff --git a/src/crwimage_int.hpp b/src/crwimage_int.hpp index ef8cb05954..67f8db2a1c 100644 --- a/src/crwimage_int.hpp +++ b/src/crwimage_int.hpp @@ -73,7 +73,7 @@ class CiffComponent { // Default assignment operator is fine //! Add a component to the composition - void add(UniquePtr component); + CiffComponent* add(UniquePtr component); /*! @brief Add \em crwTagId to the parse tree, if it doesn't exist yet. \em crwDirs contains the path of subdirectories, starting @@ -231,7 +231,7 @@ class CiffComponent { //! @name Manipulators //@{ //! Implements add() - virtual void doAdd(UniquePtr component) = 0; + virtual CiffComponent* doAdd(UniquePtr component) = 0; //! Implements add(). The default implementation does nothing. virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId); //! Implements remove(). The default implementation does nothing. @@ -291,7 +291,7 @@ class CiffEntry : public CiffComponent { //@{ using CiffComponent::doAdd; // See base class comment - void doAdd(UniquePtr component) override; + CiffComponent* doAdd(UniquePtr component) override; /*! @brief Implements write(). Writes only the value data of the entry, using writeValueData(). @@ -339,7 +339,7 @@ class CiffDirectory : public CiffComponent { //! @name Manipulators //@{ // See base class comment - void doAdd(UniquePtr component) override; + CiffComponent* doAdd(UniquePtr component) override; // See base class comment CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override; // See base class comment