Skip to content

Commit

Permalink
linux support path links starting with ~/ (#2149)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Nov 18, 2022
1 parent d3bae86 commit c669007
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/ct/ct_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 25 additions & 4 deletions src/ct/ct_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,36 @@ 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<typename ITERATOR_T>
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 {
using std::swap;
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) {
Expand Down Expand Up @@ -180,6 +198,9 @@ class path

private:
std::string _path;
#if !defined(_WIN32)
void _replaceStartingTilde();
#endif // !_WIN32
};

} // namespace fs
Expand Down

0 comments on commit c669007

Please sign in to comment.