- 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

@@ -16,14 +16,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#include <stdlib.h>
#include <errno.h>
#include <fennec/fproc/io/file.h>
#include <fennec/fproc/io/common.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <fennec/fproc/filesystem/file.h>
#include <fennec/fproc/filesystem/path.h>
#ifdef _MSC_VER
@@ -143,7 +144,7 @@ file& file::operator=(file&& file) noexcept {
return *this;
}
bool file::open(const cstring& filename, uint8_t mode) {
bool file::open(const cstring& p, uint8_t mode) {
assert(_error == nullptr, "Attempted Operation on a File in an Errored State");
// Ensure validity of the mode
@@ -157,7 +158,7 @@ bool file::open(const cstring& filename, uint8_t mode) {
}
// Attempt to open the file
_handle = fopen(filename, fmode_translate(mode));
_handle = fopen(p, fmode_translate(mode));
if (_handle == nullptr) {
_error = strerror(errno);
@@ -173,11 +174,12 @@ bool file::open(const cstring& filename, uint8_t mode) {
}
_mode = mode;
_path = absolute(filename);
_path = p;
_path = _path.absolute();
return false;
}
bool file::open(const string& filename, uint8_t mode) {
bool file::open(const string& p, uint8_t mode) {
assert(_error == nullptr, "Attempted Operation on a File in an Errored State");
// Ensure validity of the mode
@@ -191,7 +193,7 @@ bool file::open(const string& filename, uint8_t mode) {
}
// Attempt to open the file
_handle = fopen(filename, fmode_translate(mode));
_handle = fopen(p, fmode_translate(mode));
// Validate the file
if (_handle == nullptr) {
@@ -211,7 +213,47 @@ bool file::open(const string& filename, uint8_t mode) {
fwide(_handle, mode & fmode_wide ? 1 : -1);
_mode = mode;
_path = absolute(filename);
_path = p;
_path = _path.absolute();
return false;
}
bool file::open(const path& p, uint8_t mode) {
assert(_error == nullptr, "Attempted Operation on a File in an Errored State");
// Ensure validity of the mode
if (not is_valid(mode)) {
return true;
}
// Close the file if already open
if (_handle) {
close();
}
// Attempt to open the file
_handle = fopen(p.str(), fmode_translate(mode));
// Validate the file
if (_handle == nullptr) {
_error = strerror(errno);
return true;
}
// Attempt to lock the file
if (flock(fileno(_handle), LOCK_EX)) {
_error = strerror(errno);
fclose(_handle);
_handle = nullptr;
return true;
}
// Set the orientation
fwide(_handle, mode & fmode_wide ? 1 : -1);
_mode = mode;
_path = p.absolute();
return false;
}
@@ -265,13 +307,13 @@ bool file::erase() {
}
// Close the file
string path = move(_path);
path path = move(_path);
if (close()) {
return true;
}
// Erase the file
remove(path);
remove(path.str());
return false;
}
@@ -286,13 +328,14 @@ bool file::rename(const cstring& str) {
}
// Validate path
string fpath = absolute(str);
path fpath = str;
fpath = fpath.absolute();
if (_path == fpath) {
return false;
}
// Attempt to open the new file
FILE* fnew = fopen(fpath, "wx");
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {
@@ -352,7 +395,7 @@ bool file::rename(const cstring& str) {
fclose(_handle);
// Reopen the new file
_handle = freopen(&fpath[0], fmode_translate(_mode), fnew);
_handle = freopen(fpath.str(), fmode_translate(_mode), fnew);
// Check for open failure
if (_handle == nullptr) {
@@ -361,7 +404,7 @@ bool file::rename(const cstring& str) {
}
// Erase the old file
remove(_path);
remove(_path.str());
// Set the new path
_path = fpath;
@@ -379,13 +422,14 @@ bool file::rename(const string& str) {
}
// Validate path
string fpath = absolute(str);
path fpath = str;
fpath = fpath.absolute();
if (_path == fpath) {
return false;
}
// Attempt to open the new file
FILE* fnew = fopen(fpath, "wx");
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {
@@ -444,7 +488,96 @@ bool file::rename(const string& str) {
fclose(_handle);
// Reopen the new file
_handle = freopen(&_path[0], fmode_translate(_mode), fnew);
_handle = freopen(_path.str(), fmode_translate(_mode), fnew);
// Check for open failure
if (_handle == nullptr) {
_error = strerror(errno);
return true;
}
// Set the new path
_path = fpath;
return false;
}
bool file::rename(const path& p) {
static const size_t page_size = pagesize();
assert(_error == nullptr, "Attempted an Operation on a File in an Errored State");
// Check if there is a file
if (_handle == nullptr) {
return false;
}
// Validate path
path fpath = p.absolute();
if (_path == fpath) {
return false;
}
// Attempt to open the new file
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {
_error = strerror(errno);
return true;
}
// Reopen this file as read
_handle = freopen(nullptr, "r", _handle);
// Check if it failed to reopen
if (_handle == nullptr) {
_error = strerror(errno);
return true;
}
// Initialize buffer
void* buffer = operator new(page_size, static_cast<align_t>(page_size));
// Copy contents
size_t read = 0;
while ((read = fread(buffer, 1, page_size, _handle)) == page_size) {
if (fwrite(buffer, 1, page_size, fnew) != page_size) {
break;
}
}
// Handle eof
if (feof(_handle)) {
fwrite(buffer, 1, read, fnew);
}
// Check new file for errors
if (ferror(fnew)) {
_error = strerror(errno);
fclose(fnew);
_mode = fmode_read;
return true;
}
// Check the original file for errors
if (ferror(_handle)) {
_error = strerror(errno);
fclose(fnew);
fclose(_handle);
_handle = nullptr;
_mode = 0;
_path = "";
return true;
}
// Cleanup the buffer
operator delete(buffer, page_size, static_cast<align_t>(page_size));
// Close old file
fclose(_handle);
// Reopen the new file
_handle = freopen(_path.str(), fmode_translate(_mode), fnew);
// Check for open failure
if (_handle == nullptr) {
@@ -468,13 +601,14 @@ file file::copy(const cstring& str) {
}
// Validate path
string fpath = absolute(str);
path fpath = str;
fpath = fpath.absolute();
if (_path == fpath) {
return file();
}
// Attempt to open the new file
FILE* fnew = fopen(fpath, "wx");
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {
@@ -557,13 +691,105 @@ file file::copy(const string& str) {
}
// Validate path
string fpath = absolute(str);
path fpath = str;
fpath = fpath.absolute();
if (_path == fpath) {
return file();
}
// Attempt to open the new file
FILE* fnew = fopen(fpath, "wx");
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {
_error = strerror(errno);
return file();
}
// Reopen this file as read
_handle = freopen(nullptr, "r", _handle);
// Check if it failed to reopen
if (_handle == nullptr) {
_error = strerror(errno);
return file();
}
// Initialize buffer
void* buffer = operator new(page_size, static_cast<align_t>(page_size));
// Copy contents
size_t read = 0;
while ((read = fread(buffer, 1, page_size, _handle)) == page_size) {
if (fwrite(buffer, 1, page_size, fnew) != page_size) {
break;
}
}
// Handle eof
if (feof(_handle)) {
fwrite(buffer, 1, read, fnew);
}
// Check new file for errors
if (ferror(fnew)) {
operator delete(buffer, page_size, static_cast<align_t>(page_size));
_error = strerror(errno);
fclose(fnew);
_mode = fmode_read;
return file();
}
// Check the original file for errors
if (ferror(_handle)) {
operator delete(buffer, page_size, static_cast<align_t>(page_size));
_error = strerror(errno);
fclose(fnew);
fclose(_handle);
_handle = nullptr;
_mode = 0;
_path = "";
return file();
}
// Cleanup the buffer
operator delete(buffer, page_size, static_cast<align_t>(page_size));
// Reopen the new file
_handle = freopen(nullptr, fmode_translate(_mode), _handle);
// Check for open failure
if (_handle == nullptr) {
_error = strerror(errno);
return file();
}
// Set the new path
file res;
res._handle = fnew;
res._path = fpath;
res._mode = fmode_write;
return res;
}
file file::copy(const path& p) {
static const size_t page_size = pagesize();
assert(_error == nullptr, "Attempted an Operation on a File in an Errored State");
// Check if there is a file
if (_handle == nullptr) {
return file();
}
// Validate path
path fpath = p.absolute();
if (_path == fpath) {
return file();
}
// Attempt to open the new file
FILE* fnew = fopen(fpath.str(), "wx");
// Check for open failure
if (fnew == nullptr) {

View File

@@ -0,0 +1,57 @@
// =====================================================================================================================
// 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/>.
// =====================================================================================================================
#include <fennec/fproc/filesystem/path.h>
namespace fennec
{
#ifdef _MSC_VER
path path::current() {
char cstr[MAX_PATH];
if (GetCurrentDirectory(sizeof(str), str) == 0) {
return string("");
}
return path(cstr);
}
#else
#include <unistd.h>
#include <linux/limits.h>
path path::current() {
char cstr[PATH_MAX];
if (getcwd(cstr, sizeof(cstr)) == nullptr) {
return string("");
}
return path(cstring(cstr, strlen(cstr) + 1));
}
path path::current(const path& path) {
if (chdir(path._str)) {
return fennec::path("");
}
return current();
}
#endif
}

View File

@@ -1,267 +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/>.
// =====================================================================================================================
#include <fennec/fproc/io/common.h>
#ifdef _WIN32
namespace fennec
{
string getcwd() {
char cstr[MAX_PATH];
if (GetCurrentDirectory(sizeof(str), str) == 0) {
return string("");
}
string result(cstr);
return result;
}
}
#else
#include <unistd.h>
#include <linux/limits.h>
namespace fennec
{
string getcwd() {
char cstr[PATH_MAX];
if (::getcwd(cstr, sizeof(cstr)) == NULL) {
return string("");
}
return string(cstr, strlen(cstr));
}
string absolute(const cstring& path) {
// first determine if this is already an absolute path
#ifdef _WIN32
if (path[1] == ':') {
return path;
}
#else
if (path[0] == '/') {
return path;
}
#endif
string parse = path;
string res = getcwd();
size_t len = res.length();
#ifdef _WIN32
if (res[len - 1] == '/' || res[len - 1] == '\\') {
#else
if (res[len - 1] == '/') {
#endif
res = res.substring(0, len);
}
while (parse.size() > 1) {
// Parse for relative symbols
while (parse[0] == '.') {
#ifdef _WIN32
// up
if (parse[1] == '.' && (parse[2] == '/' || parse[2] == '\\')) {
size_t r = res.rfind('/');
size_t l = res.rfind('\\');
size_t size = res.size();
if (r == size) {
size = l;
}
else if (l == size) {
size = r;
}
else {
size = max(r, l);
}
size = max(size, 1);
res = res.substring(0, size);
}
// rel
else if (parse[1] == '/' || parse[1] == '\\') {
parse = parse.substring(2);
}
#else
// up
if (parse[1] == '.' && parse[2] == '/') {
size_t r = res.rfind('/');
res = res.substring(0, max(res.rfind('/'), static_cast<size_t>(1)));
parse = parse.substring(2);
}
// rel
else if (parse[1] == '/') {
parse = parse.substring(1);
}
#endif
else {
break;
}
}
// get the pos of the first directory separator
#ifdef _WIN32
size_t pos = min(parse.find('\\', 1), parse.find('/', 1));
#else
size_t pos = parse.find('/', 1);
size_t off = 0;
#endif
if (pos != parse.size()) {
if (parse[pos + 1] == '.') {
if (parse[pos + 2] == '/') {
off = 1;
}
else if (parse[pos + 2] == '.' && parse[pos + 3] == '/') {
off = 1;
}
}
else if (parse[pos + 1] == '/') {
off = 1;
}
}
// add this part
string sub = parse.substring(0, pos);
parse = parse.substring(pos + off);
res += sub;
}
return res;
}
string absolute(const string& path) {
// first determine if this is already an absolute path
#ifdef _WIN32
if (path[1] == ':') {
return path;
}
#else
if (path[0] == '/') {
return path;
}
#endif
string parse = path;
string res = getcwd();
size_t len = res.length();
#ifdef _WIN32
if (res[len - 1] == '/' || res[len - 1] == '\\') {
#else
if (res[len - 1] == '/') {
#endif
res = res.substring(0, len);
}
while (parse.size() > 1) {
// Parse for relative symbols
while (parse[0] == '.') {
#ifdef _WIN32
// up
if (parse[1] == '.' && (parse[2] == '/' || parse[2] == '\\')) {
size_t r = res.rfind('/');
size_t l = res.rfind('\\');
size_t size = res.size();
if (r == size) {
size = l;
}
else if (l == size) {
size = r;
}
else {
size = max(r, l);
}
size = max(size, 1);
res = res.substring(0, size);
}
// rel
else if (parse[1] == '/' || parse[1] == '\\') {
parse = parse.substring(2);
}
#else
// up
if (parse[1] == '.' && parse[2] == '/') {
size_t r = res.rfind('/');
res = res.substring(0, max(res.rfind('/'), static_cast<size_t>(1)));
parse = parse.substring(2);
}
// rel
else if (parse[1] == '/') {
parse = parse.substring(1);
}
#endif
else {
break;
}
}
// get the pos of the first directory separator
#ifdef _WIN32
size_t pos = min(parse.find('\\', 1), parse.find('/', 1));
size_t off = 0;
if (pos != parse.size()) {
if (parse[pos + 1] == '.') {
if (parse[pos + 2] == '/' || parse[pos + 2] == '\\') {
off = 1;
}
else if (parse[pos + 2] == '.' && (parse[pos + 3] == '/' || parse[pos + 3] == '\\')) {
off = 1;
}
}
else if (parse[pos + 1] == '/' || parse[pos + 1] == '\\') {
off = 1;
}
}
#else
size_t pos = parse.find('/', 1);
size_t off = 0;
if (pos != parse.size()) {
if (parse[pos + 1] == '.') {
if (parse[pos + 2] == '/') {
off = 1;
}
else if (parse[pos + 2] == '.' && parse[pos + 3] == '/') {
off = 1;
}
}
else if (parse[pos + 1] == '/') {
off = 1;
}
}
#endif
// add this part
string sub = parse.substring(0, pos);
parse = parse.substring(pos + off);
res += sub;
}
return res;
}
#endif
}

View File

@@ -29,7 +29,7 @@
#else
// Windows does not define ISO C aligned allocation functions
#ifdef _WIN32
#ifdef _MSC_VER
void operator delete (void* ptr) noexcept { _aligned_free(ptr); }
void operator delete[](void* ptr) noexcept { _aligned_free(ptr); }
void operator delete (void* ptr, fennec::align_t) noexcept { ::_aligned_free(ptr); }