[ox] Replace C strings in FS with StringView

This commit is contained in:
Gary Talent 2022-12-31 14:18:38 -06:00
parent 5cae7cbd24
commit ca07dc6152
16 changed files with 148 additions and 128 deletions

View File

@ -43,15 +43,6 @@ FileAddress::FileAddress(char *path) noexcept {
m_type = FileAddressType::Path;
}
FileAddress::FileAddress(const char *path) noexcept {
m_data.constPath = path;
m_type = FileAddressType::ConstPath;
}
FileAddress::~FileAddress() noexcept {
cleanup();
}
FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
if (this == &other) {
return *this;
@ -107,16 +98,4 @@ bool FileAddress::operator==(CRStringView path) const noexcept {
return path == p;
}
void FileAddress::cleanup() noexcept {
if (m_type == FileAddressType::Path) {
safeDeleteArray(m_data.path);
clear();
}
}
void FileAddress::clear() noexcept {
m_data.path = nullptr;
m_type = FileAddressType::None;
}
}

View File

@ -57,17 +57,17 @@ class FileAddress {
FileAddress(uint64_t inode) noexcept;
FileAddress(CRStringView path) noexcept;
explicit FileAddress(CRStringView path) noexcept;
template<std::size_t SmallStrSz>
FileAddress(const ox::BasicString<SmallStrSz> &path) noexcept: FileAddress(StringView(path)) {
explicit FileAddress(const ox::BasicString<SmallStrSz> &path) noexcept: FileAddress(StringView(path)) {
}
FileAddress(char *path) noexcept;
explicit FileAddress(char *path) noexcept;
FileAddress(const char *path) noexcept;
explicit constexpr FileAddress(const char *path) noexcept;
~FileAddress() noexcept;
constexpr ~FileAddress() noexcept;
FileAddress &operator=(const FileAddress &other) noexcept;
@ -115,15 +115,24 @@ class FileAddress {
/**
* Cleanup memory allocations.
*/
void cleanup() noexcept;
constexpr void cleanup() noexcept;
/**
* Clears fields, but does not delete allocations.
*/
void clear() noexcept;
constexpr void clear() noexcept;
};
constexpr FileAddress::FileAddress(const char *path) noexcept {
m_data.constPath = path;
m_type = FileAddressType::ConstPath;
}
constexpr FileAddress::~FileAddress() noexcept {
cleanup();
}
template<>
constexpr const char *getModelTypeName<FileAddress::Data>() noexcept {
return FileAddress::Data::TypeName;
@ -163,4 +172,16 @@ constexpr Error model(T *io, CommonPtrWith<FileAddress> auto *fa) noexcept {
return OxError(0);
}
constexpr void FileAddress::cleanup() noexcept {
if (m_type == FileAddressType::Path) {
safeDeleteArray(m_data.path);
clear();
}
}
constexpr void FileAddress::clear() noexcept {
m_data.path = nullptr;
m_type = FileAddressType::None;
}
}

View File

@ -19,7 +19,7 @@ Result<const char*> FileSystem::directAccess(const FileAddress &addr) noexcept {
return directAccess(addr.getInode().value);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return directAccess(addr.getPath().value);
return directAccess(StringView(addr.getPath().value));
default:
return OxError(1);
}
@ -31,7 +31,7 @@ Error FileSystem::read(const FileAddress &addr, void *buffer, std::size_t size)
return read(addr.getInode().value, buffer, size);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return read(addr.getPath().value, buffer, size);
return read(StringView(addr.getPath().value), buffer, size);
default:
return OxError(1);
}
@ -44,29 +44,32 @@ Result<Buffer> FileSystem::read(const FileAddress &addr) noexcept {
return buff;
}
Result<Buffer> FileSystem::read(CRStringView path) noexcept {
oxRequire(s, stat(path));
Buffer buff(s.size);
oxReturnError(read(path, buff.data(), buff.size()));
return buff;
}
Error FileSystem::read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return read(addr.getInode().value, readStart, readSize, buffer, size);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return read(addr.getPath().value, readStart, readSize, buffer, size);
return OxError(2, "Unsupported for path lookups");
default:
return OxError(1);
}
}
Result<Vector<String>> FileSystem::ls(const String &dir) const noexcept {
return ls(dir.c_str());
}
Error FileSystem::remove(const FileAddress &addr, bool recursive) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return remove(addr.getInode().value, recursive);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return remove(addr.getPath().value, recursive);
return remove(StringView(addr.getPath().value), recursive);
default:
return OxError(1);
}
@ -78,7 +81,7 @@ Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t si
return write(addr.getInode().value, buffer, size, fileType);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return write(addr.getPath().value, buffer, size, fileType);
return write(StringView(addr.getPath().value), buffer, size, fileType);
default:
return OxError(1);
}
@ -90,7 +93,7 @@ Result<FileStat> FileSystem::stat(const FileAddress &addr) const noexcept {
return stat(addr.getInode().value);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return stat(addr.getPath().value);
return stat(StringView(addr.getPath().value));
default:
return OxError(1);
}

