[pango/visible-things: 58/71] Add a helper for parsing flags



commit c0784d7f2ed88afe3b1d8ea4cd5b91e0ac1aa5db
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jul 22 19:25:08 2019 -0700

    Add a helper for parsing flags

 pango/pango-utils-internal.h |  4 +++
 pango/pango-utils.c          | 62 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
---
diff --git a/pango/pango-utils-internal.h b/pango/pango-utils-internal.h
index d6e55d7c..56340215 100644
--- a/pango/pango-utils-internal.h
+++ b/pango/pango-utils-internal.h
@@ -36,6 +36,10 @@ gboolean _pango_parse_enum              (GType       type,
                                          int        *value,
                                          gboolean    warn,
                                          char      **possible_values);
+gboolean pango_parse_flags              (GType       type,
+                                         const char *str,
+                                         int        *value,
+                                         char      **possible_values);
 
 char    *_pango_trim_string             (const char *str);
 
diff --git a/pango/pango-utils.c b/pango/pango-utils.c
index 5fc4475d..6f48c62c 100644
--- a/pango/pango-utils.c
+++ b/pango/pango-utils.c
@@ -772,6 +772,68 @@ _pango_parse_enum (GType       type,
   return ret;
 }
 
+gboolean
+pango_parse_flags (GType        type,
+                   const char  *str,
+                   int         *value,
+                   char       **possible_values)
+{
+  GFlagsClass *class = NULL;
+  gboolean ret = TRUE;
+  GFlagsValue *v = NULL;
+
+  class = g_type_class_ref (type);
+
+  v = g_flags_get_value_by_nick (class, str);
+
+  if (v)
+    {
+      *value = v->value;
+    }
+  else if (!parse_int (str, value))
+    {
+      char **strv = g_strsplit (str, "|", 0);
+      int i;
+
+      *value = 0;
+
+      for (i = 0; strv[i]; i++)
+        {
+          strv[i] = g_strstrip (strv[i]);
+          v = g_flags_get_value_by_nick (class, strv[i]);
+          if (!v)
+            {
+              ret = FALSE;
+              break;
+            }
+          *value |= v->value;
+        }
+      g_strfreev (strv);
+
+      if (!ret && possible_values)
+       {
+         int i;
+         GString *s = g_string_new (NULL);
+
+          for (i = 0; i < class->n_values; i++)
+            {
+              v = &class->values[i];
+              if (i)
+                g_string_append_c (s, '/');
+              g_string_append (s, v->value_nick);
+            }
+
+          *possible_values = s->str;
+
+          g_string_free (s, FALSE);
+       }
+    }
+
+  g_type_class_unref (class);
+
+  return ret;
+}
+
 /**
  * pango_lookup_aliases:
  * @fontname: an ascii string


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