2014-02-02 13:05:07 +00:00
|
|
|
#include "include/modutils.h"
|
|
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QRegularExpressionMatch>
|
|
|
|
|
|
|
|
Util::Version::Version(const QString &str) : m_string(str)
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
parse();
|
2014-02-02 13:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Util::Version::operator<(const Version &other) const
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
const int size = qMax(m_sections.size(), other.m_sections.size());
|
|
|
|
for (int i = 0; i < size; ++i)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2015-01-27 21:31:07 +00:00
|
|
|
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
const Section sec2 =
|
2015-01-27 21:31:07 +00:00
|
|
|
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
if (sec1 != sec2)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
return sec1 < sec2;
|
2014-02-02 13:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool Util::Version::operator<=(const Util::Version &other) const
|
|
|
|
{
|
|
|
|
return *this < other || *this == other;
|
|
|
|
}
|
|
|
|
bool Util::Version::operator>(const Version &other) const
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
const int size = qMax(m_sections.size(), other.m_sections.size());
|
|
|
|
for (int i = 0; i < size; ++i)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2015-01-27 21:31:07 +00:00
|
|
|
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
const Section sec2 =
|
2015-01-27 21:31:07 +00:00
|
|
|
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
if (sec1 != sec2)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
return sec1 > sec2;
|
2014-02-02 13:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-06 17:16:56 +01:00
|
|
|
bool Util::Version::operator>=(const Version &other) const
|
|
|
|
{
|
|
|
|
return *this > other || *this == other;
|
|
|
|
}
|
2014-02-02 13:05:07 +00:00
|
|
|
bool Util::Version::operator==(const Version &other) const
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
const int size = qMax(m_sections.size(), other.m_sections.size());
|
|
|
|
for (int i = 0; i < size; ++i)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2015-01-27 21:31:07 +00:00
|
|
|
const Section sec1 = (i >= m_sections.size()) ? Section("0") : m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
const Section sec2 =
|
2015-01-27 21:31:07 +00:00
|
|
|
(i >= other.m_sections.size()) ? Section("0") : other.m_sections.at(i);
|
2014-09-06 17:16:56 +01:00
|
|
|
if (sec1 != sec2)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
return false;
|
2014-02-02 13:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Util::Version::operator!=(const Version &other) const
|
|
|
|
{
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
2014-09-06 17:16:56 +01:00
|
|
|
void Util::Version::parse()
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
m_sections.clear();
|
|
|
|
|
|
|
|
QStringList parts = m_string.split('.');
|
|
|
|
|
|
|
|
for (const auto part : parts)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
2015-01-27 21:31:07 +00:00
|
|
|
m_sections.append(Section(part));
|
2014-02-02 13:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Util::versionIsInInterval(const QString &version, const QString &interval)
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
return versionIsInInterval(Util::Version(version), interval);
|
|
|
|
}
|
|
|
|
bool Util::versionIsInInterval(const Version &version, const QString &interval)
|
|
|
|
{
|
|
|
|
if (interval.isEmpty() || version.toString() == interval)
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interval notation is used
|
|
|
|
QRegularExpression exp(
|
2014-09-06 17:16:56 +01:00
|
|
|
"(?<start>[\\[\\]\\(\\)])(?<bottom>.*?)(,(?<top>.*?))?(?<end>[\\[\\]\\(\\)]),?");
|
2014-02-02 13:05:07 +00:00
|
|
|
QRegularExpressionMatch match = exp.match(interval);
|
|
|
|
if (match.hasMatch())
|
|
|
|
{
|
|
|
|
const QChar start = match.captured("start").at(0);
|
|
|
|
const QChar end = match.captured("end").at(0);
|
|
|
|
const QString bottom = match.captured("bottom");
|
|
|
|
const QString top = match.captured("top");
|
|
|
|
|
|
|
|
// check if in range (bottom)
|
|
|
|
if (!bottom.isEmpty())
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
const auto bottomVersion = Util::Version(bottom);
|
|
|
|
if ((start == '[') && !(version >= bottomVersion))
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-06 17:16:56 +01:00
|
|
|
else if ((start == '(') && !(version > bottomVersion))
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if in range (top)
|
|
|
|
if (!top.isEmpty())
|
|
|
|
{
|
2014-09-06 17:16:56 +01:00
|
|
|
const auto topVersion = Util::Version(top);
|
|
|
|
if ((end == ']') && !(version <= topVersion))
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-06 17:16:56 +01:00
|
|
|
else if ((end == ')') && !(version < topVersion))
|
2014-02-02 13:05:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|