From 64531c85e3811fd4444c07200b44352673718177 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 25 Nov 2016 22:44:24 +0000 Subject: [PATCH] Generalise per-variant localised string lookup. Will be used for per-variant long-descriptions. --- simgear/package/Package.cxx | 35 +++++++++++++++++++---------------- simgear/package/Package.hxx | 4 +++- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/simgear/package/Package.cxx b/simgear/package/Package.cxx index 70ceb1ae..5c16b5ab 100644 --- a/simgear/package/Package.cxx +++ b/simgear/package/Package.cxx @@ -206,7 +206,7 @@ size_t Package::fileSizeBytes() const std::string Package::description() const { - return getLocalisedProp("description"); + return getLocalisedProp("description", 0); } string_set Package::tags() const @@ -258,9 +258,9 @@ string_list Package::downloadUrls() const return r; } -std::string Package::getLocalisedProp(const std::string& aName) const +std::string Package::getLocalisedProp(const std::string& aName, const unsigned int vIndex) const { - return getLocalisedString(m_props, aName.c_str()); + return getLocalisedString(propsForVariant(vIndex, aName.c_str()), aName.c_str()); } std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const @@ -358,28 +358,31 @@ unsigned int Package::indexOfVariant(const std::string& vid) const std::string Package::nameForVariant(const unsigned int vIndex) const { - if (vIndex == 0) - return name(); + return propsForVariant(vIndex, "name")->getStringValue("name"); +} + +SGPropertyNode_ptr Package::propsForVariant(const unsigned int vIndex, const char* propName) const +{ + if (vIndex == 0) { + return m_props; + } // offset by minus one to allow for index 0 being the primary SGPropertyNode_ptr var = m_props->getChild("variant", vIndex - 1); - if (var) - return var->getStringValue("name"); + if (var) { + if (!propName || var->hasChild(propName)) { + return var; + } + + return m_props; + } throw sg_exception("Unknow variant in package " + id()); } Package::ThumbnailVec Package::thumbnailsForVariant(unsigned int vIndex) const { - if (vIndex == 0) { - return thumbnailsFromProps(m_props); - } - - SGPropertyNode_ptr var = m_props->getChild("variant", vIndex - 1); - if (!var) { - throw sg_exception("Unknow variant in package " + id()); - } - + SGPropertyNode_ptr var = propsForVariant(vIndex); return thumbnailsFromProps(var); } diff --git a/simgear/package/Package.hxx b/simgear/package/Package.hxx index 0f1a82bf..d7b9f746 100644 --- a/simgear/package/Package.hxx +++ b/simgear/package/Package.hxx @@ -114,7 +114,7 @@ public: */ std::string md5() const; - std::string getLocalisedProp(const std::string& aName) const; + std::string getLocalisedProp(const std::string& aName, const unsigned int vIndex = 0) const; unsigned int revision() const; @@ -196,6 +196,8 @@ private: ThumbnailVec thumbnailsFromProps(const SGPropertyNode_ptr& ptr) const; + SGPropertyNode_ptr propsForVariant(const unsigned int vIndex, const char* propName = nullptr) const; + SGPropertyNode_ptr m_props; std::string m_id; string_set m_tags;