Files
fennec/include/fennec/lang/filesystem/path.h

335 lines
8.0 KiB
C++

// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#ifndef FENNEC_LANGPROC_IO_PATH_H
#define FENNEC_LANGPROC_IO_PATH_H
#include <fennec/lang/filesystem/path.h>
#include <fennec/lang/strings/string.h>
namespace fennec
{
///
/// \brief struct for handling file paths
///
/// \details This structure makes no guarantees about the validity of a path.
/// Operations do not examine the system's file structure.
struct path
{
public:
// Definitions =========================================================================================================
class iterator;
friend iterator;
// Static Functions ====================================================================================================
/// \brief Get the current working directory
/// \returns a path containing the absolute path to the working directory
static path current();
/// \brief Set the current working directory
/// \param path the path to the new working directory
/// \returns a path containing the absolute path to the working directory
static path current(const path& path);
// Constructors ========================================================================================================
///
/// \brief Default Constructor, returns the root of the current working directory
path() : _str("/") { }
///
/// \brief C-String Conversion Constructor
/// \param str the cstring to convert
path(const cstring& str)
: _str(str) {
if (str.size() > 2 && str[str.size() - 1] == '/') {
_str = _str.substring(0, str.size() - 1);
}
}
///
/// \brief String Conversion Constructor
/// \param str the string to convert
path(const string& str)
: _str(str) {
if (str.size() > 2 && str[str.size() - 1] == '/') {
_str = _str.substring(0, str.size() - 1);
}
}
///
/// \brief Path Copy Constructor
/// \param p the path to copy
path(const path& p)
: _str(p._str) {
}
///
/// \brief Path Move Constructor
/// \param p the path to take ownership of
path(path&& p) noexcept : _str(move(p._str)) { }
// Assignment Operators ================================================================================================
///
/// \brief C-String Assignment Operator
/// \param str the cstring to assign
/// \returns a reference to `this` after assigning `p`
template<size_t n>
path& operator=(const char (&str)[n]) {
_str = str;
return *this;
}
///
/// \brief C-String Assignment Operator
/// \param p the cstring to assign
/// \returns a reference to `this` after assigning `p`
path& operator=(const cstring& p) {
_str = p;
return *this;
}
///
/// \brief String Assignment Operator
/// \param p the cstring to assign
/// \returns a reference to `this` after assigning `p`
path& operator=(const string& p) {
_str = p;
return *this;
}
///
/// \brief Path Copy Assignment Operator
/// \param p the path to copy
/// \returns a reference to `this` after copying `p`
path& operator=(const path& p) {
_str = p._str;
return *this;
}
///
/// \brief Path Move Assignment Operator
/// \param p the path to take ownership of
/// \returns a reference to `this` after taking ownership of `p`
path& operator=(path&& p) noexcept {
_str = move(p._str);
return *this;
}
// Append Operators ====================================================================================================
///
/// \brief
/// \param str
/// \return
path operator/(const cstring& str) const {
return path(_str + '/' + str);
}
path operator/(const string& str) const {
return path(_str + '/' + str);
}
path operator/(const path& p) const {
return path(_str + '/' + p._str);
}
bool operator==(const path& p) const {
return _str == p._str;
}
string filename() const {
size_t i = _str.rfind('/');
return _str.substring(i + 1);
}
const string& str() const { return _str; }
const char* cstr() const { return _str.cstr(); }
bool empty() {
size_t size = _str.size();
if (size == 0) return true;
#if FENNEC_PLATFORM_WINDOWS
return (_str[1] == ':' && size == 3);
#else
return (_str[0] == '/' && size == 1);
#endif
}
path parent() const {
#ifdef FENNEC_PLATFORM_WINDOWS
size_t start = _str.size() - 1;
start = _str[start] == '/' || _str[start] == '\\' ? start - 1 : start;
size_t r = _str.rfind('/', start);
size_t l = _str.rfind('\\', start);
if (r == _str.size()) {
start = l;
}
else if (l == _str.size()) {
start = r;
}
else {
start = max(r, l);
}
return _str.substring(0, start);
#else
size_t start = _str.size() - 1;
start = _str[start] == '/' ? start - 1 : start;
return path(_str.substring(0, _str.rfind('/', start)));
#endif
}
path absolute() const {
path parse = *this;
path working; working._str.resize(0);
// Check if this is a rooted path;
#ifdef FENNEC_PLATFORM_WINDOWS
if (_str[1] != ':') {
#else
if (_str[0] != '/') {
#endif
working = current();
}
while (not parse.empty()) {
// Handle dots
while (not parse.empty() && parse._str[0] == '.') {
// Check for ".."
if (parse._str[1] == '.') {
// ".."
if (parse._str.size() == 2) {
parse = path();
working = working.parent();
}
// "../"
else if (parse._str[2] == '/') {
working = working.parent();
parse._str = parse._str.substring(3);
}
}
// "./"
else if (parse._str[1] == '/') {
parse._str = parse._str.substring(2);
}
}
if (parse.empty()) break;
// Push the path
const size_t loc = parse._str.find('/');
working._str += '/';
working._str += parse._str.substring(0, loc);
parse._str = parse._str.substring(loc + 1);
}
return working;
}
// Iterator ============================================================================================================
iterator begin() const {
return iterator(this, 0);
}
iterator end() const {
return iterator(this, _str.size());
}
class iterator {
public:
constexpr iterator(const path* path, size_t p)
: _str(&path->_str)
, _pos(p) {
// Handle end()
if (p == _str->size()) {
return;
}
// Handle rooted paths
#ifdef FENNEC_PLATFORM_WINDOWS
if ((*_str)[1] == ':') {
_pos = max(_pos, size_t(3));
}
#else
if ((*_str)[0] == '/') {
_pos = max(_pos, size_t(1));
}
#endif
// Ensure we are at the start of a directory/file name
if (_pos != 0 && (*_str)[_pos - 1] != '/') {
_pos = _str->find('/', _pos) + 1;
}
}
constexpr iterator(const iterator&) = default;
constexpr iterator(iterator&&) noexcept = default;
constexpr string operator*() const {
if ((*_str)[_pos] == '/') {
return string("");
}
size_t e = _str->find('/', _pos);
return _str->substring(_pos, e - _pos);
}
constexpr iterator& operator++() {
_pos = min(_str->find('/', _pos) + 1, _str->size());
return *this;
}
constexpr iterator operator++(int) {
iterator it = *this;
this->operator++();
return it;
}
constexpr bool operator==(const iterator& rhs) const {
return _str == rhs._str and _pos == rhs._pos;
}
constexpr bool operator!=(const iterator& rhs) const {
return _str != rhs._str or _pos != rhs._pos;
}
private:
const string* _str;
size_t _pos;
};
private:
string _str;
};
}
#endif // FENNEC_LANGPROC_IO_PATH_H