mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 20:53:46 +08:00
[esp32_hosted] Replace sscanf with strtol for version parsing (#13658)
This commit is contained in:
@@ -34,14 +34,29 @@ static const char *const ESP_HOSTED_VERSION_STR = STRINGIFY(ESP_HOSTED_VERSION_M
|
|||||||
ESP_HOSTED_VERSION_MINOR_1) "." STRINGIFY(ESP_HOSTED_VERSION_PATCH_1);
|
ESP_HOSTED_VERSION_MINOR_1) "." STRINGIFY(ESP_HOSTED_VERSION_PATCH_1);
|
||||||
|
|
||||||
#ifdef USE_ESP32_HOSTED_HTTP_UPDATE
|
#ifdef USE_ESP32_HOSTED_HTTP_UPDATE
|
||||||
|
// Parse an integer from str, advancing ptr past the number
|
||||||
|
// Returns false if no digits were parsed
|
||||||
|
static bool parse_int(const char *&ptr, int &value) {
|
||||||
|
char *end;
|
||||||
|
value = static_cast<int>(strtol(ptr, &end, 10));
|
||||||
|
if (end == ptr)
|
||||||
|
return false;
|
||||||
|
ptr = end;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Parse version string "major.minor.patch" into components
|
// Parse version string "major.minor.patch" into components
|
||||||
// Returns true if parsing succeeded
|
// Returns true if at least major.minor was parsed
|
||||||
static bool parse_version(const std::string &version_str, int &major, int &minor, int &patch) {
|
static bool parse_version(const std::string &version_str, int &major, int &minor, int &patch) {
|
||||||
major = minor = patch = 0;
|
major = minor = patch = 0;
|
||||||
if (sscanf(version_str.c_str(), "%d.%d.%d", &major, &minor, &patch) >= 2) {
|
const char *ptr = version_str.c_str();
|
||||||
return true;
|
|
||||||
}
|
if (!parse_int(ptr, major) || *ptr++ != '.' || !parse_int(ptr, minor))
|
||||||
return false;
|
return false;
|
||||||
|
if (*ptr == '.')
|
||||||
|
parse_int(++ptr, patch);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare two versions, returns:
|
// Compare two versions, returns:
|
||||||
|
|||||||
Reference in New Issue
Block a user