diff options
author | Benjamin Otte <otte@redhat.com> | 2012-04-03 09:49:37 +0200 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2012-04-17 08:59:17 +0200 |
commit | b9ebe8c2269d2040d50448fb72f72c344d459625 (patch) | |
tree | 9d883097e33e3927703d73067235b6afc68ea49b /gtk/gtkcssarrayvalue.c | |
parent | 580b5e4a83c2adf2714c6e7f73fa864e92215188 (diff) | |
download | gtk+-b9ebe8c2269d2040d50448fb72f72c344d459625.tar.gz |
cssarrayvalue: Redo parsing arrays
Does 3 things:
1) Introduce a "none" array signleton
2) Get rid of memleaks in error paths
3) Reduce code in parse funcs
Diffstat (limited to 'gtk/gtkcssarrayvalue.c')
-rw-r--r-- | gtk/gtkcssarrayvalue.c | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/gtk/gtkcssarrayvalue.c b/gtk/gtkcssarrayvalue.c index 5260eaf653..baab5fa820 100644 --- a/gtk/gtkcssarrayvalue.c +++ b/gtk/gtkcssarrayvalue.c @@ -94,18 +94,62 @@ static const GtkCssValueClass GTK_CSS_VALUE_ARRAY = { gtk_css_value_array_print }; +static GtkCssValue none_singleton = { >K_CSS_VALUE_ARRAY, 1, 0, { NULL } }; + GtkCssValue * -_gtk_css_array_value_new (GtkCssValue **values, - guint n_values) +_gtk_css_array_value_new (GtkCssValue *content) { - GtkCssValue *result; - - g_return_val_if_fail (values != NULL || n_values == 0, NULL); + if (content == NULL) + return _gtk_css_value_ref (&none_singleton); - result = _gtk_css_value_alloc (>K_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (MAX (1, n_values) - 1)); + return _gtk_css_array_value_new_from_array (&content, 1); +} + +GtkCssValue * +_gtk_css_array_value_new_from_array (GtkCssValue **values, + guint n_values) +{ + GtkCssValue *result; + + g_return_val_if_fail (values != NULL, NULL); + g_return_val_if_fail (n_values > 0, NULL); + + result = _gtk_css_value_alloc (>K_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (n_values - 1)); result->n_values = n_values; memcpy (&result->values[0], values, sizeof (GtkCssValue *) * n_values); + + return result; +} + +GtkCssValue * +_gtk_css_array_value_parse (GtkCssParser *parser, + GtkCssValue *(* parse_func) (GtkCssParser *parser), + gboolean allow_none) +{ + GtkCssValue *value, *result; + GPtrArray *values; + + if (allow_none && + _gtk_css_parser_try (parser, "none", TRUE)) + return _gtk_css_value_ref (&none_singleton); + + values = g_ptr_array_new (); + + do { + value = parse_func (parser); + + if (value == NULL) + { + g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref); + g_ptr_array_free (values, TRUE); + return NULL; + } + + g_ptr_array_add (values, value); + } while (_gtk_css_parser_try (parser, ",", TRUE)); + result = _gtk_css_array_value_new_from_array ((GtkCssValue **) values->pdata, values->len); + g_ptr_array_free (values, TRUE); return result; } |