Files
waterlevel-software/lib/fetchOTA/utils.cpp
Tobias Maier 3b1ab2e9e9
All checks were successful
Test compiling project / test (push) Successful in 2m21s
Updated shit
2025-02-17 00:12:50 +01:00

42 lines
1.6 KiB
C++

#include <utils.h>
#include <stdio.h>
Version parseVersion(const char *versionStr) {
Version v = {0, 0, 0}; // Initialize with default values
// Buffer to check for any extra content
char extraContent;
// Try to extract three integers and check for non-numeric trailing characters
int parsedCount = sscanf(versionStr, "%d.%d.%d%c", &v.major, &v.minor, &v.patch, &extraContent);
// If parsed count is not 3, or there is an extra character, mark as invalid
if (parsedCount > 3) {
v.major = v.minor = v.patch = 0;
}
return v;
}
Configuration getLatestConfiguration(Configuration *configs, int count) {
Configuration latest = configs[0];
for (int i = 1; i < count; i++) {
const Version &currentVersion = configs[i].version;
const Version &latestVersion = latest.version;
// Compare the versions stored in the Configuration structs
if (currentVersion.major > latestVersion.major ||
(currentVersion.major == latestVersion.major && currentVersion.minor > latestVersion.minor) ||
(currentVersion.major == latestVersion.major && currentVersion.minor == latestVersion.minor && currentVersion.patch > latestVersion.patch)) {
latest = configs[i];
}
}
return latest;
}
bool isVersionNewer(Version oldVersion, Version newVersion) {
if (newVersion.major > oldVersion.major ||
(newVersion.major == oldVersion.major && newVersion.minor > oldVersion.minor) ||
(newVersion.major == oldVersion.major && newVersion.minor == oldVersion.minor && newVersion.patch > oldVersion.patch)) {
return true;
}
return false;
}