mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 08:47:17 +01:00
Updated the buffer class to include an optional numeric value to be used as an edit cursor or to mark the used buffer size.
Basic implementation of the system path class and several fuctions to retrieve information about the running system.
This commit is contained in:
parent
58891f1e8b
commit
4cac7d2d30
@ -459,10 +459,10 @@
|
||||
<Unit filename="../source/Library/Shared.hpp" />
|
||||
<Unit filename="../source/Library/String.cpp" />
|
||||
<Unit filename="../source/Library/String.hpp" />
|
||||
<Unit filename="../source/Library/SysEnv.cpp" />
|
||||
<Unit filename="../source/Library/SysEnv.hpp" />
|
||||
<Unit filename="../source/Library/SysPath.cpp" />
|
||||
<Unit filename="../source/Library/SysPath.hpp" />
|
||||
<Unit filename="../source/Library/System.cpp" />
|
||||
<Unit filename="../source/Library/System.hpp" />
|
||||
<Unit filename="../source/Library/Time.cpp" />
|
||||
<Unit filename="../source/Library/Time.hpp" />
|
||||
<Unit filename="../source/Library/Utils.cpp" />
|
||||
|
@ -1,5 +1,5 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Buffer.hpp"
|
||||
#include "Base/Buffer.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstdlib>
|
||||
@ -40,7 +40,9 @@ void ThrowMemExcept(const char * msg, ...)
|
||||
int ret = vsnprintf(buffer, sizeof(buffer), msg, args);
|
||||
// Check for formatting errors
|
||||
if (ret < 0)
|
||||
{
|
||||
throw std::runtime_error("Unknown memory error");
|
||||
}
|
||||
// Throw the actual exception
|
||||
throw std::runtime_error(buffer);
|
||||
}
|
||||
@ -54,7 +56,9 @@ static Buffer::Pointer AllocMem(Buffer::SzType size)
|
||||
Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(malloc(size));
|
||||
// Validate the allocated memory
|
||||
if (!ptr)
|
||||
{
|
||||
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
|
||||
}
|
||||
// Return the allocated memory
|
||||
return ptr;
|
||||
}
|
||||
@ -100,7 +104,9 @@ private:
|
||||
* Base constructor.
|
||||
*/
|
||||
Node(Node * next)
|
||||
: mCap(0), mPtr(nullptr), mNext(next)
|
||||
: mCap(0)
|
||||
, mPtr(nullptr)
|
||||
, mNext(next)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
@ -130,7 +136,9 @@ private:
|
||||
{
|
||||
// Free the memory (if any)
|
||||
if (node->mPtr)
|
||||
{
|
||||
free(node->mPtr);
|
||||
}
|
||||
// Save the next node
|
||||
next = node->mNext;
|
||||
// Release the node instance
|
||||
@ -149,7 +157,9 @@ private:
|
||||
{
|
||||
// Free the memory (if any)
|
||||
if (node->mPtr)
|
||||
{
|
||||
free(node->mPtr);
|
||||
}
|
||||
// Save the next node
|
||||
next = node->mNext;
|
||||
// Release the node instance
|
||||
@ -173,10 +183,14 @@ private:
|
||||
{
|
||||
// Was there a previous node?
|
||||
if (prev)
|
||||
{
|
||||
prev->mNext = node->mNext;
|
||||
}
|
||||
// Probably this was the head
|
||||
else
|
||||
{
|
||||
m_Head = node->mNext;
|
||||
}
|
||||
// Assign the memory
|
||||
ptr = node->mPtr;
|
||||
// Assign the size
|
||||
@ -208,7 +222,9 @@ private:
|
||||
void Drop(Pointer & ptr, SzType & size)
|
||||
{
|
||||
if (!ptr)
|
||||
{
|
||||
ThrowMemExcept("Cannot store invalid memory buffer");
|
||||
}
|
||||
// Request a node instance
|
||||
Node * node = Pull();
|
||||
// Assign the specified memory
|
||||
@ -232,7 +248,9 @@ private:
|
||||
s_Nodes = new Node(s_Nodes);
|
||||
// Validate the head node
|
||||
if (!s_Nodes)
|
||||
{
|
||||
ThrowMemExcept("Unable to allocate memory nodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,7 +261,9 @@ private:
|
||||
{
|
||||
// Are there any nodes available?
|
||||
if (!s_Nodes)
|
||||
{
|
||||
Make(); // Make some!
|
||||
}
|
||||
// Grab the head node
|
||||
Node * node = s_Nodes;
|
||||
// Promote the next node as the head
|
||||
@ -259,7 +279,9 @@ private:
|
||||
{
|
||||
// See if the node is even valid
|
||||
if (!node)
|
||||
{
|
||||
ThrowMemExcept("Attempting to push invalid node");
|
||||
}
|
||||
// Demote the current head node
|
||||
node->mNext = s_Nodes;
|
||||
// Promote as the head node
|
||||
@ -324,7 +346,9 @@ MemRef MemRef::s_Mem;
|
||||
void MemRef::Grab()
|
||||
{
|
||||
if (m_Ptr)
|
||||
{
|
||||
++(*m_Ref);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -354,7 +378,8 @@ const MemRef & MemRef::Get()
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Buffer::Buffer(const Buffer & o)
|
||||
: m_Ptr(nullptr)
|
||||
, m_Cap(0)
|
||||
, m_Cap(o.m_Cap)
|
||||
, m_Cur(o.m_Cur)
|
||||
, m_Mem(o.m_Mem)
|
||||
{
|
||||
if (m_Cap)
|
||||
@ -369,7 +394,9 @@ Buffer::~Buffer()
|
||||
{
|
||||
// Do we have a buffer?
|
||||
if (m_Ptr)
|
||||
{
|
||||
Release(); // Release it!
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -379,30 +406,51 @@ Buffer & Buffer::operator = (const Buffer & o)
|
||||
{
|
||||
// Can we work in the current buffer?
|
||||
if (m_Cap && o.m_Cap <= m_Cap)
|
||||
{
|
||||
// It's safe to copy the data
|
||||
memcpy(m_Ptr, o.m_Ptr, m_Cap);
|
||||
memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
|
||||
}
|
||||
// Do we even have data to copy?
|
||||
else if (!o.m_Cap)
|
||||
{
|
||||
// Do we have a buffer?
|
||||
if (m_Ptr)
|
||||
{
|
||||
Release(); // Release it!
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do we have a buffer?
|
||||
if (m_Ptr)
|
||||
{
|
||||
Release(); // Release it!
|
||||
}
|
||||
// Request a larger buffer
|
||||
Request(o.m_Cap);
|
||||
// Now it's safe to copy the data
|
||||
memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
|
||||
}
|
||||
// Also copy the edit cursor
|
||||
m_Cur = o.m_Cur;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Buffer::Grow(SzType n)
|
||||
{
|
||||
// Backup the current memory
|
||||
Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
|
||||
// Acquire a bigger buffer
|
||||
Request(bkp.m_Cap + n);
|
||||
// Copy the data from the old buffer
|
||||
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
|
||||
// Copy the previous edit cursor
|
||||
m_Cur = bkp.m_Cur;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Buffer::Request(SzType n)
|
||||
{
|
||||
@ -417,11 +465,17 @@ void Buffer::Request(SzType n)
|
||||
}
|
||||
// Find out in which category does this buffer reside
|
||||
else if (n <= 1024)
|
||||
{
|
||||
m_Mem->m_Small.Grab(m_Ptr, n);
|
||||
}
|
||||
else if (n <= 4096)
|
||||
{
|
||||
m_Mem->m_Medium.Grab(m_Ptr, n);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Mem->m_Large.Grab(m_Ptr, n);
|
||||
}
|
||||
// If no errors occurred then we can set the size
|
||||
m_Cap = n;
|
||||
}
|
||||
@ -432,32 +486,41 @@ void Buffer::Release()
|
||||
// TODO: Implement a limit on how much memory can actually be pooled.
|
||||
// Is there a memory manager available?
|
||||
if (!m_Mem)
|
||||
{
|
||||
free(m_Ptr); // Deallocate the memory directly
|
||||
}
|
||||
// Find out to which category does this buffer belong
|
||||
else if (m_Cap <= 1024)
|
||||
{
|
||||
m_Mem->m_Small.Drop(m_Ptr, m_Cap);
|
||||
}
|
||||
else if (m_Cap <= 4096)
|
||||
{
|
||||
m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Mem->m_Large.Drop(m_Ptr, m_Cap);
|
||||
}
|
||||
// Explicitly reset the buffer
|
||||
m_Ptr = nullptr;
|
||||
m_Cap = 0;
|
||||
m_Cur = 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
|
||||
{
|
||||
// Make sure the position is not out of bounds
|
||||
if (pos > m_Cap || !data || !size)
|
||||
// Do we have what to write?
|
||||
if (!data || !size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// See if the buffer size must be adjusted
|
||||
else if ((pos + size) >= m_Cap)
|
||||
{
|
||||
// Allocate a larger memory chunk and backup old data
|
||||
Buffer bkp(Adjust< Value >(NextPow2(pos + size)));
|
||||
// Copy data back from the old buffer
|
||||
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
|
||||
// Acquire a larger buffer
|
||||
Grow((pos + size) - m_Cap + 32);
|
||||
}
|
||||
// Copy the data into the internal buffer
|
||||
memcpy(m_Ptr + pos, data, size);
|
||||
@ -472,7 +535,7 @@ Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
// Call the function that takes the variable argument list
|
||||
SzType ret = WriteF(pos, fmt, args);
|
||||
const SzType ret = WriteF(pos, fmt, args);
|
||||
// Finalize the variable argument list
|
||||
va_end(args);
|
||||
// Return the result
|
||||
@ -482,9 +545,12 @@ Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
|
||||
{
|
||||
// Make sure the position is not out of bounds
|
||||
if (pos > m_Cap)
|
||||
return 0;
|
||||
// Is the specified position within range?
|
||||
if (pos >= m_Cap)
|
||||
{
|
||||
// Acquire a larger buffer
|
||||
Grow(pos - m_Cap + 32);
|
||||
}
|
||||
// Backup the variable argument list
|
||||
va_list args_cpy;
|
||||
va_copy(args_cpy, args);
|
||||
@ -494,18 +560,53 @@ Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
|
||||
// Do we need a bigger buffer?
|
||||
if ((pos + ret) >= m_Cap)
|
||||
{
|
||||
// Allocate a larger memory chunk and backup old data
|
||||
Buffer bkp(Adjust< Value >(NextPow2(pos + ret)));
|
||||
// Copy data back from the old buffer
|
||||
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
|
||||
// Acquire a larger buffer
|
||||
Grow((pos + ret) - m_Cap + 32);
|
||||
// Retry writing the requested information
|
||||
ret = vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
|
||||
}
|
||||
// Return the value 0 if data could not be written
|
||||
if (ret < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// Return the number of written characters
|
||||
return static_cast< SzType >(ret);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
|
||||
{
|
||||
// Is there any string to write?
|
||||
if (str && *str != '\0')
|
||||
{
|
||||
// Forward this to the regular write function
|
||||
return Write(pos, str, strlen(str));
|
||||
}
|
||||
// Nothing to write
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Buffer::AppendF(const char * fmt, ...)
|
||||
{
|
||||
// Initialize the variable argument list
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
// Forward this to the regular write function
|
||||
m_Cur += WriteF(m_Cur, fmt, args);
|
||||
// Finalize the variable argument list
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Buffer::AppendS(const char * str)
|
||||
{
|
||||
// Is there any string to write?
|
||||
if (str)
|
||||
{
|
||||
m_Cur += Write(m_Cur, str, strlen(str));
|
||||
}
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -52,7 +52,8 @@ public:
|
||||
* Default constructor (null).
|
||||
*/
|
||||
MemRef()
|
||||
: m_Ptr(s_Mem.m_Ptr), m_Ref(s_Mem.m_Ref)
|
||||
: m_Ptr(s_Mem.m_Ptr)
|
||||
, m_Ref(s_Mem.m_Ref)
|
||||
{
|
||||
Grab();
|
||||
}
|
||||
@ -61,7 +62,8 @@ public:
|
||||
* Copy constructor.
|
||||
*/
|
||||
MemRef(const MemRef & o)
|
||||
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
|
||||
: m_Ptr(o.m_Ptr)
|
||||
, m_Ref(o.m_Ref)
|
||||
|
||||
{
|
||||
Grab();
|
||||
@ -189,13 +191,15 @@ private:
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and take ownership of the specified buffer.
|
||||
*/
|
||||
Buffer(Pointer & ptr, SzType & cap, const MemRef & mem)
|
||||
Buffer(Pointer & ptr, SzType & cap, SzType & cur, const MemRef & mem)
|
||||
: m_Ptr(ptr)
|
||||
, m_Cap(cap)
|
||||
, m_Cur(cur)
|
||||
, m_Mem(mem)
|
||||
{
|
||||
ptr = nullptr;
|
||||
cap = 0;
|
||||
cur = 0;
|
||||
}
|
||||
|
||||
public:
|
||||
@ -206,6 +210,7 @@ public:
|
||||
Buffer()
|
||||
: m_Ptr(nullptr)
|
||||
, m_Cap(0)
|
||||
, m_Cur(0)
|
||||
, m_Mem(MemRef::Get())
|
||||
{
|
||||
/* ... */
|
||||
@ -217,6 +222,7 @@ public:
|
||||
Buffer(SzType n)
|
||||
: m_Ptr(nullptr)
|
||||
, m_Cap(0)
|
||||
, m_Cur(0)
|
||||
, m_Mem(MemRef::Get())
|
||||
{
|
||||
Request(n < 8 ? 8 : n);
|
||||
@ -231,7 +237,10 @@ public:
|
||||
* Move constructor.
|
||||
*/
|
||||
Buffer(Buffer && o)
|
||||
: m_Ptr(o.m_Ptr), m_Cap(o.m_Cap), m_Mem(o.m_Mem)
|
||||
: m_Ptr(o.m_Ptr)
|
||||
, m_Cap(o.m_Cap)
|
||||
, m_Cur(o.m_Cur)
|
||||
, m_Mem(o.m_Mem)
|
||||
{
|
||||
o.m_Ptr = nullptr;
|
||||
}
|
||||
@ -254,9 +263,12 @@ public:
|
||||
if (m_Ptr != o.m_Ptr)
|
||||
{
|
||||
if (m_Ptr)
|
||||
{
|
||||
Release();
|
||||
}
|
||||
m_Ptr = o.m_Ptr;
|
||||
m_Cap = o.m_Cap;
|
||||
m_Cur = o.m_Cur;
|
||||
m_Mem = o.m_Mem;
|
||||
o.m_Ptr = nullptr;
|
||||
}
|
||||
@ -319,72 +331,6 @@ public:
|
||||
return m_Ptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> T * Get()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> const T * Get() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the a certain element.
|
||||
*/
|
||||
template < typename T = Value> T & At(SzType n)
|
||||
{
|
||||
assert(n < m_Cap);
|
||||
return reinterpret_cast< T * >(m_Ptr)[n];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the a certain element.
|
||||
*/
|
||||
template < typename T = Value> const T & At(SzType n) const
|
||||
{
|
||||
assert(n < m_Cap);
|
||||
return reinterpret_cast< const T * >(m_Ptr)[n];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> T * Begin()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> const T * Begin() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> T * End()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value> const T * End() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer.
|
||||
*/
|
||||
@ -401,20 +347,273 @@ public:
|
||||
return m_Ptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > T * Get()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > const T * Get() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the a certain element.
|
||||
*/
|
||||
template < typename T = Value > T & At(SzType n)
|
||||
{
|
||||
assert(n < m_Cap);
|
||||
return reinterpret_cast< T * >(m_Ptr)[n];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the a certain element.
|
||||
*/
|
||||
template < typename T = Value > const T & At(SzType n) const
|
||||
{
|
||||
assert(n < m_Cap);
|
||||
return reinterpret_cast< const T * >(m_Ptr)[n];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > T * Begin()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > const T * Begin() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > T * End()
|
||||
{
|
||||
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the internal buffer casted as a different type.
|
||||
*/
|
||||
template < typename T = Value > const T * End() const
|
||||
{
|
||||
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the front of the buffer.
|
||||
*/
|
||||
template < typename T = Value > T & Front()
|
||||
{
|
||||
assert(m_Cap >= sizeof(T));
|
||||
return reinterpret_cast< T * >(m_Ptr)[0];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the front of the buffer.
|
||||
*/
|
||||
template < typename T = Value > const T & Front() const
|
||||
{
|
||||
assert(m_Cap >= sizeof(T));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[0];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element after the first element in the buffer.
|
||||
*/
|
||||
template < typename T = Value > T & Next()
|
||||
{
|
||||
assert(m_Cap >= (sizeof(T) * 2));
|
||||
return reinterpret_cast< T * >(m_Ptr)[1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element after the first element in the buffer.
|
||||
*/
|
||||
template < typename T = Value > const T & Next() const
|
||||
{
|
||||
assert(m_Cap >= (sizeof(T) * 2));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the back of the buffer.
|
||||
*/
|
||||
template < typename T = Value > T & Back()
|
||||
{
|
||||
assert(m_Cap >= sizeof(T));
|
||||
return reinterpret_cast< T * >(m_Ptr)[m_Cap-1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the back of the buffer.
|
||||
*/
|
||||
template < typename T = Value > const T & Back() const
|
||||
{
|
||||
assert(m_Cap >= sizeof(T));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[m_Cap-1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element before the last element in the buffer.
|
||||
*/
|
||||
template < typename T = Value > T & Prev()
|
||||
{
|
||||
assert(m_Cap >= (sizeof(T) * 2));
|
||||
return reinterpret_cast< T * >(m_Ptr)[m_Cap-2];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element before the last element in the buffer.
|
||||
*/
|
||||
template < typename T = Value > const T & Prev() const
|
||||
{
|
||||
assert(m_Cap >= (sizeof(T) * 2));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[m_Cap-2];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reposition the edit cursor to the specified number of elements ahead.
|
||||
*/
|
||||
template < typename T = Value > void Advance(SzType n)
|
||||
{
|
||||
// Do we need to scale the buffer?
|
||||
if ((m_Cur + (n * sizeof(T))) >= m_Cap)
|
||||
{
|
||||
Grow(m_Cur + (n * sizeof(T)));
|
||||
}
|
||||
// Advance to the specified position
|
||||
m_Cur += (n * sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reposition the edit cursor to the specified number of elements behind.
|
||||
*/
|
||||
template < typename T = Value > void Retreat(SzType n)
|
||||
{
|
||||
// Can we move that much backward?
|
||||
if ((n * sizeof(T)) <= m_Cur)
|
||||
{
|
||||
m_Cur -= (n * sizeof(T));
|
||||
}
|
||||
// Just got to the beginning
|
||||
else
|
||||
{
|
||||
m_Cur = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reposition the edit cursor to a fixed position within the buffer.
|
||||
*/
|
||||
template < typename T = Value > void Move(SzType n)
|
||||
{
|
||||
// Do we need to scale the buffer?
|
||||
if ((n * sizeof(T)) >= m_Cap)
|
||||
{
|
||||
Grow(n * sizeof(T));
|
||||
}
|
||||
// Move to the specified position
|
||||
m_Cur = (n * sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reposition the edit cursor to a fixed position within the buffer.
|
||||
*/
|
||||
template < typename T = Value > void Push(T v)
|
||||
{
|
||||
// Do we need to scale the buffer?
|
||||
if ((m_Cur + sizeof(T)) >= m_Cap)
|
||||
{
|
||||
Grow(m_Cap + sizeof(T));
|
||||
}
|
||||
// Assign the specified value
|
||||
reinterpret_cast< T * >(m_Ptr)[m_Cur] = v;
|
||||
// Move to the next element
|
||||
m_Cur += sizeof(T);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the cursor position.
|
||||
*/
|
||||
template < typename T = Value > T & Cursor()
|
||||
{
|
||||
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
|
||||
return reinterpret_cast< T * >(m_Ptr)[m_Cur];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element at the cursor position.
|
||||
*/
|
||||
template < typename T = Value > const T & Cursor() const
|
||||
{
|
||||
assert((m_Cur / sizeof(T)) < (m_Cap / sizeof(T)));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[m_Cur];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element before the cursor position.
|
||||
*/
|
||||
template < typename T = Value > T & Before()
|
||||
{
|
||||
assert(m_Cur >= sizeof(T));
|
||||
return reinterpret_cast< T * >(m_Ptr)[m_Cur-1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element before the cursor position.
|
||||
*/
|
||||
template < typename T = Value > const T & Before() const
|
||||
{
|
||||
assert(m_Cur >= sizeof(T));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[m_Cur-1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element after the cursor position.
|
||||
*/
|
||||
template < typename T = Value > T & After()
|
||||
{
|
||||
assert((m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
|
||||
return reinterpret_cast< T * >(m_Ptr)[m_Cur+1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the element after the cursor position.
|
||||
*/
|
||||
template < typename T = Value > const T & After() const
|
||||
{
|
||||
assert((m_Cur + sizeof(T)) <= (m_Cap - sizeof(T)));
|
||||
return reinterpret_cast< const T * >(m_Ptr)[m_Cur+1];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve maximum elements it can hold for a certain type.
|
||||
*/
|
||||
template < typename T = Value> static SzType Max()
|
||||
template < typename T = Value > static SzType Max()
|
||||
{
|
||||
return (0xFFFFFFFF / sizeof(T));
|
||||
return static_cast< SzType >(0xFFFFFFFF / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the current buffer capacity in element count.
|
||||
*/
|
||||
template < typename T = Value> SzType Size() const
|
||||
template < typename T = Value > SzType Size() const
|
||||
{
|
||||
return (m_Cap / sizeof(T));
|
||||
return static_cast< SzType >(m_Cap / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -425,25 +624,52 @@ public:
|
||||
return m_Cap;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the current position of the cursor in the buffer.
|
||||
*/
|
||||
template < typename T = Value > SzType Position() const
|
||||
{
|
||||
return static_cast< SzType >(m_Cur / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the amount of unused buffer after the edit cursor.
|
||||
*/
|
||||
template < typename T = Value > SzType Remaining() const
|
||||
{
|
||||
return static_cast< SzType >((m_Cap - m_Cur) / sizeof(T));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Grow the size of the internal buffer by the specified amount of bytes.
|
||||
*/
|
||||
void Grow(SzType n);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Makes sure there is enough capacity to hold the specified element count.
|
||||
*/
|
||||
template < typename T = Value> Buffer Adjust(SzType n)
|
||||
template < typename T = Value > Buffer Adjust(SzType n)
|
||||
{
|
||||
// Do we meet the minimum size?
|
||||
if (n < 8)
|
||||
{
|
||||
n = 8; // Adjust to minimum size
|
||||
}
|
||||
// See if the requested capacity doesn't exceed the limit
|
||||
if (n > Max< T >())
|
||||
{
|
||||
ThrowMemExcept("Requested buffer of (%u) elements exceeds the (%u) limit", n, Max< T >());
|
||||
}
|
||||
// Is there an existing buffer?
|
||||
else if (n && !m_Cap)
|
||||
{
|
||||
Request(n * sizeof(T)); // Request the memory
|
||||
}
|
||||
// Should the size be increased?
|
||||
else if (n > m_Cap)
|
||||
{
|
||||
// Backup the current memory
|
||||
Buffer bkp(m_Ptr, m_Cap, m_Mem);
|
||||
Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
|
||||
// Request the memory
|
||||
Request(n * sizeof(T));
|
||||
// Return the backup
|
||||
@ -459,7 +685,9 @@ public:
|
||||
void Reset()
|
||||
{
|
||||
if (m_Ptr)
|
||||
{
|
||||
Release();
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -476,10 +704,18 @@ public:
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Write a portion of a buffet to the internal buffer.
|
||||
* Write a portion of a buffer to the internal buffer.
|
||||
*/
|
||||
SzType Write(SzType pos, ConstPtr data, SzType size);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Write another buffer to the internal buffer.
|
||||
*/
|
||||
SzType Write(SzType pos, const Buffer & b)
|
||||
{
|
||||
return Write(pos, b.m_Ptr, b.m_Cur);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Write a formatted string to the internal buffer.
|
||||
*/
|
||||
@ -490,6 +726,61 @@ public:
|
||||
*/
|
||||
SzType WriteF(SzType pos, const char * fmt, va_list args);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Write a string to the internal buffer.
|
||||
*/
|
||||
SzType WriteS(SzType pos, const char * str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Write a portion of a string to the internal buffer.
|
||||
*/
|
||||
SzType WriteS(SzType pos, const char * str, SzType size)
|
||||
{
|
||||
return Write(pos, str, size);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append a portion of a buffer to the internal buffer.
|
||||
*/
|
||||
void Append(ConstPtr data, SzType size)
|
||||
{
|
||||
m_Cur += Write(m_Cur, data, size);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append another buffer to the internal buffer.
|
||||
*/
|
||||
void Append(const Buffer & b)
|
||||
{
|
||||
m_Cur += Write(m_Cur, b.m_Ptr, b.m_Cur);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append a formatted string to the internal buffer.
|
||||
*/
|
||||
void AppendF(const char * fmt, ...);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append a formatted string to the internal buffer.
|
||||
*/
|
||||
void AppendF(const char * fmt, va_list args)
|
||||
{
|
||||
m_Cur += WriteF(m_Cur, fmt, args);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append a string to the internal buffer.
|
||||
*/
|
||||
void AppendS(const char * str);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Append a portion of a string to the internal buffer.
|
||||
*/
|
||||
void AppendS(const char * str, SzType size)
|
||||
{
|
||||
m_Cur += Write(m_Cur, str, size);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -507,9 +798,10 @@ private:
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Pointer m_Ptr; /* Pointer to the memory buffer. */
|
||||
SzType m_Cap; /* The total size of the buffer. */
|
||||
SzType m_Cur; /* The buffer edit cursor. */
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
MemRef m_Mem;
|
||||
MemRef m_Mem; /* Reference to the associated memory manager. */
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -209,12 +209,12 @@ void Register_Crypt(HSQUIRRELVM vm)
|
||||
RegisterWrapper< SHA256 >(hashns, _SC("SHA256"));
|
||||
RegisterWrapper< SHA3 >(hashns, _SC("SHA3"));
|
||||
|
||||
hashns.SquirrelFunc(_SC("GetCRC32"), &HashF< CRC32 >);
|
||||
hashns.SquirrelFunc(_SC("GetKeccak"), &HashF< Keccak >);
|
||||
hashns.SquirrelFunc(_SC("GetMD5"), &HashF< MD5 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA1"), &HashF< SHA1 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA256"), &HashF< SHA256 >);
|
||||
hashns.SquirrelFunc(_SC("GetSHA3"), &HashF< SHA3 >);
|
||||
hashns.SquirrelFunc(_SC("SqCRC32"), &HashF< CRC32 >);
|
||||
hashns.SquirrelFunc(_SC("SqKeccak"), &HashF< Keccak >);
|
||||
hashns.SquirrelFunc(_SC("SqMD5"), &HashF< MD5 >);
|
||||
hashns.SquirrelFunc(_SC("SqSHA1"), &HashF< SHA1 >);
|
||||
hashns.SquirrelFunc(_SC("SqSHA256"), &HashF< SHA256 >);
|
||||
hashns.SquirrelFunc(_SC("SqSHA3"), &HashF< SHA3 >);
|
||||
|
||||
RootTable(vm).Bind(_SC("SqHash"), hashns);
|
||||
|
||||
|
1218
source/Library/SysEnv.cpp
Normal file
1218
source/Library/SysEnv.cpp
Normal file
File diff suppressed because it is too large
Load Diff
316
source/Library/SysEnv.hpp
Normal file
316
source/Library/SysEnv.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_
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,729 @@
|
||||
#ifndef _LIBRARY_SYSPATH_HPP_
|
||||
#define _LIBRARY_SYSPATH_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <vector>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* 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;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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);
|
||||
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_SYSPATH_HPP_
|
@ -38,6 +38,8 @@ extern void Register_Crypt(HSQUIRRELVM vm);
|
||||
extern void Register_Numeric(HSQUIRRELVM vm);
|
||||
extern void Register_Random(HSQUIRRELVM vm);
|
||||
extern void Register_String(HSQUIRRELVM vm);
|
||||
extern void Register_SysEnv(HSQUIRRELVM vm);
|
||||
extern void Register_SysPath(HSQUIRRELVM vm);
|
||||
extern void Register_Time(HSQUIRRELVM vm);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -80,6 +82,8 @@ bool RegisterAPI(HSQUIRRELVM vm)
|
||||
Register_Random(vm);
|
||||
Register_Numeric(vm);
|
||||
Register_String(vm);
|
||||
Register_SysEnv(vm);
|
||||
Register_SysPath(vm);
|
||||
Register_Time(vm);
|
||||
|
||||
Register_Constants(vm);
|
||||
|
Loading…
Reference in New Issue
Block a user