mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2024-11-23 06:15:38 +00:00
Common: Add FileSystem::{Get,Set}WorkingDirectory functions
This commit is contained in:
parent
de4e45a433
commit
7afb79aee6
|
@ -1015,6 +1015,26 @@ std::string GetProgramPath()
|
|||
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
|
||||
|
||||
std::unique_ptr<ChangeNotifier> CreateChangeNotifier(const char* path, bool recursiveWatch)
|
||||
|
@ -1394,6 +1414,26 @@ std::string GetProgramPath()
|
|||
#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
|
||||
|
||||
} // namespace FileSystem
|
|
@ -176,4 +176,10 @@ bool DeleteDirectory(const char* Path, bool Recursive);
|
|||
/// Returns the path to the current executable.
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue