2016-05-07 20:38:52 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
2020-06-19 15:38:51 +02:00
|
|
|
#include <regex>
|
|
|
|
|
2016-05-07 20:38:52 +02:00
|
|
|
namespace utils
|
|
|
|
{
|
|
|
|
std::string to_string(version_type type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case version_type::pre_alpha: return "Pre-Alpha";
|
|
|
|
case version_type::alpha: return "Alpha";
|
|
|
|
case version_type::beta: return "Beta";
|
|
|
|
case version_type::release_candidate: return "RC";
|
|
|
|
case version_type::release: return "Release";
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:01:06 +02:00
|
|
|
return "Unknown";
|
2016-05-07 20:38:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 00:00:31 +02:00
|
|
|
uint version::to_hex() const
|
2016-05-07 20:38:52 +02:00
|
|
|
{
|
2016-07-21 00:00:31 +02:00
|
|
|
return (m_hi << 24) | (m_mid << 16) | (m_lo << 8) | ((uint(m_type) & 0xf) << 4) | (m_type_index & 0xf);
|
2016-05-07 20:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string version::to_string() const
|
|
|
|
{
|
|
|
|
std::string version = std::to_string(hi()) + "." + std::to_string(mid());
|
|
|
|
|
|
|
|
if (lo())
|
|
|
|
{
|
2022-09-13 15:08:55 +02:00
|
|
|
version += '.';
|
|
|
|
version += std::to_string(lo());
|
2016-05-07 20:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type() != version_type::release)
|
|
|
|
{
|
|
|
|
if (!postfix().empty())
|
|
|
|
{
|
|
|
|
version += "-" + postfix();
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:08:55 +02:00
|
|
|
version += ' ';
|
|
|
|
version += utils::to_string(type());
|
2016-05-07 20:38:52 +02:00
|
|
|
|
|
|
|
if (type_index() > 1)
|
|
|
|
{
|
|
|
|
version += " " + std::to_string(type_index());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
2020-06-19 15:38:51 +02:00
|
|
|
|
|
|
|
// Based on https://www.geeksforgeeks.org/compare-two-version-numbers/
|
|
|
|
int compare_versions(const std::string& v1, const std::string& v2, bool& ok)
|
|
|
|
{
|
|
|
|
// Check if both version strings are valid
|
|
|
|
ok = std::regex_match(v1, std::regex("[0-9.]*")) && std::regex_match(v2, std::regex("[0-9.]*"));
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vnum stores each numeric part of version
|
|
|
|
int vnum1 = 0;
|
|
|
|
int vnum2 = 0;
|
|
|
|
|
|
|
|
// Loop until both strings are processed
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz i = 0, j = 0; (i < v1.length() || j < v2.length());)
|
2020-06-19 15:38:51 +02:00
|
|
|
{
|
|
|
|
// Storing numeric part of version 1 in vnum1
|
|
|
|
while (i < v1.length() && v1[i] != '.')
|
|
|
|
{
|
|
|
|
vnum1 = vnum1 * 10 + (v1[i] - '0');
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storing numeric part of version 2 in vnum2
|
|
|
|
while (j < v2.length() && v2[j] != '.')
|
|
|
|
{
|
|
|
|
vnum2 = vnum2 * 10 + (v2[j] - '0');
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vnum1 > vnum2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (vnum2 > vnum1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// If equal, reset variables and go for next numeric part
|
|
|
|
vnum1 = vnum2 = 0;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-07 20:38:52 +02:00
|
|
|
}
|