glib-aux: add _nm_utils_ascii_str_to_int64_bin() helper

(cherry picked from commit 70b7ad1a76)
This commit is contained in:
Thomas Haller 2021-04-20 12:14:13 +02:00 committed by Fernando Fernandez Mancera
parent d01912d2fc
commit b8bb585052
2 changed files with 46 additions and 0 deletions

View file

@ -1383,6 +1383,45 @@ _nm_utils_ascii_str_to_uint64(const char *str,
/*****************************************************************************/
gint64
_nm_utils_ascii_str_to_int64_bin(const char *str,
gssize len,
guint base,
gint64 min,
gint64 max,
gint64 fallback)
{
gs_free char *str_clone = NULL;
/* This is like _nm_utils_ascii_str_to_int64(), but the user may provide
* an optional string length, in which case str is not assumed to be NUL
* terminated. In that case, any NUL characters inside the first len characters
* lead to a failure, except one last NUL character is allowed. */
if (len >= 0) {
gsize l = len;
nm_assert(l == 0 || str);
if (l > 0 && str[l - 1u] == '\0') {
/* we accept one '\0' at the end of the string. */
l--;
}
if (l > 0 && memchr(str, '\0', l)) {
/* but we don't accept other NUL characters in the middle. */
errno = EINVAL;
return fallback;
}
str = nm_strndup_a(300, str, len, &str_clone);
}
return _nm_utils_ascii_str_to_int64(str, base, min, max, fallback);
}
/*****************************************************************************/
int
nm_strcmp_with_data(gconstpointer a, gconstpointer b, gpointer user_data)
{

View file

@ -760,6 +760,13 @@ guint64 _nm_utils_ascii_str_to_uint64(const char *str,
guint64 max,
guint64 fallback);
gint64 _nm_utils_ascii_str_to_int64_bin(const char *str,
gssize len,
guint base,
gint64 min,
gint64 max,
gint64 fallback);
int _nm_utils_ascii_str_to_bool(const char *str, int default_value);
/*****************************************************************************/