From d89dfafb510a1912027a6dd332a1976c96bb2cb7 Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Thu, 11 Jan 2024 13:43:31 +0100 Subject: [PATCH] Add read_version() to convert version string to numbers read_version() takes a version string in format "x.y.z" with x being major, y minor, and z patch number. It transforms reads them into separate numbers, and outputs in the correspondingly named variables. --- common/utils/utils.c | 18 ++++++++++++++++++ common/utils/utils.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/common/utils/utils.c b/common/utils/utils.c index 831b4afbd3..66b840c7d9 100644 --- a/common/utils/utils.c +++ b/common/utils/utils.c @@ -132,3 +132,21 @@ void set_priority(int priority) abort(); } } + +/** + * @brief Convert a version string x.y.z into numbers. + * + * The function takes a version string of format "x.y.z" where x is the major + * version number, y minor, z patch. It tries to match version, and outputs the + * numbers in the correspondingly named variables. + * + * @return The number of version parts matched (should be three on x.y.z). + */ +int read_version(const char *version, uint8_t *major, uint8_t *minor, uint8_t *patch) +{ + int ret = sscanf(version, "%hhu.%hhu.%hhu", major, minor, patch); + // EOF means "end of input reached or matching failure" + if (ret == EOF) + return 3; + return ret; // ret has number of items matched +} diff --git a/common/utils/utils.h b/common/utils/utils.h index 52f94fb3ce..b1a7305cd4 100644 --- a/common/utils/utils.h +++ b/common/utils/utils.h @@ -107,6 +107,10 @@ void set_priority(int priority); char *itoa(int i); +#define STRINGIFY(S) #S +#define TO_STRING(S) STRINGIFY(S) +int read_version(const char *version, uint8_t *major, uint8_t *minor, uint8_t *patch); + #define findInList(keY, result, list, element_type) {\ int i;\ for (i=0; i