[gegl] gegl-serialize: add version and indent flags for serializer



commit 082b0102d56fa0d8303d40a0c45614312c0a0fa1
Author: Øyvind Kolås <pippin gimp org>
Date:   Wed Mar 29 14:56:03 2017 +0200

    gegl-serialize: add version and indent flags for serializer

 bin/gegl.c            |    2 +-
 bin/ui.c              |    2 +-
 gegl/gegl-serialize.c |   39 ++++++++++++++++++++++++++++++++++++---
 gegl/gegl-utils.h     |    3 ++-
 4 files changed, 40 insertions(+), 6 deletions(-)
---
diff --git a/bin/gegl.c b/bin/gegl.c
index 5553efe..a1e075c 100644
--- a/bin/gegl.c
+++ b/bin/gegl.c
@@ -221,7 +221,7 @@ main (gint    argc,
       {
         fprintf (stderr, "%s\n", gegl_serialize (iter,
             gegl_node_get_producer (proxy, "input", NULL), "/",
-            GEGL_SERIALIZE_TRIM_DEFAULTS|GEGL_SERIALIZE_VERSION));
+            GEGL_SERIALIZE_VERSION|GEGL_SERIALIZE_INDENT));
       }
     }
   }
diff --git a/bin/ui.c b/bin/ui.c
index 5a8d58e..8cb6278 100644
--- a/bin/ui.c
+++ b/bin/ui.c
@@ -1948,7 +1948,7 @@ static void save_cb (MrgEvent *event, void *data1, void *data2)
   gegl_node_link_many (load, o->source, NULL);
   {
     char *containing_path = get_path_parent (o->path);
-    serialized = gegl_serialize (NULL, o->sink, containing_path, 
GEGL_SERIALIZE_TRIM_DEFAULTS|GEGL_SERIALIZE_VERSION);
+    serialized = gegl_serialize (NULL, o->sink, containing_path, 
GEGL_SERIALIZE_TRIM_DEFAULTS|GEGL_SERIALIZE_VERSION|GEGL_SERIALIZE_INDENT);
     free (containing_path);
   }
   gegl_node_remove_child (o->gegl, load);
diff --git a/gegl/gegl-serialize.c b/gegl/gegl-serialize.c
index 1c79397..2240296 100644
--- a/gegl/gegl-serialize.c
+++ b/gegl/gegl-serialize.c
@@ -563,8 +563,13 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
     else
     {
       GString *s2 = g_string_new ("");
-      g_string_append_printf (s2, " %s", gegl_node_get_operation (iter));
-      g_string_append_printf (s2, " opi=%s", gegl_node_get_op_version (iter));
+      if (!(flags & GEGL_SERIALIZE_INDENT))
+        g_string_append_printf (s2, " ");
+      g_string_append_printf (s2, "%s", gegl_node_get_operation (iter));
+      if (flags & GEGL_SERIALIZE_VERSION)
+        g_string_append_printf (s2, " opi=%s", gegl_node_get_op_version (iter));
+      if (flags & GEGL_SERIALIZE_INDENT)
+        g_string_append_printf (s2, "\n");
       {
         gint i;
         guint n_properties;
@@ -576,6 +581,8 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
           const gchar *property_name = g_param_spec_get_name (properties[i]);
           GType        property_type = G_PARAM_SPEC_VALUE_TYPE (properties[i]);
           const GValue*default_value = g_param_spec_get_default_value (properties[i]);
+          gboolean printed = FALSE;
+
 
           if (property_type == G_TYPE_FLOAT)
             {
@@ -586,7 +593,10 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               if (value != defval || (!trim_defaults))
               {
                 g_ascii_dtostr (str, sizeof(str), value);
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_string_append_printf (s2, " %s=%s", property_name, str);
+                printed = TRUE;
               }
             }
           else if (property_type == G_TYPE_DOUBLE)
@@ -597,8 +607,11 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               gegl_node_get (iter, property_name, &value, NULL);
               if (value != defval || (!trim_defaults))
               {
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_ascii_dtostr (str, sizeof(str), value);
                 g_string_append_printf (s2, " %s=%s", property_name, str);
+                printed = TRUE;
               }
             }
           else if (property_type == G_TYPE_INT)
@@ -609,8 +622,11 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               gegl_node_get (iter, properties[i]->name, &value, NULL);
               if (value != defval || (!trim_defaults))
               {
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_snprintf (str, sizeof (str), "%i", value);
                 g_string_append_printf (s2, " %s=%s", property_name, str);
+                printed = TRUE;
               }
             }
           else if (property_type == G_TYPE_BOOLEAN)
@@ -620,10 +636,13 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               gegl_node_get (iter, properties[i]->name, &value, NULL);
               if (value != defval || (!trim_defaults))
               {
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 if (value)
                   g_string_append_printf (s2, " %s=true", property_name);
                 else
                   g_string_append_printf (s2, " %s=false", property_name);
+                printed = TRUE;
               }
             }
          else if (property_type == G_TYPE_STRING)
@@ -633,7 +652,10 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               gegl_node_get (iter, properties[i]->name, &value, NULL);
               if (!g_str_equal (defval, value) || (!trim_defaults))
               {
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_string_append_printf (s2, " %s='%s'", property_name, value);
+                printed = TRUE;
               }
               g_free (value);
             }
@@ -647,7 +669,10 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               if (value != defval || (!trim_defaults))
               {
                 GEnumValue *evalue = g_enum_get_value (eclass, value);
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_string_append_printf (s2, " %s=%s", property_name, evalue->value_nick);
+                printed = TRUE;
               }
             }
           else if (property_type == GEGL_TYPE_COLOR)
@@ -664,16 +689,23 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               }
               g_object_unref (color);
               if ((defvalue && !g_str_equal (defvalue, value)) || (!trim_defaults))
+              {
+                if (flags & GEGL_SERIALIZE_INDENT)
+                   g_string_append_printf (s2, "  ");
                 g_string_append_printf (s2, " %s='%s'", property_name, value);
+                printed = TRUE;
+              }
               g_free (value);
             }
           else
             {
               g_warning ("%s: serialization of %s properties not implemented",
                          property_name, g_type_name (property_type));
-
             }
 
+          if (printed && (flags & GEGL_SERIALIZE_INDENT))
+            g_string_append_printf (s2, "\n");
+
           {
             GeglNode *aux = gegl_node_get_producer (iter, "aux", NULL);
             if (aux)
@@ -683,6 +715,7 @@ static gchar *gegl_serialize2 (GeglNode *start, GeglNode *end, const char *basep
               g_free (str);
             }
           }
+
         }
       }
 
diff --git a/gegl/gegl-utils.h b/gegl/gegl-utils.h
index 62ba75b..d087d00 100644
--- a/gegl/gegl-utils.h
+++ b/gegl/gegl-utils.h
@@ -262,7 +262,8 @@ gint        _gegl_float_epsilon_equal (float     v1,
 
 typedef enum GeglSerializeFlag {
   GEGL_SERIALIZE_TRIM_DEFAULTS = (1<<0),
-  GEGL_SERIALIZE_VERSION       = (1<<1)
+  GEGL_SERIALIZE_VERSION       = (1<<1),
+  GEGL_SERIALIZE_INDENT        = (1<<2)
 } GeglSerializeFlag;
 
 /**


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