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.
This commit is contained in:
James Turner
2020-04-09 14:39:12 +01:00
parent 8dbabc8b43
commit 92ebdbfacc
3 changed files with 44 additions and 35 deletions
+34
View File
@@ -588,6 +588,40 @@ namespace simgear {
return v1parts.size() - v2parts.size(); 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) string join(const string_list& l, const string& joinWith)
{ {
string result; string result;
+7
View File
@@ -248,6 +248,13 @@ namespace simgear {
const std::string& v2, const std::string& v2,
int maxComponents = 0 ); 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. * Convert a string to upper case.
* @return upper case string * @return upper case string
+3 -35
View File
@@ -39,43 +39,11 @@ namespace simgear {
namespace pkg { 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) 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()); std::string s(v->getStringValue());
if (checkVersionString(aVersion, s)) { if (strutils::compareVersionToWildcard(aVersion, s)) {
return true; return true;
} }
} }
@@ -86,7 +54,7 @@ SGPropertyNode_ptr alternateForVersion(const std::string& aVersion, SGPropertyNo
{ {
for (auto v : props->getChildren("alternate-version")) { for (auto v : props->getChildren("alternate-version")) {
for (auto versionSpec : v->getChildren("version")) { for (auto versionSpec : v->getChildren("version")) {
if (checkVersionString(aVersion, versionSpec->getStringValue())) { if (strutils::compareVersionToWildcard(aVersion, versionSpec->getStringValue())) {
return v; return v;
} }
} }