[tepl] Utils: add get_file_extension() and get_file_shortname()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] Utils: add get_file_extension() and get_file_shortname()
- Date: Mon, 18 Nov 2019 10:00:41 +0000 (UTC)
commit 8c00726eed3f03f3a23f8d2ef49d1035c8c890d7
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon Nov 18 10:27:21 2019 +0100
Utils: add get_file_extension() and get_file_shortname()
Copied from gnome-latex, it will be used there.
docs/reference/tepl-4.0-sections.txt | 2 +
tepl/tepl-utils.c | 73 ++++++++++++++++++++++++++++++++++++
tepl/tepl-utils.h | 4 ++
testsuite/test-utils.c | 50 ++++++++++++++++++++++++
4 files changed, 129 insertions(+)
---
diff --git a/docs/reference/tepl-4.0-sections.txt b/docs/reference/tepl-4.0-sections.txt
index 72d8933..e4795f5 100644
--- a/docs/reference/tepl-4.0-sections.txt
+++ b/docs/reference/tepl-4.0-sections.txt
@@ -389,6 +389,8 @@ tepl_notebook_get_type
<FILE>utils</FILE>
tepl_utils_str_middle_truncate
tepl_utils_str_end_truncate
+tepl_utils_get_file_extension
+tepl_utils_get_file_shortname
</SECTION>
<SECTION>
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 0a6cad9..45a609b 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -131,6 +131,79 @@ tepl_utils_str_end_truncate (const gchar *str,
return str_truncate (str, truncate_length, FALSE);
}
+static gint
+get_extension_position (const gchar *filename)
+{
+ const gchar *pos;
+ gint length;
+
+ if (filename == NULL)
+ {
+ return 0;
+ }
+
+ length = strlen (filename);
+ pos = filename + length;
+ g_assert (pos[0] == '\0');
+
+ while (TRUE)
+ {
+ pos = g_utf8_find_prev_char (filename, pos);
+
+ if (pos == NULL || pos[0] == '/')
+ {
+ break;
+ }
+
+ if (pos[0] == '.')
+ {
+ return pos - filename;
+ }
+ }
+
+ return length;
+}
+
+/**
+ * tepl_utils_get_file_extension:
+ * @filename: a filename.
+ *
+ * Examples:
+ * - "file.pdf" returns ".pdf".
+ * - "file.PDF" returns ".pdf".
+ * - "file.tar.gz" returns ".gz".
+ * - "path/to/file.pdf" returns ".pdf".
+ * - "file" (without an extension) returns "" (the empty string).
+ *
+ * Returns: the @filename's extension with the dot, in lowercase. Free with
+ * g_free().
+ * Since: 4.4
+ */
+gchar *
+tepl_utils_get_file_extension (const gchar *filename)
+{
+ gint pos = get_extension_position (filename);
+
+ return g_ascii_strdown (filename + pos, -1);
+}
+
+/**
+ * tepl_utils_get_file_shortname:
+ * @filename: a filename.
+ *
+ * Returns @filename without its extension. With the “extension” having the same
+ * definition as in tepl_utils_get_file_extension(); in other words it returns
+ * the other part of @filename.
+ *
+ * Returns: the @filename without its extension. Free with g_free().
+ * Since: 4.4
+ */
+gchar *
+tepl_utils_get_file_shortname (const gchar *filename)
+{
+ return g_strndup (filename, get_extension_position (filename));
+}
+
/*
* _tepl_utils_replace_home_dir_with_tilde:
* @filename: the filename.
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 117b18d..b1b7342 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -38,6 +38,10 @@ gchar * tepl_utils_str_end_truncate (const gchar *str,
/* File utilities */
+gchar * tepl_utils_get_file_extension (const gchar *filename);
+
+gchar * tepl_utils_get_file_shortname (const gchar *filename);
+
G_GNUC_INTERNAL
gchar * _tepl_utils_replace_home_dir_with_tilde (const gchar *filename);
diff --git a/testsuite/test-utils.c b/testsuite/test-utils.c
index 6b2ee1d..85db02d 100644
--- a/testsuite/test-utils.c
+++ b/testsuite/test-utils.c
@@ -39,6 +39,54 @@ test_str_end_truncate (void)
g_free (truncated_str);
}
+static void
+test_get_file_extension (void)
+{
+ gchar *extension;
+
+ extension = tepl_utils_get_file_extension ("file.pdf");
+ g_assert_cmpstr (extension, ==, ".pdf");
+ g_free (extension);
+
+ extension = tepl_utils_get_file_extension ("file.PDF");
+ g_assert_cmpstr (extension, ==, ".pdf");
+ g_free (extension);
+
+ extension = tepl_utils_get_file_extension ("file.tar.gz");
+ g_assert_cmpstr (extension, ==, ".gz");
+ g_free (extension);
+
+ extension = tepl_utils_get_file_extension ("path/to/file.pdf");
+ g_assert_cmpstr (extension, ==, ".pdf");
+ g_free (extension);
+
+ extension = tepl_utils_get_file_extension ("file");
+ g_assert_cmpstr (extension, ==, "");
+ g_free (extension);
+}
+
+static void
+test_get_file_shortname (void)
+{
+ gchar *shortname;
+
+ shortname = tepl_utils_get_file_shortname ("file.txt");
+ g_assert_cmpstr (shortname, ==, "file");
+ g_free (shortname);
+
+ shortname = tepl_utils_get_file_shortname ("file.tar.gz");
+ g_assert_cmpstr (shortname, ==, "file.tar");
+ g_free (shortname);
+
+ shortname = tepl_utils_get_file_shortname ("file");
+ g_assert_cmpstr (shortname, ==, "file");
+ g_free (shortname);
+
+ shortname = tepl_utils_get_file_shortname ("dir.ext/blah");
+ g_assert_cmpstr (shortname, ==, "dir.ext/blah");
+ g_free (shortname);
+}
+
static void
test_replace_home_dir_with_tilde (void)
{
@@ -120,6 +168,8 @@ main (gint argc,
g_test_add_func ("/utils/str-middle-truncate", test_str_middle_truncate);
g_test_add_func ("/utils/str-end-truncate", test_str_end_truncate);
+ g_test_add_func ("/utils/get-file-extension", test_get_file_extension);
+ g_test_add_func ("/utils/get-file-shortname", test_get_file_shortname);
g_test_add_func ("/utils/replace-home-dir-with-tilde", test_replace_home_dir_with_tilde);
g_test_add_func ("/utils/decode-uri", test_decode_uri);
g_test_add_func ("/utils/get-fallback-basename-for-display", test_get_fallback_basename_for_display);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]