[glibmm/glibmm-2-64] Add Glib::canonicalize_filename()



commit 98ef563141416d0ea50ef5f6aa0114e6ca29460f
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Mon Mar 16 10:23:38 2020 +0100

    Add Glib::canonicalize_filename()
    
    Fixes #59

 glib/src/miscutils.ccg |  7 +++++++
 glib/src/miscutils.hg  | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
---
diff --git a/glib/src/miscutils.ccg b/glib/src/miscutils.ccg
index 67e0db81..b059f1db 100644
--- a/glib/src/miscutils.ccg
+++ b/glib/src/miscutils.ccg
@@ -220,6 +220,13 @@ path_get_dirname(const std::string& filename)
   return convert_return_gchar_ptr_to_stdstring(g_path_get_dirname(filename.c_str()));
 }
 
+std::string
+canonicalize_filename(StdStringView filename, StdStringView relative_to)
+{
+  return convert_return_gchar_ptr_to_stdstring(g_canonicalize_filename(
+    filename.c_str(), relative_to.c_str()));
+}
+
 std::string
 build_filename(const Glib::ArrayHandle<std::string>& elements)
 {
diff --git a/glib/src/miscutils.hg b/glib/src/miscutils.hg
index a606447d..4ec3137b 100644
--- a/glib/src/miscutils.hg
+++ b/glib/src/miscutils.hg
@@ -349,6 +349,32 @@ std::string path_get_basename(const std::string& filename);
 GLIBMM_API
 std::string path_get_dirname(const std::string& filename);
 
+/** Gets the canonical file name from @a filename.
+ *
+ * All triple slashes are turned into single slashes, and all `..` and `.`s
+ * resolved against @a relative_to.
+ *
+ * Symlinks are not followed, and the returned path is guaranteed to be absolute.
+ *
+ * If @a filename is an absolute path, @a relative_to is ignored. Otherwise,
+ * @a relative_to will be prepended to @a filename to make it absolute. @a relative_to
+ * must be an absolute path, or <tt>nullptr</tt>. If @a relative_to is <tt>nullptr</tt>,
+ * it'll fallback to get_current_dir().
+ *
+ * This function never fails, and will canonicalize file paths even if they don't exist.
+ *
+ * No file system I/O is done.
+ *
+ * @param filename The name of the file.
+ * @param relative_to The relative directory, or <tt>nullptr</tt> to use the
+ *                    current working directory.
+ * @return The canonical file path.
+ *
+ * @newin{2,64}
+ */
+GLIBMM_API
+std::string canonicalize_filename(StdStringView filename, StdStringView relative_to = nullptr);
+
 /** Creates a filename from a series of elements using the correct
  * separator for filenames.
  * This function behaves identically to Glib::build_path(G_DIR_SEPARATOR_S,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]