shared: move _nm_utils_ascii_str_to_int64() to "shared/nm-shared-utils.h"
authorThomas Haller <thaller@redhat.com>
Sun, 20 Mar 2016 09:32:43 +0000 (10:32 +0100)
committerThomas Haller <thaller@redhat.com>
Sat, 26 Mar 2016 11:10:53 +0000 (12:10 +0100)
_nm_utils_ascii_str_to_int64() was declared in libnm-core's internal
header "nm-core-internal.h" and thus available for libnm-core, libnm,
NetworkManager and related.

It also means, the function was not available in libnm-util, libnm-glib,
clients or dispatcher. So, we either reimplemented it (nmc_string_to_int_base)
or struggle with the awkward strtol* API.

libnm-core/nm-core-internal.h
libnm-core/nm-utils.c
shared/nm-shared-utils.c
shared/nm-shared-utils.h

index 3e1236b..92de9d1 100644 (file)
@@ -179,8 +179,6 @@ GByteArray *nm_utils_rsa_key_encrypt (const guint8 *data,
                                       char **out_password,
                                       GError **error);
 
-gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
-
 gulong _nm_dbus_signal_connect_data (GDBusProxy *proxy,
                                      const char *signal_name,
                                      const GVariantType *signature,
index 10d54fa..a846708 100644 (file)
@@ -3789,81 +3789,6 @@ _nm_utils_strstrdictkey_create (const char *v1, const char *v2)
        return k;
 }
 
-/**********************************************************************************************/
-
-/* _nm_utils_ascii_str_to_int64:
- *
- * A wrapper for g_ascii_strtoll, that checks whether the whole string
- * can be successfully converted to a number and is within a given
- * range. On any error, @fallback will be returned and %errno will be set
- * to a non-zero value. On success, %errno will be set to zero, check %errno
- * for errors. Any trailing or leading (ascii) white space is ignored and the
- * functions is locale independent.
- *
- * The function is guaranteed to return a value between @min and @max
- * (inclusive) or @fallback. Also, the parsing is rather strict, it does
- * not allow for any unrecognized characters, except leading and trailing
- * white space.
- **/
-gint64
-_nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback)
-{
-       gint64 v;
-       size_t len;
-       char buf[64], *s, *str_free = NULL;
-
-       if (str) {
-               while (g_ascii_isspace (str[0]))
-                       str++;
-       }
-       if (!str || !str[0]) {
-               errno = EINVAL;
-               return fallback;
-       }
-
-       len = strlen (str);
-       if (g_ascii_isspace (str[--len])) {
-               /* backward search the first non-ws character.
-                * We already know that str[0] is non-ws. */
-               while (g_ascii_isspace (str[--len]))
-                       ;
-
-               /* str[len] is now the last non-ws character... */
-               len++;
-
-               if (len >= sizeof (buf))
-                       s = str_free = g_malloc (len + 1);
-               else
-                       s = buf;
-
-               memcpy (s, str, len);
-               s[len] = 0;
-
-               nm_assert (len > 0 && len < strlen (str) && len == strlen (s));
-               nm_assert (!g_ascii_isspace (str[len-1]) && g_ascii_isspace (str[len]));
-               nm_assert (strncmp (str, s, len) == 0);
-
-               str = s;
-       }
-
-       errno = 0;
-       v = g_ascii_strtoll (str, &s, base);
-
-       if (errno != 0)
-               v = fallback;
-       else if (s[0] != 0) {
-               errno = EINVAL;
-               v = fallback;
-       } else if (v > max || v < min) {
-               errno = ERANGE;
-               v = fallback;
-       }
-
-       if (G_UNLIKELY (str_free))
-               g_free (str_free);
-       return v;
-}
-
 static gboolean
 validate_dns_option (const char *name, gboolean numeric, gboolean ipv6,
                      const NMUtilsDNSOptionDesc *option_descs)
index b2c164e..b80b9f2 100644 (file)
 
 #include "nm-shared-utils.h"
 
+#include <errno.h>
+
+/*****************************************************************************/
+
+/* _nm_utils_ascii_str_to_int64:
+ *
+ * A wrapper for g_ascii_strtoll, that checks whether the whole string
+ * can be successfully converted to a number and is within a given
+ * range. On any error, @fallback will be returned and %errno will be set
+ * to a non-zero value. On success, %errno will be set to zero, check %errno
+ * for errors. Any trailing or leading (ascii) white space is ignored and the
+ * functions is locale independent.
+ *
+ * The function is guaranteed to return a value between @min and @max
+ * (inclusive) or @fallback. Also, the parsing is rather strict, it does
+ * not allow for any unrecognized characters, except leading and trailing
+ * white space.
+ **/
+gint64
+_nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback)
+{
+       gint64 v;
+       size_t len;
+       char buf[64], *s, *str_free = NULL;
+
+       if (str) {
+               while (g_ascii_isspace (str[0]))
+                       str++;
+       }
+       if (!str || !str[0]) {
+               errno = EINVAL;
+               return fallback;
+       }
+
+       len = strlen (str);
+       if (g_ascii_isspace (str[--len])) {
+               /* backward search the first non-ws character.
+                * We already know that str[0] is non-ws. */
+               while (g_ascii_isspace (str[--len]))
+                       ;
+
+               /* str[len] is now the last non-ws character... */
+               len++;
+
+               if (len >= sizeof (buf))
+                       s = str_free = g_malloc (len + 1);
+               else
+                       s = buf;
+
+               memcpy (s, str, len);
+               s[len] = 0;
+
+               nm_assert (len > 0 && len < strlen (str) && len == strlen (s));
+               nm_assert (!g_ascii_isspace (str[len-1]) && g_ascii_isspace (str[len]));
+               nm_assert (strncmp (str, s, len) == 0);
+
+               str = s;
+       }
+
+       errno = 0;
+       v = g_ascii_strtoll (str, &s, base);
+
+       if (errno != 0)
+               v = fallback;
+       else if (s[0] != 0) {
+               errno = EINVAL;
+               v = fallback;
+       } else if (v > max || v < min) {
+               errno = ERANGE;
+               v = fallback;
+       }
+
+       if (G_UNLIKELY (str_free))
+               g_free (str_free);
+       return v;
+}
+
+/*****************************************************************************/
index 2619d04..943e467 100644 (file)
@@ -24,4 +24,8 @@
 
 /******************************************************************************/
 
+gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
+
+/******************************************************************************/
+
 #endif /* __NM_SHARED_UTILS_H__ */