From ca07dc61529276446477135534ebee84da5e1e1c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 31 Dec 2022 14:18:38 -0600 Subject: [PATCH] [ox] Replace C strings in FS with StringView --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 21 ----- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 35 +++++++-- deps/ox/src/ox/fs/filesystem/filesystem.cpp | 23 +++--- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 76 +++++++++++-------- .../ox/src/ox/fs/filesystem/passthroughfs.cpp | 33 ++++---- .../ox/src/ox/fs/filesystem/passthroughfs.hpp | 27 ++++--- deps/ox/src/ox/fs/filesystem/pathiterator.cpp | 3 + deps/ox/src/ox/fs/filesystem/pathiterator.hpp | 2 + deps/ox/src/ox/fs/tool.cpp | 2 +- deps/ox/src/ox/mc/read.hpp | 3 +- deps/ox/src/ox/model/fieldcounter.hpp | 5 +- deps/ox/src/ox/std/assert.cpp | 2 + deps/ox/src/ox/std/assert.hpp | 12 +-- deps/ox/src/ox/std/stringview.hpp | 5 ++ deps/ox/src/ox/std/strops.hpp | 19 ++--- deps/ox/src/ox/std/tracehook.cpp | 8 +- 16 files changed, 148 insertions(+), 128 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 3b6cb223..b12cfd96 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -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; -} - } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index 947a6236..d2ba6a57 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -57,17 +57,17 @@ class FileAddress { FileAddress(uint64_t inode) noexcept; - FileAddress(CRStringView path) noexcept; + explicit FileAddress(CRStringView path) noexcept; template - FileAddress(const ox::BasicString &path) noexcept: FileAddress(StringView(path)) { + explicit FileAddress(const ox::BasicString &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() noexcept { return FileAddress::Data::TypeName; @@ -163,4 +172,16 @@ constexpr Error model(T *io, CommonPtrWith 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; +} + } diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.cpp b/deps/ox/src/ox/fs/filesystem/filesystem.cpp index 7261a8bb..5c52f107 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.cpp @@ -19,7 +19,7 @@ Result 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 FileSystem::read(const FileAddress &addr) noexcept { return buff; } +Result 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> 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 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); } diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index f628b73a..c22b61c9 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -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 directAccess(const char *path) noexcept = 0; + virtual Result 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 read(const FileAddress &addr) noexcept; + Result 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 directAccess(const FileAddress &addr) noexcept; - Result> ls(const String &dir) const noexcept; + virtual Result> ls(CRStringView dir) const noexcept = 0; - virtual Result> 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 stat(uint64_t inode) const noexcept = 0; - virtual Result stat(const char *path) const noexcept = 0; + virtual Result stat(CRStringView path) const noexcept = 0; Result stat(const FileAddress &addr) const noexcept; [[nodiscard]] virtual uint64_t spaceNeeded(uint64_t size) const noexcept = 0; + [[nodiscard]] virtual Result available() const noexcept = 0; + [[nodiscard]] virtual Result 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 directAccess(const char*) noexcept override; + Result 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 directAccess(uint64_t) noexcept override; - Result> ls(const char *path) const noexcept override; + Result> ls(CRStringView dir) const noexcept override; template - 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 stat(uint64_t inode) const noexcept override; - Result stat(const char *path) const noexcept override; + Result 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 find(const char *path) const noexcept; + Result find(CRStringView path) const noexcept; Result rootDir() const noexcept; @@ -227,14 +237,14 @@ Error FileSystemTemplate::format(void *buff, uint64_t buff } template -Error FileSystemTemplate::mkdir(const char *path, bool recursive) noexcept { +Error FileSystemTemplate::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 -Error FileSystemTemplate::move(const char *src, const char *dest) noexcept { +Error FileSystemTemplate::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::move(const char *src, const char } template -Error FileSystemTemplate::read(const char *path, void *buffer, std::size_t buffSize) noexcept { +Error FileSystemTemplate::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::read(const char *path, void *buf } template -Result FileSystemTemplate::directAccess(const char *path) noexcept { +Result FileSystemTemplate::directAccess(CRStringView path) noexcept { oxRequire(fd, fileSystemData()); Directory rootDir(m_fs, fd.rootDirInode); oxRequire(inode, rootDir.find(path)); @@ -273,15 +283,15 @@ template Result FileSystemTemplate::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(data.get()); } template -Result> FileSystemTemplate::ls(const char *path) const noexcept { +Result> FileSystemTemplate::ls(CRStringView path) const noexcept { Vector 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> FileSystemTemplate::ls(const char * template template -Error FileSystemTemplate::ls(const char *path, F cb) const { +Error FileSystemTemplate::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::ls(const char *path, F cb) const } template -Error FileSystemTemplate::remove(const char *path, bool recursive) noexcept { +Error FileSystemTemplate::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::resize(uint64_t size, void *buff } template -Error FileSystemTemplate::write(const char *path, const void *buffer, uint64_t size, FileType fileType) noexcept { +Error FileSystemTemplate::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 FileSystemTemplate::stat(uint64_t inode) } template -Result FileSystemTemplate::stat(const char *path) const noexcept { +Result FileSystemTemplate::stat(CRStringView path) const noexcept { oxRequire(inode, find(path)); return stat(inode); } @@ -400,10 +410,10 @@ Result::FileSystemData> FileSy } template -Result FileSystemTemplate::find(const char *path) const noexcept { +Result FileSystemTemplate::find(CRStringView path) const noexcept { oxRequire(fd, fileSystemData()); // return root as a special case - if (ox_strcmp(path, "/") == 0) { + if (path == "/") { return static_cast(fd.rootDirInode); } Directory rootDir(m_fs, fd.rootDirInode); diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index 5dafc2f9..caa3d515 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -13,21 +13,21 @@ #if defined(OX_HAS_PASSTHROUGHFS) #include +#include 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(buffer), buffSize); + file.read(static_cast(buffer), static_cast(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 PassThroughFS::directAccess(const char*) noexcept { +Result PassThroughFS::directAccess(CRStringView) noexcept { return OxError(1, "PassThroughFS::directAccess not supported"); } @@ -99,7 +98,7 @@ Result PassThroughFS::directAccess(uint64_t) noexcept { return OxError(1, "PassThroughFS::directAccess not supported"); } -Result> PassThroughFS::ls(const char *dir) const noexcept { +Result> PassThroughFS::ls(CRStringView dir) const noexcept { Vector out; std::error_code ec; const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec); @@ -111,7 +110,7 @@ Result> 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 PassThroughFS::stat(uint64_t) const noexcept { return OxError(1, "stat(uint64_t) is not supported by PassThroughFS"); } -Result PassThroughFS::stat(const char *path) const noexcept { +Result 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()); } } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 7e2ca50a..16146b8f 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -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 directAccess(const char*) noexcept override; + Result 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 directAccess(uint64_t) noexcept override; - Result> ls(const char *dir) const noexcept override; + Result> ls(CRStringView dir) const noexcept override; template - 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 stat(uint64_t inode) const noexcept override; - Result stat(const char *path) const noexcept override; + Result stat(CRStringView path) const noexcept override; + [[nodiscard]] uint64_t spaceNeeded(uint64_t size) const noexcept override; Result available() const noexcept override; Result 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 -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")); diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp index 4b6f21fc..e3b5a39d 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp @@ -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 */ diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.hpp b/deps/ox/src/ox/fs/filesystem/pathiterator.hpp index a12e021f..353e7089 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.hpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.hpp @@ -26,6 +26,8 @@ class PathIterator { PathIterator(const char *path); + PathIterator(CRStringView path); + /** * @return 0 if no error */ diff --git a/deps/ox/src/ox/fs/tool.cpp b/deps/ox/src/ox/fs/tool.cpp index da4a5438..108f1d00 100644 --- a/deps/ox/src/ox/fs/tool.cpp +++ b/deps/ox/src/ox/fs/tool.cpp @@ -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); } diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index d7965642..9174c62b 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -331,8 +331,7 @@ constexpr Error MetalClawReaderTemplate::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 = ""; diff --git a/deps/ox/src/ox/model/fieldcounter.hpp b/deps/ox/src/ox/model/fieldcounter.hpp index 38142cdd..c5a30030 100644 --- a/deps/ox/src/ox/model/fieldcounter.hpp +++ b/deps/ox/src/ox/model/fieldcounter.hpp @@ -59,12 +59,13 @@ class FieldCounter { namespace detail { template -consteval auto modelFieldCount() { +[[nodiscard]] +consteval auto modelFieldCount() noexcept { auto a = std::allocator(); auto t = a.allocate(1); detail::FieldCounter c; const auto err = model(&c, t); - //oxAssert(err, "Count failed"); + oxAssert(err, "Count failed"); a.deallocate(t, 1); return c.fields; } diff --git a/deps/ox/src/ox/std/assert.cpp b/deps/ox/src/ox/std/assert.cpp index b49d6e16..2651a83a 100644 --- a/deps/ox/src/ox/std/assert.cpp +++ b/deps/ox/src/ox/std/assert.cpp @@ -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 } diff --git a/deps/ox/src/ox/std/assert.hpp b/deps/ox/src/ox/std/assert.hpp index b5abb015..a9fcfff1 100644 --- a/deps/ox/src/ox/std/assert.hpp +++ b/deps/ox/src/ox/std/assert.hpp @@ -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); diff --git a/deps/ox/src/ox/std/stringview.hpp b/deps/ox/src/ox/std/stringview.hpp index 72e2b5f1..2c819b50 100644 --- a/deps/ox/src/ox/std/stringview.hpp +++ b/deps/ox/src/ox/std/stringview.hpp @@ -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")); diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index 5dfd91f5..c9148000 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -15,9 +15,10 @@ template constexpr char *ox_strcpy(T1 dest, T2 src) noexcept { + using T1Type = typename ox::remove_reference::type; std::size_t i = 0; while (src[i]) { - dest[i] = src[i]; + dest[i] = static_cast(src[i]); ++i; } // set null terminator @@ -27,9 +28,10 @@ constexpr char *ox_strcpy(T1 dest, T2 src) noexcept { template constexpr char *ox_strncpy(T1 dest, T2 src, std::size_t maxLen) noexcept { + using T1Type = typename ox::remove_reference::type; std::size_t i = 0; while (i < maxLen && src[i]) { - dest[i] = src[i]; + dest[i] = static_cast(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(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) { diff --git a/deps/ox/src/ox/std/tracehook.cpp b/deps/ox/src/ox/std/tracehook.cpp index 749b61e5..0fe550ed 100644 --- a/deps/ox/src/ox/std/tracehook.cpp +++ b/deps/ox/src/ox/std/tracehook.cpp @@ -43,7 +43,7 @@ void initConsole() { if (REG_MGBA_DEBUG_ENABLE == 0x1DEA) { infoLog = log; debugLog = log; // use INFO because mGBA disables DEBUG on start - errorLog = log; + errorLog = log; } } @@ -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'; }