19#ifndef FENNEC_FILESYSTEM_PATH_H
20#define FENNEC_FILESYSTEM_PATH_H
22#include <fennec/filesystem/path.h>
23#include <fennec/string/string.h>
65 if (str.
size() > 2 && str[str.
size() - 1] ==
'/') {
75 if (str.
size() > 2 && str[str.
size() - 1] ==
'/') {
149 return path(_str +
'/' + str);
153 return path(_str +
'/' + str);
157 return path(_str +
'/' + p._str);
160 bool operator==(
const path& p)
const {
161 return _str == p._str;
164 string filename()
const {
165 size_t i = _str.
rfind(
'/');
169 const string& str()
const {
return _str; }
170 const char* cstr()
const {
return _str.cstr(); }
173 size_t size = _str.
size();
174 if (size == 0)
return true;
175#if FENNEC_PLATFORM_WINDOWS
176 return (_str[1] ==
':' && size == 3);
178 return (_str[0] ==
'/' && size == 1);
182 path parent()
const {
183#ifdef FENNEC_PLATFORM_WINDOWS
184 size_t start = _str.
size() - 1;
185 start = _str[start] ==
'/' || _str[start] ==
'\\' ? start - 1 : start;
187 size_t r = _str.
rfind(
'/', start);
188 size_t l = _str.
rfind(
'\\', start);
189 if (r == _str.
size()) {
192 else if (l == _str.
size()) {
200 size_t start = _str.
size() - 1;
201 start = _str[start] ==
'/' ? start - 1 : start;
206 path absolute()
const {
208 path working; working._str.resize(0);
211#ifdef FENNEC_PLATFORM_WINDOWS
212 if (_str[1] !=
':') {
214 if (_str[0] !=
'/') {
219 while (not parse.empty()) {
221 while (not parse.empty() && parse._str[0] ==
'.') {
223 if (parse._str[1] ==
'.') {
225 if (parse._str.size() == 2) {
227 working = working.parent();
230 else if (parse._str[2] ==
'/') {
231 working = working.parent();
232 parse._str = parse._str.substring(3);
236 else if (parse._str[1] ==
'/') {
237 parse._str = parse._str.substring(2);
241 if (parse.empty())
break;
244 const size_t loc = parse._str.find(
'/');
246 working._str += parse._str.substring(0, loc);
247 parse._str = parse._str.substring(loc + 1);
256 iterator begin()
const {
257 return iterator(
this, 0);
260 iterator end()
const {
261 return iterator(
this, _str.
size());
266 constexpr iterator(
const path*
path,
size_t p)
271 if (p == _str->size()) {
276#ifdef FENNEC_PLATFORM_WINDOWS
277 if ((*_str)[1] ==
':') {
278 _pos =
max(_pos,
size_t(3));
281 if ((*_str)[0] ==
'/') {
282 _pos =
max(_pos,
size_t(1));
287 if (_pos != 0 && (*_str)[_pos - 1] !=
'/') {
288 _pos = _str->find(
'/', _pos) + 1;
292 constexpr iterator(
const iterator&) =
default;
293 constexpr iterator(iterator&&) noexcept = default;
295 constexpr
string operator*()
const {
296 if ((*_str)[_pos] ==
'/') {
300 size_t e = _str->find(
'/', _pos);
301 return _str->substring(_pos, e - _pos);
304 constexpr iterator& operator++() {
305 _pos =
min(_str->find(
'/', _pos) + 1, _str->size());
309 constexpr iterator operator++(
int) {
315 constexpr bool operator==(
const iterator& rhs)
const {
316 return _str == rhs._str and _pos == rhs._pos;
319 constexpr bool operator!=(
const iterator& rhs)
const {
320 return _str != rhs._str or _pos != rhs._pos;
constexpr genType min(genType x, genType y)
Returns if otherwise it returns .
Definition common.h:688
constexpr genType max(genType x, genType y)
Returns if , otherwise it returns .
Definition common.h:705
constexpr genType e()
Definition constants.h:635
constexpr size_t rfind(char c, size_t i=npos) const
Finds the index of the last occurrence of c in the string.
Definition string.h:291
constexpr size_t size() const
Definition string.h:150
constexpr _string substring(size_t i, size_t n=npos) const
Retrieve a substring of a string.
Definition string.h:351
This struct wraps c-style strings.
Definition cstring.h:64
constexpr size_t size() const
Definition cstring.h:187
struct for handling file paths
Definition path.h:34
static path current()
Get the current working directory.
Definition path.cpp:40
path & operator=(const char(&str)[n])
C-String Assignment Operator.
Definition path.h:100
path & operator=(path &&p) noexcept
Path Move Assignment Operator.
Definition path.h:136
path & operator=(const cstring &p)
C-String Assignment Operator.
Definition path.h:109
path(path &&p) noexcept
Path Move Constructor.
Definition path.h:90
path operator/(const cstring &str) const
Definition path.h:148
path(const cstring &str)
C-String Conversion Constructor.
Definition path.h:63
path(const string &str)
String Conversion Constructor.
Definition path.h:73
path & operator=(const string &p)
String Assignment Operator.
Definition path.h:118
path()
Default Constructor, returns the root of the current working directory.
Definition path.h:58
path(const path &p)
Path Copy Constructor.
Definition path.h:83
path & operator=(const path &p)
Path Copy Assignment Operator.
Definition path.h:127
constexpr remove_reference_t< T > && move(T &&x) noexcept
produces an x-value type to indicate x may be "moved"
Definition utility.h:92