- Wrote and Debugged Unit Tests for fennec::file

This commit is contained in:
2025-07-14 21:15:39 -04:00
parent 6ae682aff6
commit 89f59c75f3
14 changed files with 669 additions and 369 deletions

View File

@@ -19,7 +19,7 @@
#ifndef FENNEC_FPROC_IO_FILE_H
#define FENNEC_FPROC_IO_FILE_H
#include <asm-generic/errno-base.h>
#include <fennec/fproc/filesystem/path.h>
#include <fennec/fproc/strings/cstring.h>
#include <fennec/fproc/strings/string.h>
@@ -127,7 +127,7 @@ public:
///
/// \returns the path the stream
const string& path() const {
const path& get_path() const {
return _path;
}
@@ -151,14 +151,21 @@ public:
/// \param path the path to the file
/// \param mode the mode flags to open the file with
/// \returns false on success, true on error
bool open(const cstring& path, uint8_t mode);
bool open(const cstring& p, uint8_t mode);
///
/// \brief open a file
/// \param path the path to the file
/// \param mode the mode flags to open the file with
/// \returns false on success, true on error
bool open(const string& path, uint8_t mode);
bool open(const string& p, uint8_t mode);
///
/// \brief open a file
/// \param path the path to the file
/// \param mode the mode flags to open the file with
/// \returns false on success, true on error
bool open(const path& p, uint8_t mode);
///
/// \brief close a stream
@@ -188,7 +195,7 @@ public:
/// copies the contents of this file to the new stream,
/// reopen the new stream with the flags of this file and binds to it,
/// closes the old file.
bool rename(const cstring& path);
bool rename(const cstring& p);
///
/// \brief rebinds the stream, copying contents to path, and erasing the old file
@@ -200,19 +207,37 @@ public:
/// copies the contents of this file to the new stream,
/// reopen the new stream with the flags of this file and binds to it,
/// closes the old file.
bool rename(const string& path);
bool rename(const string& p);
///
/// \brief rebinds the stream, copying contents to path, and erasing the old file
/// \param path the new path
/// \returns false on success, true on error
///
/// \details attempts to open a write-only stream at path,
/// attempts to reopen this file as read-only,
/// copies the contents of this file to the new stream,
/// reopen the new stream with the flags of this file and binds to it,
/// closes the old file.
bool rename(const path& p);
///
/// \brief copies the contents of this file to path.
/// \param path the path to copy to
/// \returns a file at the new path with the copied contents
file copy(const cstring& path);
file copy(const cstring& p);
///
/// \brief copies the contents of this file to path.
/// \param path the path to copy to
/// \returns a file at the new path with the copied contents
file copy(const string& path);
file copy(const string& p);
///
/// \brief copies the contents of this file to path.
/// \param path the path to copy to
/// \returns a file at the new path with the copied contents
file copy(const path& p);
// File Positioning ====================================================================================================
@@ -269,7 +294,7 @@ public:
private:
FILE* _handle;
string _path;
path _path;
uint8_t _mode;
char* _error;
};

View File

