[glibmm] Add Glib::canonicalize_filename()
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Add Glib::canonicalize_filename()
- Date: Wed, 23 Oct 2019 18:11:03 +0000 (UTC)
commit dad82ed48581ea4cb9200a335de78dc48ef99824
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date: Wed Oct 23 20:05:44 2019 +0200
Add Glib::canonicalize_filename()
Fixes #59
glib/src/miscutils.ccg | 7 +++++++
glib/src/miscutils.hg | 25 +++++++++++++++++++++++++
2 files changed, 32 insertions(+)
---
diff --git a/glib/src/miscutils.ccg b/glib/src/miscutils.ccg
index b32fe57a..8e2fbdab 100644
--- a/glib/src/miscutils.ccg
+++ b/glib/src/miscutils.ccg
@@ -184,6 +184,13 @@ path_get_dirname(StdStringView 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 std::vector<std::string>& elements)
{
diff --git a/glib/src/miscutils.hg b/glib/src/miscutils.hg
index 390f203d..045ca561 100644
--- a/glib/src/miscutils.hg
+++ b/glib/src/miscutils.hg
@@ -299,6 +299,31 @@ std::string path_get_basename(StdStringView filename);
*/
std::string path_get_dirname(StdStringView 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}
+ */
+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]