From 92ebdbfacc8b28ac93fe3d73ee2b7b9a7fccc8a1 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 9 Apr 2020 14:39:12 +0100 Subject: [PATCH] Expose compare-version helper publicly. This helper was added for Catalog version checking, but will be used in FlightGear now, so move it to strutils. --- simgear/misc/strutils.cxx | 34 +++++++++++++++++++++++++++++++++ simgear/misc/strutils.hxx | 7 +++++++ simgear/package/Catalog.cxx | 38 +++---------------------------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 2509601e..21f0ed47 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -587,6 +587,40 @@ namespace simgear { // reached end - longer wins return v1parts.size() - v2parts.size(); } + + bool compareVersionToWildcard(const std::string& aVersion, const std::string& aCandidate) + { + if (aCandidate == aVersion) { + return true; + } + + // examine each dot-seperated component in turn, supporting a wildcard + // in the versions from the catalog. + string_list parts = split(aVersion, "."); + string_list candidateParts = split(aCandidate, "."); + + const size_t partCount = parts.size(); + const size_t candidatePartCount = candidateParts.size(); + + bool previousCandidatePartWasWildcard = false; + + for (unsigned int p=0; p < partCount; ++p) { + // candidate string is too short, can match if it ended with wildcard + // part. This allows candidate '2016.*' to match '2016.1.2' and so on + if (candidatePartCount <= p) { + return previousCandidatePartWasWildcard; + } + + if (candidateParts.at(p) == "*") { + // always passes + previousCandidatePartWasWildcard = true; + } else if (parts.at(p) != candidateParts.at(p)) { + return false; + } + } + + return true; + } string join(const string_list& l, const string& joinWith) { diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 07c065c7..81e6afd0 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -248,6 +248,13 @@ namespace simgear { const std::string& v2, int maxComponents = 0 ); + /** + @brief COmpare a version string to a template version string (which can contain wildcards) + @param aVersion : a regular version such as 2017.6 or 2020.1.2 + @param aCandidate : a version specifier, eg 2020.* or 21.5.* + */ + bool compareVersionToWildcard(const std::string& aVersion, const std::string& aCandidate); + /** * Convert a string to upper case. * @return upper case string diff --git a/simgear/package/Catalog.cxx b/simgear/package/Catalog.cxx index 8086ab64..89d9eddf 100644 --- a/simgear/package/Catalog.cxx +++ b/simgear/package/Catalog.cxx @@ -39,43 +39,11 @@ namespace simgear { namespace pkg { -bool checkVersionString(const std::string& aVersion, const std::string& aCandidate) -{ - if (aCandidate == aVersion) { - return true; - } - - // examine each dot-seperated component in turn, supporting a wildcard - // in the versions from the catalog. - string_list appVersionParts = simgear::strutils::split(aVersion, "."); - string_list catVersionParts = simgear::strutils::split(aCandidate, "."); - - size_t partCount = appVersionParts.size(); - bool previousCandidatePartWasWildcard = false; - - for (unsigned int p=0; p < partCount; ++p) { - // candidate string is too short, can match if it ended with wildcard - // part. This allows candidate '2016.*' to match '2016.1.2' and so on - if (catVersionParts.size() <= p) { - return previousCandidatePartWasWildcard; - } - - if (catVersionParts[p] == "*") { - // always passes - previousCandidatePartWasWildcard = true; - } else if (appVersionParts[p] != catVersionParts[p]) { - return false; - } - } - - return true; -} - bool checkVersion(const std::string& aVersion, SGPropertyNode_ptr props) { - BOOST_FOREACH(SGPropertyNode* v, props->getChildren("version")) { + for (SGPropertyNode* v : props->getChildren("version")) { std::string s(v->getStringValue()); - if (checkVersionString(aVersion, s)) { + if (strutils::compareVersionToWildcard(aVersion, s)) { return true; } } @@ -86,7 +54,7 @@ SGPropertyNode_ptr alternateForVersion(const std::string& aVersion, SGPropertyNo { for (auto v : props->getChildren("alternate-version")) { for (auto versionSpec : v->getChildren("version")) { - if (checkVersionString(aVersion, versionSpec->getStringValue())) { + if (strutils::compareVersionToWildcard(aVersion, versionSpec->getStringValue())) { return v; } }