From c669007a68bbecd8708fa0ed943ba90eee9b7e01 Mon Sep 17 00:00:00 2001 From: Giuseppe Penone Date: Fri, 18 Nov 2022 15:15:20 +0000 Subject: [PATCH] linux support path links starting with ~/ (#2149) --- src/ct/ct_filesystem.cc | 9 +++++++++ src/ct/ct_filesystem.h | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/ct/ct_filesystem.cc b/src/ct/ct_filesystem.cc index 0ca29a6ef..c40c00dbf 100644 --- a/src/ct/ct_filesystem.cc +++ b/src/ct/ct_filesystem.cc @@ -610,4 +610,13 @@ std::string path::string_unix() const return str::replace(_path, CtConst::CHAR_BSLASH, CtConst::CHAR_SLASH); } +#if !defined(_WIN32) +void path::_replaceStartingTilde() +{ + if (str::startswith(_path, "~/")) { + _path = Glib::get_home_dir() + _path.substr(1); + } +} +#endif // !_WIN32 + } // namespace fs diff --git a/src/ct/ct_filesystem.h b/src/ct/ct_filesystem.h index b1fc38479..57a096575 100644 --- a/src/ct/ct_filesystem.h +++ b/src/ct/ct_filesystem.h @@ -128,10 +128,22 @@ class path } path() = default; - path(std::string p) : _path(std::move(p)) {} - path(const char* cpath) : _path(cpath) {} + path(std::string p) : _path{std::move(p)} { +#if !defined(_WIN32) + _replaceStartingTilde(); +#endif // !_WIN32 + } + path(const char* cpath) : _path{cpath} { +#if !defined(_WIN32) + _replaceStartingTilde(); +#endif // !_WIN32 + } template - path(ITERATOR_T begin, ITERATOR_T end) : _path(begin, end) {} + path(ITERATOR_T begin, ITERATOR_T end) : _path{begin, end} { +#if !defined(_WIN32) + _replaceStartingTilde(); +#endif // !_WIN32 + } ~path() = default; void swap(path& other) noexcept { @@ -139,7 +151,13 @@ class path swap(*this, other); } - path& operator=(std::string other) { _path = other; return *this; } + path& operator=(std::string other) { + _path = other; +#if !defined(_WIN32) + _replaceStartingTilde(); +#endif // !_WIN32 + return *this; + } path& operator=(const char* other) { return operator=(std::string(other)); } friend path operator/(const path& lhs, const path& rhs) { @@ -180,6 +198,9 @@ class path private: std::string _path; +#if !defined(_WIN32) + void _replaceStartingTilde(); +#endif // !_WIN32 }; } // namespace fs