mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-02-21 12:17:12 +01:00
Add a helper funtion to the system path library to obtain the full path of a file.
This commit is contained in:
parent
fe70560234
commit
c8d5200dc0
@ -4,8 +4,8 @@
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cctype>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -14,7 +14,7 @@
|
||||
#else
|
||||
#include <linux/limits.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif // SQMOD_OS_WINDOWS
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
@ -32,6 +32,49 @@ namespace SqMod {
|
||||
typedef CharT PChar;
|
||||
#endif // SQMOD_OS_WINDOWS
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Buffer GetRealFilePath(CSStr path)
|
||||
{
|
||||
// Make sure the specified path is valid
|
||||
if (!path || *path == '\0')
|
||||
{
|
||||
STHROWF("Cannot obtain real path of empty or invalid path");
|
||||
}
|
||||
// Allocate a buffer large enough to hold a full path
|
||||
Buffer b(SQMOD_MAX_PATH);
|
||||
|
||||
#ifdef SQMOD_OS_WINDOWS
|
||||
// Attempt to obtain the full path to the file
|
||||
DWORD ret = ::GetFullPathName(path, b.Size< PChar >(), b.Get< PChar >(), nullptr);
|
||||
// Should we allocate a bigger buffer?
|
||||
if (ret > b.Size< PChar >())
|
||||
{
|
||||
// Grab a bigger buffer
|
||||
b.Adjust(ret);
|
||||
// Grab the path again
|
||||
ret = GetFullPathName(path, b.Size< PChar >(), b.Get< PChar >(), nullptr);
|
||||
}
|
||||
// Did we fail to obtain a path?
|
||||
if (ret == 0 && ::GetLastError() != 0)
|
||||
{
|
||||
STHROWLASTF("Cannot obtain real path of (%s)", path);
|
||||
}
|
||||
// Adjust the buffer cursor
|
||||
b.Move(ret);
|
||||
#else
|
||||
// Attempt to obtain the full path to the file
|
||||
if (!realpath(path, b.Data()))
|
||||
{
|
||||
STHROWLASTF("Cannot obtain real path of (%s)", path);
|
||||
}
|
||||
// Adjust the buffer cursor
|
||||
b.Move(std::strlen(b.Data()));
|
||||
#endif // SQMOD_OS_WINDOWS
|
||||
|
||||
// Return ownership of the buffer
|
||||
return std::move(b);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger SysPath::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
|
@ -10,6 +10,11 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the full path of file.
|
||||
*/
|
||||
Buffer GetRealFilePath(CSStr path);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* This class represents filesystem paths in a platform-independent manner.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user