[amtk] utils: update internal copy of tepl_utils_replace_home_dir_with_tilde()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [amtk] utils: update internal copy of tepl_utils_replace_home_dir_with_tilde()
- Date: Wed, 1 Jun 2022 13:02:10 +0000 (UTC)
commit d55ab113b480502fb4b1500840e09c41583f1877
Author: Sébastien Wilmet <swilmet informatique-libre be>
Date: Fri May 27 21:24:31 2022 +0200
utils: update internal copy of tepl_utils_replace_home_dir_with_tilde()
There was a bug fix in the Tepl function if $HOME ends with a slash.
amtk/amtk-utils.c | 107 ++++++++++++++++++++++++++++++++++++------------------
1 file changed, 72 insertions(+), 35 deletions(-)
---
diff --git a/amtk/amtk-utils.c b/amtk/amtk-utils.c
index 97c77ad..e1ed7d9 100644
--- a/amtk/amtk-utils.c
+++ b/amtk/amtk-utils.c
@@ -1,4 +1,4 @@
-/* SPDX-FileCopyrightText: 2017 - Sébastien Wilmet <swilmet gnome org>
+/* SPDX-FileCopyrightText: 2017-2022 - Sébastien Wilmet <swilmet gnome org>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
@@ -15,57 +15,94 @@
* Utility functions.
*/
-/*
- * _amtk_utils_replace_home_dir_with_tilde:
- * @filename: the filename.
- *
- * Replaces the home directory with a tilde, if the home directory is present in
- * the @filename.
- *
- * Returns: the new filename. Free with g_free().
- */
-/* This function is a copy from tepl-utils, which originally comes from gedit. */
-gchar *
-_amtk_utils_replace_home_dir_with_tilde (const gchar *filename)
+static gchar *
+get_home_dir_without_trailing_slash (const gchar *home_dir)
{
- gchar *tmp;
- gchar *home;
+ gchar *utf8_home_dir;
+ gsize length;
- g_return_val_if_fail (filename != NULL, NULL);
+ if (home_dir == NULL)
+ {
+ return NULL;
+ }
- /* Note that g_get_home_dir returns a const string */
- tmp = (gchar *) g_get_home_dir ();
+ utf8_home_dir = g_filename_to_utf8 (home_dir, -1, NULL, NULL, NULL);
+ if (utf8_home_dir == NULL)
+ {
+ return NULL;
+ }
- if (tmp == NULL)
+ length = strlen (utf8_home_dir);
+ if (length == 0)
{
- return g_strdup (filename);
+ g_free (utf8_home_dir);
+ return NULL;
}
- home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
- if (home == NULL)
+ if (utf8_home_dir[length - 1] == '/')
{
- return g_strdup (filename);
+ utf8_home_dir[length - 1] = '\0';
}
- if (g_str_equal (filename, home))
+ return utf8_home_dir;
+}
+
+/* Like tepl_utils_replace_home_dir_with_tilde() but with an additional home_dir
+ * parameter, for unit tests.
+ */
+static gchar *
+_tepl_utils_replace_home_dir_with_tilde_with_param (const gchar *filename,
+ const gchar *home_dir)
+{
+ gchar *home_dir_without_trailing_slash;
+ gchar *home_dir_with_trailing_slash;
+ gchar *ret;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ home_dir_without_trailing_slash = get_home_dir_without_trailing_slash (home_dir);
+ if (home_dir_without_trailing_slash == NULL)
{
- g_free (home);
- return g_strdup ("~");
+ return g_strdup (filename);
}
- tmp = home;
- home = g_strdup_printf ("%s/", tmp);
- g_free (tmp);
+ home_dir_with_trailing_slash = g_strdup_printf ("%s/", home_dir_without_trailing_slash);
- if (g_str_has_prefix (filename, home))
+ if (g_str_equal (filename, home_dir_without_trailing_slash) ||
+ g_str_equal (filename, home_dir_with_trailing_slash))
{
- gchar *res = g_strdup_printf ("~/%s", filename + strlen (home));
- g_free (home);
- return res;
+ ret = g_strdup ("~");
+ goto out;
}
- g_free (home);
- return g_strdup (filename);
+ if (g_str_has_prefix (filename, home_dir_with_trailing_slash))
+ {
+ ret = g_strdup_printf ("~/%s", filename + strlen (home_dir_with_trailing_slash));
+ goto out;
+ }
+
+ ret = g_strdup (filename);
+
+out:
+ g_free (home_dir_without_trailing_slash);
+ g_free (home_dir_with_trailing_slash);
+ return ret;
+}
+
+/*
+ * _amtk_utils_replace_home_dir_with_tilde:
+ * @filename: the filename.
+ *
+ * Replaces the home directory with a tilde, if the home directory is present in
+ * the @filename.
+ *
+ * Returns: the new filename. Free with g_free().
+ */
+/* This function is a copy from tepl-utils, which originally comes from gedit. */
+gchar *
+_amtk_utils_replace_home_dir_with_tilde (const gchar *filename)
+{
+ return _tepl_utils_replace_home_dir_with_tilde_with_param (filename, g_get_home_dir ());
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]