[network-manager-openvpn: 1/14] shared: update shared files



commit d4b8ff8b6449ab86e7c27b6ea6abb19063ce7acf
Author: Thomas Haller <thaller redhat com>
Date:   Sun Feb 12 09:36:13 2017 +0100

    shared: update shared files

 shared/nm-utils/nm-glib.h            |   19 ++++---
 shared/nm-utils/nm-macros-internal.h |   87 +++++++++++++++++++++++-----------
 shared/nm-utils/nm-shared-utils.c    |   50 +++++++++++++++++++-
 shared/nm-utils/nm-shared-utils.h    |    4 ++
 4 files changed, 123 insertions(+), 37 deletions(-)
---
diff --git a/shared/nm-utils/nm-glib.h b/shared/nm-utils/nm-glib.h
index 824a08c..03251ca 100644
--- a/shared/nm-utils/nm-glib.h
+++ b/shared/nm-utils/nm-glib.h
@@ -134,14 +134,17 @@ __g_type_ensure (GType type)
 
 /* Rumtime check for glib version. First do a compile time check which
  * (if satisfied) shortcuts the runtime check. */
-#define nm_glib_check_version(major, minor, micro) \
-    (   GLIB_CHECK_VERSION ((major), (minor), (micro)) \
-     || (   (   glib_major_version > (major)) \
-         || (   glib_major_version == (major) \
-             && glib_minor_version > (minor)) \
-         || (   glib_major_version == (major) \
-             && glib_minor_version == (minor) \
-             && glib_micro_version >= (micro))))
+inline static gboolean
+nm_glib_check_version (guint major, guint minor, guint micro)
+{
+       return    GLIB_CHECK_VERSION (major, minor, micro)
+              || (   (   glib_major_version > major)
+                  || (   glib_major_version == major
+                      && glib_minor_version > minor)
+                  || (   glib_major_version == major
+                      && glib_minor_version == minor
+                      && glib_micro_version < micro));
+}
 
 /* g_test_skip() is only available since glib 2.38. Add a compatibility wrapper. */
 inline static void
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 70476b7..2548665 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -341,6 +341,40 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
 
 /*****************************************************************************/
 
+/* NM_CACHED_QUARK() returns the GQuark for @string, but caches
+ * it in a static variable to speed up future lookups.
+ *
+ * @string must be a string literal.
+ */
+#define NM_CACHED_QUARK(string) \
+       ({ \
+               static GQuark _nm_cached_quark = 0; \
+               \
+               (G_LIKELY (_nm_cached_quark != 0) \
+                       ? _nm_cached_quark \
+                       : (_nm_cached_quark = g_quark_from_static_string (""string""))); \
+       })
+
+/* NM_CACHED_QUARK_FCN() is essentially the same as G_DEFINE_QUARK
+ * with two differences:
+ * - @string must be a quited string-literal
+ * - @fcn must be the full function name, while G_DEFINE_QUARK() appends
+ *   "_quark" to the function name.
+ * Both properties of G_DEFINE_QUARK() are non favorable, because you can no
+ * longer grep for string/fcn -- unless you are aware that you are searching
+ * for G_DEFINE_QUARK() and omit quotes / append _quark(). With NM_CACHED_QUARK_FCN(),
+ * ctags/cscope can locate the use of @fcn (though it doesn't recognize that
+ * NM_CACHED_QUARK_FCN() defines it).
+ */
+#define NM_CACHED_QUARK_FCN(string, fcn) \
+GQuark \
+fcn (void) \
+{ \
+       return NM_CACHED_QUARK (string); \
+}
+
+/*****************************************************************************/
+
 #define nm_streq(s1, s2)  (strcmp (s1, s2) == 0)
 #define nm_streq0(s1, s2) (g_strcmp0 (s1, s2) == 0)
 
