ota.cpp: improved software version comparision function

This commit is contained in:
Klaus K Wilting 2018-11-05 21:27:28 +01:00
parent 8963aeccb9
commit 8ae5b6e970
2 changed files with 13 additions and 28 deletions

View File

@ -10,6 +10,7 @@
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <BintrayClient.h> #include <BintrayClient.h>
#include <string> #include <string>
#include <algorithm>
int do_ota_update(); int do_ota_update();
void start_ota_update(); void start_ota_update();

View File

@ -327,38 +327,22 @@ void show_progress(unsigned long current, unsigned long size) {
} }
#endif #endif
// helper function to compare two versions. Returns 1 if v2 is // helper function to convert strings into lower case
bool comp(char s1, char s2) { return tolower(s1) < tolower(s2); }
// helper function to lexicographically compare two versions. Returns 1 if v2 is
// smaller, -1 if v1 is smaller, 0 if equal // smaller, -1 if v1 is smaller, 0 if equal
int version_compare(const String v1, const String v2) { int version_compare(const String v1, const String v2) {
// vnum stores each numeric part of version
int vnum1 = 0, vnum2 = 0;
// loop until both string are processed if (v1 == v2)
for (int i = 0, j = 0; (i < v1.length() || j < v2.length());) { return 0;
// 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 const char *a1 = v1.c_str(), *a2 = v2.c_str();
while (j < v2.length() && v2[j] != '.') {
vnum2 = vnum2 * 10 + (v2[j] - '0');
j++;
}
if (vnum1 > vnum2) if (lexicographical_compare(a1, a1 + strlen(a1), a2, a2 + strlen(a2), comp))
return 1; return -1;
if (vnum2 > vnum1) else
return -1; return 1;
// if equal, reset variables and go for next numeric
// part
vnum1 = vnum2 = 0;
i++;
j++;
}
return 0;
} }
#endif // USE_OTA #endif // USE_OTA