contentformats: Add gdk_content_formats_parse()
authorBenjamin Otte <otte@redhat.com>
Mon, 26 Jul 2021 00:33:31 +0000 (02:33 +0200)
committerBenjamin Otte <otte@redhat.com>
Tue, 27 Jul 2021 02:13:42 +0000 (04:13 +0200)
gdk/gdkcontentformats.c
gdk/gdkcontentformats.h

index 24ac00ad149f4a1d2ca756e96e5687da9b406b24..662ff1b825fdd8b5711a96863f58b128c81ea133 100644 (file)
@@ -179,6 +179,76 @@ gdk_content_formats_new_for_gtype (GType type)
   return gdk_content_formats_new_take (data, 1, NULL, 0);
 }
 
+/**
+ * gdk_content_formats_parse:
+ * @string: the string to parse
+ *
+ * Parses the given @string into `GdkContentFormats` and
+ * returns the formats. 
+ *
+ * Strings printed via [method@Gdk.ContentFormats.to_string]
+ * can be read in again successfully using this function.
+ *
+ * If @string does not describe valid content formats, %NULL
+ * is returned.
+ *
+ * Returns: (nullable): the content formats if @string is valid
+ *
+ * Since: 4.4
+ */
+GdkContentFormats *
+gdk_content_formats_parse (const char *string)
+{
+  GdkContentFormatsBuilder *builder;
+  char **split;
+  gsize i;
+
+  g_return_val_if_fail (string != NULL, NULL);
+
+  split = g_strsplit_set (string, "\t\n\f\r ", -1); /* same as g_ascii_isspace() */
+  builder = gdk_content_formats_builder_new ();
+
+  /* first the GTypes */
+  for (i = 0; split[i] != NULL; i++)
+    {
+      GType type;
+
+      if (split[i][0] == 0)
+        continue;
+
+      type = g_type_from_name (split[i]);
+      if (type != 0)
+        gdk_content_formats_builder_add_gtype (builder, type);
+      else
+        break;
+    }
+
+  /* then the mime types */
+  for (; split[i] != NULL; i++)
+    {
+      const char *mime_type;
+
+      if (split[i][0] == 0)
+        continue;
+
+      mime_type = gdk_intern_mime_type (split[i]);
+      if (mime_type)
+        gdk_content_formats_builder_add_mime_type (builder, mime_type);
+      else
+        break;
+    }
+
+  if (split[i] != NULL)
+    {
+      g_strfreev (split);
+      gdk_content_formats_builder_unref (builder);
+      return NULL;
+    }
+
+  g_strfreev (split);
+  return gdk_content_formats_builder_free_to_formats (builder);
+}
+
 /**
  * gdk_content_formats_ref:
  * @formats:  a `GdkContentFormats`
@@ -227,10 +297,8 @@ gdk_content_formats_unref (GdkContentFormats *formats)
  *
  * Prints the given @formats into a string for human consumption.
  *
- * This is meant for debugging and logging.
- *
- * The form of the representation may change at any time and is
- * not guaranteed to stay identical.
+ * The result of this function can later be parsed with
+ * [func@Gdk.ContentFormats.parse].
  */
 void
 gdk_content_formats_print (GdkContentFormats *formats,
@@ -261,6 +329,8 @@ gdk_content_formats_print (GdkContentFormats *formats,
  *
  * Prints the given @formats into a human-readable string.
  *
+ * The resulting string can be parsed with [func@Gdk.ContentFormats.parse].
+ *
  * This is a small wrapper around [method@Gdk.ContentFormats.print]
  * to help when debugging.
  *
index 1798c4828490563ab7a9f23e467544284f17b03e..143a66934014791c27ad53e35ad4de2202ad34cd 100644 (file)
@@ -40,6 +40,8 @@ GdkContentFormats *     gdk_content_formats_new                 (const char
                                                                  guint                           n_mime_types);
 GDK_AVAILABLE_IN_ALL
 GdkContentFormats *     gdk_content_formats_new_for_gtype       (GType                           type);
+GDK_AVAILABLE_IN_4_4
+GdkContentFormats *     gdk_content_formats_parse               (const char                     *string);
 GDK_AVAILABLE_IN_ALL
 GdkContentFormats *     gdk_content_formats_ref                 (GdkContentFormats              *formats);
 GDK_AVAILABLE_IN_ALL