diff options
author | Matthias Clasen <mclasen@redhat.com> | 2019-07-22 19:25:08 -0700 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2019-07-25 00:40:16 -0400 |
commit | fd009f03a1263aa30cdf574664e6180dacbf31d7 (patch) | |
tree | 0df47ac1a8f7ce7f6f57ba8d2c2bdb2009ebca2b | |
parent | 201afb727c0ca2e428711d25f74d0f0e717da7de (diff) | |
download | pango-fd009f03a1263aa30cdf574664e6180dacbf31d7.tar.gz |
Add a helper for parsing flags
We are about to add an attribute whose values
are flags.
-rw-r--r-- | pango/pango-utils-internal.h | 4 | ||||
-rw-r--r-- | pango/pango-utils.c | 62 |
2 files changed, 66 insertions, 0 deletions
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 |