Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create filesystem::current_path() and filesystem::absolute() #3100

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions synfig-core/src/synfig/filesystem_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#include <glibmm/miscutils.h>

#include <synfig/os.h>

# ifdef _WIN32
# include <codecvt>
# include <locale>
Expand Down Expand Up @@ -778,6 +780,18 @@ filesystem::swap(Path& lhs, Path& rhs) noexcept
return lhs.swap(rhs);
}

filesystem::Path
filesystem::current_path()
{
return synfig::OS::get_current_working_directory();
}

filesystem::Path
filesystem::absolute(const Path& p)
{
return current_path() / p;
}

std::string
filesystem::Path::basename(const std::string& str)
{
Expand Down
22 changes: 22 additions & 0 deletions synfig-core/src/synfig/filesystem_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ class Path {

void swap(synfig::filesystem::Path& lhs, synfig::filesystem::Path& rhs) noexcept;

/**
* Returns the current path, also known as current working directory.
*
* It just calls the synfig::OS::get_current_working_directory().
* It is defined here for convinience and to imitate the C++17 filesystem namespace.
*
* @return the current working directory
*/
Path current_path();

/**
* The absolute path of relative (or absolute) path @a p.
*
* If @a p is an absolute path, returns itself.
* Otherwise, resolves the rlative path p based on the current working directory.
* @see synfig::filesystem::current_path()
*
* @param p the path to find its absolute path version
* @return current_path() / p
*/
Path absolute(const synfig::filesystem::Path& p);

inline bool operator==(const Path& lhs, const Path& rhs) noexcept
{ return lhs.compare(rhs) == 0; }

Expand Down
37 changes: 37 additions & 0 deletions synfig-core/src/synfig/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
# define UNIX_PIPE_TO_PROCESSES
# include <cstdlib> // for system()
# include <fcntl.h> // for O_ flags
# include <limits.h> // PATH_MAX
# include <sys/stat.h> // for S_ISDIR() and stat()
# include <sys/wait.h> // for waitpid()
# include <unistd.h> // for popen()
#elif defined(_WIN32)
Expand Down Expand Up @@ -778,3 +780,38 @@ OS::get_user_lang()
#endif
return language_list;
}

filesystem::Path
OS::get_current_working_directory()
{
#ifdef _WIN32
DWORD length = GetCurrentDirectoryW(0, nullptr);

std::vector<filesystem::Path::value_type> current_dir_str(length);
if (GetCurrentDirectoryW(length, current_dir_str.data()) != length - 1)
return filesystem::Path("/");

return filesystem::Path::from_native(current_dir_str.data()).lexically_normal();
#else

struct stat st;
if (char* pwd = getenv("PWD")) {
if (stat(pwd, &st) == 0 && S_ISDIR(st.st_mode)) {
return filesystem::Path::from_native(pwd).lexically_normal();
}
}

std::vector<char> buffer(PATH_MAX);
char* ptr = nullptr;

while (!ptr) {
ptr = getcwd(buffer.data(), buffer.size());
if (!ptr) {
if (errno != ERANGE)
return filesystem::Path("/");
buffer.resize(buffer.size() * 2);
}
}
return filesystem::Path::from_native(buffer.data()).lexically_normal();
#endif
}
11 changes: 11 additions & 0 deletions synfig-core/src/synfig/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ const std::vector<std::string>& get_user_lang();
*/
filesystem::Path get_binary_path(const std::string& fallback_path);

/**
* Returns the current working directory.
*
* The current working directory is that used as the starting point in pathname
* resolution. It is initially defined by OS and associated to the current process
* at its launch, but it can be changed at runtime.
*
* @return the current working directory
*/
filesystem::Path get_current_working_directory();

} // END of namespace OS

} // END of namespace synfig
Expand Down