View File

@ -29,18 +29,18 @@ class FileSystem {
public:
virtual ~FileSystem() noexcept = default;
virtual Error mkdir(const char *path, bool recursive) noexcept = 0;
virtual Error mkdir(CRStringView path, bool recursive) noexcept = 0;
/**
* Moves an entry from one directory to another.
* @param src the path to the file
* @param dest the path of the destination directory
*/
virtual Error move(const char *src, const char *dest) noexcept = 0;
virtual Error move(CRStringView src, CRStringView dest) noexcept = 0;
virtual Error read(const char *path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Result<const char*> directAccess(const char *path) noexcept = 0;
virtual Result<const char*> directAccess(CRStringView path) noexcept = 0;
virtual Error read(uint64_t inode, void *buffer, std::size_t size) noexcept = 0;
@ -52,38 +52,48 @@ class FileSystem {
Result<Buffer> read(const FileAddress &addr) noexcept;
Result<Buffer> read(CRStringView path) noexcept;
Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept;
[[maybe_unused]]
Result<const char*> directAccess(const FileAddress &addr) noexcept;
Result<Vector<String>> ls(const String &dir) const noexcept;
virtual Result<Vector<String>> ls(CRStringView dir) const noexcept = 0;
virtual Result<Vector<String>> ls(const char *dir) const noexcept = 0;
virtual Error remove(const char *path, bool recursive) noexcept = 0;
virtual Error remove(CRStringView path, bool recursive) noexcept = 0;
Error remove(const FileAddress &addr, bool recursive = false) noexcept;
virtual Error resize(uint64_t size, void *buffer) noexcept = 0;
virtual Error write(const char *path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
Error write(CRStringView path, const void *buffer, uint64_t size) noexcept {
return write(path, buffer, size, FileType::NormalFile);
}
Error write(uint64_t inode, const void *buffer, uint64_t size) noexcept {
return write(inode, buffer, size, FileType::NormalFile);
}
Error write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept;
virtual Result<FileStat> stat(uint64_t inode) const noexcept = 0;
virtual Result<FileStat> stat(const char *path) const noexcept = 0;
virtual Result<FileStat> stat(CRStringView path) const noexcept = 0;
Result<FileStat> stat(const FileAddress &addr) const noexcept;
[[nodiscard]]
virtual uint64_t spaceNeeded(uint64_t size) const noexcept = 0;
[[nodiscard]]
virtual Result<uint64_t> available() const noexcept = 0;
[[nodiscard]]
virtual Result<uint64_t> size() const noexcept = 0;
[[nodiscard]]
@ -125,13 +135,13 @@ class FileSystemTemplate: public FileSystem {
static Error format(void *buff, uint64_t buffSize) noexcept;
Error mkdir(const char *path, bool recursive) noexcept override;
Error mkdir(CRStringView path, bool recursive) noexcept override;
Error move(const char *src, const char *dest) noexcept override;
Error move(CRStringView src, CRStringView dest) noexcept override;
Error read(const char *path, void *buffer, std::size_t buffSize) noexcept override;
Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Result<const char*> directAccess(const char*) noexcept override;
Result<const char*> directAccess(CRStringView) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
@ -139,12 +149,12 @@ class FileSystemTemplate: public FileSystem {
Result<const char*> directAccess(uint64_t) noexcept override;
Result<Vector<String>> ls(const char *path) const noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
template<typename F>
Error ls(const char *path, F cb) const;
Error ls(CRStringView path, F cb) const;
Error remove(const char *path, bool recursive) noexcept override;
Error remove(CRStringView path, bool recursive) noexcept override;
/**
* Resizes FileSystem to minimum possible size.
@ -153,13 +163,13 @@ class FileSystemTemplate: public FileSystem {
Error resize(uint64_t size, void *buffer) noexcept override;
Error write(const char *path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Result<FileStat> stat(uint64_t inode) const noexcept override;
Result<FileStat> stat(const char *path) const noexcept override;
Result<FileStat> stat(CRStringView path) const noexcept override;
uint64_t spaceNeeded(uint64_t size) const noexcept override;
@ -180,7 +190,7 @@ class FileSystemTemplate: public FileSystem {
/**
* Finds the inode ID at the given path.
*/
Result<uint64_t> find(const char *path) const noexcept;
Result<uint64_t> find(CRStringView path) const noexcept;
Result<Directory> rootDir() const noexcept;
@ -227,14 +237,14 @@ Error FileSystemTemplate<FileStore, Directory>::format(void *buff, uint64_t buff
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::mkdir(const char *path, bool recursive) noexcept {
Error FileSystemTemplate<FileStore, Directory>::mkdir(CRStringView path, bool recursive) noexcept {
oxTracef("ox::fs::FileSystemTemplate::mkdir", "path: {}, recursive: {}", path, recursive);
oxRequireM(rootDir, this->rootDir());
return rootDir.mkdir(path, recursive);
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::move(const char *src, const char *dest) noexcept {
Error FileSystemTemplate<FileStore, Directory>::move(CRStringView src, CRStringView dest) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequireM(inode, rootDir.find(src));
@ -244,7 +254,7 @@ Error FileSystemTemplate<FileStore, Directory>::move(const char *src, const char
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::read(const char *path, void *buffer, std::size_t buffSize) noexcept {
Error FileSystemTemplate<FileStore, Directory>::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
@ -252,7 +262,7 @@ Error FileSystemTemplate<FileStore, Directory>::read(const char *path, void *buf
}
template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(const char *path) noexcept {
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(CRStringView path) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
@ -273,15 +283,15 @@ template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(uint64_t inode) noexcept {
auto data = m_fs.read(inode);
if (!data.valid()) {
return OxError(1);
return OxError(1, "Data not valid");
}
return reinterpret_cast<char*>(data.get());
}
template<typename FileStore, typename Directory>
Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(const char *path) const noexcept {
Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(CRStringView path) const noexcept {
Vector<String> out;
oxReturnError(ls(path, [&out](const char *name, typename FileStore::InodeId_t) {
oxReturnError(ls(path, [&out](CRStringView name, typename FileStore::InodeId_t) {
out.emplace_back(name);
return OxError(0);
}));
@ -290,7 +300,7 @@ Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(const char *
template<typename FileStore, typename Directory>
template<typename F>
Error FileSystemTemplate<FileStore, Directory>::ls(const char *path, F cb) const {
Error FileSystemTemplate<FileStore, Directory>::ls(CRStringView path, F cb) const {
oxTracef("ox::fs::FileSystemTemplate::ls", "path: {}", path);
oxRequire(s, stat(path));
Directory dir(m_fs, s.inode);
@ -298,7 +308,7 @@ Error FileSystemTemplate<FileStore, Directory>::ls(const char *path, F cb) const
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::remove(const char *path, bool recursive) noexcept {
Error FileSystemTemplate<FileStore, Directory>::remove(CRStringView path, bool recursive) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
@ -328,7 +338,7 @@ Error FileSystemTemplate<FileStore, Directory>::resize(uint64_t size, void *buff
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::write(const char *path, const void *buffer, uint64_t size, FileType fileType) noexcept {
Error FileSystemTemplate<FileStore, Directory>::write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
auto [inode, err] = find(path);
if (err) {
oxRequire(generatedId, m_fs.generateInodeId());
@ -357,7 +367,7 @@ Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(uint64_t inode)
}
template<typename FileStore, typename Directory>
Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(const char *path) const noexcept {
Result<FileStat> FileSystemTemplate<FileStore, Directory>::stat(CRStringView path) const noexcept {
oxRequire(inode, find(path));
return stat(inode);
}
@ -400,10 +410,10 @@ Result<typename FileSystemTemplate<FileStore, Directory>::FileSystemData> FileSy
}
template<typename FileStore, typename Directory>
Result<uint64_t> FileSystemTemplate<FileStore, Directory>::find(const char *path) const noexcept {
Result<uint64_t> FileSystemTemplate<FileStore, Directory>::find(CRStringView path) const noexcept {
oxRequire(fd, fileSystemData());
// return root as a special case
if (ox_strcmp(path, "/") == 0) {
if (path == "/") {
return static_cast<uint64_t>(fd.rootDirInode);
}
Directory rootDir(m_fs, fd.rootDirInode);

View File

@ -13,21 +13,21 @@
#if defined(OX_HAS_PASSTHROUGHFS)
#include <fstream>
#include <string_view>
namespace ox {
PassThroughFS::PassThroughFS(const char *dirPath) {
m_path = dirPath;
PassThroughFS::PassThroughFS(CRStringView dirPath) {
m_path = std::string_view(dirPath.data(), dirPath.bytes());
}
PassThroughFS::~PassThroughFS() noexcept {
}
PassThroughFS::~PassThroughFS() noexcept = default;
String PassThroughFS::basePath() const noexcept {
return m_path.string().c_str();
}
Error PassThroughFS::mkdir(const char *path, bool recursive) noexcept {
Error PassThroughFS::mkdir(CRStringView path, bool recursive) noexcept {
bool success = false;
const auto p = m_path / stripSlash(path);
const auto u8p = p.u8string();
@ -47,7 +47,6 @@ Error PassThroughFS::mkdir(const char *path, bool recursive) noexcept {
if (isDir) {
success = true;
} else {
std::error_code ec;
success = std::filesystem::create_directory(p, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed"));
}
@ -55,7 +54,7 @@ Error PassThroughFS::mkdir(const char *path, bool recursive) noexcept {
return OxError(success ? 0 : 1);
}
Error PassThroughFS::move(const char *src, const char *dest) noexcept {
Error PassThroughFS::move(CRStringView src, CRStringView dest) noexcept {
std::error_code ec;
std::filesystem::rename(m_path / stripSlash(src), m_path / stripSlash(dest), ec);
if (ec.value()) {
@ -64,7 +63,7 @@ Error PassThroughFS::move(const char *src, const char *dest) noexcept {
return OxError(0);
}
Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) noexcept {
Error PassThroughFS::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
try {
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
const std::size_t size = file.tellg();
@ -73,7 +72,7 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize)
oxTracef("ox::fs::PassThroughFS::read::error", "Read failed: Buffer too small: {}", path);
return OxError(1);
}
file.read(static_cast<char*>(buffer), buffSize);
file.read(static_cast<char*>(buffer), static_cast<std::streamsize>(buffSize));
} catch (const std::fstream::failure &f) {
oxTracef("ox::fs::PassThroughFS::read::error", "Read of {} failed: {}", path, f.what());
return OxError(2);
@ -81,7 +80,7 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize)
return OxError(0);
}
Result<const char*> PassThroughFS::directAccess(const char*) noexcept {
Result<const char*> PassThroughFS::directAccess(CRStringView) noexcept {
return OxError(1, "PassThroughFS::directAccess not supported");
}
@ -99,7 +98,7 @@ Result<const char*> PassThroughFS::directAccess(uint64_t) noexcept {
return OxError(1, "PassThroughFS::directAccess not supported");
}
Result<Vector<String>> PassThroughFS::ls(const char *dir) const noexcept {
Result<Vector<String>> PassThroughFS::ls(CRStringView dir) const noexcept {
Vector<String> out;
std::error_code ec;
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
@ -111,7 +110,7 @@ Result<Vector<String>> PassThroughFS::ls(const char *dir) const noexcept {
return out;
}
Error PassThroughFS::remove(const char *path, bool recursive) noexcept {
Error PassThroughFS::remove(CRStringView path, bool recursive) noexcept {
if (recursive) {
return OxError(std::filesystem::remove_all(m_path / stripSlash(path)) != 0);
} else {
@ -124,7 +123,7 @@ Error PassThroughFS::resize(uint64_t, void*) noexcept {
return OxError(1, "resize is not supported by PassThroughFS");
}
Error PassThroughFS::write(const char *path, const void *buffer, uint64_t size, FileType) noexcept {
Error PassThroughFS::write(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept {
const auto p = (m_path / stripSlash(path));
try {
std::ofstream f(p, std::ios::binary);
@ -146,7 +145,7 @@ Result<FileStat> PassThroughFS::stat(uint64_t) const noexcept {
return OxError(1, "stat(uint64_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::stat(const char *path) const noexcept {
Result<FileStat> PassThroughFS::stat(CRStringView path) const noexcept {
std::error_code ec;
const auto p = m_path / stripSlash(path);
const FileType type = std::filesystem::is_directory(p, ec) ?
@ -194,12 +193,12 @@ bool PassThroughFS::valid() const noexcept {
return false;
}
const char *PassThroughFS::stripSlash(const char *path) noexcept {
std::string_view PassThroughFS::stripSlash(StringView path) noexcept {
const auto pathLen = ox_strlen(path);
for (auto i = 0u; i < pathLen && path[0] == '/'; i++) {
path++;
path = path.substr(1);
}
return path;
return std::string_view(path.data(), path.bytes());
}
}

View File

@ -30,20 +30,20 @@ class PassThroughFS: public FileSystem {
std::filesystem::path m_path;
public:
explicit PassThroughFS(const char *dirPath);
explicit PassThroughFS(CRStringView dirPath);
~PassThroughFS() noexcept override;
[[nodiscard]]
String basePath() const noexcept;
Error mkdir(const char *path, bool recursive = false) noexcept override;
Error mkdir(CRStringView path, bool recursive = false) noexcept override;
Error move(const char *src, const char *dest) noexcept override;
Error move(CRStringView src, CRStringView dest) noexcept override;
Error read(const char *path, void *buffer, std::size_t buffSize) noexcept override;
Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Result<const char*> directAccess(const char*) noexcept override;
Result<const char*> directAccess(CRStringView) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
@ -51,29 +51,31 @@ class PassThroughFS: public FileSystem {
Result<const char*> directAccess(uint64_t) noexcept override;
Result<Vector<String>> ls(const char *dir) const noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
template<typename F>
Error ls(const char *dir, F cb) const noexcept;
Error ls(CRStringView dir, F cb) const noexcept;
Error remove(const char *path, bool recursive = false) noexcept override;
Error remove(CRStringView path, bool recursive = false) noexcept override;
Error resize(uint64_t size, void *buffer = nullptr) noexcept override;
Error write(const char *path, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override;
Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override;
Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override;
Result<FileStat> stat(uint64_t inode) const noexcept override;
Result<FileStat> stat(const char *path) const noexcept override;
Result<FileStat> stat(CRStringView path) const noexcept override;
[[nodiscard]]
uint64_t spaceNeeded(uint64_t size) const noexcept override;
Result<uint64_t> available() const noexcept override;
Result<uint64_t> size() const noexcept override;
[[nodiscard]]
char *buff() noexcept override;
Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) noexcept override;
@ -85,12 +87,13 @@ class PassThroughFS: public FileSystem {
/**
* Strips the leading slashes from a string.
*/
static const char *stripSlash(const char *path) noexcept;
[[nodiscard]]
static std::string_view stripSlash(StringView path) noexcept;
};
template<typename F>
Error PassThroughFS::ls(const char *dir, F cb) const noexcept {
Error PassThroughFS::ls(CRStringView dir, F cb) const noexcept {
std::error_code ec;
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed"));

View File

@ -22,6 +22,9 @@ PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t it
PathIterator::PathIterator(const char *path): PathIterator(path, ox_strlen(path)) {
}
PathIterator::PathIterator(CRStringView path): PathIterator(path.data(), path.bytes()) {
}
/**
* @return 0 if no error
*/

View File

@ -26,6 +26,8 @@ class PathIterator {
PathIterator(const char *path);
PathIterator(CRStringView path);
/**
* @return 0 if no error
*/

View File

@ -56,7 +56,7 @@ static ox::Error runRead(ox::FileSystem *fs, int argc, const char **argv) noexce
oxErr("Must provide a path to a file to read\n");
return OxError(1);
}
oxRequire(buff, fs->read(argv[1]));
oxRequire(buff, fs->read(ox::StringView(argv[1])));
fwrite(buff.data(), sizeof(decltype(buff)::value_type), buff.size(), stdout);
return OxError(0);
}

View File

@ -331,8 +331,7 @@ constexpr Error MetalClawReaderTemplate<HandlerMaker>::field(const char*, BasicS
if (m_buffIt + size > m_buffLen) {
return OxError(MC_BUFFENDED);
}
memcpy(data, &m_buff[m_buffIt], size);
data[size] = 0;
ox_strncpy(data, &m_buff[m_buffIt], size);
m_buffIt += size;
} else {
*val = "";

View File

@ -59,12 +59,13 @@ class FieldCounter {
namespace detail {
template<typename T>
consteval auto modelFieldCount() {
[[nodiscard]]
consteval auto modelFieldCount() noexcept {
auto a = std::allocator<T>();
auto t = a.allocate(1);
detail::FieldCounter<T> c;
const auto err = model(&c, t);
//oxAssert(err, "Count failed");
oxAssert(err, "Count failed");
a.deallocate(t, 1);
return c.fields;
}

View File

@ -26,6 +26,8 @@ void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_u
printStackTrace(2);
oxTrace("panic").del("") << "Panic: " << panicMsg << " (" << file << ":" << line << ")";
std::abort();
#else
while (1);
#endif
}

View File

@ -36,12 +36,14 @@ constexpr void assertFunc(const char *file, int line, bool pass, [[maybe_unused]
if (!pass) {
if (!std::is_constant_evaluated()) {
#ifdef OX_USE_STDLIB
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
printStackTrace(2);
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
std::abort();
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
printStackTrace(2);
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
std::abort();
#else
constexprPanic(file, line, msg);
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
constexprPanic(file, line, msg);
#endif
} else {
while (true);

View File

@ -259,6 +259,11 @@ class StringView {
using CRStringView = const StringView&;
constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept {
const auto endingLen = ending.len();
return base.len() >= endingLen && ox_strcmp(base.data() + (base.len() - endingLen), ending) == 0;
}
static_assert(StringView("Read").bytes() == 4);
static_assert(StringView("Read") == StringView("Read"));
static_assert(StringView("Read") != StringView("Write"));

View File

@ -15,9 +15,10 @@
template<typename T1, typename T2>
constexpr char *ox_strcpy(T1 dest, T2 src) noexcept {
using T1Type = typename ox::remove_reference<decltype(dest[0])>::type;
std::size_t i = 0;
while (src[i]) {
dest[i] = src[i];
dest[i] = static_cast<T1Type>(src[i]);
++i;
}
// set null terminator
@ -27,9 +28,10 @@ constexpr char *ox_strcpy(T1 dest, T2 src) noexcept {
template<typename T1, typename T2>
constexpr char *ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept {
using T1Type = typename ox::remove_reference<decltype(dest[0])>::type;
std::size_t i = 0;
while (i < maxLen && src[i]) {
dest[i] = src[i];
dest[i] = static_cast<T1Type>(src[i]);
++i;
}
// set null terminator
@ -113,18 +115,7 @@ constexpr char *ox_strchr(char *str, int character, std::size_t maxLen = 0xFFFFF
}
[[nodiscard]]
constexpr int ox_lastIndexOf(const char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
int retval = -1;
for (std::size_t i = 0; i < maxLen && str[i]; i++) {
if (str[i] == character) {
retval = static_cast<int>(i);
}
}
return retval;
}
[[nodiscard]]
constexpr int ox_lastIndexOf(char *str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen = 0xFFFFFFFF) noexcept {
int retval = -1;
for (std::size_t i = 0; i < maxLen && str[i]; i++) {
if (str[i] == character) {

View File

@ -43,7 +43,7 @@ void initConsole() {
if (REG_MGBA_DEBUG_ENABLE == 0x1DEA) {
infoLog = log<LogChan::Info>;
debugLog = log<LogChan::Info>; // use INFO because mGBA disables DEBUG on start
errorLog = log<LogChan::Error>;
errorLog = log<LogChan::Info>;
}
}
@ -71,11 +71,11 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
std::cout << std::setw(65) << std::left << msg << '|';
std::cout << " " << file << ':' << line << "\n";
} else if (ox_strcmp(ch, "debug") == 0 || ox_strcmp(ch, "info") == 0) {
std::cout << msg << '\n';
printf("%s\n", msg);
} else if (ox_strcmp(ch, "stdout") == 0) {
std::cout << msg;
printf("%s", msg);
} else if (ox_strcmp(ch, "stderr") == 0) {
std::cerr << msg;
printf("%s", msg);
} else if (ox_strcmp(ch, "error") == 0) {
std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
}