2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include "Library/System/Path.hpp"
|
2021-01-30 07:51:39 +01:00
|
|
|
#include "Library/System/Env.hpp"
|
2016-03-11 03:14:28 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <cctype>
|
2016-07-04 15:56:49 +02:00
|
|
|
#include <cstring>
|
2021-01-30 07:51:39 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <sqratConst.h>
|
2016-03-11 03:14:28 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2016-03-27 17:55:34 +02:00
|
|
|
#include <linux/limits.h>
|
2016-03-11 03:14:28 +01:00
|
|
|
#include <unistd.h>
|
2016-06-18 19:27:51 +02:00
|
|
|
#endif // SQMOD_OS_WINDOWS
|
2016-03-11 03:14:28 +01:00
|
|
|
|
2016-03-27 17:55:34 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
// Maximum path size in characters
|
|
|
|
#define SQMOD_MAX_PATH (sizeof(TCHAR) * MAX_PATH)
|
|
|
|
// Character to be used when working with path
|
|
|
|
typedef TCHAR PChar;
|
|
|
|
#else
|
|
|
|
// Maximum path size in characters
|
|
|
|
#define SQMOD_MAX_PATH (PATH_MAX)
|
|
|
|
// Character to be used when working with path
|
|
|
|
typedef CharT PChar;
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
|
2016-11-15 20:42:27 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_DECL_TYPENAME(Typename, _SC("SqSysPath"))
|
2016-11-15 20:42:27 +01:00
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
Buffer GetRealFilePath(const SQChar * path)
|
2016-06-18 19:27:51 +02:00
|
|
|
{
|
|
|
|
// 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
|
2021-01-30 07:51:39 +01:00
|
|
|
DWORD ret = ::GetFullPathNameA(path, b.Size< char >(), b.Get< char >(), nullptr);
|
2016-06-18 19:27:51 +02:00
|
|
|
// Should we allocate a bigger buffer?
|
|
|
|
if (ret > b.Size< PChar >())
|
|
|
|
{
|
|
|
|
// Grab a bigger buffer
|
|
|
|
b.Adjust(ret);
|
|
|
|
// Grab the path again
|
2021-01-30 07:51:39 +01:00
|
|
|
ret = GetFullPathNameA(path, b.Size< char >(), b.Get< char >(), nullptr);
|
2016-06-18 19:27:51 +02:00
|
|
|
}
|
|
|
|
// 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
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-06-18 19:27:51 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::SysPath()
|
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::SysPath(bool absolute)
|
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(absolute)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const SQChar * path, int32_t style)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, static_cast< Style >(style));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const SQChar * path, Style style)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const Buffer & path, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, Style::Guess, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const Buffer & path, Style style, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, style, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::SysPath(const String & path)
|
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, Style::Guess);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::SysPath(const String & path, Style style)
|
|
|
|
: m_Dirs()
|
|
|
|
, m_Name()
|
|
|
|
, m_Drive(0)
|
|
|
|
, m_Absolute(false)
|
|
|
|
{
|
|
|
|
Assign(path, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const SysPath & parent, const SQChar * name)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs(parent.m_Dirs)
|
|
|
|
, m_Name(name ? name : "")
|
|
|
|
, m_Drive(parent.m_Drive)
|
|
|
|
, m_Absolute(parent.m_Absolute)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath::SysPath(const SysPath & parent, const String & name) // NOLINT(modernize-pass-by-value)
|
2016-03-11 03:14:28 +01:00
|
|
|
: m_Dirs(parent.m_Dirs)
|
|
|
|
, m_Name(name)
|
|
|
|
, m_Drive(parent.m_Drive)
|
|
|
|
, m_Absolute(parent.m_Absolute)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::SysPath(const SysPath & parent, const SysPath & relative)
|
|
|
|
: m_Dirs(parent.m_Dirs)
|
|
|
|
, m_Name(parent.m_Name)
|
|
|
|
, m_Drive(parent.m_Drive)
|
|
|
|
, m_Absolute(parent.m_Absolute)
|
|
|
|
{
|
|
|
|
// Resolve the specified path
|
|
|
|
Resolve(relative);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::operator = (const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
Assign(path);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::operator = (const String & path)
|
|
|
|
{
|
|
|
|
Assign(path);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool SysPath::operator == (const SysPath & o) const
|
|
|
|
{
|
|
|
|
return (m_Drive == o.m_Drive) && (m_Absolute == o.m_Absolute)
|
|
|
|
&& (m_Name == o.m_Name) && (m_Dirs == o.m_Dirs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool SysPath::operator != (const SysPath & o) const
|
|
|
|
{
|
|
|
|
return !(*this == o);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath::operator bool () const
|
|
|
|
{
|
|
|
|
return (m_Dirs.empty() && m_Name.empty() && (m_Drive == 0) && !m_Absolute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const String & SysPath::operator [] (uint32_t n) const
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is this within the bounds of the directory list?
|
|
|
|
if (n < m_Dirs.size())
|
|
|
|
{
|
|
|
|
return m_Dirs[n];
|
|
|
|
}
|
|
|
|
// Fall back to the file name
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
int32_t SysPath::Cmp(const SysPath & o) const
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
if (*this == o)
|
|
|
|
return 0;
|
|
|
|
else if (ToBuffer().Position() > o.ToBuffer().Position())
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object SysPath::ToString() const
|
|
|
|
{
|
2016-03-11 03:23:59 +01:00
|
|
|
return BufferToStrObj(ToBuffer());
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void SysPath::Swap(SysPath & path)
|
|
|
|
{
|
|
|
|
m_Dirs.swap(path.m_Dirs);
|
|
|
|
m_Name.swap(path.m_Name);
|
|
|
|
std::swap(m_Drive, path.m_Drive);
|
|
|
|
std::swap(m_Absolute, path.m_Absolute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void SysPath::Clear()
|
|
|
|
{
|
|
|
|
m_Dirs.clear();
|
|
|
|
m_Name.clear();
|
|
|
|
m_Drive = 0;
|
|
|
|
m_Absolute = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (!path || *path == '\0')
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path, path + strlen(path));
|
|
|
|
#else
|
|
|
|
ParseUnix(path, path + strlen(path));
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const SQChar * path, int32_t style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Assign(path, static_cast< Style >(style));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const SQChar * path, Style style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (!path || *path == '\0')
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Identify which style was requested
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case Style::Unix:
|
|
|
|
ParseUnix(path, path + strlen(path));
|
|
|
|
break;
|
2021-01-30 07:51:39 +01:00
|
|
|
case Style::Windows: // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseWindows(path, path + strlen(path));
|
|
|
|
break;
|
|
|
|
case Style::Native:
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path, path + strlen(path));
|
|
|
|
#else
|
|
|
|
ParseUnix(path, path + strlen(path));
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
break;
|
|
|
|
case Style::Guess:
|
|
|
|
ParseGuess(path, path + strlen(path));
|
|
|
|
break;
|
|
|
|
case Style::Dynamic:
|
|
|
|
ParseDynamic(path, path + strlen(path));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const Buffer & path, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else if (size < 0)
|
|
|
|
{
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.Data(), &path.Cursor());
|
|
|
|
#else
|
|
|
|
ParseUnix(path.Data(), &path.Cursor());
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
else if (static_cast< uint32_t >(size) < path.Capacity())
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.Data(), path.Data() + size);
|
|
|
|
#else
|
|
|
|
ParseUnix(path.Data(), path.Data() + size);
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("The specified path size is out of range: {} >= {}", size, path.Capacity());
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const Buffer & path, Style style, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else if (size < 0)
|
|
|
|
{
|
|
|
|
// Identify which style was requested
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case Style::Unix:
|
|
|
|
ParseUnix(path.Data(), &path.Cursor());
|
|
|
|
break;
|
2021-01-30 07:51:39 +01:00
|
|
|
case Style::Windows: // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseWindows(path.Data(), &path.Cursor());
|
|
|
|
break;
|
|
|
|
case Style::Native:
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.Data(), &path.Cursor());
|
|
|
|
#else
|
|
|
|
ParseUnix(path.Data(), &path.Cursor());
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
break;
|
|
|
|
case Style::Guess:
|
|
|
|
ParseGuess(path.Data(), &path.Cursor());
|
|
|
|
break;
|
|
|
|
case Style::Dynamic:
|
|
|
|
ParseDynamic(path.Data(), &path.Cursor());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 07:51:39 +01:00
|
|
|
else if (static_cast< uint32_t >(size) < path.Capacity())
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Identify which style was requested
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case Style::Unix:
|
|
|
|
ParseUnix(path.Data(), path.Data() + size);
|
|
|
|
break;
|
2021-01-30 07:51:39 +01:00
|
|
|
case Style::Windows: // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseWindows(path.Data(), path.Data() + size);
|
|
|
|
break;
|
|
|
|
case Style::Native:
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.Data(), path.Data() + size);
|
|
|
|
#else
|
|
|
|
ParseUnix(path.Data(), path.Data() + size);
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
break;
|
|
|
|
case Style::Guess:
|
|
|
|
ParseGuess(path.Data(), path.Data() + size);
|
|
|
|
break;
|
|
|
|
case Style::Dynamic:
|
|
|
|
ParseDynamic(path.Data(), path.Data() + size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("The specified path size is out of range: {} >= {}", size, path.Capacity());
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(const String & path)
|
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.data(), path.data() + path.size());
|
|
|
|
#else
|
|
|
|
ParseUnix(path.data(), path.data() + path.size());
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(const String & path, Style style)
|
|
|
|
{
|
|
|
|
// Is the specified path valid?
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
// Just clear current path
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Identify which style was requested
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case Style::Unix:
|
|
|
|
ParseUnix(path.data(), path.data() + path.size());
|
|
|
|
break;
|
2021-01-30 07:51:39 +01:00
|
|
|
case Style::Windows: // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseWindows(path.data(), path.data() + path.size());
|
|
|
|
break;
|
|
|
|
case Style::Native:
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
ParseWindows(path.data(), path.data() + path.size());
|
|
|
|
#else
|
|
|
|
ParseUnix(path.data(), path.data() + path.size());
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
break;
|
|
|
|
case Style::Guess:
|
|
|
|
ParseGuess(path.data(), path.data() + path.size());
|
|
|
|
break;
|
|
|
|
case Style::Dynamic:
|
|
|
|
ParseDynamic(path.data(), path.data() + path.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Assign(const SysPath & parent, const SQChar * name)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Copy the parent values
|
|
|
|
*this = parent;
|
|
|
|
// Set the specified file name
|
|
|
|
SetFilename(name);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(const SysPath & parent, const String & name)
|
|
|
|
{
|
|
|
|
// Copy the parent values
|
|
|
|
*this = parent;
|
|
|
|
// Set the specified file name
|
|
|
|
SetFilename(name);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(const SysPath & parent, const SysPath & relative)
|
|
|
|
{
|
|
|
|
// Copy the parent values
|
|
|
|
*this = parent;
|
|
|
|
// Resolve the specified path
|
|
|
|
Resolve(relative);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(const SysPath & path)
|
|
|
|
{
|
|
|
|
// Just use regular assignment
|
|
|
|
*this = path;
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Assign(SysPath && path)
|
|
|
|
{
|
|
|
|
// Just use regular assignment
|
|
|
|
*this = path;
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::AssignDir(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::AssignDir(const SQChar * path, int32_t style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path, style);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::AssignDir(const SQChar * path, Style style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path, style);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::AssignDir(const Buffer & path, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path, size);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::AssignDir(const Buffer & path, Style style, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path, style, size);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::AssignDir(const String & path)
|
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::AssignDir(const String & path, Style style)
|
|
|
|
{
|
|
|
|
// Assign the specified path
|
|
|
|
Assign(path, style);
|
|
|
|
// Force it to be a directory and allow chaining
|
|
|
|
return MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer SysPath::ToBuffer() const
|
|
|
|
{
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
return BuildWindows();
|
|
|
|
#else
|
|
|
|
return BuildUnix();
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer SysPath::ToBuffer(Style style) const
|
|
|
|
{
|
|
|
|
if (style == Style::Unix)
|
|
|
|
{
|
|
|
|
return BuildUnix();
|
|
|
|
}
|
|
|
|
else if (style == Style::Windows)
|
|
|
|
{
|
|
|
|
return BuildWindows();
|
|
|
|
}
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
return BuildWindows();
|
|
|
|
#else
|
|
|
|
return BuildUnix();
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
Object SysPath::ToStr(int32_t style) const
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2016-03-11 03:23:59 +01:00
|
|
|
return BufferToStrObj(ToBuffer(static_cast< Style >(style)));
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
void SysPath::FromString(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
Assign(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeDirectory()
|
|
|
|
{
|
|
|
|
// Do we even have a name?
|
|
|
|
if (!m_Name.empty())
|
|
|
|
{
|
|
|
|
// Make it a directory
|
|
|
|
m_Dirs.push_back(std::move(m_Name));
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeFile()
|
|
|
|
{
|
|
|
|
// Do we have some directories and no existing file?
|
|
|
|
if (!m_Dirs.empty() && m_Name.empty())
|
|
|
|
{
|
|
|
|
// Use the last directory as a file
|
|
|
|
m_Name = std::move(m_Dirs.back());
|
|
|
|
// Remove it from the directory list
|
|
|
|
m_Dirs.pop_back();
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeParent()
|
|
|
|
{
|
|
|
|
// Do we have a name?
|
|
|
|
if (m_Name.empty())
|
|
|
|
{
|
|
|
|
// Do we have any existing directories?
|
|
|
|
if (m_Dirs.empty())
|
|
|
|
{
|
|
|
|
// Make sure this path isn't absolute
|
|
|
|
if (!m_Absolute)
|
|
|
|
{
|
|
|
|
// Reference the parent directory
|
|
|
|
m_Dirs.emplace_back("..");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Are we already referencing a parent?
|
2021-01-30 07:51:39 +01:00
|
|
|
if (m_Dirs.back().compare("..") == 0) // NOLINT(readability-string-compare)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Then reference the parent of that parent
|
|
|
|
m_Dirs.emplace_back("..");
|
|
|
|
}
|
|
|
|
// Just pop the last directory
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Dirs.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Just clear the name
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Name.clear();
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeAbsolute()
|
|
|
|
{
|
|
|
|
// Is this path already absolute?
|
|
|
|
if (!m_Absolute)
|
|
|
|
{
|
|
|
|
MakeAbsolute(Working());
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeAbsolute(const SysPath & base)
|
|
|
|
{
|
|
|
|
// Is this path already absolute?
|
|
|
|
if (!m_Absolute)
|
|
|
|
{
|
|
|
|
MakeAbsolute(base);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::MakeAbsolute(SysPath && base)
|
|
|
|
{
|
|
|
|
// Is this path already absolute?
|
|
|
|
if (!m_Absolute)
|
|
|
|
{
|
|
|
|
// Reserve the space upfront
|
|
|
|
base.m_Dirs.reserve(base.m_Dirs.size() + m_Dirs.size());
|
|
|
|
// Move our directories at the back
|
|
|
|
for (auto & dir : m_Dirs)
|
|
|
|
{
|
|
|
|
base.m_Dirs.push_back(std::move(dir));
|
|
|
|
}
|
|
|
|
// Take ownership of base directories
|
|
|
|
m_Dirs.swap(base.m_Dirs);
|
|
|
|
// Copy the drive letter
|
|
|
|
m_Drive = base.m_Drive;
|
|
|
|
// Make absolute only if base is
|
|
|
|
m_Absolute = base.m_Absolute;
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Append(const SysPath & path)
|
|
|
|
{
|
|
|
|
// Make sure this is a directory
|
|
|
|
MakeDirectory();
|
|
|
|
// Only attempt to append if not empty
|
|
|
|
if (!path.Empty())
|
|
|
|
{
|
|
|
|
// Append the directories from the specified path
|
|
|
|
m_Dirs.insert(m_Dirs.end(), path.m_Dirs.begin(), path.m_Dirs.end());
|
|
|
|
// Copy the file name if any
|
|
|
|
m_Name = path.m_Name;
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Append(SysPath && path)
|
|
|
|
{
|
|
|
|
// Make sure this is a directory
|
|
|
|
MakeDirectory();
|
|
|
|
// Only attempt to append if not empty
|
|
|
|
if (!path.Empty())
|
|
|
|
{
|
|
|
|
// Request the necessary directory list size upfront
|
|
|
|
m_Dirs.reserve(m_Dirs.size() + path.m_Dirs.size());
|
|
|
|
// Move all directories at the back of our list
|
|
|
|
for (auto & dir : path.m_Dirs)
|
|
|
|
{
|
|
|
|
m_Dirs.push_back(std::move(dir));
|
|
|
|
}
|
|
|
|
// Move the file name if any
|
|
|
|
m_Name = std::move(path.m_Name);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Append(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Append(SysPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Append(const SQChar * path, int32_t style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Append(SysPath(path, style));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Append(const SQChar * path, Style style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Append(SysPath(path, style));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Append(const Buffer & path, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Append(SysPath(path, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Append(const Buffer & path, Style style, int32_t size)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return Append(SysPath(path, style, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Append(const String & path)
|
|
|
|
{
|
|
|
|
return Append(SysPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Append(const String & path, Style style)
|
|
|
|
{
|
|
|
|
return Append(SysPath(path, style));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const String & SysPath::Directory(uint32_t n) const
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is this within the bounds of the directory list?
|
|
|
|
if (n < m_Dirs.size())
|
|
|
|
{
|
|
|
|
return m_Dirs[n];
|
|
|
|
}
|
|
|
|
// Fall back to the file name
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::Push(const SQChar * dir)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the specified directory valid?
|
|
|
|
if (dir && *dir != 0)
|
|
|
|
{
|
|
|
|
Push(String(dir));
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Push(const String & dir)
|
|
|
|
{
|
|
|
|
// Is the specified directory valid?
|
2021-01-30 07:51:39 +01:00
|
|
|
if (!dir.empty() && dir.compare(".") != 0) // NOLINT(readability-string-compare)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
Push(String(dir));
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Push(String && dir)
|
|
|
|
{
|
|
|
|
// Is the specified directory valid?
|
2021-01-30 07:51:39 +01:00
|
|
|
if (!dir.empty() && dir.compare(".") != 0) // NOLINT(readability-string-compare)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Does it refer to a parent directory?
|
2021-01-30 07:51:39 +01:00
|
|
|
if (dir.compare("..") == 0) // NOLINT(readability-string-compare)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
2020-08-30 17:57:13 +02:00
|
|
|
// Is our last directory already a reference to a parent?
|
2021-01-30 07:51:39 +01:00
|
|
|
if (!m_Dirs.empty() && m_Dirs.back().compare("..") != 0) // NOLINT(readability-string-compare)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
m_Dirs.pop_back();
|
|
|
|
}
|
|
|
|
// Move it at the back of our list
|
|
|
|
else if (!m_Absolute)
|
|
|
|
{
|
|
|
|
m_Dirs.push_back(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move it at the back of our list
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Dirs.push_back(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::PopBack()
|
|
|
|
{
|
|
|
|
// Do we even have any directories?
|
|
|
|
if (!m_Dirs.empty())
|
|
|
|
{
|
|
|
|
// Erase the last one
|
|
|
|
m_Dirs.pop_back();
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::PopFront()
|
|
|
|
{
|
|
|
|
// Do we even have any directories?
|
|
|
|
if (!m_Dirs.empty())
|
|
|
|
{
|
|
|
|
// Erase the first one
|
|
|
|
m_Dirs.erase(m_Dirs.begin());
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::SetFilename(const SQChar * name)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the file name even valid?
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
m_Name.assign(name);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::SetFilename(const String & name)
|
|
|
|
{
|
|
|
|
// Is the file name even valid?
|
|
|
|
m_Name.assign(name);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::SetFilename(String && name)
|
|
|
|
{
|
|
|
|
// Is the file name even valid?
|
|
|
|
m_Name.assign(name);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::SetBasename(const SQChar * name)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Is the file name even valid?
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
// Extract the current extension
|
|
|
|
String ext = GetExtension();
|
|
|
|
// Assign the new base name
|
|
|
|
m_Name.assign(name);
|
|
|
|
// Was there an extension before?
|
|
|
|
if (!ext.empty())
|
|
|
|
{
|
|
|
|
// Add the extension separator
|
|
|
|
m_Name.push_back('.');
|
|
|
|
// Add the original extension
|
|
|
|
m_Name.append(ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::SetBasename(const String & name)
|
|
|
|
{
|
|
|
|
// Extract the current extension
|
|
|
|
String ext = GetExtension();
|
|
|
|
// Assign the new base name
|
|
|
|
m_Name.assign(name);
|
|
|
|
// Was there an extension before?
|
|
|
|
if (!ext.empty())
|
|
|
|
{
|
|
|
|
// Add the extension separator
|
|
|
|
m_Name.push_back('.');
|
|
|
|
// Add the original extension
|
|
|
|
m_Name.append(ext);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::SetBasename(String && name)
|
|
|
|
{
|
|
|
|
// Extract the current extension
|
|
|
|
String ext = GetExtension();
|
|
|
|
// Assign the new base name
|
|
|
|
m_Name.assign(name);
|
|
|
|
// Was there an extension before?
|
|
|
|
if (!ext.empty())
|
|
|
|
{
|
|
|
|
// Add the extension separator
|
|
|
|
m_Name.push_back('.');
|
|
|
|
// Add the original extension
|
|
|
|
m_Name.append(ext);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
String SysPath::GetBasename() const
|
|
|
|
{
|
|
|
|
// Attempt to find the last dot in the file name
|
|
|
|
const String::size_type pos = m_Name.rfind('.');
|
|
|
|
// Was there an extension separator?
|
|
|
|
if (pos != String::npos)
|
|
|
|
{
|
|
|
|
// Return everything before the separator
|
|
|
|
return m_Name.substr(0, pos);
|
|
|
|
}
|
|
|
|
// Return the whole name
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath & SysPath::SetExtension(const SQChar * ext)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Attempt to find the last dot in the file name
|
|
|
|
const String::size_type pos = m_Name.rfind('.');
|
|
|
|
// Was there an extension separator?
|
|
|
|
if (pos != String::npos)
|
|
|
|
{
|
|
|
|
// Erase the current extension
|
|
|
|
m_Name.resize(pos);
|
|
|
|
}
|
|
|
|
// Is there an extension to append?
|
|
|
|
if (ext && *ext != 0)
|
|
|
|
{
|
|
|
|
m_Name.push_back('.');
|
|
|
|
m_Name.append(ext);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::SetExtension(const String & ext)
|
|
|
|
{
|
|
|
|
// Attempt to find the last dot in the file name
|
|
|
|
const String::size_type pos = m_Name.rfind('.');
|
|
|
|
// Was there an extension separator?
|
|
|
|
if (pos != String::npos)
|
|
|
|
{
|
|
|
|
// Erase the current extension
|
|
|
|
m_Name.resize(pos);
|
|
|
|
}
|
|
|
|
// Is there an extension to append?
|
|
|
|
if (!ext.empty())
|
|
|
|
{
|
|
|
|
m_Name.push_back('.');
|
|
|
|
m_Name.append(ext);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
String SysPath::GetExtension() const
|
|
|
|
{
|
|
|
|
// Attempt to find the last dot in the file name
|
|
|
|
const String::size_type pos = m_Name.rfind('.');
|
|
|
|
// Was there an extension separator?
|
|
|
|
if (pos != String::npos)
|
|
|
|
{
|
|
|
|
// Return everything after the separator
|
|
|
|
return m_Name.substr(pos + 1);
|
|
|
|
}
|
|
|
|
// Default to an empty string
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * SysPath::GetExtensionC() const
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Attempt to find the last dot in the file name
|
|
|
|
const String::size_type pos = m_Name.rfind('.');
|
|
|
|
// Was there an extension separator?
|
|
|
|
if (pos != String::npos)
|
|
|
|
{
|
|
|
|
// Because indexing starts from 0, the separator is skipped
|
|
|
|
return &m_Name[pos+1];
|
|
|
|
}
|
|
|
|
// Default to an empty string
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Parent() const
|
|
|
|
{
|
|
|
|
// Make a copy
|
|
|
|
SysPath p(*this);
|
|
|
|
// Force the copy to be parent
|
|
|
|
p.MakeParent();
|
|
|
|
// Return ownership of copy
|
2021-01-27 06:27:48 +01:00
|
|
|
return p;
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath & SysPath::Resolve(const SysPath & path)
|
|
|
|
{
|
|
|
|
// Is the specified path absolute?
|
|
|
|
if (path.m_Absolute)
|
|
|
|
{
|
|
|
|
// Copy it's values
|
|
|
|
Assign(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Append its directories
|
|
|
|
for (const auto & dir : path.m_Dirs)
|
|
|
|
{
|
|
|
|
Push(dir);
|
|
|
|
}
|
|
|
|
// Copy its file name
|
|
|
|
m_Name = path.m_Name;
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
void SysPath::ParseUnix(const SQChar * pos, const SQChar * end)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Clear previous path information
|
|
|
|
Clear();
|
|
|
|
// Is there even a path to parse?
|
|
|
|
if (pos == end)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If the path starts with a forward slash then the path is absolute
|
|
|
|
else if (*pos == '/')
|
|
|
|
{
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
// Skip this directory separator
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
// If the path starts with the tilde character then the home directory was requested
|
|
|
|
else if (*pos == '~')
|
|
|
|
{
|
|
|
|
// To be expanded, the tilde character must be followed by a slash or no other character
|
|
|
|
if (pos == end || *(++pos) == '/')
|
|
|
|
{
|
|
|
|
// Obtain the home path
|
|
|
|
SysPath up(Home());
|
|
|
|
// Take ownership, don't copy
|
|
|
|
m_Dirs.swap(up.m_Dirs);
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
}
|
|
|
|
// Go back to the previous character and use it literally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make another iterator to slice directory names in one go
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * itr = pos;
|
2016-03-11 03:14:28 +01:00
|
|
|
// Extract the remaining directories from the specified path
|
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
// Have we encountered a directory separator?
|
|
|
|
if (*itr == '/')
|
|
|
|
{
|
|
|
|
// Slice the name from the path if valid
|
|
|
|
if (itr != pos)
|
|
|
|
{
|
|
|
|
m_Dirs.emplace_back(pos, itr);
|
|
|
|
}
|
|
|
|
// Move the iterator to the current position
|
|
|
|
pos = ++itr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Grab the last name if any
|
|
|
|
if (pos != end)
|
|
|
|
{
|
|
|
|
m_Name.assign(pos, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
void SysPath::ParseWindows(const SQChar * pos, const SQChar * end)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Clear previous path information
|
|
|
|
Clear();
|
|
|
|
// Is there even a path to parse?
|
|
|
|
if (pos == end)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If the path starts with a forward slash then the path is absolute
|
|
|
|
else if (*pos == '\\')
|
|
|
|
{
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
// Skip this directory separator
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
// If the path starts with the tilde character then the home directory was requested
|
|
|
|
else if (*pos == '~')
|
|
|
|
{
|
|
|
|
// To be expanded, the tilde character must be followed by a slash or no other character
|
|
|
|
if (pos == end || *(++pos) == '\\')
|
|
|
|
{
|
|
|
|
// Obtain the home path
|
|
|
|
SysPath up(Home());
|
|
|
|
// Take ownership, don't copy
|
|
|
|
m_Dirs.swap(up.m_Dirs);
|
|
|
|
// Also copy the drive letter
|
|
|
|
m_Drive = up.m_Drive;
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
}
|
|
|
|
// Go back to the previous character and use it literally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Is it possible to have a drive letter?
|
|
|
|
else if ((end - pos) > 2 && pos[1] == ':' && pos[2] == '\\' && isalpha(*pos) != 0)
|
|
|
|
{
|
|
|
|
// Grab the drive letter
|
|
|
|
m_Drive = *pos;
|
|
|
|
// Skip the drive path and colon
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
// Is it possible to have just the drive letter?
|
|
|
|
else if ((end - pos) == 2 && pos[1] == ':' && isalpha(*pos) != 0)
|
|
|
|
{
|
|
|
|
// Grab the drive letter
|
|
|
|
m_Drive = *pos;
|
|
|
|
// Nothing left to parse
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make another iterator to slice directory names in one go
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * itr = pos;
|
2016-03-11 03:14:28 +01:00
|
|
|
// Extract the remaining directories from the specified path
|
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
// Have we encountered a directory separator?
|
|
|
|
if (*itr == '\\')
|
|
|
|
{
|
|
|
|
// Slice the name from the path if valid
|
|
|
|
if (itr != pos)
|
|
|
|
{
|
|
|
|
m_Dirs.emplace_back(pos, itr);
|
|
|
|
}
|
|
|
|
// Move the iterator to the current position
|
|
|
|
pos = ++itr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Grab the last name if any
|
|
|
|
if (pos != end)
|
|
|
|
{
|
|
|
|
m_Name.assign(pos, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
void SysPath::ParseDynamic(const SQChar * pos, const SQChar * end)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Clear previous path information
|
|
|
|
Clear();
|
|
|
|
// Is there even a path to parse?
|
|
|
|
if (pos == end)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If the path starts with a slash then the path is absolute
|
|
|
|
else if (*pos == '\\' || *pos == '/')
|
|
|
|
{
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
// Skip this directory separator
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
// If the path starts with the tilde character then the home directory was requested
|
|
|
|
else if (*pos == '~')
|
|
|
|
{
|
|
|
|
// Skip the tilde character
|
|
|
|
++pos;
|
|
|
|
// The tilde character must be followed by a slash or no other character to be expanded
|
|
|
|
if (pos == end || *pos == '/' || *pos == '\\')
|
|
|
|
{
|
|
|
|
// Obtain the home path
|
|
|
|
SysPath up(Home());
|
|
|
|
// Take ownership, don't copy
|
|
|
|
m_Dirs.swap(up.m_Dirs);
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
// Also copy the drive letter
|
|
|
|
m_Drive = up.m_Drive;
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
// This is now an absolute path
|
|
|
|
m_Absolute = true;
|
|
|
|
}
|
|
|
|
// Go back to the previous character and use it literally
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef SQMOD_OS_WINDOWS
|
|
|
|
// Is it possible to have a drive letter?
|
|
|
|
else if ((end - pos) > 2 && pos[1] == ':' && (pos[2] == '\\' || pos[2] == '/') && isalpha(*pos) != 0)
|
|
|
|
{
|
|
|
|
// Grab the drive letter
|
|
|
|
m_Drive = *pos;
|
|
|
|
// Skip the drive path and colon
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
// Is it possible to have just the drive letter?
|
|
|
|
else if ((end - pos) == 2 && pos[1] == ':' && isalpha(*pos) != 0)
|
|
|
|
{
|
|
|
|
// Grab the drive letter
|
|
|
|
m_Drive = *pos;
|
|
|
|
// Nothing left to parse
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // SQMOD_OS_WINDOWS
|
|
|
|
// Make another iterator to slice directory names in one go
|
2021-01-30 07:51:39 +01:00
|
|
|
const SQChar * itr = pos;
|
2016-03-11 03:14:28 +01:00
|
|
|
// Extract the remaining directories from the specified path
|
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
// Have we encountered a directory separator?
|
|
|
|
if (*itr == '/' || *itr == '\\')
|
|
|
|
{
|
|
|
|
// Slice the name from the path if valid
|
|
|
|
if (itr != pos)
|
|
|
|
{
|
|
|
|
m_Dirs.emplace_back(pos, itr);
|
|
|
|
}
|
|
|
|
// Move the iterator to the current position
|
|
|
|
pos = ++itr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Grab the last name if any
|
|
|
|
if (pos != end)
|
|
|
|
{
|
|
|
|
m_Name.assign(pos, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
void SysPath::ParseGuess(const SQChar * pos, const SQChar * end)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
// Scan for forward slash
|
2021-01-30 07:51:39 +01:00
|
|
|
const bool has_fwslash = (strchr(pos, '/') != nullptr);
|
|
|
|
const bool has_bwslash = (strchr(pos, '\\') != nullptr);
|
2016-03-11 03:14:28 +01:00
|
|
|
// Does it contain both forward and backward slashes?
|
|
|
|
if (has_fwslash && has_bwslash)
|
2021-01-30 07:51:39 +01:00
|
|
|
{ // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseDynamic(pos, end);
|
|
|
|
}
|
|
|
|
// Does it contain the forward slash?
|
|
|
|
else if (has_fwslash)
|
|
|
|
{
|
|
|
|
ParseUnix(pos, end);
|
|
|
|
}
|
|
|
|
// Does it contain the backward slash?
|
|
|
|
else if (has_bwslash)
|
2021-01-30 07:51:39 +01:00
|
|
|
{ // NOLINT(bugprone-branch-clone)
|
2016-03-11 03:14:28 +01:00
|
|
|
ParseWindows(pos, end);
|
|
|
|
}
|
|
|
|
// Does it contain a drive letter?
|
|
|
|
else if ((end - pos) == 2 && pos[1] == ':' && isalpha(*pos) != 0)
|
|
|
|
{
|
|
|
|
ParseWindows(pos, end);
|
|
|
|
}
|
|
|
|
// Try to parse it as a dynamic path
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ParseDynamic(pos, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer SysPath::BuildUnix() const
|
|
|
|
{
|
|
|
|
// Obtain a buffer capable of storing a full path
|
|
|
|
Buffer b(SQMOD_MAX_PATH);
|
|
|
|
// Is this an absolute path?
|
|
|
|
if (m_Absolute)
|
|
|
|
{
|
|
|
|
// Start with a slash
|
|
|
|
b.Push('/');
|
|
|
|
}
|
|
|
|
// Concatenate all directories
|
|
|
|
for (const auto & dir : m_Dirs)
|
|
|
|
{
|
|
|
|
// Append the name
|
2021-01-30 07:51:39 +01:00
|
|
|
b.AppendS(dir.c_str(), static_cast< uint32_t >(dir.size()));
|
2016-03-11 03:14:28 +01:00
|
|
|
// Separate from next
|
|
|
|
b.Push('/');
|
|
|
|
}
|
|
|
|
// Is there a file name to add?
|
|
|
|
if (!m_Name.empty())
|
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
b.AppendS(m_Name.c_str(), static_cast< uint32_t >(m_Name.size()));
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
// Make sure the string is null terminated
|
|
|
|
b.Cursor() = '\0';
|
|
|
|
// Return ownership of buffer
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Buffer SysPath::BuildWindows() const
|
|
|
|
{
|
|
|
|
// Obtain a buffer capable of storing a full path
|
|
|
|
Buffer b(SQMOD_MAX_PATH);
|
|
|
|
// Does it have a drive letter?
|
|
|
|
if (isalpha(m_Drive) != 0)
|
|
|
|
{
|
|
|
|
// Add the drive letter
|
|
|
|
b.Push(m_Drive);
|
|
|
|
// Add the colon
|
|
|
|
b.Push(':');
|
|
|
|
// Add the slash
|
|
|
|
b.Push('\\');
|
|
|
|
}
|
|
|
|
// Is this an absolute path?
|
|
|
|
else if (m_Absolute)
|
|
|
|
{
|
|
|
|
// Start with a slash
|
|
|
|
b.Push('\\');
|
|
|
|
}
|
|
|
|
// Concatenate all directories
|
|
|
|
for (const auto & dir : m_Dirs)
|
|
|
|
{
|
|
|
|
// Append the name
|
2021-01-30 07:51:39 +01:00
|
|
|
b.AppendS(dir.c_str(), static_cast< uint32_t >(dir.size()));
|
2016-03-11 03:14:28 +01:00
|
|
|
// Separate from next
|
|
|
|
b.Push('\\');
|
|
|
|
}
|
|
|
|
// Is there a file name to add?
|
|
|
|
if (!m_Name.empty())
|
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
b.AppendS(m_Name.c_str(), static_cast< uint32_t >(m_Name.size()));
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
// Make sure the string is null terminated
|
|
|
|
b.Cursor() = '\0';
|
|
|
|
// Return ownership of buffer
|
2021-01-27 06:27:48 +01:00
|
|
|
return b;
|
2016-03-11 03:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::ForDirectory(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return SysPath(path).MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::ForDirectory(const SQChar * path, int32_t style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return SysPath(path, style).MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::ForDirectory(const SQChar * path, Style style)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return SysPath(path, style).MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::ForDirectory(const String & path)
|
|
|
|
{
|
|
|
|
return SysPath(path).MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::ForDirectory(const String & path, Style style)
|
|
|
|
{
|
|
|
|
return SysPath(path, style).MakeDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::Expand(const SQChar * path)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return SysPath(SysEnv::ExpandPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Home()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::HomeDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::ConfigHome()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::ConfigHomeDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::DataHome()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::DataHomeDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::TempHome()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::TempHomeDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::CacheHome()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::CacheHomeDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Working()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::WorkingDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Temp()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::TempDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Config()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::ConfigDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::System()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::SystemDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SysPath SysPath::Null()
|
|
|
|
{
|
|
|
|
return SysPath(SysEnv::NullDir());
|
|
|
|
}
|
|
|
|
|
2016-09-03 23:49:32 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::Real(const SQChar * path)
|
2016-09-03 23:49:32 +02:00
|
|
|
{
|
|
|
|
return SysPath(GetRealFilePath(path));
|
|
|
|
}
|
|
|
|
|
2016-03-11 03:14:28 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::With(const SysPath & parent, const SQChar * name)
|
2016-03-11 03:14:28 +01:00
|
|
|
{
|
|
|
|
return SysPath(parent, name);
|
|
|
|
}
|
|
|
|
|
2016-05-23 02:34:35 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::MakeUnix(const SQChar * path)
|
2016-05-23 02:34:35 +02:00
|
|
|
{
|
|
|
|
return SysPath(path, Style::Unix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::MakeWindows(const SQChar * path)
|
2016-05-23 02:34:35 +02:00
|
|
|
{
|
|
|
|
return SysPath(path, Style::Windows);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::MakeNative(const SQChar * path)
|
2016-05-23 02:34:35 +02:00
|
|
|
{
|
|
|
|
return SysPath(path, Style::Native);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::MakeGuess(const SQChar * path)
|
2016-05-23 02:34:35 +02:00
|
|
|
{
|
|
|
|
return SysPath(path, Style::Guess);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
SysPath SysPath::MakeDynamic(const SQChar * path)
|
2016-05-23 02:34:35 +02:00
|
|
|
{
|
|
|
|
return SysPath(path, Style::Dynamic);
|
|
|
|
}
|
|
|
|
|
2019-08-14 03:33:18 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
String SysPath::NormalizePath(SQInteger s, StackStrF & val)
|
|
|
|
{
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.Proc(true)))
|
|
|
|
{
|
|
|
|
STHROWLASTF("Invalid string");
|
|
|
|
}
|
|
|
|
// Is the string empty?
|
|
|
|
else if (!val.mLen)
|
|
|
|
{
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
// Turn it into a string that we can edit
|
2021-01-30 07:51:39 +01:00
|
|
|
String str(val.mPtr, static_cast< size_t >(val.mLen));
|
|
|
|
// Replace all occurrences of the specified character
|
2019-08-14 03:33:18 +02:00
|
|
|
for (String::reference c : str)
|
|
|
|
{
|
|
|
|
if (c == '/' || c == '\\')
|
|
|
|
{
|
|
|
|
c = static_cast< String::value_type >(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return the new string
|
|
|
|
return str;
|
|
|
|
}
|
2016-03-11 03:14:28 +01:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_SysPath(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-11-15 20:55:03 +01:00
|
|
|
RootTable(vm).Bind(Typename::Str,
|
|
|
|
Class< SysPath >(vm, Typename::Str)
|
2016-03-11 03:14:28 +01:00
|
|
|
// Constructors
|
|
|
|
.Ctor()
|
2021-07-04 19:21:47 +02:00
|
|
|
.Ctor< StackStrF & >()
|
|
|
|
.Ctor< StackStrF &, int32_t >()
|
2016-06-03 20:26:19 +02:00
|
|
|
// Meta-methods
|
2016-11-15 20:42:27 +01:00
|
|
|
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("_tostring"), &SysPath::ToString)
|
2016-11-15 20:55:03 +01:00
|
|
|
.Func(_SC("cmp"), &SysPath::Cmp)
|
2016-03-11 03:14:28 +01:00
|
|
|
// Properties
|
2021-07-04 19:21:47 +02:00
|
|
|
.Prop(_SC("String"), &SysPath::ToString, &SysPath::FromStringSq)
|
2016-09-03 23:46:12 +02:00
|
|
|
.Prop(_SC("IsAbs"), &SysPath::IsAbsolute)
|
|
|
|
.Prop(_SC("IsAbsolute"), &SysPath::IsAbsolute)
|
|
|
|
.Prop(_SC("IsRel"), &SysPath::IsRelative)
|
|
|
|
.Prop(_SC("IsRelative"), &SysPath::IsRelative)
|
|
|
|
.Prop(_SC("IsDir"), &SysPath::IsDirectory)
|
|
|
|
.Prop(_SC("IsDirectory"), &SysPath::IsDirectory)
|
|
|
|
.Prop(_SC("IsFile"), &SysPath::IsFile)
|
|
|
|
.Prop(_SC("IsEmpty"), &SysPath::Empty)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Prop(_SC("Drive"), &SysPath::GetDrive, &SysPath::SetDrive)
|
|
|
|
.Prop(_SC("Depth"), &SysPath::Depth)
|
2021-07-04 19:21:47 +02:00
|
|
|
.Prop(_SC("Filename"), &SysPath::GetFilename, &SysPath::SetFilenameSq)
|
|
|
|
.Prop(_SC("Basename"), &SysPath::GetBasename, &SysPath::SetBasenameSq)
|
|
|
|
.Prop(_SC("Extension"), &SysPath::GetExtensionC, &SysPath::SetExtensionSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Prop(_SC("Parent"), &SysPath::Parent)
|
|
|
|
// Member Methods
|
|
|
|
.Func(_SC("Swap"), &SysPath::Swap)
|
|
|
|
.Func(_SC("Clear"), &SysPath::Clear)
|
2021-07-04 19:21:47 +02:00
|
|
|
.Func(_SC("AssignPath"), &SysPath::AssignPath)
|
|
|
|
.Func(_SC("AppendPath"), &SysPath::AppendPath)
|
2016-03-11 19:52:00 +01:00
|
|
|
.Func(_SC("Build"), &SysPath::ToStr)
|
|
|
|
.Func(_SC("ToStr"), &SysPath::ToStr)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("MakeDir"), &SysPath::MakeDirectory)
|
|
|
|
.Func(_SC("MakeDirectory"), &SysPath::MakeDirectory)
|
|
|
|
.Func(_SC("MakeFile"), &SysPath::MakeFile)
|
|
|
|
.Func(_SC("MakeParent"), &SysPath::MakeParent)
|
|
|
|
.Func(_SC("Dir"), &SysPath::Directory)
|
|
|
|
.Func(_SC("Directory"), &SysPath::Directory)
|
2021-07-04 19:21:47 +02:00
|
|
|
.FmtFunc(_SC("Push"), &SysPath::PushSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("PopBack"), &SysPath::PopBack)
|
|
|
|
.Func(_SC("PopFront"), &SysPath::PopFront)
|
2021-07-04 19:22:25 +02:00
|
|
|
.FmtFunc(_SC("SetFilename"), &SysPath::SetFilenameSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("GetFilename"), &SysPath::GetFilename)
|
2021-07-04 19:22:25 +02:00
|
|
|
.FmtFunc(_SC("SetBasename"), &SysPath::SetBasenameSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("GetBasename"), &SysPath::GetBasename)
|
2021-07-04 19:22:25 +02:00
|
|
|
.FmtFunc(_SC("SetExtension"), &SysPath::SetExtensionSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.Func(_SC("GetExtension"), &SysPath::GetExtension)
|
|
|
|
.Func(_SC("Resolve"), &SysPath::Resolve)
|
2021-07-04 19:21:47 +02:00
|
|
|
.FmtFunc(_SC("Append"), &SysPath::AppendSq)
|
|
|
|
.FmtFunc(_SC("AppendAs"), &SysPath::AppendAs)
|
|
|
|
.FmtFunc(_SC("Assign"), &SysPath::AssignSq)
|
|
|
|
.FmtFunc(_SC("AssignAs"), &SysPath::AssignAs)
|
|
|
|
.FmtFunc(_SC("AssignDir"), &SysPath::AssignDirSq)
|
|
|
|
.FmtFunc(_SC("AssignDirAs"), &SysPath::AssignDirAs)
|
2016-03-11 03:14:28 +01:00
|
|
|
// Member Overloads
|
2021-07-04 19:21:47 +02:00
|
|
|
.Overload(_SC("MakeAbsolute"), &SysPath::MakeAbsolute0)
|
|
|
|
.Overload(_SC("MakeAbsolute"), &SysPath::MakeAbsolute1)
|
2016-03-11 03:14:28 +01:00
|
|
|
// Static Functions
|
|
|
|
.StaticFunc(_SC("Separator"), &SysPath::Separator)
|
|
|
|
.StaticFunc(_SC("PathSeparator"), &SysPath::PathSeparator)
|
2021-07-04 19:21:47 +02:00
|
|
|
.StaticFunc(_SC("Expand"), &SysPath::ExpandSq)
|
2016-03-11 03:14:28 +01:00
|
|
|
.StaticFunc(_SC("Home"), &SysPath::Home)
|
|
|
|
.StaticFunc(_SC("ConfigHome"), &SysPath::ConfigHome)
|
|
|
|
.StaticFunc(_SC("DataHome"), &SysPath::DataHome)
|
|
|
|
.StaticFunc(_SC("TempHome"), &SysPath::TempHome)
|
|
|
|
.StaticFunc(_SC("CacheHome"), &SysPath::CacheHome)
|
|
|
|
.StaticFunc(_SC("Current"), &SysPath::Working)
|
|
|
|
.StaticFunc(_SC("Working"), &SysPath::Working)
|
|
|
|
.StaticFunc(_SC("Temp"), &SysPath::Temp)
|
|
|
|
.StaticFunc(_SC("Config"), &SysPath::Config)
|
|
|
|
.StaticFunc(_SC("System"), &SysPath::System)
|
|
|
|
.StaticFunc(_SC("Null"), &SysPath::Null)
|
2021-07-04 19:21:47 +02:00
|
|
|
.StaticFmtFunc(_SC("Real"), &SysPath::RealSq)
|
|
|
|
.StaticFmtFunc(_SC("With"), &SysPath::WithSq)
|
|
|
|
.StaticFmtFunc(_SC("Unix"), &SysPath::MakeUnixSq)
|
|
|
|
.StaticFmtFunc(_SC("Windows"), &SysPath::MakeWindowsSq)
|
|
|
|
.StaticFmtFunc(_SC("Native"), &SysPath::MakeNativeSq)
|
|
|
|
.StaticFmtFunc(_SC("Guess"), &SysPath::MakeGuessSq)
|
|
|
|
.StaticFmtFunc(_SC("Dynamic"), &SysPath::MakeDynamicSq)
|
2019-08-14 03:33:18 +02:00
|
|
|
.StaticFmtFunc(_SC("Normalize"), &SysPath::NormalizePath)
|
2016-03-11 03:14:28 +01:00
|
|
|
// Static Overloads
|
2021-07-04 19:21:47 +02:00
|
|
|
.StaticFmtFunc(_SC("ForDir"), &SysPath::ForDirectorySq)
|
|
|
|
.StaticFmtFunc(_SC("ForDirAs"), &SysPath::ForDirectoryAs)
|
|
|
|
.StaticFmtFunc(_SC("ForDirectory"), &SysPath::ForDirectorySq)
|
|
|
|
.StaticFmtFunc(_SC("ForDirectoryAs"), &SysPath::ForDirectoryAs)
|
2016-03-11 03:14:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
ConstTable(vm).Enum(_SC("SqSysPathStyle"), Enumeration(vm)
|
2021-01-30 07:51:39 +01:00
|
|
|
.Const(_SC("Unix"), static_cast< int32_t >(SysPath::Style::Unix))
|
|
|
|
.Const(_SC("Windows"), static_cast< int32_t >(SysPath::Style::Windows))
|
|
|
|
.Const(_SC("Native"), static_cast< int32_t >(SysPath::Style::Native))
|
|
|
|
.Const(_SC("Guess"), static_cast< int32_t >(SysPath::Style::Guess))
|
|
|
|
.Const(_SC("Dynamic"), static_cast< int32_t >(SysPath::Style::Dynamic))
|
2016-03-11 03:14:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|