2023-08-28 12:31:18 +02:00
|
|
|
/* Version.
|
2018-08-27 11:09:25 +02:00
|
|
|
*
|
2023-08-31 17:35:12 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2023-08-31 11:17:07 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-27 11:09:25 +02:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
|
|
|
#include <villas/version.hpp>
|
|
|
|
|
|
|
|
using namespace villas::utils;
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Version::Version(const std::string &str) {
|
|
|
|
size_t endpos;
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
auto comp = tokenize(str, ".");
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (comp.size() > 3)
|
|
|
|
throw std::invalid_argument("Not a valid version string");
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (unsigned i = 0; i < 3; i++) {
|
|
|
|
if (i < comp.size()) {
|
|
|
|
components[i] = std::stoi(comp[i], &endpos, 10);
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (comp[i].begin() + endpos != comp[i].end())
|
|
|
|
throw std::invalid_argument("Not a valid version string");
|
|
|
|
} else
|
|
|
|
components[i] = 0;
|
|
|
|
}
|
2018-08-27 11:09:25 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Version::Version(int maj, int min, int pat) : components{maj, min, pat} {}
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int Version::cmp(const Version &lhs, const Version &rhs) {
|
|
|
|
int d;
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
d = lhs.components[i] - rhs.components[i];
|
|
|
|
if (d)
|
|
|
|
return d;
|
|
|
|
}
|
2018-08-27 11:09:25 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return 0;
|
2018-08-27 11:09:25 +02:00
|
|
|
}
|