Common: Add FileSystem::{Get,Set}WorkingDirectory functions

This commit is contained in:
Connor McLaughlin 2020-01-24 14:50:58 +10:00
parent de4e45a433
commit 7afb79aee6
2 changed files with 46 additions and 0 deletions

View file

@ -1015,6 +1015,26 @@ std::string GetProgramPath()
return buffer; return buffer;
} }
std::string GetWorkingDirectory()
{
DWORD required_size = GetCurrentDirectoryA(0, nullptr);
if (!required_size)
return {};
std::string buffer;
buffer.resize(required_size - 1);
if (!GetCurrentDirectoryA(static_cast<DWORD>(buffer.size() + 1), buffer.data()))
return {};
return buffer;
}
bool SetWorkingDirectory(const char* path)
{
return (SetCurrentDirectoryA(path) == TRUE);
}
#else #else
std::unique_ptr<ChangeNotifier> CreateChangeNotifier(const char* path, bool recursiveWatch) std::unique_ptr<ChangeNotifier> CreateChangeNotifier(const char* path, bool recursiveWatch)
@ -1394,6 +1414,26 @@ std::string GetProgramPath()
#endif #endif
} }
std::string GetWorkingDirectory()
{
std::string buffer;
buffer.resize(PATH_MAX);
while (!getcwd(buffer.data(), buffer.size()))
{
if (errno != ERANGE)
return {};
buffer.resize(buffer.size() * 2);
}
return buffer;
}
bool SetWorkingDirectory(const char* path)
{
return (chdir(path) == 0);
}
#endif #endif
} // namespace FileSystem } // namespace FileSystem

View file

@ -176,4 +176,10 @@ bool DeleteDirectory(const char* Path, bool Recursive);
/// Returns the path to the current executable. /// Returns the path to the current executable.
std::string GetProgramPath(); std::string GetProgramPath();
/// Retrieves the current working directory.
std::string GetWorkingDirectory();
/// Sets the current working directory. Returns true if successful.
bool SetWorkingDirectory(const char* path);
}; // namespace FileSystem }; // namespace FileSystem