diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2019-03-05 10:06:45 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@gnu.org> | 2019-03-29 10:07:29 +0000 |
commit | 7330f6d5812df26c0518e04eccd3092833c6e4f3 (patch) | |
tree | e3f470c3a0b86c799ce6efe40d1be91080f9e3b6 | |
parent | 0a5d63872685691d43a87a27c0b700a327cf5098 (diff) | |
download | ModemManager-7330f6d5812df26c0518e04eccd3092833c6e4f3.tar.gz |
libmm-glib,common: new helper methods to read guint64 values
We use strtoull() to read a "unsigned long long" that is always at
least 64bits, even in 32bit systems.
-rw-r--r-- | libmm-glib/mm-common-helpers.c | 89 | ||||
-rw-r--r-- | libmm-glib/mm-common-helpers.h | 35 |
2 files changed, 73 insertions, 51 deletions
diff --git a/libmm-glib/mm-common-helpers.c b/libmm-glib/mm-common-helpers.c index 67a310f32..a3cd16ab9 100644 --- a/libmm-glib/mm-common-helpers.c +++ b/libmm-glib/mm-common-helpers.c @@ -1353,22 +1353,24 @@ mm_get_int_from_match_info (GMatchInfo *match_info, return ret; } -/** - * mm_get_uint_from_str: - * @str: the string to convert to an unsigned int - * @out: on success, the number - * - * Converts a string to an unsigned number. All characters in the string - * MUST be valid digits (0 - 9), otherwise FALSE is returned. - * - * Returns: %TRUE if the string was converted, %FALSE if it was not or if it - * did not contain only digits. - */ gboolean mm_get_uint_from_str (const gchar *str, - guint *out) + guint *out) +{ + guint64 num; + + if (!mm_get_u64_from_str (str, &num) || num > G_MAXUINT) + return FALSE; + + *out = (guint)num; + return TRUE; +} + +gboolean +mm_get_u64_from_str (const gchar *str, + guint64 *out) { - gulong num; + guint64 num; if (!str || !str[0]) return FALSE; @@ -1379,33 +1381,32 @@ mm_get_uint_from_str (const gchar *str, } errno = 0; - num = strtoul (str, NULL, 10); - if (!errno && num <= G_MAXUINT) { - *out = (guint)num; + num = (guint64) strtoull (str, NULL, 10); + if (!errno) { + *out = num; return TRUE; } return FALSE; } -/** - * mm_get_uint_from_hex_str: - * @str: the hex string to convert to an unsigned int - * @out: on success, the number - * - * Converts a string to an unsigned number. All characters in the string - * MUST be valid hexadecimal digits (0-9, A-F, a-f), otherwise FALSE is - * returned. - * - * An optional "0x" prefix may be given in @str. - * - * Returns: %TRUE if the string was converted, %FALSE if it was not or if it - * did not contain only digits. - */ gboolean mm_get_uint_from_hex_str (const gchar *str, guint *out) { - gulong num; + guint64 num; + + if (!mm_get_u64_from_hex_str (str, &num) || num > G_MAXUINT) + return FALSE; + + *out = (guint)num; + return TRUE; +} + +gboolean +mm_get_u64_from_hex_str (const gchar *str, + guint64 *out) +{ + guint64 num; if (!str) return FALSE; @@ -1422,9 +1423,9 @@ mm_get_uint_from_hex_str (const gchar *str, } errno = 0; - num = strtoul (str, NULL, 16); - if (!errno && num <= G_MAXUINT) { - *out = (guint)num; + num = (guint64) strtoull (str, NULL, 16); + if (!errno) { + *out = num; return TRUE; } return FALSE; @@ -1432,8 +1433,22 @@ mm_get_uint_from_hex_str (const gchar *str, gboolean mm_get_uint_from_match_info (GMatchInfo *match_info, - guint32 match_index, - guint *out) + guint32 match_index, + guint *out) +{ + guint64 num; + + if (!mm_get_u64_from_match_info (match_info, match_index, &num) || num > G_MAXUINT) + return FALSE; + + *out = (guint)num; + return TRUE; +} + +gboolean +mm_get_u64_from_match_info (GMatchInfo *match_info, + guint32 match_index, + guint64 *out) { gchar *s; gboolean ret; @@ -1442,7 +1457,7 @@ mm_get_uint_from_match_info (GMatchInfo *match_info, if (!s) return FALSE; - ret = mm_get_uint_from_str (s, out); + ret = mm_get_u64_from_str (s, out); g_free (s); return ret; diff --git a/libmm-glib/mm-common-helpers.h b/libmm-glib/mm-common-helpers.h index fce9ff259..71ef978af 100644 --- a/libmm-glib/mm-common-helpers.h +++ b/libmm-glib/mm-common-helpers.h @@ -146,24 +146,31 @@ gboolean mm_common_parse_key_value_string (const gchar *str, /* Common parsers */ gboolean mm_get_int_from_str (const gchar *str, - gint *out); -gboolean mm_get_int_from_match_info (GMatchInfo *match_info, - guint32 match_index, - gint *out); + gint *out); +gboolean mm_get_int_from_match_info (GMatchInfo *match_info, + guint32 match_index, + gint *out); gboolean mm_get_uint_from_str (const gchar *str, - guint *out); + guint *out); +gboolean mm_get_u64_from_str (const gchar *str, + guint64 *out); gboolean mm_get_uint_from_hex_str (const gchar *str, guint *out); -gboolean mm_get_uint_from_match_info (GMatchInfo *match_info, - guint32 match_index, - guint *out); +gboolean mm_get_u64_from_hex_str (const gchar *str, + guint64 *out); +gboolean mm_get_uint_from_match_info (GMatchInfo *match_info, + guint32 match_index, + guint *out); +gboolean mm_get_u64_from_match_info (GMatchInfo *match_info, + guint32 match_index, + guint64 *out); gboolean mm_get_double_from_str (const gchar *str, - gdouble *out); -gboolean mm_get_double_from_match_info (GMatchInfo *match_info, - guint32 match_index, - gdouble *out); -gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info, - guint32 match_index); + gdouble *out); +gboolean mm_get_double_from_match_info (GMatchInfo *match_info, + guint32 match_index, + gdouble *out); +gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info, + guint32 match_index); const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state); |