Files
waterlevel-software/lib/fetchOTA/utils.cpp
Tobias Maier 5738eb4451
Some checks failed
Test compiling project / test (push) Failing after 1m38s
A ton of things
2025-02-14 21:27:04 +01:00

33 lines
1.3 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;
}