summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-utils.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-10-12 10:11:47 +0100
committerRichard Hughes <richard@hughsie.com>2016-10-12 10:11:47 +0100
commit65c520ed1d141df1b0cbca153f62820f989fdfa3 (patch)
tree7d648c013cb35201a2b634fbe5c77cc0d8ecd5da /libappstream-glib/as-utils.c
parent57f6d00ff98cdc5e70913701b8a70d47b3e5649b (diff)
downloadappstream-glib-65c520ed1d141df1b0cbca153f62820f989fdfa3.tar.gz
Add functions to build and validate an AppStream ID
Diffstat (limited to 'libappstream-glib/as-utils.c')
-rw-r--r--libappstream-glib/as-utils.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c
index 2241911..0e8331c 100644
--- a/libappstream-glib/as-utils.c
+++ b/libappstream-glib/as-utils.c
@@ -1899,3 +1899,69 @@ as_utils_unique_id_hash (const gchar *unique_id)
}
return hash;
}
+
+static gboolean
+as_utils_appstream_id_is_valid_char (gchar ch)
+{
+ if (g_ascii_isalnum (ch))
+ return TRUE;
+ if (ch == '.')
+ return TRUE;
+ if (ch == '-')
+ return TRUE;
+ return FALSE;
+}
+
+/**
+ * as_utils_appstream_id_build:
+ * @str: a string to build the AppStream ID from
+ *
+ * Fixes a string to be a valid AppStream ID.
+ *
+ * This function replaces any invalid chars with an underscore.
+ *
+ * Returns: a valid AppStream ID, or %NULL if @str is invalid
+ *
+ * Since: 0.6.4
+ */
+gchar *
+as_utils_appstream_id_build (const gchar *str)
+{
+ gchar *tmp;
+ guint i;
+
+ /* invalid */
+ if (str == NULL)
+ return NULL;
+ if (str[0] == '\0')
+ return NULL;
+
+ tmp = g_strdup (str);
+ for (i = 0; tmp[i] != '\0'; i++) {
+ if (!as_utils_appstream_id_is_valid_char (tmp[i]))
+ tmp[i] = '_';
+ }
+ return tmp;
+}
+
+/**
+ * as_utils_appstream_id_valid:
+ * @str: a string
+ *
+ * Checks to see if a string is a valid AppStream ID. A valid AppStream ID only
+ * contains alpha-numeric chars, dots and dashes.
+ *
+ * Returns: %TRUE if the string is a valid AppStream ID
+ *
+ * Since: 0.6.4
+ */
+gboolean
+as_utils_appstream_id_valid (const gchar *str)
+{
+ guint i;
+ for (i = 0; str[i] != '\0'; i++) {
+ if (!as_utils_appstream_id_is_valid_char (str[i]))
+ return FALSE;
+ }
+ return TRUE;
+}