[gimp] libgimpconfig: implement serializing GimpParasite into a GimpConfig.



commit 0b01b812a97dfec57e5159bf0d6b9dc79d13bd1d
Author: Jehan <jehan girinstud io>
Date:   Mon Apr 5 21:49:41 2021 +0200

    libgimpconfig: implement serializing GimpParasite into a GimpConfig.
    
    Used for instance if a plug-in had a GimpParasite argument (or aux
    argument), which might not seem too useful at first. But anyway we
    propose the type, so let's support it properly.
    
    And actually I am going to use it in the next commit as a trick to save
    binary data in stored last values for a plug-in.

 libgimpconfig/gimpconfig-deserialize.c | 132 +++++++++++++++++++++++----------
 libgimpconfig/gimpconfig-serialize.c   |  33 ++++++++-
 2 files changed, 122 insertions(+), 43 deletions(-)
---
diff --git a/libgimpconfig/gimpconfig-deserialize.c b/libgimpconfig/gimpconfig-deserialize.c
index ce5b8a6517..431143ccf6 100644
--- a/libgimpconfig/gimpconfig-deserialize.c
+++ b/libgimpconfig/gimpconfig-deserialize.c
@@ -57,47 +57,50 @@
  *  couldn't parse it.
  */
 
-static GTokenType  gimp_config_deserialize_value       (GValue     *value,
-                                                        GimpConfig *config,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_fundamental (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_enum        (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_memsize     (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_path        (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_rgb         (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_matrix2     (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_object      (GValue     *value,
-                                                        GimpConfig *config,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner,
-                                                        gint        nest_level);
-static GTokenType  gimp_config_deserialize_value_array (GValue     *value,
-                                                        GimpConfig *config,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_unit        (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_file_value  (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_deserialize_any         (GValue     *value,
-                                                        GParamSpec *prop_spec,
-                                                        GScanner   *scanner);
-static GTokenType  gimp_config_skip_unknown_property   (GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_value          (GValue     *value,
+                                                           GimpConfig *config,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_fundamental    (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_enum           (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_memsize        (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_path           (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_rgb            (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_matrix2        (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_object         (GValue     *value,
+                                                           GimpConfig *config,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner,
+                                                           gint        nest_level);
+static GTokenType  gimp_config_deserialize_value_array    (GValue     *value,
+                                                           GimpConfig *config,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_unit           (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_file_value     (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_parasite_value (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_deserialize_any            (GValue     *value,
+                                                           GParamSpec *prop_spec,
+                                                           GScanner   *scanner);
+static GTokenType  gimp_config_skip_unknown_property      (GScanner   *scanner);
 
 static inline gboolean  scanner_string_utf8_valid (GScanner    *scanner,
                                                    const gchar *token_name);
@@ -377,6 +380,10 @@ gimp_config_deserialize_value (GValue     *value,
     {
       return gimp_config_deserialize_file_value (value, prop_spec, scanner);
     }
+  else if (prop_spec->value_type == GIMP_TYPE_PARASITE)
+    {
+      return gimp_config_deserialize_parasite_value (value, prop_spec, scanner);
+    }
 
   /*  This fallback will only work for value_types that
    *  can be transformed from a string value.
@@ -946,6 +953,49 @@ gimp_config_deserialize_file_value (GValue     *value,
   return G_TOKEN_RIGHT_PAREN;
 }
 
+/*
+ * Note: this is different from gimp_config_deserialize_parasite()
+ * which is a public API to deserialize random properties into a config
+ * object from a parasite. Here we are deserializing the contents of a
+ * parasite itself in @scanner.
+ */
+static GTokenType
+gimp_config_deserialize_parasite_value (GValue     *value,
+                                        GParamSpec *prop_spec,
+                                        GScanner   *scanner)
+{
+  GimpParasite *parasite;
+  gchar        *name;
+  guint8       *data;
+  gint          data_length;
+  gint64        flags;
+
+  if (! gimp_scanner_parse_string (scanner, &name))
+    return G_TOKEN_STRING;
+
+  if (! (name && *name))
+    {
+      g_scanner_error (scanner, "Parasite name is empty");
+      g_free (name);
+      return G_TOKEN_NONE;
+    }
+
+  if (! gimp_scanner_parse_int64 (scanner, &flags))
+    return G_TOKEN_INT;
+
+  if (! gimp_scanner_parse_int (scanner, &data_length))
+    return G_TOKEN_INT;
+
+  if (! gimp_scanner_parse_data (scanner, data_length, &data))
+    return G_TOKEN_STRING;
+
+  parasite = gimp_parasite_new (name, flags, data_length, data);
+
+  g_value_take_boxed (value, parasite);
+
+  return G_TOKEN_RIGHT_PAREN;
+}
+
 static GTokenType
 gimp_config_deserialize_any (GValue     *value,
                              GParamSpec *prop_spec,
diff --git a/libgimpconfig/gimpconfig-serialize.c b/libgimpconfig/gimpconfig-serialize.c
index cf873c0aff..728f021cb9 100644
--- a/libgimpconfig/gimpconfig-serialize.c
+++ b/libgimpconfig/gimpconfig-serialize.c
@@ -237,8 +237,37 @@ gimp_config_serialize_property (GimpConfig       *config,
 
   if (! success)
     {
-      if (G_VALUE_HOLDS_OBJECT (&value) &&
-          G_VALUE_TYPE (&value) != G_TYPE_FILE)
+      if (G_VALUE_TYPE (&value) == GIMP_TYPE_PARASITE)
+        {
+          GimpParasite *parasite = g_value_get_boxed (&value);
+
+          gimp_config_writer_open (writer, param_spec->name);
+
+          if (parasite)
+            {
+              const gchar   *name;
+              gconstpointer  data;
+              guint32        data_length;
+              gulong         flags;
+
+              name = gimp_parasite_get_name (parasite);
+              gimp_config_writer_string (writer, name);
+
+              flags = gimp_parasite_get_flags (parasite);
+              data = gimp_parasite_get_data (parasite, &data_length);
+              gimp_config_writer_printf (writer, "%lu %u", flags, data_length);
+              gimp_config_writer_data (writer, data_length, data);
+
+              success = TRUE;
+            }
+
+          if (success)
+            gimp_config_writer_close (writer);
+          else
+            gimp_config_writer_revert (writer);
+        }
+      else if (G_VALUE_HOLDS_OBJECT (&value) &&
+               G_VALUE_TYPE (&value) != G_TYPE_FILE)
         {
           GimpConfigInterface *config_iface = NULL;
           GimpConfig          *prop_object;


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