[gtk/wip/otte/css: 181/225] cssimagefallback: Use gtk_css_parser_consume_function()



commit ce6987b3c2a0b833343127eec8e948dc3184ffa3
Author: Benjamin Otte <otte redhat com>
Date:   Sun Mar 31 00:52:22 2019 +0100

    cssimagefallback: Use gtk_css_parser_consume_function()

 gtk/gtkcssimagefallback.c | 79 +++++++++++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 34 deletions(-)
---
diff --git a/gtk/gtkcssimagefallback.c b/gtk/gtkcssimagefallback.c
index f109337468..c33912e00f 100644
--- a/gtk/gtkcssimagefallback.c
+++ b/gtk/gtkcssimagefallback.c
@@ -175,57 +175,68 @@ gtk_css_image_fallback_compute (GtkCssImage      *image,
     return GTK_CSS_IMAGE (g_object_ref (fallback));
 }
 
-static gboolean
-gtk_css_image_fallback_parse (GtkCssImage  *image,
-                              GtkCssParser *parser)
+typedef struct
 {
-  GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
+  GtkCssValue *color;
   GPtrArray *images;
-  GtkCssImage *child;
+} ParseData;
+
+static guint
+gtk_css_image_fallback_parse_arg (GtkCssParser *parser,
+                                  guint         arg,
+                                  gpointer      _data)
+{
+  ParseData *data = _data;
 
-  if (!_gtk_css_parser_try (parser, "image", TRUE))
+  if (data->color != NULL)
     {
-      _gtk_css_parser_error (parser, "'image'");
-      return FALSE;
+      _gtk_css_parser_error (parser, "The color must be the last parameter");
+      return 0;
     }
+  else if (_gtk_css_image_can_parse (parser))
+    {
+      GtkCssImage *image = _gtk_css_image_new_parse (parser);
+      if (image == NULL)
+        return 0;
 
-  if (!_gtk_css_parser_try (parser, "(", TRUE))
+      g_ptr_array_add (data->images, image);
+      return 1;
+    }
+  else
     {
-      _gtk_css_parser_error (parser,
-                             "Expected '(' after 'image'");
-      return FALSE;
+      data->color = _gtk_css_color_value_parse (parser);
+      if (data->color == NULL)
+        return 0;
+
+      return 1;
     }
+}
 
-  images = g_ptr_array_new_with_free_func (g_object_unref);
+static gboolean
+gtk_css_image_fallback_parse (GtkCssImage  *image,
+                              GtkCssParser *parser)
+{
+  GtkCssImageFallback *self = GTK_CSS_IMAGE_FALLBACK (image);
+  ParseData data = { NULL, NULL };
 
-  do
+  if (!gtk_css_parser_has_function (parser, "image"))
     {
-      child = NULL;
-      if (_gtk_css_image_can_parse (parser))
-        child = _gtk_css_image_new_parse (parser);
-      if (child == NULL)
-        {
-          fallback->color = _gtk_css_color_value_parse (parser);
-          if (fallback->color)
-            break;
-
-          g_ptr_array_free (images, TRUE);
-          return FALSE;
-        }
-      g_ptr_array_add (images, child);
+      _gtk_css_parser_error (parser, "Expected 'image('");
+      return FALSE;
     }
-  while ( _gtk_css_parser_try (parser, ",", TRUE));
 
-  if (!_gtk_css_parser_try (parser, ")", TRUE))
+  data.images = g_ptr_array_new_with_free_func (g_object_unref);
+
+  if (!gtk_css_parser_consume_function (parser, 1, G_MAXUINT, gtk_css_image_fallback_parse_arg, &data))
     {
-      g_ptr_array_free (images, TRUE);
-      _gtk_css_parser_error (parser,
-                             "Expected ')' at end of 'image'");
+      g_clear_pointer (&data.color, _gtk_css_value_unref);
+      g_ptr_array_free (data.images, TRUE);
       return FALSE;
     }
 
-  fallback->n_images = images->len;
-  fallback->images = (GtkCssImage **) g_ptr_array_free (images, FALSE);
+  self->color = data.color;
+  self->n_images = data.images->len;
+  self->images = (GtkCssImage **) g_ptr_array_free (data.images, FALSE);
 
   return TRUE;
 }


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