2020-03-22 00:45:04 +01:00
|
|
|
#pragma once
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
#include "Core/Common.hpp"
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
#include <cwchar>
|
2019-08-14 03:36:45 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <tinydir.h>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
typedef std::unique_ptr< tinydir_dir > TinyDir;
|
|
|
|
typedef std::unique_ptr< tinydir_file > TinyFile;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* This class represents file-system directories in a platform-independent manner.
|
|
|
|
*/
|
|
|
|
class SysDir
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
TinyDir mHandle{}; /* Handle to the managed directory. */
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Make sure a valid handle is being managed before attempting to use it.
|
|
|
|
*/
|
|
|
|
void Validate() const
|
|
|
|
{
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid directory handle. Please open a directory first.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Make sure a valid handle is being managed before attempting to use it.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Validate(const SQChar * action) const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("Cannot {}. Invalid directory handle.", action);
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Defaults to a null handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysDir() = default;
|
2019-08-14 03:36:45 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Opens the directory at the specified path.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
explicit SysDir(StackStrF & path)
|
2019-08-14 03:36:45 +02:00
|
|
|
: SysDir(false, path)
|
|
|
|
{
|
|
|
|
/*...*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Construct from an existing file handle.
|
|
|
|
*/
|
|
|
|
explicit SysDir(tinydir_dir * handle)
|
|
|
|
: mHandle(handle)
|
|
|
|
{
|
|
|
|
/*...*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Opens the directory at the specified path.
|
|
|
|
*/
|
|
|
|
SysDir(bool sorted, StackStrF & path)
|
|
|
|
: SysDir()
|
|
|
|
{
|
|
|
|
// Should we open this in sorted mode?
|
|
|
|
if (sorted)
|
|
|
|
{
|
|
|
|
OpenSorted(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Open(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor (disabled).
|
|
|
|
*/
|
|
|
|
SysDir(const SysDir &) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysDir(SysDir && o) noexcept = default;
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~SysDir()
|
|
|
|
{
|
|
|
|
// Is there handle being managed?
|
|
|
|
if (mHandle)
|
|
|
|
{
|
|
|
|
tinydir_close(mHandle.get()); // Close it!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator (disabled).
|
|
|
|
*/
|
|
|
|
SysDir & operator = (const SysDir &) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysDir & operator = (SysDir && o) noexcept
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Avoid self assignment
|
|
|
|
if (this != &o)
|
|
|
|
{
|
|
|
|
// Is there handle being managed?
|
|
|
|
if (mHandle)
|
|
|
|
{
|
|
|
|
tinydir_close(mHandle.get()); // Close it!
|
|
|
|
}
|
|
|
|
// Take ownership of the new handle
|
|
|
|
mHandle = std::move(o.mHandle);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the raw managed handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_dir * Get() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
return mHandle.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the raw managed handle and make one if it doesn't exist already.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_dir * GetOrMake()
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Do we have a handle already?
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
mHandle = std::make_unique< tinydir_dir >(); // Make one
|
|
|
|
}
|
|
|
|
// Return it like we promised
|
|
|
|
return mHandle.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Take ownership of the managed handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_dir * Release()
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
return mHandle.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Release the managed handle.
|
|
|
|
*/
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
mHandle.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to convert an instance of this type to a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String ToString() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return mHandle ? String(mHandle->path, mHandle->path + std::wcslen(mHandle->path)) : _SC("");
|
|
|
|
#else
|
|
|
|
return mHandle ? String(mHandle->path) : String();
|
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Check for the presence of a handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD bool IsValid() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
return static_cast< bool >(mHandle);
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open a handle to the directory at the specified path.
|
|
|
|
*/
|
|
|
|
void Open(StackStrF & path)
|
|
|
|
{
|
|
|
|
// Get the string from the script
|
|
|
|
if ((SQ_FAILED(path.Proc(true))))
|
|
|
|
{
|
|
|
|
STHROWF("Unable to extract the specified path.");
|
|
|
|
}
|
|
|
|
// Allocate handle memory, if necessary
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
mHandle = std::make_unique< tinydir_dir >();
|
|
|
|
}
|
|
|
|
// If there was a handle open, we close it
|
|
|
|
// If we just allocated one, we initialize it (win, either way)
|
|
|
|
tinydir_close(mHandle.get());
|
|
|
|
// Attempt to open the specified directory
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
if (tinydir_open(mHandle.get(), std::wstring(path.mPtr, path.mPtr + path.GetSize()).c_str()) == -1)
|
|
|
|
#else
|
2019-08-14 03:36:45 +02:00
|
|
|
if (tinydir_open(mHandle.get(), path.mPtr) == -1)
|
2021-01-30 07:51:39 +01:00
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Don't keep a bad handle
|
|
|
|
mHandle.reset();
|
|
|
|
// Now we can throw the exception
|
|
|
|
STHROWLASTF("Failed to open directory: %s", path.mPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open a handle to the directory at the specified path.
|
|
|
|
*/
|
|
|
|
void OpenSorted(StackStrF & path)
|
|
|
|
{
|
|
|
|
// Get the string from the script
|
|
|
|
if ((SQ_FAILED(path.Proc(true))))
|
|
|
|
{
|
|
|
|
STHROWF("Unable to extract the specified path.");
|
|
|
|
}
|
|
|
|
// Allocate handle memory, if necessary
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
mHandle = std::make_unique< tinydir_dir >();
|
|
|
|
}
|
|
|
|
// If there was a handle open, we close it
|
|
|
|
// If we just allocated one, we initialize it (win, either way)
|
|
|
|
tinydir_close(mHandle.get());
|
|
|
|
// Attempt to open the specified directory
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
if (tinydir_open_sorted(mHandle.get(), std::wstring(path.mPtr, path.mPtr + path.GetSize()).c_str()) == -1)
|
|
|
|
#else
|
2019-08-14 03:36:45 +02:00
|
|
|
if (tinydir_open_sorted(mHandle.get(), path.mPtr) == -1)
|
2021-01-30 07:51:39 +01:00
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Don't keep a bad handle
|
|
|
|
mHandle.reset();
|
|
|
|
// Now we can throw the exception
|
|
|
|
STHROWLASTF("Failed to open directory: %s", path.mPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open a handle to the directory at the specified path.
|
|
|
|
*/
|
|
|
|
SysDir & OpenSubDir(SQInteger i)
|
|
|
|
{
|
|
|
|
Validate("open sub directory");
|
|
|
|
// Discard current error number
|
|
|
|
errno = 0;
|
|
|
|
// Make sure the specified directory index is valid
|
|
|
|
if (i < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Directory index (" PRINT_INT_FMT " < 0) our of bounds.", i);
|
|
|
|
}
|
|
|
|
if (static_cast< size_t >(i) >= mHandle->n_files)
|
|
|
|
{
|
|
|
|
STHROWF("Directory index (" PRINT_INT_FMT " >= " PRINT_SZ_FMT ") our of bounds.", i, mHandle->n_files);
|
|
|
|
}
|
|
|
|
// Make sure there is a directory at the specified index
|
|
|
|
else if (!mHandle->_files[i].is_dir)
|
|
|
|
{
|
|
|
|
STHROWF("The specified index (" PRINT_INT_FMT ") is not a directory.", i);
|
|
|
|
}
|
|
|
|
// Attempt to open the specified sub-directory
|
|
|
|
if (tinydir_open_subdir_n(mHandle.get(), static_cast< size_t >(i)) == -1)
|
|
|
|
{
|
|
|
|
// Don't keep a bad handle
|
|
|
|
mHandle.reset();
|
|
|
|
// Now we can throw the exception
|
|
|
|
STHROWLASTF("Failed to open sub directory (" PRINT_INT_FMT ").", i);
|
|
|
|
}
|
|
|
|
// Return self to allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Advance to the next element in the opened directory.
|
|
|
|
*/
|
|
|
|
void Next()
|
|
|
|
{
|
|
|
|
Validate("advance to next element");
|
|
|
|
// See if there is a next element
|
|
|
|
if (!mHandle->has_next)
|
|
|
|
{
|
|
|
|
STHROWF("Nothing left to advance to.");
|
|
|
|
}
|
|
|
|
// Discard current error number
|
|
|
|
errno = 0;
|
|
|
|
// Perform the requested action
|
|
|
|
if (tinydir_next(mHandle.get()) == -1)
|
|
|
|
{
|
|
|
|
// This particular error number means the directory was closed
|
|
|
|
if (errno == EIO) mHandle.reset();
|
|
|
|
// Now the exception can be thrown
|
|
|
|
STHROWLASTF("Failed to advance to the next element");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Close the currently associated directory handle.
|
|
|
|
*/
|
|
|
|
void Close()
|
|
|
|
{
|
|
|
|
Validate("close directory");
|
|
|
|
// Perform the requested action
|
|
|
|
tinydir_close(mHandle.get());
|
|
|
|
// Release any associated memory
|
|
|
|
mHandle.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the opened path.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String GetPath() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("obtain path");
|
|
|
|
// Return the requested information
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return String(mHandle->path, mHandle->path + std::wcslen(mHandle->path));
|
|
|
|
#else
|
|
|
|
return String(mHandle->path);
|
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See if there's a next element in the opened directory.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD bool HasNext() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("check for next");
|
|
|
|
// Return the requested information
|
|
|
|
return static_cast< bool >(mHandle->has_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the number of files in the opened directory (only when opened in sorted mode).
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD SQInteger FileCount() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("obtain file count");
|
|
|
|
// Return the requested information
|
|
|
|
return static_cast< SQInteger >(mHandle->n_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open current file from the specified directory.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD LightObj ReadFile() const;
|
2019-08-14 03:36:45 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open current file from the specified directory.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD LightObj ReadFileAt(SQInteger i) const;
|
2019-08-14 03:36:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* This class represents file-system files in a platform-independent manner.
|
|
|
|
*/
|
|
|
|
class SysFile
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2021-01-30 07:51:39 +01:00
|
|
|
TinyFile mHandle{}; /* Handle to the managed file. */
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Make sure a valid handle is being managed before attempting to use it.
|
|
|
|
*/
|
|
|
|
void Validate() const
|
|
|
|
{
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid file handle. Please open a file first.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Make sure a valid handle is being managed before attempting to use it.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Validate(const SQChar * action) const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("Cannot {}. Invalid file handle.", action);
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Defaults to a null handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysFile() = default;
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Opens the file at the specified path.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
explicit SysFile(StackStrF & path)
|
2019-08-14 03:36:45 +02:00
|
|
|
: SysFile()
|
|
|
|
{
|
|
|
|
Open(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor (disabled).
|
|
|
|
*/
|
|
|
|
SysFile(const SysFile &) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysFile(SysFile && o) noexcept = default;
|
2019-08-14 03:36:45 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator (disabled).
|
|
|
|
*/
|
|
|
|
SysFile & operator = (const SysFile &) = delete;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SysFile & operator = (SysFile && o) noexcept
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Avoid self assignment
|
|
|
|
if (this != &o)
|
|
|
|
{
|
|
|
|
// Take ownership of the new handle
|
|
|
|
mHandle = std::move(o.mHandle);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the raw managed handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_file * Get() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
return mHandle.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the raw managed handle and make one if it doesn't exist already.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_file * GetOrMake()
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Do we have a handle already?
|
|
|
|
if (!mHandle)
|
|
|
|
{
|
|
|
|
mHandle = std::make_unique< tinydir_file >(); // Make one
|
|
|
|
}
|
|
|
|
// Return it like we promised
|
|
|
|
return mHandle.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Take ownership of the managed handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD tinydir_file * Release()
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
return mHandle.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Release the managed handle.
|
|
|
|
*/
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
mHandle.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to convert an instance of this type to a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String ToString() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return mHandle ? String(mHandle->path, mHandle->path + std::wcslen(mHandle->path)) : _SC("");
|
|
|
|
#else
|
|
|
|
return mHandle ? String(mHandle->path) : String();
|
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Check for the presence of a handle.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD bool IsValid() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
2021-01-30 07:51:39 +01:00
|
|
|
return static_cast< bool >(mHandle);
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Open a handle to the file at the specified path.
|
|
|
|
*/
|
|
|
|
void Open(StackStrF & path)
|
|
|
|
{
|
|
|
|
// Get the string from the script
|
|
|
|
if ((SQ_FAILED(path.Proc(true))))
|
|
|
|
{
|
|
|
|
STHROWF("Unable to extract the specified path.");
|
|
|
|
}
|
|
|
|
// Allocate the handle memory
|
|
|
|
mHandle = std::make_unique< tinydir_file >();
|
|
|
|
// Discard current error number
|
|
|
|
errno = 0;
|
|
|
|
// Attempt to open the specified file
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
if (tinydir_file_open(mHandle.get(), std::wstring(path.mPtr, path.mPtr + path.GetSize()).c_str()) == -1)
|
|
|
|
#else
|
2019-08-14 03:36:45 +02:00
|
|
|
if (tinydir_file_open(mHandle.get(), path.mPtr) == -1)
|
2021-01-30 07:51:39 +01:00
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
// Don't keep a bad handle
|
|
|
|
mHandle.reset();
|
|
|
|
// Now we can throw the exception
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWF("Failed to open file: {} [{}]", path.mPtr, strerror(errno));
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-03 16:50:39 +01:00
|
|
|
STHROWLASTF("Failed to open file: {}", path.mPtr);
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Check if the opened element is a directory.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD bool IsDir() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("check type");
|
|
|
|
// Return the requested information
|
|
|
|
return static_cast< bool >(mHandle->is_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Check if the opened element is a regular file.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD bool IsReg() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("check type");
|
|
|
|
// Return the requested information
|
|
|
|
return static_cast< bool >(mHandle->is_reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the path of the opened file.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String GetPath() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("retrieve path");
|
|
|
|
// Return the requested information
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return String(mHandle->path, mHandle->path + std::wcslen(mHandle->path));
|
|
|
|
#else
|
|
|
|
return String(mHandle->path);
|
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the name of the opened file.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String GetName() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("retrieve name");
|
|
|
|
// Return the requested information
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return String(mHandle->name, mHandle->name + std::wcslen(mHandle->name));
|
|
|
|
#else
|
|
|
|
return String(mHandle->name);
|
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the extension of the opened file.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String GetExtension() const
|
2019-08-14 03:36:45 +02:00
|
|
|
{
|
|
|
|
Validate("retrieve extension");
|
|
|
|
// Return the requested information
|
2021-01-30 07:51:39 +01:00
|
|
|
#if defined(UNICODE) || defined(_UNICODE)
|
|
|
|
return String(mHandle->extension, mHandle->extension + std::wcslen(mHandle->extension));
|
|
|
|
#else
|
2022-01-28 14:23:48 +01:00
|
|
|
return String(mHandle->extension);
|
2021-01-30 07:51:39 +01:00
|
|
|
#endif
|
2019-08-14 03:36:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|