@@ -743,32 +777,29 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
 /*****************************************************************************/
 
 /* if @str is NULL, return "(null)". Otherwise, allocate a buffer using
- * alloca() of size @bufsize and fill it with @str. @str will be quoted with
- * single quote, and in case @str is too long, the final quote will be '^'. */
-#define nm_strquote_a(bufsize, str) \
+ * alloca() of and fill it with @str. @str will be quoted with double quote.
+ * If @str is longer then @trunc_at, the string is truncated and the closing
+ * quote is instead '^' to indicate truncation.
+ *
+ * Thus, the maximum stack allocated buffer will be @trunc_at+3. */
+#define nm_strquote_a(trunc_at, str) \
        ({ \
-               G_STATIC_ASSERT ((bufsize) >= 6); \
-               const gsize _BUFSIZE = (bufsize); \
-               const char *const _s = (str); \
-               char *_r; \
-               gsize _l; \
-               gboolean _truncated; \
-               \
-               nm_assert (_BUFSIZE >= 6); \
+               const char *const _str = (str); \
                \
-               if (_s) { \
-                       _l = strlen (_s) + 3; \
-                       if ((_truncated = (_BUFSIZE < _l))) \
-                               _l = _BUFSIZE; \
-                       \
-                       _r = g_alloca (_l); \
-                       _r[0] = '\''; \
-                       memcpy (&_r[1], _s, _l - 3); \
-                       _r[_l - 2] = _truncated ? '^' : '\''; \
-                       _r[_l - 1] = '\0'; \
-               } else \
-                       _r = "(null)"; \
-               _r; \
+               (_str \
+                       ? ({ \
+                               const gsize _trunc_at = (trunc_at); \
+                               const gsize _strlen_trunc = NM_MIN (strlen (_str), _trunc_at); \
+                               char *_buf; \
+                               \
+                               _buf = g_alloca (_strlen_trunc + 3); \
+                               _buf[0] = '"'; \
+                               memcpy (&_buf[1], _str, _strlen_trunc); \
+                               _buf[_strlen_trunc + 1] = _str[_strlen_trunc] ? '^' : '"'; \
+                               _buf[_strlen_trunc + 2] = '\0'; \
+                               _buf; \
+                       }) \
+                       : "(null)"); \
        })
 
 #define nm_sprintf_buf(buf, format, ...) ({ \
@@ -788,12 +819,12 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
        ({ \
                char *_buf; \
                int _buf_len; \
+               typeof (n_elements) _n_elements = (n_elements); \
                \
-               G_STATIC_ASSERT (sizeof (char[MAX ((n_elements), 1)]) == (n_elements)); \
-               _buf = g_alloca (n_elements); \
-               _buf_len = g_snprintf (_buf, (n_elements), \
+               _buf = g_alloca (_n_elements); \
+               _buf_len = g_snprintf (_buf, _n_elements, \
                                       ""format"", ##__VA_ARGS__); \
-               nm_assert (_buf_len < (n_elements)); \
+               nm_assert (_buf_len < _n_elements); \
                _buf; \
        })
 
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index 38bb818..413526d 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -158,6 +158,54 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
 
 /*****************************************************************************/
 
+/**
+ * nm_utils_strv_find_first:
+ * @list: the strv list to search
+ * @len: the length of the list, or a negative value if @list is %NULL terminated.
+ * @needle: the value to search for. The search is done using strcmp().
+ *
+ * Searches @list for @needle and returns the index of the first match (based
+ * on strcmp()).
+ *
+ * For convenience, @list has type 'char**' instead of 'const char **'.
+ *
+ * Returns: index of first occurrence or -1 if @needle is not found in @list.
+ */
+gssize
+nm_utils_strv_find_first (char **list, gssize len, const char *needle)
+{
+       gssize i;
+
+       if (len > 0) {
+               g_return_val_if_fail (list, -1);
+
+               if (!needle) {
+                       /* if we search a list with known length, %NULL is a valid @needle. */
+                       for (i = 0; i < len; i++) {
+                               if (!list[i])
+                                       return i;
+                       }
+               } else {
+                       for (i = 0; i < len; i++) {
+                               if (list[i] && !strcmp (needle, list[i]))
+                                       return i;
+                       }
+               }
+       } else if (len < 0) {
+               g_return_val_if_fail (needle, -1);
+
+               if (list) {
+                       for (i = 0; list[i]; i++) {
+                               if (strcmp (needle, list[i]) == 0)
+                                       return i;
+                       }
+               }
+       }
+       return -1;
+}
+
+/*****************************************************************************/
+
 gint
 _nm_utils_ascii_str_to_bool (const char *str,
                              gint default_value)
@@ -192,7 +240,7 @@ _nm_utils_ascii_str_to_bool (const char *str,
 
 /*****************************************************************************/
 
-G_DEFINE_QUARK (nm-utils-error-quark, nm_utils_error)
+NM_CACHED_QUARK_FCN ("nm-utils-error-quark", nm_utils_error_quark)
 
 void
 nm_utils_error_set_cancelled (GError **error,
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index 5d8a3a8..f1f9f51 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -43,6 +43,10 @@ void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str);
 
 /*****************************************************************************/
 
+gssize nm_utils_strv_find_first (char **list, gssize len, const char *needle);
+
+/*****************************************************************************/
+
 gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
 
 gint _nm_utils_ascii_str_to_bool (const char *str,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]