diff options
author | Richard Hughes <richard@hughsie.com> | 2016-10-12 10:11:47 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2016-10-12 10:11:47 +0100 |
commit | 65c520ed1d141df1b0cbca153f62820f989fdfa3 (patch) | |
tree | 7d648c013cb35201a2b634fbe5c77cc0d8ecd5da | |
parent | 57f6d00ff98cdc5e70913701b8a70d47b3e5649b (diff) | |
download | appstream-glib-65c520ed1d141df1b0cbca153f62820f989fdfa3.tar.gz |
Add functions to build and validate an AppStream ID
-rw-r--r-- | libappstream-glib/as-self-test.c | 11 | ||||
-rw-r--r-- | libappstream-glib/as-utils.c | 66 | ||||
-rw-r--r-- | libappstream-glib/as-utils.h | 2 |
3 files changed, 79 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 4f6f307..835ebd9 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -3886,6 +3886,16 @@ as_test_store_speed_desktop_func (void) } static void +as_test_utils_appstream_id_func (void) +{ + g_autofree gchar *id = NULL; + g_assert (as_utils_appstream_id_valid ("org.gnome.Software")); + g_assert (!as_utils_appstream_id_valid ("xml:gravatar@jr.rlabs.io")); + id = as_utils_appstream_id_build ("gravatar@jr.rlabs.io"); + g_assert_cmpstr (id, ==, "gravatar_jr.rlabs.io"); +} + +static void as_test_utils_guid_func (void) { g_autofree gchar *guid1 = NULL; @@ -5068,6 +5078,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/utils{markup-import}", as_test_utils_markup_import_func); g_test_add_func ("/AppStream/utils{version}", as_test_utils_version_func); g_test_add_func ("/AppStream/utils{guid}", as_test_utils_guid_func); + g_test_add_func ("/AppStream/utils{appstream-id}", as_test_utils_appstream_id_func); g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func); g_test_add_func ("/AppStream/utils{spdx-token}", as_test_utils_spdx_token_func); g_test_add_func ("/AppStream/utils{install-filename}", as_test_utils_install_filename_func); 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; +} diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h index 7f7cde1..b9037f5 100644 --- a/libappstream-glib/as-utils.h +++ b/libappstream-glib/as-utils.h @@ -142,6 +142,8 @@ gboolean as_utils_unique_id_equal (const gchar *unique_id1, const gchar *unique_id2); gboolean as_utils_unique_id_valid (const gchar *unique_id); guint as_utils_unique_id_hash (const gchar *unique_id); +gchar *as_utils_appstream_id_build (const gchar *str); +gboolean as_utils_appstream_id_valid (const gchar *str); G_END_DECLS |