@@ -0,0 +1,228 @@
// =====================================================================================================================
// 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_FPROC_IO_PATH_H
#define FENNEC_FPROC_IO_PATH_H
#include <fennec/fproc/strings/string.h>
namespace fennec
{
///
/// \brief struct for handling file paths
struct path
{
public:
// 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) {
while (not _str.empty() && _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) {
while (_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 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;
}
const string& str() const { return _str; }
bool empty() {
size_t size = _str.size();
#ifdef _WIN32
return (_str[1] == ':' && size == 3) || size == 0;
#else
return (_str[0] == '/' && size == 1) || size == 0;
#endif
}
path parent() const {
#ifdef _WIN32
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 _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 _WIN32
if (_str[1] != ':') {
#else
if (_str[0] != '/') {
#endif
working = current();
}
while (not parse.empty()) {
// Handle dots
while (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
size_t loc = parse._str.find('/');
working._str += '/';
working._str += parse._str.substring(0, loc);
parse._str = parse._str.substring(loc + 1);
}
return working;
}
private:
string _str;
};
}
#endif // FENNEC_FPROC_IO_PATH_H

View File

@@ -1,33 +0,0 @@
// =====================================================================================================================
// 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_FPROC_IO_COMMON_H
#define FENNEC_FPROC_IO_COMMON_H
#include <fennec/fproc/strings/string.h>
namespace fennec
{
string getcwd();
string absolute(const cstring& path);
string absolute(const string& path);
}
#endif // FENNEC_FPROC_IO_COMMON_H

View File

@@ -330,7 +330,7 @@ public:
/// \brief Copy Assignment Operator
/// \param str the string to copy
/// \returns a reference to `this`
constexpr string& operator=(const cstring& str) {
constexpr _string& operator=(const cstring& str) {
resize(str.size());
fennec::memcpy(_str.data(), str, str.size());
_str[str.size()] = '\0';
@@ -341,7 +341,7 @@ public:
/// \brief Copy Assignment Operator
/// \param str the string to copy
/// \returns a reference to `this`
constexpr string& operator=(const string& str) {
constexpr _string& operator=(const string& str) {
resize(str.size());
fennec::memcpy(_str.data(), str, str.size());
_str[str.size()] = '\0';
@@ -352,64 +352,75 @@ public:
/// \brief Move Assignment Operator
/// \param str the string to move
/// \returns a reference to `this`
constexpr string& operator=(string&& str) noexcept {
_str = fennec::move(str._str);
constexpr _string& operator=(string&& str) noexcept {
_str = move(str._str);
return *this;
}
///
/// \brief Replace all instances of `x` with `y`
/// \param x the character to search for
/// \param y the character to replace with
void replace(char x, char y) {
size_t i = 0;
while ((i = find(x, 0)) != size()) {
_str[i] = y;
}
}
///
/// \brief Retrieve a substring of a string
/// \param i the start index
/// \param n the number of characters
/// \return
constexpr string substring(size_t i, size_t n = npos) const {
constexpr _string substring(size_t i, size_t n = npos) const {
if (i >= size()) {
return string("");
return _string("");
}
n = min(n, size() - i);
return string(_str.data() + i, n);
return _string(_str.data() + i, n);
}
///
/// \brief Returns a string with `c` appended to it
/// \param c
/// \returns
constexpr string operator+(char c) const {
constexpr _string operator+(char c) const {
// Copy contents with one additional byte.
string res(_str, _str.size());
_string res(_str.data(), _str.size());
res[size()] = c; // Set the last character to c
return res;
}
constexpr string operator+(const cstring& str) const {
string res(size() + str.size()); // Make a new string with the size of this + str
constexpr _string operator+(const cstring& str) const {
_string res(size() + str.size()); // Make a new string with the size of this + str
fennec::memcpy(&res[0], _str.data(), size()); // Copy the contents of this
fennec::memcpy(&res[size()], str, str.size()); // Append the contents of str
return res;
}
constexpr string operator+(const string& str) const {
string res(size() + str.size()); // Make a new string with the size of this + str
constexpr _string operator+(const _string& str) const {
_string res(size() + str.size()); // Make a new string with the size of this + str
fennec::memcpy(&res[0], _str.data(), size()); // Copy the contents of this
fennec::memcpy(&res[size()], str, str.size()); // Append the contents of str
return res;
}
constexpr string& operator+=(char c) {
constexpr _string& operator+=(char c) {
size_t x = size();
resize(x + 1);
_str[x] = c;
return *this;
}
constexpr string& operator+=(const cstring& str) {
constexpr _string& operator+=(const cstring& str) {
size_t x = size();
resize(x + str.size());
fennec::memcpy(&_str[x], str, str.size());
return *this;
}
constexpr string& operator+=(const string& str) {
constexpr _string& operator+=(const _string& str) {
size_t x = size();
resize(x + str.size());
fennec::memcpy(&_str[x], str, str.size());