[gtk/wip/otte/widgetfactory-dnd: 1/7] contentformats: Add gdk_content_formats_parse()
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/widgetfactory-dnd: 1/7] contentformats: Add gdk_content_formats_parse()
- Date: Tue, 27 Jul 2021 02:27:33 +0000 (UTC)
commit 131be5f2e8c307fd70a7b3e302564159c4d935bf
Author: Benjamin Otte <otte redhat com>
Date: Mon Jul 26 02:33:31 2021 +0200
contentformats: Add gdk_content_formats_parse()
gdk/gdkcontentformats.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++---
gdk/gdkcontentformats.h | 2 ++
2 files changed, 76 insertions(+), 4 deletions(-)
---
diff --git a/gdk/gdkcontentformats.c b/gdk/gdkcontentformats.c
index 24ac00ad14..662ff1b825 100644
--- a/gdk/gdkcontentformats.c
+++ b/gdk/gdkcontentformats.c
@@ -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.
*
diff --git a/gdk/gdkcontentformats.h b/gdk/gdkcontentformats.h
index 1798c48284..143a669340 100644
--- a/gdk/gdkcontentformats.h
+++ b/gdk/gdkcontentformats.h
@@ -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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]