mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-09-16 17:27:18 +02:00
Rename source to module.
This commit is contained in:
126
module/Library/System/Dir.cpp
Normal file
126
module/Library/System/Dir.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/System/Dir.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(TypenameD, _SC("SqSysDir"))
|
||||
SQMODE_DECL_TYPENAME(TypenameF, _SC("SqSysFile"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LightObj SysDir::ReadFile() const
|
||||
{
|
||||
Validate("read current file");
|
||||
// Create a file instance
|
||||
std::unique_ptr< SysFile > mem = std::make_unique< SysFile >();
|
||||
// Turn it into a script object
|
||||
LightObj obj(mem.get());
|
||||
// Will hold the raw object pointer
|
||||
SysFile * ptr = nullptr;
|
||||
// Release it if it was taken over by the script engine
|
||||
if (obj.IsNull())
|
||||
{
|
||||
STHROWF("Failed to create a SqSysFile object.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = mem.release();
|
||||
}
|
||||
// The file handle where it will be opened
|
||||
tinydir_file * handle = ptr->GetOrMake();
|
||||
// Attempt to read the current file
|
||||
if (tinydir_readfile(mHandle.get(), handle) == -1)
|
||||
{
|
||||
STHROWF("Failed to read current file.");
|
||||
}
|
||||
// Return the resulted object
|
||||
return obj;
|
||||
}
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LightObj SysDir::ReadFileAt(SQInteger i) const
|
||||
{
|
||||
Validate("read scanned file");
|
||||
// Make sure the specified directory index is valid
|
||||
if (i < 0)
|
||||
{
|
||||
STHROWF("File index (" PRINT_INT_FMT " < 0) our of bounds.", i);
|
||||
}
|
||||
if (static_cast< size_t >(i) >= mHandle->n_files)
|
||||
{
|
||||
STHROWF("File index (" PRINT_INT_FMT " >= " PRINT_SZ_FMT ") our of bounds.", i, mHandle->n_files);
|
||||
}
|
||||
// Create a file instance
|
||||
std::unique_ptr< SysFile > mem = std::make_unique< SysFile >();
|
||||
// Turn it into a script object
|
||||
LightObj obj(mem.get());
|
||||
// Will hold the raw object pointer
|
||||
SysFile * ptr = nullptr;
|
||||
// Release it if it was taken over by the script engine
|
||||
if (obj.IsNull())
|
||||
{
|
||||
STHROWF("Failed to create a SqSysFile object.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = mem.release();
|
||||
}
|
||||
// The file handle where it will be opened
|
||||
tinydir_file * handle = ptr->GetOrMake();
|
||||
// Attempt to read the current file
|
||||
if (tinydir_readfile_n(mHandle.get(), handle, i) == -1)
|
||||
{
|
||||
STHROWF("Failed to read file at index (" PRINT_INT_FMT ")", i);
|
||||
}
|
||||
// Return the resulted object
|
||||
return obj;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_SysDir(HSQUIRRELVM vm)
|
||||
{
|
||||
RootTable(vm).Bind(TypenameD::Str,
|
||||
Class< SysDir, NoCopy< SysDir > >(vm, TypenameD::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< StackStrF & >()
|
||||
.Ctor< bool, StackStrF & >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &TypenameD::Fn)
|
||||
.Func(_SC("_tostring"), &SysDir::ToString)
|
||||
// Member Properties
|
||||
.Prop(_SC("IsValid"), &SysDir::IsValid)
|
||||
.Prop(_SC("Path"), &SysDir::GetPath)
|
||||
.Prop(_SC("HasNext"), &SysDir::HasNext)
|
||||
.Prop(_SC("FileCount"), &SysDir::FileCount)
|
||||
// Member Methods
|
||||
.FmtFunc(_SC("Open"), &SysDir::Open)
|
||||
.FmtFunc(_SC("OpenSorted"), &SysDir::OpenSorted)
|
||||
.Func(_SC("OpenSubDir"), &SysDir::OpenSubDir)
|
||||
.Func(_SC("Next"), &SysDir::Next)
|
||||
.Func(_SC("Close"), &SysDir::Close)
|
||||
.Func(_SC("ReadFile"), &SysDir::ReadFile)
|
||||
.Func(_SC("ReadFileAt"), &SysDir::ReadFileAt)
|
||||
);
|
||||
RootTable(vm).Bind(TypenameF::Str,
|
||||
Class< SysFile, NoCopy< SysFile > >(vm, TypenameF::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< StackStrF & >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &TypenameF::Fn)
|
||||
.Func(_SC("_tostring"), &SysFile::ToString)
|
||||
// Member Properties
|
||||
.Prop(_SC("IsValid"), &SysFile::IsValid)
|
||||
.Prop(_SC("IsDir"), &SysFile::IsDir)
|
||||
.Prop(_SC("IsReg"), &SysFile::IsReg)
|
||||
.Prop(_SC("Path"), &SysFile::GetPath)
|
||||
.Prop(_SC("Name"), &SysFile::GetName)
|
||||
.Prop(_SC("Extension"), &SysFile::GetExtension)
|
||||
// Member Methods
|
||||
.FmtFunc(_SC("Open"), &SysFile::Open)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
587
module/Library/System/Dir.hpp
Normal file
587
module/Library/System/Dir.hpp
Normal file
@@ -0,0 +1,587 @@
|
||||
#ifndef _LIBRARY_SYSDIR_HPP_
|
||||
#define _LIBRARY_SYSDIR_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#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
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
TinyDir mHandle; /* Handle to the managed directory. */
|
||||
|
||||
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.
|
||||
*/
|
||||
void Validate(CSStr action) const
|
||||
{
|
||||
if (!mHandle)
|
||||
{
|
||||
STHROWF("Cannot %s. Invalid directory handle.", action);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Defaults to a null handle.
|
||||
*/
|
||||
SysDir()
|
||||
: mHandle()
|
||||
{
|
||||
/*...*/
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Opens the directory at the specified path.
|
||||
*/
|
||||
SysDir(StackStrF & path)
|
||||
: 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.
|
||||
*/
|
||||
SysDir(SysDir && o)
|
||||
: mHandle(std::forward< TinyDir >(o.mHandle))
|
||||
{
|
||||
/*...*/
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
SysDir & operator = (SysDir && o)
|
||||
{
|
||||
// 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.
|
||||
*/
|
||||
tinydir_dir * Get() const
|
||||
{
|
||||
return mHandle.get();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw managed handle and make one if it doesn't exist already.
|
||||
*/
|
||||
tinydir_dir * GetOrMake()
|
||||
{
|
||||
// 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.
|
||||
*/
|
||||
tinydir_dir * Release()
|
||||
{
|
||||
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.
|
||||
*/
|
||||
CSStr ToString() const
|
||||
{
|
||||
return mHandle ? mHandle->path : _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check for the presence of a handle.
|
||||
*/
|
||||
bool IsValid() const
|
||||
{
|
||||
return !!mHandle;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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
|
||||
if (tinydir_open(mHandle.get(), path.mPtr) == -1)
|
||||
{
|
||||
// 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
|
||||
if (tinydir_open_sorted(mHandle.get(), path.mPtr) == -1)
|
||||
{
|
||||
// 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.
|
||||
*/
|
||||
CSStr GetPath() const
|
||||
{
|
||||
Validate("obtain path");
|
||||
// Return the requested information
|
||||
return mHandle->path;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See if there's a next element in the opened directory.
|
||||
*/
|
||||
bool HasNext() const
|
||||
{
|
||||
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).
|
||||
*/
|
||||
SQInteger FileCount() const
|
||||
{
|
||||
Validate("obtain file count");
|
||||
// Return the requested information
|
||||
return static_cast< SQInteger >(mHandle->n_files);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Open current file from the specified directory.
|
||||
*/
|
||||
LightObj ReadFile() const;
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Open current file from the specified directory.
|
||||
*/
|
||||
LightObj ReadFileAt(SQInteger i) const;
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* This class represents file-system files in a platform-independent manner.
|
||||
*/
|
||||
class SysFile
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
TinyFile mHandle; /* Handle to the managed file. */
|
||||
|
||||
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.
|
||||
*/
|
||||
void Validate(CSStr action) const
|
||||
{
|
||||
if (!mHandle)
|
||||
{
|
||||
STHROWF("Cannot %s. Invalid file handle.", action);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Defaults to a null handle.
|
||||
*/
|
||||
SysFile()
|
||||
: mHandle()
|
||||
{
|
||||
/*...*/
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Opens the file at the specified path.
|
||||
*/
|
||||
SysFile(StackStrF & path)
|
||||
: SysFile()
|
||||
{
|
||||
Open(path);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor (disabled).
|
||||
*/
|
||||
SysFile(const SysFile &) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
SysFile(SysFile && o)
|
||||
: mHandle(std::forward< TinyFile >(o.mHandle))
|
||||
{
|
||||
/*...*/
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator (disabled).
|
||||
*/
|
||||
SysFile & operator = (const SysFile &) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
SysFile & operator = (SysFile && o)
|
||||
{
|
||||
// Avoid self assignment
|
||||
if (this != &o)
|
||||
{
|
||||
// Take ownership of the new handle
|
||||
mHandle = std::move(o.mHandle);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw managed handle.
|
||||
*/
|
||||
tinydir_file * Get() const
|
||||
{
|
||||
return mHandle.get();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw managed handle and make one if it doesn't exist already.
|
||||
*/
|
||||
tinydir_file * GetOrMake()
|
||||
{
|
||||
// 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.
|
||||
*/
|
||||
tinydir_file * Release()
|
||||
{
|
||||
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.
|
||||
*/
|
||||
CSStr ToString() const
|
||||
{
|
||||
return mHandle ? mHandle->path : _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check for the presence of a handle.
|
||||
*/
|
||||
bool IsValid() const
|
||||
{
|
||||
return !!mHandle;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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
|
||||
if (tinydir_file_open(mHandle.get(), path.mPtr) == -1)
|
||||
{
|
||||
// Don't keep a bad handle
|
||||
mHandle.reset();
|
||||
// Now we can throw the exception
|
||||
if (errno != 0)
|
||||
{
|
||||
STHROWF("Failed to open file: %s [%s]", path.mPtr, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
STHROWLASTF("Failed to open file: %s", path.mPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if the opened element is a directory.
|
||||
*/
|
||||
bool IsDir() const
|
||||
{
|
||||
Validate("check type");
|
||||
// Return the requested information
|
||||
return static_cast< bool >(mHandle->is_dir);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if the opened element is a regular file.
|
||||
*/
|
||||
bool IsReg() const
|
||||
{
|
||||
Validate("check type");
|
||||
// Return the requested information
|
||||
return static_cast< bool >(mHandle->is_reg);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the path of the opened file.
|
||||
*/
|
||||
CSStr GetPath() const
|
||||
{
|
||||
Validate("retrieve path");
|
||||
// Return the requested information
|
||||
return mHandle->path;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the name of the opened file.
|
||||
*/
|
||||
CSStr GetName() const
|
||||
{
|
||||
Validate("retrieve name");
|
||||
// Return the requested information
|
||||
return mHandle->name;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the extension of the opened file.
|
||||
*/
|
||||
CSStr GetExtension() const
|
||||
{
|
||||
Validate("retrieve extension");
|
||||
// Return the requested information
|
||||
return mHandle->extension != nullptr ? mHandle->extension : _SC("");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_SYSDIR_HPP_
|
1208
module/Library/System/Environment.cpp
Normal file
1208
module/Library/System/Environment.cpp
Normal file
File diff suppressed because it is too large
Load Diff
316
module/Library/System/Environment.hpp
Normal file
316
module/Library/System/Environment.hpp
Normal file
@@ -0,0 +1,316 @@
|
||||
#ifndef _LIBRARY_SYSENV_HPP_
|
||||
#define _LIBRARY_SYSENV_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Base/Shared.hpp"
|
||||
#include "Base/Buffer.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* This class provides access to environment variables and some general system information.
|
||||
*/
|
||||
struct SysEnv
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (disabled)
|
||||
*/
|
||||
SysEnv() = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
SysEnv(const SysEnv &) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
SysEnv(SysEnv &&) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor. (disabled)
|
||||
*/
|
||||
~SysEnv() = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
SysEnv & operator = (const SysEnv &) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
SysEnv & operator = (SysEnv &&) = delete;
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns true if an environment variable with the given name is defined.
|
||||
*/
|
||||
static bool Has(CCStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns true if an environment variable with the given name is defined.
|
||||
*/
|
||||
static bool Has(const String & name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
* If the environment variable is undefined, returns fallback value instead.
|
||||
*/
|
||||
static void Get(Buffer & b, CCStr name, CCStr fallback);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
*/
|
||||
static Buffer Get(CCStr name)
|
||||
{
|
||||
return Get(name, nullptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
*/
|
||||
static Buffer Get(const String & name)
|
||||
{
|
||||
return Get(name.c_str(), nullptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
* If the environment variable is undefined, returns fallback value instead.
|
||||
*/
|
||||
static Buffer Get(CCStr name, CCStr fallback);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
* If the environment variable is undefined, returns fallback value instead.
|
||||
*/
|
||||
static Buffer Get(CCStr name, const String & fallback)
|
||||
{
|
||||
return Get(name, fallback.c_str());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
* If the environment variable is undefined, returns fallback value instead.
|
||||
*/
|
||||
static Buffer Get(const String & name, CCStr fallback)
|
||||
{
|
||||
return Get(name.c_str(), fallback);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the value of the environment variable with the given name.
|
||||
* If the environment variable is undefined, returns fallback value instead.
|
||||
*/
|
||||
static Buffer Get(const String & name, const String & fallback)
|
||||
{
|
||||
return Get(name.c_str(), fallback.c_str());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the environment variable with the given name to the given value.
|
||||
*/
|
||||
static bool Set(CCStr name, CCStr value);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the environment variable with the given name to the given value.
|
||||
*/
|
||||
static bool Set(const String & name, const String & value);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the operating system name.
|
||||
*/
|
||||
static String OSName();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the operating system name in a more "user-friendly" way. This only affects Windows.
|
||||
*/
|
||||
static String OSDisplayName();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the operating system version.
|
||||
*/
|
||||
static String OSVersion();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the operating system architecture.
|
||||
*/
|
||||
static String OSArchitecture();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the node (or host) name.
|
||||
*/
|
||||
static String NodeName();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the number of processors installed in the system. If the number of processors
|
||||
* cannot be determined, returns 1.
|
||||
*/
|
||||
static Uint32 ProcessorCount();
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure that the path in the specified buffer contains a trailing slash.
|
||||
*/
|
||||
static void TerminatePath(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the string.
|
||||
*/
|
||||
static void ExpandVars(Buffer & b, CCStr pos, CCStr end);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. Uses the Unix variable style.
|
||||
*/
|
||||
static void ExpandPath(Buffer & b, CCStr pos, CCStr end);
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the string.
|
||||
*/
|
||||
static void ExpandVars(Buffer & b, CCStr str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the string.
|
||||
*/
|
||||
static void ExpandVars(Buffer & b, const String & str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the string.
|
||||
*/
|
||||
static Buffer ExpandVars(CCStr str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the string.
|
||||
*/
|
||||
static Buffer ExpandVars(const String & str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. Uses the Unix variable style.
|
||||
*/
|
||||
static void ExpandPath(Buffer & b, CCStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. Uses the Unix variable style.
|
||||
*/
|
||||
static void ExpandPath(Buffer & b, const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. Uses the Unix variable style.
|
||||
*/
|
||||
static Buffer ExpandPath(CCStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. Uses the Unix variable style.
|
||||
*/
|
||||
static Buffer ExpandPath(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the current working directory within the specified buffer.
|
||||
*/
|
||||
static void WorkingDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the current working directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer WorkingDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's home directory within the specified buffer.
|
||||
*/
|
||||
static void HomeDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's home directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer HomeDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's config directory within the specified buffer.
|
||||
*/
|
||||
static void ConfigHomeDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's config directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer ConfigHomeDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's data directory within the specified buffer.
|
||||
*/
|
||||
static void DataHomeDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's data directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer DataHomeDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's temporary directory within the specified buffer.
|
||||
*/
|
||||
static void TempHomeDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's temporary directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer TempHomeDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's cache directory within the specified buffer.
|
||||
*/
|
||||
static void CacheHomeDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the user's cache directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer CacheHomeDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the temporary directory within the specified buffer.
|
||||
*/
|
||||
static void TempDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the temporary directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer TempDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the systemwide config directory within the specified buffer.
|
||||
*/
|
||||
static void ConfigDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the systemwide config directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer ConfigDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the system directory within the specified buffer.
|
||||
*/
|
||||
static void SystemDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the system directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer SystemDir();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the null directory within the specified buffer.
|
||||
*/
|
||||
static void NullDir(Buffer & b);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Obtain the null directory within a buffer and return it.
|
||||
*/
|
||||
static Buffer NullDir();
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_SYSENV_HPP_
|
1840
module/Library/System/Path.cpp
Normal file
1840
module/Library/System/Path.cpp
Normal file
File diff suppressed because it is too large
Load Diff
763
module/Library/System/Path.hpp
Normal file
763
module/Library/System/Path.hpp
Normal file
@@ -0,0 +1,763 @@
|
||||
#ifndef _LIBRARY_SYSPATH_HPP_
|
||||
#define _LIBRARY_SYSPATH_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <vector>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the full path of file.
|
||||
*/
|
||||
Buffer GetRealFilePath(CSStr path);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* This class represents filesystem paths in a platform-independent manner.
|
||||
*/
|
||||
class SysPath
|
||||
{
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef std::vector< String > StrVec; // Directory list.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Styles of directories to expect when parsing or to export.
|
||||
*/
|
||||
enum struct Style
|
||||
{
|
||||
Unix = 0,
|
||||
Windows,
|
||||
Native,
|
||||
Guess,
|
||||
Dynamic
|
||||
};
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates an empty relative path.
|
||||
*/
|
||||
SysPath();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates an empty absolute or relative path.
|
||||
*/
|
||||
SysPath(bool absolute);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in native format from a string.
|
||||
*/
|
||||
SysPath(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a string.
|
||||
*/
|
||||
SysPath(CSStr path, Int32 style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a string.
|
||||
*/
|
||||
SysPath(CSStr path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in native format from a string.
|
||||
*/
|
||||
SysPath(const Buffer & path, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a string.
|
||||
*/
|
||||
SysPath(const Buffer & path, Style style, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in native format from a string.
|
||||
*/
|
||||
SysPath(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a string.
|
||||
*/
|
||||
SysPath(const String & path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a file name. The parent path is expected to reference
|
||||
* a directory.
|
||||
*/
|
||||
SysPath(const SysPath & parent, CSStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a file name. The parent path is expected to reference
|
||||
* a directory.
|
||||
*/
|
||||
SysPath(const SysPath & parent, const String & name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a relative path. The parent path is expected
|
||||
* to reference a directory. The relative path is appended to the parent path.
|
||||
*/
|
||||
SysPath(const SysPath & parent, const SysPath & relative);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
SysPath(const SysPath & o);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
SysPath(SysPath && o);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~SysPath();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
SysPath & operator = (const SysPath & o);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
SysPath & operator = (SysPath && o);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path in native format.
|
||||
*/
|
||||
SysPath & operator = (CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path in native format.
|
||||
*/
|
||||
SysPath & operator = (const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Equality comparison.
|
||||
*/
|
||||
bool operator == (const SysPath & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Inequality comparison.
|
||||
*/
|
||||
bool operator != (const SysPath & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to boolean operator.
|
||||
*/
|
||||
operator bool () const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the n'th directory in the directory list. If n == depth(), returns the file name.
|
||||
*/
|
||||
const String & operator [] (Uint32 n) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to compare two instances of this type.
|
||||
*/
|
||||
Int32 Cmp(const SysPath & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
Object ToString() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Swaps the path with another one.
|
||||
*/
|
||||
void Swap(SysPath & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Clears all components.
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path in native format.
|
||||
*/
|
||||
SysPath & Assign(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path.
|
||||
*/
|
||||
SysPath & Assign(CSStr path, Int32 style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path.
|
||||
*/
|
||||
SysPath & Assign(CSStr path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path in native format.
|
||||
*/
|
||||
SysPath & Assign(const Buffer & path, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path.
|
||||
*/
|
||||
SysPath & Assign(const Buffer & path, Style style, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path in native format.
|
||||
*/
|
||||
SysPath & Assign(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path.
|
||||
*/
|
||||
SysPath & Assign(const String & path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a file name. The parent path is expected to reference
|
||||
* a directory.
|
||||
*/
|
||||
SysPath & Assign(const SysPath & parent, CSStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a file name. The parent path is expected to reference
|
||||
* a directory.
|
||||
*/
|
||||
SysPath & Assign(const SysPath & parent, const String & name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a relative path. The parent path is expected
|
||||
* to reference a directory. The relative path is appended to the parent path.
|
||||
*/
|
||||
SysPath & Assign(const SysPath & parent, const SysPath & relative);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy the components from another path.
|
||||
*/
|
||||
SysPath & Assign(const SysPath & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move the components of another path into this instance.
|
||||
*/
|
||||
SysPath & Assign(SysPath && path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(CSStr path, Int32 style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(CSStr path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(const Buffer & path, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(const Buffer & path, Style style, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The resulting path always refers to a directory and the filename part is empty.
|
||||
*/
|
||||
SysPath & AssignDir(const String & path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns a string containing the path in native format.
|
||||
*/
|
||||
Buffer ToBuffer() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns a string containing the path in the given format.
|
||||
*/
|
||||
Buffer ToBuffer(Style style) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns a string containing the path in the given format.
|
||||
*/
|
||||
Object ToStr(Int32 style) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assigns a string containing a path.
|
||||
*/
|
||||
void FromString(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether the path is absolute.
|
||||
*/
|
||||
bool IsAbsolute() const
|
||||
{
|
||||
return m_Absolute;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether the path is relative.
|
||||
*/
|
||||
bool IsRelative() const
|
||||
{
|
||||
return !m_Absolute;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether the path references a directory.
|
||||
*/
|
||||
bool IsDirectory() const
|
||||
{
|
||||
return m_Name.empty();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether the path references a file.
|
||||
*/
|
||||
bool IsFile() const
|
||||
{
|
||||
return !m_Name.empty();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether the path Does not contain a drive, directories or file name.
|
||||
*/
|
||||
bool Empty() const
|
||||
{
|
||||
return (m_Dirs.empty() && m_Name.empty() && m_Drive == 0);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* If the path contains a file name, the file name is appended to the directory list and cleared.
|
||||
*/
|
||||
SysPath & MakeDirectory();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* If the path contains no file name, the last directory becomes the file name.
|
||||
*/
|
||||
SysPath & MakeFile();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes the path refer to its parent.
|
||||
*/
|
||||
SysPath & MakeParent();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes the path absolute if it is relative. The current working directory is taken
|
||||
* as base directory.
|
||||
*/
|
||||
SysPath & MakeAbsolute();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes the path absolute if it is relative. The given path is taken as base.
|
||||
*/
|
||||
SysPath & MakeAbsolute(const SysPath & base);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes the path absolute if it is relative. The given path is taken as base.
|
||||
*/
|
||||
SysPath & MakeAbsolute(SysPath && base);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Appends the given path.
|
||||
*/
|
||||
SysPath & Append(const SysPath & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Appends the given path.
|
||||
*/
|
||||
SysPath & Append(SysPath && path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(CSStr path, Int32 style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(CSStr path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(const Buffer & path, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(const Buffer & path, Style style, Int32 size = -1);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse the given string and append the resulted path.
|
||||
*/
|
||||
SysPath & Append(const String & path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the drive letter.
|
||||
*/
|
||||
CharT GetDrive() const
|
||||
{
|
||||
return m_Drive;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modifies the drive letter.
|
||||
*/
|
||||
void SetDrive(CharT drive)
|
||||
{
|
||||
m_Drive = drive;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the number of directories in the directory list.
|
||||
*/
|
||||
Uint32 Depth() const
|
||||
{
|
||||
return m_Dirs.size();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the n'th directory in the directory list. If n == depth(), returns the file name.
|
||||
*/
|
||||
const String & Directory(Uint32 n) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Adds a directory to the directory list.
|
||||
*/
|
||||
SysPath & Push(CSStr dir);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Adds a directory to the directory list.
|
||||
*/
|
||||
SysPath & Push(const String & dir);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Adds a directory to the directory list.
|
||||
*/
|
||||
SysPath & Push(String && dir);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Removes the last directory from the directory list.
|
||||
*/
|
||||
SysPath & PopBack();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Removes the first directory from the directory list.
|
||||
*/
|
||||
SysPath & PopFront();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Set the specified file name.
|
||||
*/
|
||||
SysPath & SetFilename(CSStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Set the specified file name.
|
||||
*/
|
||||
SysPath & SetFilename(const String & name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Set the specified file name.
|
||||
*/
|
||||
SysPath & SetFilename(String && name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieves the file name.
|
||||
*/
|
||||
const String & GetFilename() const
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Set the specified file name.
|
||||
*/
|
||||
void SqSetFilename(CSStr name)
|
||||
{
|
||||
SetFilename(name);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the basename part of the file name and does not change the extension.
|
||||
*/
|
||||
SysPath & SetBasename(CSStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the basename part of the file name and does not change the extension.
|
||||
*/
|
||||
SysPath & SetBasename(const String & name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the basename part of the file name and does not change the extension.
|
||||
*/
|
||||
SysPath & SetBasename(String && name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the basename (the file name without extension) of the path.
|
||||
*/
|
||||
String GetBasename() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the basename part of the file name and does not change the extension.
|
||||
*/
|
||||
void SqSetBasename(CSStr name)
|
||||
{
|
||||
SetBasename(name);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the file name extension.
|
||||
*/
|
||||
SysPath & SetExtension(CSStr ext);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the file name extension.
|
||||
*/
|
||||
SysPath & SetExtension(const String & ext);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the file name extension.
|
||||
*/
|
||||
String GetExtension() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sets the file name extension.
|
||||
*/
|
||||
void SqSetExtension(CSStr ext)
|
||||
{
|
||||
SetExtension(ext);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns a pointer to the internal name string where the extension starts.
|
||||
*/
|
||||
CSStr GetExtensionC() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns a path referring to the path's directory.
|
||||
*/
|
||||
SysPath Parent() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Resolves the given path against the current one. If the given path is absolute, it replaces
|
||||
* the current one. Otherwise, the relative path is appended to the current path.
|
||||
*/
|
||||
SysPath & Resolve(const SysPath & path);
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse a path using the unix standards.
|
||||
*/
|
||||
void ParseUnix(CSStr pos, CSStr end);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse a path using the windows standards.
|
||||
*/
|
||||
void ParseWindows(CSStr pos, CSStr end);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse a path and expect combined windows and unix styles.
|
||||
*/
|
||||
void ParseDynamic(CSStr pos, CSStr end);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Parse a path and try to detect it's type automatically.
|
||||
*/
|
||||
void ParseGuess(CSStr pos, CSStr end);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Build a path string using the Unix conventions.
|
||||
*/
|
||||
Buffer BuildUnix() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Build a path string using the Windows conventions.
|
||||
*/
|
||||
Buffer BuildWindows() const;
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
StrVec m_Dirs; /* The list of directories that form the path. */
|
||||
String m_Name; /* The file name if one was specified. */
|
||||
CharT m_Drive; /* The drive letter if one was specified. */
|
||||
bool m_Absolute; /* Whether this path is an absolute path. */
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the platform's path name separator, which separates the components (names) in a path.
|
||||
*/
|
||||
static CharT Separator()
|
||||
{
|
||||
#ifdef GMOD_OS_WINDOWS
|
||||
return '\\';
|
||||
#else
|
||||
return '/';
|
||||
#endif // GMOD_OS_WINDOWS
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the platform's path separator, which separates single paths in a list of paths.
|
||||
*/
|
||||
static CharT PathSeparator()
|
||||
{
|
||||
#ifdef GMOD_OS_WINDOWS
|
||||
return ';';
|
||||
#else
|
||||
return ':';
|
||||
#endif // GMOD_OS_WINDOWS
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path referring to a directory.
|
||||
*/
|
||||
static SysPath ForDirectory(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path referring to a directory.
|
||||
*/
|
||||
static SysPath ForDirectory(CSStr path, Int32 style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path referring to a directory.
|
||||
*/
|
||||
static SysPath ForDirectory(CSStr path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path referring to a directory.
|
||||
*/
|
||||
static SysPath ForDirectory(const String & path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path referring to a directory.
|
||||
*/
|
||||
static SysPath ForDirectory(const String & path, Style style);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path. On Unix, a tilde as first character
|
||||
* in the path is replaced with the path to user's home directory.
|
||||
*/
|
||||
static SysPath Expand(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Expands all environment variables contained in the path.
|
||||
*/
|
||||
static SysPath Expand(const String & path)
|
||||
{
|
||||
return Expand(path.c_str());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the user's home directory.
|
||||
*/
|
||||
static SysPath Home();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the user's config directory.
|
||||
*/
|
||||
static SysPath ConfigHome();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the user's data directory.
|
||||
*/
|
||||
static SysPath DataHome();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the user's temp directory.
|
||||
*/
|
||||
static SysPath TempHome();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the user's temp directory.
|
||||
*/
|
||||
static SysPath CacheHome();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the current working directory.
|
||||
*/
|
||||
static SysPath Working();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the temporary directory.
|
||||
*/
|
||||
static SysPath Temp();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the systemwide config directory.
|
||||
*/
|
||||
static SysPath Config();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the system directory.
|
||||
*/
|
||||
static SysPath System();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the name of the null device.
|
||||
*/
|
||||
static SysPath Null();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Returns the real path to the specified file or directory.
|
||||
*/
|
||||
static SysPath Real(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path from a parent path and a file name. The parent path is expected to reference
|
||||
* a directory.
|
||||
*/
|
||||
static SysPath With(const SysPath & parent, CSStr name);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in unix format from a string.
|
||||
*/
|
||||
static SysPath MakeUnix(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in windows format from a string.
|
||||
*/
|
||||
static SysPath MakeWindows(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in native format from a string.
|
||||
*/
|
||||
static SysPath MakeNative(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in and guess the format from a string.
|
||||
*/
|
||||
static SysPath MakeGuess(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Creates a path in dynamic format from a string.
|
||||
*/
|
||||
static SysPath MakeDynamic(CSStr path);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes sure all separators from a path are the same.
|
||||
*/
|
||||
static String NormalizePath(SQInteger s, StackStrF & val);
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_SYSPATH_HPP_
|
Reference in New Issue
Block a user