diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-03-16 10:23:38 +0100 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2020-03-16 10:23:38 +0100 |
commit | 98ef563141416d0ea50ef5f6aa0114e6ca29460f (patch) | |
tree | fc617756f39a3520d1550d900fb0a77e16d3aeeb | |
parent | 36e765338a11897e3c22814086685ab406fbed79 (diff) | |
download | glibmm-98ef563141416d0ea50ef5f6aa0114e6ca29460f.tar.gz |
Add Glib::canonicalize_filename()
Fixes #59
-rw-r--r-- | glib/src/miscutils.ccg | 7 | ||||
-rw-r--r-- | glib/src/miscutils.hg | 26 |
2 files changed, 33 insertions, 0 deletions
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 @@ -221,6 +221,13 @@ path_get_dirname(const std::string& filename) } 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) { return convert_return_gchar_ptr_to_stdstring( 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, |