Squashed 'deps/nostalgia/' changes from 5f10edd3..0daf938f

0daf938f [nostalgia/core/studio] Cleanup, make all number keys after num colors jump to last
b90ab27a [nostalgia/core/studio] Fix Palette Color Name input to properly take focus
c711f435 [nostalgia/core/studio] Fix PaletteEditor 0 key shortcut
84cb03d8 [nostalgia/core/studio] Cleanup
945a55f9 [studio] Fix Project to cut off correct end of OC data
2173b12c [nostalgia/core/studio] Give PaletteEditor keyboard shortcuts
aa970b1f [keel,studio] Cleanup
6ad79b30 [ox] Cleanup
a7cf2673 [studio] Remove null terminator from OC output
1a9f0d49 [ox] Rename CRString to StringCR
a1b5b565 [olympic,nostalgia] Rename CRStringView to StringViewCR
256be6da [glutils] Rename CRStringView to StringViewCR
cc10631b [ox] Rename CRStringView to StringViewCR
829dc029 [keel] Fix Linux build
e8a1ff06 [ox/oc] Fix Linux build
bdfb5e97 [nostalgia/core] Cleanup
396fecab [ox/oc] Add option for writeOC to return a string
5373b63c [keel,studio] Removing null terminator from JSON file output
8b655c40 [ox/std] Add HashMap::values
92d85d11 Merge commit '9f5f3e26efed6cd27f2a8ff0746f018d75986934'
118fef61 [buildcore] Remove python -m prefix from mypy command
8769305d [nostalgia] Allow disabling of BUILD_SHARED_LIBS
c5999050 [nostalgia] Add support for partial tilesheet loading
da23c930 [ox/std] Add oxModelFwdDecl macro for broken Apple Clang
3ae1d6c8 [ox/std] Make operator[] in Array and Vector nodiscard
a7af6c66 [keel] Cleanup
0cc6757c [keel] Add manifest to pack output
3b8eaef3 [keel] Move vald and repair funcs to their own file, make conversion to validation
b7990ed2 [keel] Make pack file copy logging nest for dir level
71313ed8 [ox/std] Cleanup
10531b6e [keel] Cleanup
dfbc298d [keel] Add pack file copy status to logging
76760daf [ox/std] Cleanup Defer
5834b9c9 [ox/std] Cleanup logging output
2a584905 [ox/fs] More cleanup and bug fix from previous cleanup
702b166b [ox/fs] Cleanup
8dd837b3 [nostalgia/core] Add a valid function for CompactTileSheet
1d262597 [keel] Make default repair return a no repair error
712299fa [studio] Cleanup
c45efa60 [ox/std] Make Result copyTo and moveTo able to convert

git-subtree-dir: deps/nostalgia
git-subtree-split: 0daf938f765b3a3ce8ba7fb292572a6a5a004634
This commit is contained in:
2024-09-28 23:44:24 -05:00
parent 9f5f3e26ef
commit 511548a2ee
99 changed files with 932 additions and 681 deletions

View File

@ -44,6 +44,9 @@ ifdef BC_VAR_USE_DOCKER_DEVENV
exit 1
endif
endif
ifndef BC_VAR_DEVENV_ROOT
BC_VAR_DEVENV_ROOT="."
endif
else
BC_CMD_PY3=${BC_CMD_HOST_PY3}
endif
@ -90,7 +93,7 @@ purge:
${BC_CMD_RM_RF} compile_commands.json
.PHONY: test
test: build
${BC_CMD_ENVRUN} ${BC_CMD_PY3} -m mypy ${BC_VAR_SCRIPTS}
${BC_CMD_ENVRUN} mypy ${BC_VAR_SCRIPTS}
${BC_CMD_CMAKE_BUILD} ${BC_VAR_BUILD_PATH} test
.PHONY: test-verbose
test-verbose: build
@ -102,7 +105,7 @@ test-rerun-verbose: build
ifdef BC_VAR_USE_DOCKER_DEVENV
.PHONY: devenv-image
devenv-image:
docker build . -t ${BC_VAR_DEVENV_IMAGE}
docker build ${BC_VAR_DEVENV_ROOT} -t ${BC_VAR_DEVENV_IMAGE}
.PHONY: devenv-create
devenv-create:
docker run -d \

View File

@ -73,7 +73,7 @@ void bind(const FrameBuffer &fb) noexcept {
static ox::Result<GLShader> buildShader(
GLuint shaderType,
const GLchar *src,
ox::CRStringView shaderName) noexcept {
ox::StringViewCR shaderName) noexcept {
GLShader shader(glCreateShader(shaderType));
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);

View File

@ -37,32 +37,32 @@ ClArgs::ClArgs(int argc, const char **args) noexcept {
}
}
bool ClArgs::getBool(ox::CRStringView arg, bool defaultValue) const noexcept {
bool ClArgs::getBool(ox::StringViewCR arg, bool defaultValue) const noexcept {
auto [value, err] = m_ints.at(arg);
return !err ? *value : defaultValue;
}
String ClArgs::getString(ox::CRStringView arg, ox::StringView defaultValue) const noexcept {
String ClArgs::getString(ox::StringViewCR arg, ox::StringView defaultValue) const noexcept {
auto [value, err] = m_strings.at(arg);
return !err ? ox::String(*value) : ox::String(defaultValue);
}
int ClArgs::getInt(ox::CRStringView arg, int defaultValue) const noexcept {
int ClArgs::getInt(ox::StringViewCR arg, int defaultValue) const noexcept {
auto [value, err] = m_ints.at(arg);
return !err ? *value : defaultValue;
}
Result<bool> ClArgs::getBool(ox::CRStringView arg) const noexcept {
Result<bool> ClArgs::getBool(ox::StringViewCR arg) const noexcept {
oxRequire(out, m_bools.at(arg));
return *out;
}
Result<String> ClArgs::getString(ox::CRStringView argName) const noexcept {
Result<String> ClArgs::getString(ox::StringViewCR argName) const noexcept {
oxRequire(out, m_strings.at(argName));
return *out;
}
Result<int> ClArgs::getInt(ox::CRStringView arg) const noexcept {
Result<int> ClArgs::getInt(ox::StringViewCR arg) const noexcept {
oxRequire(out, m_ints.at(arg));
return *out;
}

View File

@ -23,21 +23,21 @@ class ClArgs {
ClArgs(int argc, const char **args) noexcept;
[[nodiscard]]
bool getBool(ox::CRStringView arg, bool defaultValue) const noexcept;
bool getBool(ox::StringViewCR arg, bool defaultValue) const noexcept;
[[nodiscard]]
String getString(ox::CRStringView argName, ox::StringView defaultValue) const noexcept;
String getString(ox::StringViewCR argName, ox::StringView defaultValue) const noexcept;
[[nodiscard]]
int getInt(ox::CRStringView arg, int defaultValue) const noexcept;
int getInt(ox::StringViewCR arg, int defaultValue) const noexcept;
[[nodiscard]]
Result<bool> getBool(ox::CRStringView arg) const noexcept;
Result<bool> getBool(ox::StringViewCR arg) const noexcept;
[[nodiscard]]
Result<String> getString(ox::CRStringView argName) const noexcept;
Result<String> getString(ox::StringViewCR argName) const noexcept;
Result<int> getInt(ox::CRStringView arg) const noexcept;
Result<int> getInt(ox::StringViewCR arg) const noexcept;
};

View File

@ -315,7 +315,7 @@ Error FileStoreTemplate<size_t>::remove(uint64_t id) {
template<typename size_t>
Error FileStoreTemplate<size_t>::read(uint64_t id, void *out, FsSize_t outSize, FsSize_t *size) const {
oxTracef("ox.fs.FileStoreTemplate.read", "Attempting to read from inode {}", id);
oxTracef("ox.fs.FileStoreTemplate.read", "Attempting to read inode {}", id);
auto src = find(static_cast<size_t>(id));
// error check
if (!src.valid()) {
@ -333,7 +333,7 @@ Error FileStoreTemplate<size_t>::read(uint64_t id, void *out, FsSize_t outSize,
oxTracef("ox.fs.FileStoreTemplate.read.fail", "Could not read data section of item: {}", id);
oxTracef("ox.fs.FileStoreTemplate.read.fail",
"Item data section size: {}, Expected size: {}", srcData.size(), outSize);
return OxError(1);
return OxError(1, "Invalid inode");
}
ox::memcpy(out, srcData, srcData.size());
@ -341,7 +341,7 @@ Error FileStoreTemplate<size_t>::read(uint64_t id, void *out, FsSize_t outSize,
*size = src.size();
}
return OxError(0);
return {};
}
template<typename size_t>

View File

@ -43,13 +43,15 @@ struct OX_PACKED DirectoryEntry {
public:
constexpr DirectoryEntry() noexcept = default;
Error init(InodeId_t inode, const char *name, InodeId_t bufferSize) noexcept {
m_bufferSize = bufferSize;
Error init(InodeId_t inode, ox::StringViewCR name, size_t bufferSize) noexcept {
oxTracef("ox.fs.DirectoryEntry.init", "inode: {}, name: {}, bufferSize: {}", inode, name, bufferSize);
m_bufferSize = static_cast<InodeId_t>(bufferSize);
auto d = data();
if (d.valid()) {
d->inode = inode;
ox::strncpy(d->name, name, ox::min(bufferSize, static_cast<InodeId_t>(MaxFileNameLength)));
return OxError(0);
auto const maxStrSz = bufferSize - 1 - sizeof(*this);
ox::strncpy(d->name, name.data(), ox::min(maxStrSz, name.len()));
return {};
}
return OxError(1);
}
@ -103,23 +105,23 @@ class Directory {
*/
Error init() noexcept;
Error mkdir(PathIterator path, bool parents, FileName *nameBuff = nullptr);
Error mkdir(PathIterator path, bool parents);
/**
* @param parents indicates the operation should create non-existent directories in the path, like mkdir -p
*/
Error write(PathIterator path, uint64_t inode64, FileName *nameBuff = nullptr) noexcept;
Error write(PathIterator path, uint64_t inode64) noexcept;
Error remove(PathIterator path, FileName *nameBuff = nullptr) noexcept;
Error remove(PathIterator path) noexcept;
template<typename F>
Error ls(F cb) noexcept;
[[nodiscard]]
Result<typename FileStore::InodeId_t> findEntry(const FileName &name) const noexcept;
Result<typename FileStore::InodeId_t> findEntry(const StringView &name) const noexcept;
[[nodiscard]]
Result<typename FileStore::InodeId_t> find(PathIterator name, FileName *nameBuff = nullptr) const noexcept;
Result<typename FileStore::InodeId_t> find(PathIterator path) const noexcept;
};
@ -145,22 +147,17 @@ Error Directory<FileStore, InodeId_t>::init() noexcept {
}
new (buff) Buffer(Size);
m_size = Size;
return OxError(0);
return {};
}
template<typename FileStore, typename InodeId_t>
Error Directory<FileStore, InodeId_t>::mkdir(PathIterator path, bool parents, FileName *nameBuff) {
Error Directory<FileStore, InodeId_t>::mkdir(PathIterator path, bool parents) {
if (path.valid()) {
oxTrace("ox.fs.Directory.mkdir", path.fullPath());
// reuse nameBuff if it has already been allocated, as it is a rather large variable
if (nameBuff == nullptr) {
nameBuff = new (ox_alloca(sizeof(FileName))) FileName;
}
// determine if already exists
auto name = nameBuff;
oxReturnError(path.get(*name));
auto childInode = find(PathIterator(*name));
ox::StringView name;
oxReturnError(path.get(name));
auto childInode = find(PathIterator(name));
if (!childInode.ok()) {
// if this is not the last item in the path and parents is disabled,
// return an error
@ -171,12 +168,10 @@ Error Directory<FileStore, InodeId_t>::mkdir(PathIterator path, bool parents, Fi
oxTracef("ox.fs.Directory.mkdir", "Generated Inode ID: {}", childInode.value);
oxLogError(childInode.error);
oxReturnError(childInode.error);
// initialize the directory
Directory<FileStore, InodeId_t> child(m_fs, childInode.value);
oxReturnError(child.init());
auto err = write(PathIterator(*name), childInode.value);
auto err = write(PathIterator(name), childInode.value);
if (err) {
oxLogError(err);
// could not index the directory, delete it
@ -184,46 +179,35 @@ Error Directory<FileStore, InodeId_t>::mkdir(PathIterator path, bool parents, Fi
return err;
}
}
Directory<FileStore, InodeId_t> child(m_fs, childInode.value);
if (path.hasNext()) {
oxReturnError(child.mkdir(path.next(), parents, nameBuff));
oxReturnError(child.mkdir(path.next(), parents));
}
}
return OxError(0);
return {};
}
template<typename FileStore, typename InodeId_t>
Error Directory<FileStore, InodeId_t>::write(PathIterator path, uint64_t inode64, FileName *nameBuff) noexcept {
Error Directory<FileStore, InodeId_t>::write(PathIterator path, uint64_t inode64) noexcept {
const auto inode = static_cast<InodeId_t>(inode64);
// reuse nameBuff if it has already been allocated, as it is a rather large variable
if (nameBuff == nullptr) {
nameBuff = new (ox_alloca(sizeof(FileName))) FileName;
}
auto name = nameBuff;
ox::StringView name;
if (path.next().hasNext()) { // not yet at target directory, recurse to next one
oxReturnError(path.get(*name));
oxTracef("ox.fs.Directory.write", "Attempting to write to next sub-Directory: {} of {}",
*name, path.fullPath());
oxRequire(nextChild, findEntry(*name));
oxTracef("ox.fs.Directory.write", "{}: {}", *name, nextChild);
name, path.fullPath());
oxReturnError(path.get(name));
oxRequire(nextChild, findEntry(name));
oxTracef("ox.fs.Directory.write", "{}: {}", name, nextChild);
if (nextChild) {
// reuse name because it is a rather large variable and will not be used again
// be attentive that this remains true
name = nullptr;
return Directory(m_fs, nextChild).write(path.next(), inode, nameBuff);
return Directory(m_fs, nextChild).write(path.next(), inode);
} else {
oxTracef("ox.fs.Directory.write", "{} not found and not allowed to create it.", *name);
oxTracef("ox.fs.Directory.write", "{} not found and not allowed to create it.", name);
return OxError(1, "File not found and not allowed to create it.");
}
} else {
oxTrace("ox.fs.Directory.write", path.fullPath());
oxReturnError(path.next(name));
// insert the new entry on this directory
// get the name
oxReturnError(path.next(*name));
// find existing version of directory
oxTracef("ox.fs.Directory.write", "Searching for directory inode {}", m_inodeId);
oxRequire(oldStat, m_fs.stat(m_inodeId));
@ -233,8 +217,7 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, uint64_t inode64
oxTrace("ox.fs.Directory.write.fail", "Could not read existing version of Directory");
return OxError(1, "Could not read existing version of Directory");
}
const auto pathSize = name->len() + 1;
const auto pathSize = name.len() + 1;
const auto entryDataSize = DirectoryEntry<InodeId_t>::DirectoryEntryData::spaceNeeded(pathSize);
const auto newSize = oldStat.size + Buffer::spaceNeeded(entryDataSize);
auto cpy = ox_malloca(newSize, Buffer, *old, oldStat.size);
@ -242,29 +225,22 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, uint64_t inode64
oxTrace("ox.fs.Directory.write.fail", "Could not allocate memory for copy of Directory");
return OxError(1, "Could not allocate memory for copy of Directory");
}
oxReturnError(cpy->setSize(newSize));
auto val = cpy->malloc(entryDataSize).value;
if (!val.valid()) {
oxTrace("ox.fs.Directory.write.fail", "Could not allocate memory for new directory entry");
return OxError(1, "Could not allocate memory for new directory entry");
}
oxTracef("ox.fs.Directory.write", "Attempting to write Directory entry: {}", name->data());
oxReturnError(val->init(inode, name->data(), val.size()));
oxTracef("ox.fs.Directory.write", "Attempting to write Directory entry: {}", name);
oxReturnError(val->init(inode, name, val.size()));
return m_fs.write(m_inodeId, cpy.get(), cpy->size(), static_cast<uint8_t>(FileType::Directory));
}
}
template<typename FileStore, typename InodeId_t>
Error Directory<FileStore, InodeId_t>::remove(PathIterator path, FileName *nameBuff) noexcept {
// reuse nameBuff if it has already been allocated, as it is a rather large variable
if (nameBuff == nullptr) {
nameBuff = new (ox_alloca(sizeof(FileName))) FileName;
}
auto &name = *nameBuff;
Error Directory<FileStore, InodeId_t>::remove(PathIterator path) noexcept {
ox::StringView name;
oxReturnError(path.get(name));
oxTrace("ox.fs.Directory.remove", name);
auto buff = m_fs.read(m_inodeId).template to<Buffer>();
if (buff.valid()) {
@ -283,7 +259,7 @@ Error Directory<FileStore, InodeId_t>::remove(PathIterator path, FileName *nameB
oxTrace("ox.fs.Directory.remove.fail", "Could not find directory buffer");
return OxError(1, "Could not find directory buffer");
}
return OxError(0);
return {};
}
template<typename FileStore, typename InodeId_t>
@ -295,7 +271,6 @@ Error Directory<FileStore, InodeId_t>::ls(F cb) noexcept {
oxTrace("ox.fs.Directory.ls.fail", "Could not directory buffer");
return OxError(1, "Could not directory buffer");
}
oxTrace("ox.fs.Directory.ls", "Found directory buffer.");
for (auto i = buff->iterator(); i.valid(); i.next()) {
auto data = i->data();
@ -305,12 +280,11 @@ Error Directory<FileStore, InodeId_t>::ls(F cb) noexcept {
oxTrace("ox.fs.Directory.ls", "INVALID DIRECTORY ENTRY");
}
}
return OxError(0);
return {};
}
template<typename FileStore, typename InodeId_t>
Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::findEntry(const FileName &name) const noexcept {
Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::findEntry(StringViewCR name) const noexcept {
oxTrace("ox.fs.Directory.findEntry", name);
auto buff = m_fs.read(m_inodeId).template to<Buffer>();
if (!buff.valid()) {
@ -327,7 +301,7 @@ Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::findEntry
return static_cast<InodeId_t>(data->inode);
}
} else {
oxTrace("ox.fs.Directory.findEntry") << "INVALID DIRECTORY ENTRY";
oxTrace("ox.fs.Directory.findEntry", "INVALID DIRECTORY ENTRY");
}
}
oxTrace("ox.fs.Directory.findEntry.fail", "Entry not present");
@ -335,22 +309,15 @@ Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::findEntry
}
template<typename FileStore, typename InodeId_t>
Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::find(PathIterator path, FileName *nameBuff) const noexcept {
// reuse nameBuff if it has already been allocated, as it is a rather large variable
if (nameBuff == nullptr) {
nameBuff = new (ox_alloca(sizeof(FileName))) FileName;
}
Result<typename FileStore::InodeId_t> Directory<FileStore, InodeId_t>::find(PathIterator path) const noexcept {
// determine if already exists
auto name = nameBuff;
oxReturnError(path.get(*name));
oxRequire(v, findEntry(*name));
ox::StringView name;
oxReturnError(path.get(name));
oxRequire(v, findEntry(name));
// recurse if not at end of path
if (auto p = path.next(); p.valid()) {
Directory dir(m_fs, v);
name = nullptr;
return dir.find(p, nameBuff);
return dir.find(p);
}
return v;
}

View File

@ -28,7 +28,7 @@ FileAddress::FileAddress(uint64_t inode) noexcept {
m_type = FileAddressType::Inode;
}
FileAddress::FileAddress(ox::CRStringView path) noexcept {
FileAddress::FileAddress(ox::StringViewCR path) noexcept {
auto pathSize = path.bytes();
m_data.path = new char[pathSize + 1];
memcpy(m_data.path, path.data(), pathSize);
@ -114,7 +114,7 @@ bool FileAddress::operator==(FileAddress const&other) const noexcept {
return true;
}
bool FileAddress::operator==(CRStringView path) const noexcept {
bool FileAddress::operator==(StringViewCR path) const noexcept {
auto [p, err] = getPath();
if (err) {
return false;

View File

@ -57,7 +57,7 @@ class FileAddress {
FileAddress(uint64_t inode) noexcept;
explicit FileAddress(CRStringView path) noexcept;
explicit FileAddress(StringViewCR path) noexcept;
constexpr FileAddress(ox::StringLiteral path) noexcept;
@ -69,7 +69,7 @@ class FileAddress {
bool operator==(const FileAddress &other) const noexcept;
bool operator==(CRStringView path) const noexcept;
bool operator==(StringViewCR path) const noexcept;
[[nodiscard]]
constexpr FileAddressType type() const noexcept {

View File

@ -44,7 +44,7 @@ Result<Buffer> FileSystem::read(const FileAddress &addr) noexcept {
return buff;
}
Result<Buffer> FileSystem::read(CRStringView path) noexcept {
Result<Buffer> FileSystem::read(StringViewCR path) noexcept {
oxRequire(s, statPath(path));
Buffer buff(static_cast<std::size_t>(s.size));
oxReturnError(readFilePath(path, buff.data(), buff.size()));

View File

@ -30,22 +30,22 @@ class FileSystem {
public:
virtual ~FileSystem() noexcept = default;
virtual Error mkdir(CRStringView path, bool recursive) noexcept = 0;
virtual Error mkdir(StringViewCR 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(CRStringView src, CRStringView dest) noexcept = 0;
virtual Error move(StringViewCR src, StringViewCR dest) noexcept = 0;
Error read(const FileAddress &addr, void *buffer, std::size_t size) noexcept;
Result<Buffer> read(const FileAddress &addr) noexcept;
Result<Buffer> read(CRStringView path) noexcept;
Result<Buffer> read(StringViewCR path) noexcept;
inline Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
inline Error read(StringViewCR path, void *buffer, std::size_t buffSize) noexcept {
return readFilePath(path, buffer, buffSize);
}
@ -55,19 +55,19 @@ class FileSystem {
Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept;
virtual Result<Vector<String>> ls(CRStringView dir) const noexcept = 0;
virtual Result<Vector<String>> ls(StringViewCR dir) const noexcept = 0;
virtual Error remove(CRStringView path, bool recursive) noexcept = 0;
virtual Error remove(StringViewCR path, bool recursive) noexcept = 0;
Error remove(const FileAddress &addr, bool recursive = false) noexcept;
virtual Error resize(uint64_t size, void *buffer) noexcept = 0;
Error write(CRStringView path, const void *buffer, uint64_t size) noexcept {
Error write(StringViewCR path, const void *buffer, uint64_t size) noexcept {
return writeFilePath(path, buffer, size, FileType::NormalFile);
}
Error write(CRStringView path, ox::Span<char> const&buff) noexcept {
Error write(StringViewCR path, ox::Span<char> const&buff) noexcept {
return write(path, buff.data(), buff.size(), FileType::NormalFile);
}
@ -81,7 +81,7 @@ class FileSystem {
Error write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept;
inline Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
inline Error write(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept {
return writeFilePath(path, buffer, size, fileType);
}
@ -93,7 +93,7 @@ class FileSystem {
return statInode(inode);
}
inline Result<FileStat> stat(CRStringView path) const noexcept {
inline Result<FileStat> stat(StringViewCR path) const noexcept {
return statPath(path);
}
@ -134,15 +134,15 @@ class FileSystem {
protected:
virtual Result<FileStat> statInode(uint64_t inode) const noexcept = 0;
virtual Result<FileStat> statPath(CRStringView path) const noexcept = 0;
virtual Result<FileStat> statPath(StringViewCR path) const noexcept = 0;
virtual Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Error readFilePath(StringViewCR path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept = 0;
virtual Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0;
virtual Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
virtual Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0;
@ -152,7 +152,7 @@ class MemFS: public FileSystem {
public:
Result<const char*> directAccess(const FileAddress &addr) const noexcept;
inline Result<const char*> directAccess(CRStringView path) const noexcept {
inline Result<const char*> directAccess(StringViewCR path) const noexcept {
return directAccessPath(path);
}
@ -161,7 +161,7 @@ class MemFS: public FileSystem {
}
protected:
virtual Result<const char*> directAccessPath(CRStringView path) const noexcept = 0;
virtual Result<const char*> directAccessPath(StringViewCR path) const noexcept = 0;
virtual Result<const char*> directAccessInode(uint64_t inode) const noexcept = 0;
};
@ -197,13 +197,13 @@ class FileSystemTemplate: public MemFS {
static Error format(void *buff, uint64_t buffSize) noexcept;
Error mkdir(CRStringView path, bool recursive) noexcept override;
Error mkdir(StringViewCR path, bool recursive) noexcept override;
Error move(CRStringView src, CRStringView dest) noexcept override;
Error move(StringViewCR src, StringViewCR dest) noexcept override;
Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Error readFilePath(StringViewCR path, void *buffer, std::size_t buffSize) noexcept override;
Result<const char*> directAccessPath(CRStringView) const noexcept override;
Result<const char*> directAccessPath(StringViewCR) const noexcept override;
Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override;
@ -211,12 +211,12 @@ class FileSystemTemplate: public MemFS {
Result<const char*> directAccessInode(uint64_t) const noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
Result<Vector<String>> ls(StringViewCR dir) const noexcept override;
template<typename F>
Error ls(CRStringView path, F cb) const;
Error ls(StringViewCR path, F cb) const;
Error remove(CRStringView path, bool recursive) noexcept override;
Error remove(StringViewCR path, bool recursive) noexcept override;
/**
* Resizes FileSystem to minimum possible size.
@ -225,13 +225,13 @@ class FileSystemTemplate: public MemFS {
Error resize(uint64_t size, void *buffer) noexcept override;
Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Result<FileStat> statInode(uint64_t inode) const noexcept override;
Result<FileStat> statPath(CRStringView path) const noexcept override;
Result<FileStat> statPath(StringViewCR path) const noexcept override;
[[nodiscard]]
uint64_t spaceNeeded(uint64_t size) const noexcept override;
@ -253,7 +253,7 @@ class FileSystemTemplate: public MemFS {
/**
* Finds the inode ID at the given path.
*/
Result<uint64_t> find(CRStringView path) const noexcept;
Result<uint64_t> find(StringViewCR path) const noexcept;
Result<Directory> rootDir() const noexcept;
@ -305,14 +305,14 @@ Error FileSystemTemplate<FileStore, Directory>::format(void *buff, uint64_t buff
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::mkdir(CRStringView path, bool recursive) noexcept {
Error FileSystemTemplate<FileStore, Directory>::mkdir(StringViewCR 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(CRStringView src, CRStringView dest) noexcept {
Error FileSystemTemplate<FileStore, Directory>::move(StringViewCR src, StringViewCR dest) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequireM(inode, rootDir.find(src));
@ -322,7 +322,8 @@ Error FileSystemTemplate<FileStore, Directory>::move(CRStringView src, CRStringV
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
Error FileSystemTemplate<FileStore, Directory>::readFilePath(StringViewCR path, void *buffer, std::size_t buffSize) noexcept {
oxTrace("ox.fs.FileSystemTemplate.readFilePath", path);
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(s, stat(path));
@ -333,7 +334,7 @@ Error FileSystemTemplate<FileStore, Directory>::readFilePath(CRStringView path,
}
template<typename FileStore, typename Directory>
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessPath(CRStringView path) const noexcept {
Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessPath(StringViewCR path) const noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
@ -342,6 +343,7 @@ Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessPath(C
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::readFileInode(uint64_t inode, void *buffer, std::size_t buffSize) noexcept {
oxTracef("ox.fs.FileSystemTemplate.readFileInode", "{}", inode);
oxRequire(s, stat(inode));
if (s.size > buffSize) {
return OxError(1, "Buffer to small to load file");
@ -364,9 +366,9 @@ Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccessInode(
}
template<typename FileStore, typename Directory>
Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(CRStringView path) const noexcept {
Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(StringViewCR path) const noexcept {
Vector<String> out;
oxReturnError(ls(path, [&out](CRStringView name, typename FileStore::InodeId_t) {
oxReturnError(ls(path, [&out](StringViewCR name, typename FileStore::InodeId_t) {
out.emplace_back(name);
return OxError(0);
}));
@ -375,7 +377,7 @@ Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(CRStringView
template<typename FileStore, typename Directory>
template<typename F>
Error FileSystemTemplate<FileStore, Directory>::ls(CRStringView path, F cb) const {
Error FileSystemTemplate<FileStore, Directory>::ls(StringViewCR path, F cb) const {
oxTracef("ox.fs.FileSystemTemplate.ls", "path: {}", path);
oxRequire(s, stat(path));
Directory dir(m_fs, s.inode);
@ -383,7 +385,7 @@ Error FileSystemTemplate<FileStore, Directory>::ls(CRStringView path, F cb) cons
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::remove(CRStringView path, bool recursive) noexcept {
Error FileSystemTemplate<FileStore, Directory>::remove(StringViewCR path, bool recursive) noexcept {
oxRequire(fd, fileSystemData());
Directory rootDir(m_fs, fd.rootDirInode);
oxRequire(inode, rootDir.find(path));
@ -409,24 +411,29 @@ Error FileSystemTemplate<FileStore, Directory>::resize() noexcept {
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::resize(uint64_t size, void *buffer) noexcept {
oxReturnError(m_fs.resize(static_cast<size_t>(size), buffer));
return OxError(0);
return {};
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept {
Error FileSystemTemplate<FileStore, Directory>::writeFilePath(
StringViewCR path,
const void *buffer,
uint64_t size,
FileType fileType) noexcept {
oxTrace("ox.fs.FileSystemTemplate.writeFilePath", path);
auto [inode, err] = find(path);
if (err) {
oxRequire(generatedId, m_fs.generateInodeId());
inode = generatedId;
oxReturnError(m_fs.generateInodeId().moveTo(inode));
oxRequireM(rootDir, this->rootDir());
oxReturnError(rootDir.write(path, inode));
}
oxReturnError(writeFileInode(inode, buffer, size, fileType));
return OxError(0);
return {};
}
template<typename FileStore, typename Directory>
Error FileSystemTemplate<FileStore, Directory>::writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept {
oxTrace("ox.fs.FileSystemTemplate.writeFileInode", ox::itoa(inode));
return m_fs.write(inode, buffer, static_cast<size_t>(size), static_cast<uint8_t>(fileType));
}
@ -442,7 +449,7 @@ Result<FileStat> FileSystemTemplate<FileStore, Directory>::statInode(uint64_t in
}
template<typename FileStore, typename Directory>
Result<FileStat> FileSystemTemplate<FileStore, Directory>::statPath(CRStringView path) const noexcept {
Result<FileStat> FileSystemTemplate<FileStore, Directory>::statPath(StringViewCR path) const noexcept {
oxRequire(inode, find(path));
return stat(inode);
}
@ -485,7 +492,7 @@ Result<typename FileSystemTemplate<FileStore, Directory>::FileSystemData> FileSy
}
template<typename FileStore, typename Directory>
Result<uint64_t> FileSystemTemplate<FileStore, Directory>::find(CRStringView path) const noexcept {
Result<uint64_t> FileSystemTemplate<FileStore, Directory>::find(StringViewCR path) const noexcept {
oxRequire(fd, fileSystemData());
// return root as a special case
if (path == "/") {

View File

@ -17,7 +17,7 @@
namespace ox {
PassThroughFS::PassThroughFS(CRStringView dirPath) {
PassThroughFS::PassThroughFS(StringViewCR dirPath) {
m_path = std::string_view(dirPath.data(), dirPath.bytes());
}
@ -27,7 +27,7 @@ String PassThroughFS::basePath() const noexcept {
return ox::String(m_path.string().c_str());
}
Error PassThroughFS::mkdir(CRStringView path, bool recursive) noexcept {
Error PassThroughFS::mkdir(StringViewCR path, bool recursive) noexcept {
bool success = false;
const auto p = m_path / stripSlash(path);
const auto u8p = p.u8string();
@ -54,7 +54,7 @@ Error PassThroughFS::mkdir(CRStringView path, bool recursive) noexcept {
return OxError(success ? 0 : 1);
}
Error PassThroughFS::move(CRStringView src, CRStringView dest) noexcept {
Error PassThroughFS::move(StringViewCR src, StringViewCR dest) noexcept {
std::error_code ec;
std::filesystem::rename(m_path / stripSlash(src), m_path / stripSlash(dest), ec);
if (ec.value()) {
@ -63,7 +63,7 @@ Error PassThroughFS::move(CRStringView src, CRStringView dest) noexcept {
return OxError(0);
}
Result<Vector<String>> PassThroughFS::ls(CRStringView dir) const noexcept {
Result<Vector<String>> PassThroughFS::ls(StringViewCR dir) const noexcept {
Vector<String> out;
std::error_code ec;
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
@ -75,7 +75,7 @@ Result<Vector<String>> PassThroughFS::ls(CRStringView dir) const noexcept {
return out;
}
Error PassThroughFS::remove(CRStringView path, bool recursive) noexcept {
Error PassThroughFS::remove(StringViewCR path, bool recursive) noexcept {
if (recursive) {
return OxError(std::filesystem::remove_all(m_path / stripSlash(path)) != 0);
} else {
@ -93,14 +93,13 @@ Result<FileStat> PassThroughFS::statInode(uint64_t) const noexcept {
return OxError(1, "statInode(uint64_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::statPath(CRStringView path) const noexcept {
Result<FileStat> PassThroughFS::statPath(StringViewCR path) const noexcept {
std::error_code ec;
const auto p = m_path / stripSlash(path);
const FileType type = std::filesystem::is_directory(p, ec) ?
FileType::Directory : FileType::NormalFile;
oxTracef("ox.fs.PassThroughFS.statInode", "{} {}", ec.message(), path);
const uint64_t size = type == FileType::Directory ? 0 : std::filesystem::file_size(p, ec);
oxTracef("ox.fs.PassThroughFS.statInode", "{} {}", ec.message(), path);
oxTracef("ox.fs.PassThroughFS.statInode.size", "{} {}", path, size);
oxReturnError(OxError(static_cast<ox::ErrorCode>(ec.value()), "PassThroughFS: stat failed"));
return FileStat{0, 0, size, type};
@ -141,7 +140,7 @@ bool PassThroughFS::valid() const noexcept {
return false;
}
Error PassThroughFS::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept {
Error PassThroughFS::readFilePath(StringViewCR 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 = static_cast<std::size_t>(file.tellg());
@ -168,7 +167,7 @@ Error PassThroughFS::readFileInodeRange(uint64_t, std::size_t, std::size_t, void
return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS");
}
Error PassThroughFS::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept {
Error PassThroughFS::writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType) noexcept {
const auto p = (m_path / stripSlash(path));
try {
std::ofstream f(p, std::ios::binary);

View File

@ -29,29 +29,29 @@ class PassThroughFS: public FileSystem {
std::filesystem::path m_path;
public:
explicit PassThroughFS(CRStringView dirPath);
explicit PassThroughFS(StringViewCR dirPath);
~PassThroughFS() noexcept override;
[[nodiscard]]
String basePath() const noexcept;
Error mkdir(CRStringView path, bool recursive) noexcept override;
Error mkdir(StringViewCR path, bool recursive) noexcept override;
Error move(CRStringView src, CRStringView dest) noexcept override;
Error move(StringViewCR src, StringViewCR dest) noexcept override;
Result<Vector<String>> ls(CRStringView dir) const noexcept override;
Result<Vector<String>> ls(StringViewCR dir) const noexcept override;
template<typename F>
Error ls(CRStringView dir, F cb) const noexcept;
Error ls(StringViewCR dir, F cb) const noexcept;
Error remove(CRStringView path, bool recursive) noexcept override;
Error remove(StringViewCR path, bool recursive) noexcept override;
Error resize(uint64_t size, void *buffer) noexcept override;
Result<FileStat> statInode(uint64_t inode) const noexcept override;
Result<FileStat> statPath(CRStringView path) const noexcept override;
Result<FileStat> statPath(StringViewCR path) const noexcept override;
[[nodiscard]]
uint64_t spaceNeeded(uint64_t size) const noexcept override;
@ -69,13 +69,13 @@ class PassThroughFS: public FileSystem {
bool valid() const noexcept override;
protected:
Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override;
Error readFilePath(StringViewCR path, void *buffer, std::size_t buffSize) noexcept override;
Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept override;
Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;
@ -89,7 +89,7 @@ class PassThroughFS: public FileSystem {
};
template<typename F>
Error PassThroughFS::ls(CRStringView dir, F cb) const noexcept {
Error PassThroughFS::ls(StringViewCR dir, F cb) const noexcept {
std::error_code ec;
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
oxReturnError(OxError(static_cast<ox::ErrorCode>(ec.value()), "PassThroughFS: ls failed"));

View File

@ -22,7 +22,7 @@ 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()) {
PathIterator::PathIterator(StringViewCR path): PathIterator(path.data(), path.bytes()) {
}
/**
@ -40,30 +40,9 @@ Error PathIterator::dirPath(char *out, std::size_t outSize) {
}
}
/**
* @return 0 if no error
*/
Error PathIterator::fileName(char *out, std::size_t outSize) {
auto idx = ox::lastIndexOf(m_path, '/', m_maxSize);
if (idx >= 0) {
idx++; // pass up the preceding /
std::size_t fileNameSize = static_cast<size_t>(ox::strlen(&m_path[idx]));
if (fileNameSize < outSize) {
ox::memcpy(out, &m_path[idx], fileNameSize);
out[fileNameSize] = 0;
return OxError(0);
} else {
return OxError(1);
}
} else {
return OxError(2);
}
}
// Gets the get item in the path
Error PathIterator::get(IString<MaxFileNameLength> &fileName) {
Error PathIterator::get(StringView &fileName) {
std::size_t size = 0;
std::ignore = fileName.resize(MaxFileNameLength);
if (m_iterator >= m_maxSize) {
oxTracef("ox.fs.PathIterator.get", "m_iterator ({}) >= m_maxSize ({})", m_iterator, m_maxSize);
return OxError(1);
@ -88,22 +67,21 @@ Error PathIterator::get(IString<MaxFileNameLength> &fileName) {
if (size >= MaxFileNameLength || size == 0) {
return OxError(1);
}
ox::memcpy(fileName.data(), &m_path[start], size);
fileName = ox::substr(m_path, start, start + size);
// truncate trailing /
if (size && fileName[size - 1] == '/') {
size--;
fileName = ox::substr(m_path, start, start + size - 1);
}
oxReturnError(fileName.resize(size));
oxAssert(fileName[fileName.len()-1] != '/', "name ends in /");
return {};
}
/**
* @return 0 if no error
*/
Error PathIterator::next(IString<MaxFileNameLength> &fileName) {
Error PathIterator::next(StringView &fileName) {
std::size_t size = 0;
auto retval = OxError(1);
std::ignore = fileName.resize(MaxFileNameLength);
if (m_iterator < m_maxSize && ox::strlen(&m_path[m_iterator])) {
retval = OxError(0);
if (m_path[m_iterator] == '/') {
@ -122,15 +100,14 @@ Error PathIterator::next(IString<MaxFileNameLength> &fileName) {
if (size >= MaxFileNameLength) {
return OxError(1);
}
ox::memcpy(fileName.data(), &m_path[start], size);
fileName = ox::substr(m_path, start, start + size);
// truncate trailing /
while (fileName.len() && fileName[fileName.len() - 1] == '/') {
fileName = ox::substr(m_path, start, start + size);
}
m_iterator += size;
oxAssert(fileName.len() == 0 || fileName[fileName.len()-1] != '/', "name ends in /");
}
// truncate trailing /
if (size && fileName[size - 1] == '/') {
size--;
}
fileName[size] = 0; // end with null terminator
oxReturnError(fileName.resize(size));
m_iterator += size;
return retval;
}

View File

@ -13,7 +13,6 @@
namespace ox {
constexpr std::size_t MaxFileNameLength = 255;
using FileName = IString<MaxFileNameLength>;
class PathIterator {
private:
@ -26,31 +25,14 @@ class PathIterator {
PathIterator(const char *path);
PathIterator(CRStringView path);
PathIterator(StringViewCR path);
/**
* @return 0 if no error
*/
Error dirPath(char *pathOut, std::size_t pathOutSize);
/**
* @return 0 if no error
*/
Error fileName(char *out, std::size_t outSize);
Error next(StringView &fileName);
/**
* @return 0 if no error
*/
Error next(FileName &fileName);
Error get(StringView &fileName);
/**
* @return 0 if no error
*/
Error get(FileName &fileName);
/**
* @return 0 if no error
*/
Result<std::size_t> nextSize() const;
[[nodiscard]]

View File

@ -174,7 +174,7 @@ template<typename size_t, typename Item>
NodeBuffer<size_t, Item>::NodeBuffer(std::size_t size) noexcept {
m_header.size = static_cast<size_t>(size);
ox::memset(this + 1, 0, size - sizeof(*this));
oxTracef("ox.NodeBuffer.constructor", "{}", m_header.firstItem.get());
oxTracef("ox.ptrarith.NodeBuffer.constructor", "{}", m_header.firstItem.get());
}
template<typename size_t, typename Item>

View File

@ -224,7 +224,7 @@ constexpr const Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(s
template<typename T, typename size_t, size_t minOffset>
template<typename SubT>
constexpr const Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t offset) const noexcept {
oxTracef("ox.fs.Ptr.subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset));
oxTracef("ox.ptrarith.Ptr.subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset));
return subPtr<SubT>(offset, m_itemSize - offset);
}
@ -237,7 +237,7 @@ constexpr Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t
template<typename T, typename size_t, size_t minOffset>
template<typename SubT>
constexpr Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t offset) noexcept {
oxTracef("ox.fs.Ptr.subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset));
oxTracef("ox.ptrarith.Ptr.subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset));
return subPtr<SubT>(offset, m_itemSize - offset);
}

View File

@ -19,7 +19,6 @@ add_test("[ox/fs] PathIterator::next5" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests
add_test("[ox/fs] PathIterator::hasNext" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests PathIterator::hasNext)
add_test("[ox/fs] PathIterator::dirPath" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests PathIterator::dirPath)
add_test("[ox/fs] PathIterator::fileName" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests PathIterator::fileName)
add_test("[ox/fs] NodeBuffer::insert" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests "NodeBuffer::insert")
add_test("[ox/fs] FileStore::readWrite" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FSTests "FileStore::readWrite")

View File

@ -58,9 +58,9 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
{
"PathIterator::next1",
[](ox::StringView) {
auto const path = ox::String("/usr/share/charset.gbag");
auto constexpr path = ox::StringLiteral("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len());
ox::FileName buff;
ox::StringView buff;
oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && buff == "charset.gbag", "PathIterator shows wrong next");
@ -70,11 +70,13 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
{
"PathIterator::next2",
[](ox::StringView) {
auto const path = ox::String("/usr/share/");
ox::PathIterator it(path.c_str(), path.len());
ox::FileName buff;
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
auto constexpr path = ox::StringView("/usr/share/");
ox::PathIterator it(path);
ox::StringView buff;
oxAssert(it.next(buff), "PathIterator::next returned error");
oxExpect(buff, "usr");
oxAssert(it.next(buff), "PathIterator::next returned error");
oxExpect(buff, "share");
return OxError(0);
}
},
@ -83,20 +85,20 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
[](ox::StringView) {
auto const path = ox::String("/");
ox::PathIterator it(path.c_str(), path.len());
ox::FileName buff;
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "\0") == 0, "PathIterator shows wrong next");
ox::StringView buff;
oxAssert(it.next(buff) == 0 && buff == "\0", "PathIterator shows wrong next");
return OxError(0);
}
},
{
"PathIterator::next4",
[](ox::StringView) {
auto const path = ox::String("usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len());
ox::FileName buff;
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows wrong next");
auto constexpr path = ox::StringLiteral("usr/share/charset.gbag");
ox::PathIterator it(path);
ox::StringView buff;
oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && buff == "charset.gbag", "PathIterator shows wrong next");
return OxError(0);
}
},
@ -105,32 +107,22 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
[](ox::StringView) {
auto const path = ox::String("usr/share/");
ox::PathIterator it(path.c_str(), path.len());
ox::FileName buff;
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "usr") == 0, "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && ox::strcmp(buff, "share") == 0, "PathIterator shows wrong next");
ox::StringView buff;
oxAssert(it.next(buff) == 0 && buff == "usr", "PathIterator shows wrong next");
oxAssert(it.next(buff) == 0 && buff == "share", "PathIterator shows wrong next");
return OxError(0);
}
},
{
"PathIterator::dirPath",
[] (ox::StringView) {
auto const path = ox::String("/usr/share/charset.gbag");
auto constexpr path = ox::StringLiteral("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.dirPath(buff, path.len()) == 0 && ox::strcmp(buff, "/usr/share/") == 0, "PathIterator shows incorrect dir path");
return OxError(0);
}
},
{
"PathIterator::fileName",
[](ox::StringView) {
auto const path = ox::String("/usr/share/charset.gbag");
ox::PathIterator it(path.c_str(), path.len());
auto buff = static_cast<char*>(ox_alloca(path.len() + 1));
oxAssert(it.fileName(buff, path.len()) == 0 && ox::strcmp(buff, "charset.gbag") == 0, "PathIterator shows incorrect file name");
return OxError(0);
}
},
{
"PathIterator::hasNext",
[](ox::StringView) {
@ -221,7 +213,6 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
oxTrace("ox.fs.test.FileSystem") << "format";
oxAssert(ox::FileSystem32::format(fsBuff.data(), fsBuff.size()), "FileSystem format failed");
ox::FileSystem32 fs(ox::FileStore32(fsBuff.data(), fsBuff.size()));
oxTrace("ox.fs.test.FileSystem") << "mkdir";
oxAssert(fs.mkdir("/dir", true), "mkdir failed");
oxAssert(fs.stat("/dir").error, "mkdir failed");
@ -229,7 +220,6 @@ const std::map<ox::StringView, std::function<ox::Error(ox::StringView)>> tests =
oxAssert(fs.stat("/l1d1/l2d1/l3d1").error, "mkdir failed");
oxAssert(fs.mkdir("/l1d1/l2d2", true), "mkdir failed");
oxAssert(fs.stat("/l1d1/l2d2").error, "mkdir failed");
return OxError(0);
}
},

View File

@ -51,7 +51,7 @@ LoggerConn::~LoggerConn() noexcept {
}
}
ox::Error LoggerConn::initConn(ox::CRStringView appName) noexcept {
ox::Error LoggerConn::initConn(ox::StringViewCR appName) noexcept {
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

View File

@ -38,7 +38,7 @@ class LoggerConn: public trace::Logger {
LoggerConn &operator=(const LoggerConn&) noexcept = delete;
ox::Error send(const trace::TraceMsg&) noexcept final;
ox::Error sendInit(const trace::InitTraceMsg&) noexcept final;
ox::Error initConn(ox::CRStringView appName) noexcept;
ox::Error initConn(ox::StringViewCR appName) noexcept;
ox::Error send(const char *buff, std::size_t len) const noexcept;
private:
void msgSend() noexcept;

View File

@ -316,7 +316,7 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
testIn.Union.Int = 93;
oxAssert(ox::writeMC(dataBuff.data(), dataBuff.size(), testIn), "Data generation failed");
ox::TypeStore typeStore;
const auto [type, typeErr] = ox::buildTypeDef(&typeStore, &testIn);
const auto [type, typeErr] = ox::buildTypeDef(typeStore, testIn);
oxAssert(typeErr, "Descriptor write failed");
ox::ModelObject testOut;
oxReturnError(testOut.setType(type));
@ -368,7 +368,7 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
testIn.Struct.IString = "Test String 2";
oxAssert(ox::writeMC(dataBuff, dataBuffLen, testIn), "Data generation failed");
ox::TypeStore typeStore;
const auto [type, typeErr] = ox::buildTypeDef(&typeStore, &testIn);
const auto [type, typeErr] = ox::buildTypeDef(typeStore, testIn);
oxAssert(typeErr, "Descriptor write failed");
ox::BufferReader br({dataBuff, dataBuffLen});
oxReturnError(ox::walkModel<ox::MetalClawReader>(type, br,

View File

@ -10,8 +10,10 @@
#include <ox/std/concepts.hpp>
// oxModelFwdDecl is necessary because Apple-Clang is broken...
#define oxModelFwdDecl(modelName) constexpr ox::Error model(auto *io, ox::CommonPtrWith<modelName> auto *o) noexcept
#define oxModelBegin(modelName) constexpr ox::Error model(auto *io, [[maybe_unused]] ox::CommonPtrWith<modelName> auto *o) noexcept { oxReturnError(io->template setTypeInfo<modelName>());
#define oxModelEnd() return OxError(0); }
#define oxModelField(fieldName) oxReturnError(io->field(#fieldName, &o->fieldName));
#define oxModelFieldRename(objFieldName, serFieldName) oxReturnError(io->field(#serFieldName, &o->objFieldName));
#define oxModelFriend(modelName) friend constexpr ox::Error model(auto*, ox::CommonPtrWith<modelName> auto*) noexcept
#define oxModelFriend(modelName) friend constexpr ox::Error model(auto *io, ox::CommonPtrWith<modelName> auto *o) noexcept

View File

@ -35,7 +35,7 @@ constexpr auto buildTypeId() noexcept {
}
static constexpr auto buildTypeId(
CRStringView name, int version,
StringViewCR name, int version,
const TypeParamPack &typeParams = {}) noexcept {
String tp;
if (!typeParams.empty()) {

View File

@ -91,23 +91,23 @@ class TypeDescWriter {
constexpr ~TypeDescWriter() noexcept = default;
template<typename T = std::nullptr_t>
constexpr ox::Error setTypeInfo(CRStringView name = T::TypeName,
int version = T::TypeVersion,
const TypeParamPack &typeParams = {},
std::size_t fields = ModelFieldCount_v<T>) noexcept;
constexpr ox::Error setTypeInfo(StringViewCR name = T::TypeName,
int version = T::TypeVersion,
const TypeParamPack &typeParams = {},
std::size_t fields = ModelFieldCount_v<T>) noexcept;
template<typename T>
constexpr Error field(CRStringView name, const T *val, std::size_t valLen,
constexpr Error field(StringViewCR name, const T *val, std::size_t valLen,
const SubscriptStack &subscriptStack = {}) noexcept;
template<typename T, bool force>
constexpr Error field(CRStringView name, UnionView<T, force> val) noexcept;
constexpr Error field(StringViewCR name, UnionView<T, force> val) noexcept;
template<typename T>
constexpr Error field(CRStringView name, const T *val) noexcept;
constexpr Error field(StringViewCR name, const T *val) noexcept;
template<typename ...Args>
constexpr Error fieldCString(CRStringView name, Args&&...) noexcept;
constexpr Error fieldCString(StringViewCR name, Args&&...) noexcept;
[[nodiscard]]
constexpr DescriptorType *definition() noexcept {
@ -167,7 +167,7 @@ class TypeDescWriter {
constexpr const DescriptorType *type(UnionView<U> val) const noexcept;
[[nodiscard]]
constexpr const DescriptorType *getType(CRStringView tn, int typeVersion, PrimitiveType t, int b,
constexpr const DescriptorType *getType(StringViewCR tn, int typeVersion, PrimitiveType t, int b,
const TypeParamPack &typeParams = {}) const noexcept;
};
@ -176,8 +176,8 @@ constexpr TypeDescWriter::TypeDescWriter(TypeStore *typeStore) noexcept: m_typeS
template<typename T>
constexpr ox::Error TypeDescWriter::setTypeInfo(
CRStringView typeName, int typeVersion,
const TypeParamPack &typeParams, std::size_t) noexcept {
StringViewCR typeName, int typeVersion,
const TypeParamPack &typeParams, std::size_t) noexcept {
PrimitiveType pt;
if constexpr(is_union_v<T>) {
pt = PrimitiveType::Union;
@ -193,7 +193,7 @@ constexpr ox::Error TypeDescWriter::setTypeInfo(
// array handler
template<typename T>
constexpr Error TypeDescWriter::field(CRStringView name, const T*, std::size_t, const SubscriptStack &subscriptStack) noexcept {
constexpr Error TypeDescWriter::field(StringViewCR name, const T*, std::size_t, const SubscriptStack &subscriptStack) noexcept {
if (m_type) {
constexpr typename remove_pointer<T>::type *p = nullptr;
const auto t = type(p);
@ -205,7 +205,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, const T*, std::size_t,
}
template<typename T, bool force>
constexpr Error TypeDescWriter::field(CRStringView name, UnionView<T, force> val) noexcept {
constexpr Error TypeDescWriter::field(StringViewCR name, UnionView<T, force> val) noexcept {
if (m_type) {
const auto t = type(val);
oxAssert(t != nullptr, "field(const char *name, T val): Type not found or generated");
@ -216,7 +216,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, UnionView<T, force> val
}
template<typename T>
constexpr Error TypeDescWriter::field(CRStringView name, const T *val) noexcept {
constexpr Error TypeDescWriter::field(StringViewCR name, const T *val) noexcept {
if (m_type) {
if constexpr(isVector_v<T> || isArray_v<T>) {
typename T::value_type *data = nullptr;
@ -237,7 +237,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, const T *val) noexcept
}
template<typename ...Args>
constexpr Error TypeDescWriter::fieldCString(CRStringView name, Args&&...) noexcept {
constexpr Error TypeDescWriter::fieldCString(StringViewCR name, Args&&...) noexcept {
constexpr auto s = "";
const auto t = type(s);
m_type->fieldList.emplace_back(t, String(name), 0, SubscriptStack{}, ox::String(t->typeName));
@ -339,7 +339,7 @@ constexpr const DescriptorType *TypeDescWriter::type(const IString<sz>*) const n
return getType(types::BString, 0, PT, 0);
}
constexpr const DescriptorType *TypeDescWriter::getType(CRStringView tn, int typeVersion, PrimitiveType pt, int b,
constexpr const DescriptorType *TypeDescWriter::getType(StringViewCR tn, int typeVersion, PrimitiveType pt, int b,
const TypeParamPack &typeParams) const noexcept {
auto t = m_typeStore->get(tn, typeVersion, typeParams);
if (!t.error) {
@ -357,8 +357,8 @@ constexpr const DescriptorType *TypeDescWriter::getType(CRStringView tn, int typ
}
template<typename T>
constexpr Result<DescriptorType*> buildTypeDef(TypeStore *typeStore) noexcept {
TypeDescWriter writer(typeStore);
constexpr Result<DescriptorType*> buildTypeDef(TypeStore &typeStore) noexcept {
TypeDescWriter writer(&typeStore);
ModelHandlerInterface<TypeDescWriter, ox::OpType::Reflect> handler(&writer);
if (std::is_constant_evaluated()) {
std::allocator<T> a;
@ -373,10 +373,10 @@ constexpr Result<DescriptorType*> buildTypeDef(TypeStore *typeStore) noexcept {
}
template<typename T>
constexpr Result<DescriptorType*> buildTypeDef(TypeStore *typeStore, T *val) noexcept {
TypeDescWriter writer(typeStore);
constexpr Result<DescriptorType*> buildTypeDef(TypeStore &typeStore, T &val) noexcept {
TypeDescWriter writer(&typeStore);
ModelHandlerInterface<TypeDescWriter, ox::OpType::Reflect> handler(&writer);
oxReturnError(model(&handler, val));
oxReturnError(model(&handler, &val));
return writer.definition();
}

View File

@ -24,24 +24,24 @@ class FieldCounter {
std::size_t fields = 0;
template<typename U = std::nullptr_t>
constexpr ox::Error setTypeInfo(CRStringView = "", int = 0, const Vector<String>& = {}, std::size_t = 0) {
constexpr ox::Error setTypeInfo(StringViewCR = "", int = 0, const Vector<String>& = {}, std::size_t = 0) {
return {};
}
template<typename U>
constexpr ox::Error field(CRStringView, U) noexcept {
constexpr ox::Error field(StringViewCR, U) noexcept {
++fields;
return OxError(0);
}
template<typename U>
constexpr ox::Error field(CRStringView, U, std::size_t) noexcept {
constexpr ox::Error field(StringViewCR, U, std::size_t) noexcept {
++fields;
return OxError(0);
}
template<typename U, typename Handler>
constexpr Error field(CRStringView, Handler) {
constexpr Error field(StringViewCR, Handler) {
++fields;
return OxError(0);
}

View File

@ -652,7 +652,7 @@ class ModelObject {
}
[[nodiscard]]
constexpr CRString typeName() const noexcept {
constexpr StringCR typeName() const noexcept {
return m_type->typeName;
}

View File

@ -44,7 +44,7 @@ class TypeStore {
return out->get();
}
constexpr DescriptorType *getInit(CRStringView typeName, int typeVersion, PrimitiveType pt,
constexpr DescriptorType *getInit(StringViewCR typeName, int typeVersion, PrimitiveType pt,
const TypeParamPack &typeParams) noexcept {
const auto typeId = buildTypeId(typeName, typeVersion, typeParams);
auto &out = m_cache[typeId];
@ -105,7 +105,7 @@ class TypeStore {
return OxError(1);
}
Result<UniquePtr<DescriptorType>> loadDescriptor(ox::CRStringView name, int version,
Result<UniquePtr<DescriptorType>> loadDescriptor(ox::StringViewCR name, int version,
const ox::TypeParamPack &typeParams) noexcept {
const auto typeId = buildTypeId(name, version, typeParams);
return loadDescriptor(typeId);

View File

@ -207,7 +207,7 @@ const std::map<ox::StringView, ox::Error(*)()> tests = {
testIn.Union.Int = 93;
oxAssert(ox::writeOC(testIn).moveTo(dataBuff), "Data generation failed");
ox::TypeStore typeStore;
auto type = ox::buildTypeDef(&typeStore, &testIn);
auto type = ox::buildTypeDef(typeStore, testIn);
oxAssert(type.error, "Descriptor write failed");
ox::ModelObject testOut;
oxReturnError(testOut.setType(type.value));
@ -257,7 +257,7 @@ const std::map<ox::StringView, ox::Error(*)()> tests = {
auto [oc, ocErr] = ox::writeOC(testIn);
oxAssert(ocErr, "Data generation failed");
ox::TypeStore typeStore;
auto type = ox::buildTypeDef(&typeStore, &testIn);
auto type = ox::buildTypeDef(typeStore, testIn);
oxAssert(type.error, "Descriptor write failed");
oxReturnError(ox::walkModel<ox::OrganicClawReader>(type.value, oc.data(), oc.size(),
[](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::String>&, const ox::DescriptorField &f,

View File

@ -24,7 +24,8 @@ namespace ox {
class OrganicClawWriter {
friend Result<Buffer> writeOC(const auto &val) noexcept;
friend Result<ox::Buffer> writeOC(const auto &val) noexcept;
friend Result<ox::String> writeOCString(const auto &val) noexcept;
protected:
Json::Value m_json{Json::Value(Json::objectValue)};
@ -247,14 +248,27 @@ Error OrganicClawWriter::field(const char *key, UnionView<U, force> val) noexcep
return OxError(0);
}
Result<Buffer> writeOC(const auto &val) noexcept {
Result<ox::Buffer> writeOC(const auto &val) noexcept {
OrganicClawWriter writer;
ModelHandlerInterface<OrganicClawWriter, OpType::Write> handler(&writer);
oxReturnError(model(&handler, &val));
Json::StreamWriterBuilder jsonBuilder;
Json::StreamWriterBuilder const jsonBuilder;
const auto str = Json::writeString(jsonBuilder, writer.m_json);
Buffer buff(str.size() + 1);
memcpy(buff.data(), str.c_str(), str.size() + 1);
Result<Buffer> buff;
buff.value.resize(str.size() + 1);
memcpy(buff.value.data(), str.data(), str.size() + 1);
return buff;
}
Result<ox::String> writeOCString(const auto &val) noexcept {
OrganicClawWriter writer;
ModelHandlerInterface<OrganicClawWriter, OpType::Write> handler(&writer);
oxReturnError(model(&handler, &val));
Json::StreamWriterBuilder const jsonBuilder;
const auto str = Json::writeString(jsonBuilder, writer.m_json);
Result<ox::String> buff;
buff.value.resize(str.size());
memcpy(buff.value.data(), str.data(), str.size() + 1);
return buff;
}

View File

@ -46,12 +46,12 @@ struct AlignmentCatcher: public ModelHandlerBase<AlignmentCatcher<PlatSpec>, OpT
}
template<typename T, bool force>
constexpr ox::Error field(CRStringView name, const UnionView<T, force> val) noexcept {
constexpr ox::Error field(StringViewCR name, const UnionView<T, force> val) noexcept {
return field(name, val.get());
}
template<typename T>
constexpr ox::Error field(CRStringView, const T *val) noexcept {
constexpr ox::Error field(StringViewCR, const T *val) noexcept {
if constexpr(ox::is_pointer_v<T> || ox::is_integer_v<T>) {
biggestAlignment = ox::max(biggestAlignment, PlatSpec::alignOf(*val));
} else {
@ -61,7 +61,7 @@ struct AlignmentCatcher: public ModelHandlerBase<AlignmentCatcher<PlatSpec>, OpT
}
template<typename T>
constexpr ox::Error field(CRStringView, const T *val, std::size_t cnt) noexcept {
constexpr ox::Error field(StringViewCR, const T *val, std::size_t cnt) noexcept {
for (std::size_t i = 0; i < cnt; ++i) {
oxReturnError(field(nullptr, &val[i]));
}

View File

@ -94,19 +94,19 @@ class Preloader: public ModelHandlerBase<Preloader<PlatSpec>, OpType::Reflect> {
}
template<typename U, bool force>
constexpr ox::Error field(CRStringView, ox::UnionView<U, force> val) noexcept;
constexpr ox::Error field(StringViewCR, ox::UnionView<U, force> val) noexcept;
template<typename T>
constexpr ox::Error field(CRStringView, const T *val) noexcept;
constexpr ox::Error field(StringViewCR, const T *val) noexcept;
template<std::size_t SmallStringSize>
constexpr ox::Error field(CRStringView, const ox::BasicString<SmallStringSize> *val) noexcept;
constexpr ox::Error field(StringViewCR, const ox::BasicString<SmallStringSize> *val) noexcept;
template<typename T, std::size_t sz>
constexpr ox::Error field(CRStringView, const ox::Array<T, sz> *valArray) noexcept;
constexpr ox::Error field(StringViewCR, const ox::Array<T, sz> *valArray) noexcept;
template<typename T>
constexpr ox::Error field(CRStringView, const T **val, std::size_t cnt) noexcept;
constexpr ox::Error field(StringViewCR, const T **val, std::size_t cnt) noexcept;
constexpr ox::Result<std::size_t> startAlloc(size_t sz, size_t align) noexcept;
@ -125,14 +125,14 @@ class Preloader: public ModelHandlerBase<Preloader<PlatSpec>, OpType::Reflect> {
constexpr ox::Error pad(const T*) noexcept;
private:
constexpr ox::Error fieldVector(CRStringView name, const ox::ModelValueVector *val) noexcept;
constexpr ox::Error fieldVector(StringViewCR name, const ox::ModelValueVector *val) noexcept;
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr ox::Error fieldVector(CRStringView, const ox::Vector<T, SmallVectorSize, Allocator> *val) noexcept;
constexpr ox::Error fieldVector(StringViewCR, const ox::Vector<T, SmallVectorSize, Allocator> *val) noexcept;
constexpr ox::Error fieldVector(CRStringView, const auto *val, ox::VectorMemMap<PlatSpec> vecVal) noexcept;
constexpr ox::Error fieldVector(StringViewCR, const auto *val, ox::VectorMemMap<PlatSpec> vecVal) noexcept;
constexpr ox::Error fieldArray(CRStringView name, ox::ModelValueArray const*val) noexcept;
constexpr ox::Error fieldArray(StringViewCR name, ox::ModelValueArray const*val) noexcept;
constexpr bool unionCheckAndIt() noexcept;
@ -158,7 +158,7 @@ Preloader<PlatSpec>::make(ox::ios_base::seekdir anchor, std::size_t sz) noexcept
template<typename PlatSpec>
template<typename U, bool force>
constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::UnionView<U, force> val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::field(StringViewCR, const ox::UnionView<U, force> val) noexcept {
if (!unionCheckAndIt()) {
return {};
}
@ -171,7 +171,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::UnionView
template<typename PlatSpec>
template<typename T>
constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const T *val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::field(StringViewCR name, const T *val) noexcept {
if (!unionCheckAndIt()) {
return {};
}
@ -200,7 +200,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const T *val)
template<typename PlatSpec>
template<std::size_t SmallStringSize>
constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::BasicString<SmallStringSize> *val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::field(StringViewCR, const ox::BasicString<SmallStringSize> *val) noexcept {
if (!unionCheckAndIt()) {
return {};
}
@ -230,7 +230,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::BasicStri
template<typename PlatSpec>
template<typename T, std::size_t sz>
constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const ox::Array<T, sz> *val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::field(StringViewCR name, const ox::Array<T, sz> *val) noexcept {
if (!unionCheckAndIt()) {
return {};
}
@ -248,7 +248,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView name, const ox::Arra
template<typename PlatSpec>
template<typename T>
constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const T **val, std::size_t cnt) noexcept {
constexpr ox::Error Preloader<PlatSpec>::field(StringViewCR, const T **val, std::size_t cnt) noexcept {
if (!unionCheckAndIt()) {
return {};
}
@ -327,7 +327,7 @@ constexpr ox::Error Preloader<PlatSpec>::pad(const T *v) noexcept {
template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::fieldVector(
CRStringView name, const ox::ModelValueVector *val) noexcept {
StringViewCR name, const ox::ModelValueVector *val) noexcept {
// serialize the Vector
ox::VectorMemMap<PlatSpec> vecVal{
.size = PlatSpec::correctEndianness(static_cast<typename PlatSpec::size_t>(val->size())),
@ -339,7 +339,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
template<typename PlatSpec>
template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr ox::Error Preloader<PlatSpec>::fieldVector(
CRStringView name, const ox::Vector<T, SmallVectorSize, Allocator> *val) noexcept {
StringViewCR name, const ox::Vector<T, SmallVectorSize, Allocator> *val) noexcept {
// serialize the Vector
ox::VectorMemMap<PlatSpec> vecVal{
.smallVecSize = SmallVectorSize * sizeOf<PlatSpec>(static_cast<T*>(nullptr)),
@ -353,7 +353,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::fieldVector(
CRStringView, const auto *val, ox::VectorMemMap<PlatSpec> vecVal) noexcept {
StringViewCR, const auto *val, ox::VectorMemMap<PlatSpec> vecVal) noexcept {
oxReturnError(pad(&vecVal));
const auto vecValPt = m_writer.tellp();
// serialize the Vector elements
@ -383,7 +383,7 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
}
template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::fieldArray(CRStringView, ox::ModelValueArray const*val) noexcept {
constexpr ox::Error Preloader<PlatSpec>::fieldArray(StringViewCR, ox::ModelValueArray const*val) noexcept {
oxReturnError(pad(&(*val)[0]));
for (auto const&v : *val) {
oxReturnError(this->interface()->field({}, &v));

View File

@ -41,7 +41,7 @@ class UnionSizeCatcher: public ModelHandlerBase<UnionSizeCatcher<PlatSpec>, OpTy
}
template<typename T, bool force>
constexpr ox::Error field(CRStringView, const UnionView<T, force> val) noexcept {
constexpr ox::Error field(StringViewCR, const UnionView<T, force> val) noexcept {
UnionSizeCatcher<PlatSpec> sc;
oxReturnError(model(sc.interface(), val.get()));
m_size += sc.size();
@ -49,10 +49,10 @@ class UnionSizeCatcher: public ModelHandlerBase<UnionSizeCatcher<PlatSpec>, OpTy
}
template<typename T>
constexpr ox::Error field(CRStringView, const T *val) noexcept;
constexpr ox::Error field(StringViewCR, const T *val) noexcept;
template<typename T>
constexpr ox::Error field(CRStringView, const T **val, std::size_t cnt) noexcept;
constexpr ox::Error field(StringViewCR, const T **val, std::size_t cnt) noexcept;
[[nodiscard]]
constexpr auto size() const noexcept {
@ -61,24 +61,24 @@ class UnionSizeCatcher: public ModelHandlerBase<UnionSizeCatcher<PlatSpec>, OpTy
private:
template<typename T, std::size_t SmallVecSize>
constexpr ox::Error fieldStr(CRStringView, const ox::BasicString<SmallVecSize> *val) noexcept;
constexpr ox::Error fieldStr(StringViewCR, const ox::BasicString<SmallVecSize> *val) noexcept;
template<typename T, std::size_t SmallVecSize>
constexpr ox::Error fieldVector(CRStringView, const ox::Vector<T, SmallVecSize> *val) noexcept;
constexpr ox::Error fieldVector(StringViewCR, const ox::Vector<T, SmallVecSize> *val) noexcept;
constexpr void setSize(std::size_t sz) noexcept;
};
template<typename PlatSpec>
template<typename T>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(CRStringView, const T *val) noexcept {
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(StringViewCR, const T *val) noexcept {
setSize(sizeOf<PlatSpec>(val));
return {};
}
template<typename PlatSpec>
template<typename T>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(CRStringView, const T **val, std::size_t cnt) noexcept {
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(StringViewCR, const T **val, std::size_t cnt) noexcept {
for (std::size_t i = 0; i < cnt; ++i) {
oxReturnError(field("", &val[i]));
}
@ -87,7 +87,7 @@ constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(CRStringView, const T **va
template<typename PlatSpec>
template<typename T, std::size_t SmallStrSize>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldStr(CRStringView, const ox::BasicString<SmallStrSize>*) noexcept {
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldStr(StringViewCR, const ox::BasicString<SmallStrSize>*) noexcept {
ox::VectorMemMap<PlatSpec> v;
setSize(sizeOf<PlatSpec>(v));
return {};
@ -95,7 +95,7 @@ constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldStr(CRStringView, const ox:
template<typename PlatSpec>
template<typename T, std::size_t SmallVecSize>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldVector(CRStringView, const ox::Vector<T, SmallVecSize>*) noexcept {
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldVector(StringViewCR, const ox::Vector<T, SmallVecSize>*) noexcept {
ox::VectorMemMap<PlatSpec> v;
setSize(sizeOf<PlatSpec>(v));
return {};

View File

@ -41,7 +41,7 @@ class Array {
constexpr Array(Array &&other) noexcept;
~Array() = default;
constexpr ~Array() = default;
constexpr iterator<> begin() noexcept {
return iterator<>(&m_items[0], 0, ArraySize);
@ -81,8 +81,10 @@ class Array {
constexpr Array &operator=(Array &&other) noexcept;
[[nodiscard]]
constexpr T &operator[](std::size_t i) noexcept;
[[nodiscard]]
constexpr const T &operator[](std::size_t i) const noexcept;
[[nodiscard]]

View File

@ -22,9 +22,9 @@
namespace ox {
void panic(CRStringView file, int line, CRStringView panicMsg, const Error &err = OxError(0)) noexcept;
void panic(StringViewCR file, int line, StringViewCR panicMsg, const Error &err = OxError(0)) noexcept;
constexpr void constexprPanic(CRStringView file, int line, CRStringView panicMsg, const Error &err = OxError(0)) noexcept {
constexpr void constexprPanic(StringViewCR file, int line, StringViewCR panicMsg, const Error &err = OxError(0)) noexcept {
if (!std::is_constant_evaluated()) {
panic(file, line, panicMsg, err);
} else {
@ -32,12 +32,12 @@ constexpr void constexprPanic(CRStringView file, int line, CRStringView panicMsg
}
}
constexpr void assertFunc(CRStringView file, int line, bool pass, [[maybe_unused]]CRStringView assertTxt, [[maybe_unused]]CRStringView msg) noexcept {
constexpr void assertFunc(StringViewCR file, int line, bool pass, [[maybe_unused]]StringViewCR assertTxt, [[maybe_unused]]StringViewCR msg) noexcept {
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);
auto output = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
output += genStackTrace(2);
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
std::abort();
#else
@ -51,19 +51,20 @@ constexpr void assertFunc(CRStringView file, int line, bool pass, [[maybe_unused
}
}
constexpr void assertFunc(CRStringView file, int line, const Error &err, CRStringView, CRStringView assertMsg) noexcept {
constexpr void assertFunc(StringViewCR file, int line, const Error &err, StringViewCR, StringViewCR assertMsg) noexcept {
if (err) {
if (!std::is_constant_evaluated()) {
#if defined(OX_USE_STDLIB)
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, assertMsg);
auto msg = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, assertMsg);
if (err.msg) {
oxErrf("\tError Message:\t{}\n", err.msg);
msg += sfmt("\tError Message:\t{}\n", err.msg);
}
oxErrf("\tError Code:\t{}\n", static_cast<ErrorCode>(err));
msg += sfmt("\tError Code:\t{}\n", static_cast<ErrorCode>(err));
if (err.file != nullptr) {
oxErrf("\tError Location:\t{}:{}\n", err.file, err.line);
msg += sfmt("\tError Location:\t{}:{}\n", err.file, err.line);
}
printStackTrace(2);
msg += genStackTrace(2);
oxErr(msg);
oxTracef("assert", "Failed assert: {} [{}:{}]", assertMsg, file, line);
std::abort();
#else
@ -75,7 +76,7 @@ constexpr void assertFunc(CRStringView file, int line, const Error &err, CRStrin
}
}
constexpr void expect(CRStringView file, int line, const auto &actual, const auto &expected) noexcept {
constexpr void expect(StringViewCR file, int line, const auto &actual, const auto &expected) noexcept {
if (actual != expected) {
if (!std::is_constant_evaluated()) {
#if defined(OX_USE_STDLIB)

View File

@ -8,8 +8,6 @@
#pragma once
#include "error.hpp"
namespace ox {
template<typename T>
@ -18,13 +16,12 @@ class Defer {
T m_deferredFunc;
public:
Defer(T deferredFunc) {
m_deferredFunc = deferredFunc;
constexpr Defer(T deferredFunc) noexcept: m_deferredFunc(deferredFunc) {
}
Defer(const Defer&) = delete;
~Defer() {
constexpr ~Defer() {
m_deferredFunc();
}
@ -34,4 +31,4 @@ class Defer {
}
#define oxDefer ox::Defer oxConcat(oxDefer_, __LINE__) = [&]
#define oxDefer ox::Defer const oxConcat(oxDefer_, __LINE__) =

View File

@ -165,21 +165,24 @@ struct [[nodiscard]] Result {
return error == 0;
}
constexpr Error copyTo(type &val) const& noexcept {
template<typename U>
constexpr Error copyTo(U &val) const& noexcept {
if (!error) [[likely]] {
val = value;
}
return error;
}
constexpr Error copyTo(type &val) && noexcept {
template<typename U>
constexpr Error copyTo(U &val) && noexcept {
if (!error) [[likely]] {
val = std::move(value);
}
return error;
}
constexpr Error moveTo(type &val) noexcept {
template<typename U>
constexpr Error moveTo(U &val) noexcept {
if (!error) [[likely]] {
val = std::move(value);
}

View File

@ -64,6 +64,9 @@ class HashMap {
[[nodiscard]]
constexpr Vector<K> const&keys() const noexcept;
[[nodiscard]]
constexpr Vector<T> values() const noexcept;
constexpr void clear();
private:
@ -198,6 +201,16 @@ constexpr Vector<K> const&HashMap<K, T>::keys() const noexcept {
return m_keys;
}
template<typename K, typename T>
constexpr Vector<T> HashMap<K, T>::values() const noexcept {
Vector<T> out;
out.reserve(m_pairs.size());
for (auto const&p : m_pairs) {
out.emplace_back(p->value);
}
return out;
}
template<typename K, typename T>
constexpr void HashMap<K, T>::clear() {
for (std::size_t i = 0; i < m_pairs.size(); i++) {

View File

@ -35,7 +35,7 @@ class IString {
constexpr IString(const char *str) noexcept;
constexpr IString &operator=(CRStringView str) noexcept;
constexpr IString &operator=(StringViewCR str) noexcept;
constexpr IString &operator=(const char *str) noexcept;
@ -119,7 +119,7 @@ constexpr IString<size> &IString<size>::operator=(Integer_c auto i) noexcept {
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator=(ox::CRStringView str) noexcept {
constexpr IString<size> &IString<size>::operator=(ox::StringViewCR str) noexcept {
std::size_t strLen = str.len();
if (cap() < strLen) {
strLen = cap();

View File

@ -57,6 +57,28 @@ static auto symbolicate([[maybe_unused]]void **frames,
}
#endif // defined(OX_USE_STDLIB) && __has_include(<unistd.h>)
ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept {
ox::String out;
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) && __has_include(<execinfo.h>)
constexpr auto FrameCnt = 100;
Vector<void*, FrameCnt> frames(FrameCnt);
#ifdef OX_OS_FreeBSD
using FrameCnt_t = unsigned;
#else
using FrameCnt_t = signed;
#endif
frames.resize(static_cast<std::size_t>(backtrace(frames.data(), static_cast<FrameCnt_t>(frames.size()))));
if (frames.size() - shave > 2) {
const auto symbolicatedStacktrace = symbolicate(frames.data() + shave, frames.size() - shave, "\t");
out+= "Stacktrace:\n";
for (const auto &s : symbolicatedStacktrace) {
out += sfmt("\t{}\n", s);
}
}
#endif
return out;
}
void printStackTrace([[maybe_unused]]unsigned shave) noexcept {
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) && __has_include(<execinfo.h>)
constexpr auto FrameCnt = 100;

View File

@ -8,8 +8,13 @@
#pragma once
#include "string.hpp"
namespace ox {
[[nodiscard]]
ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept;
/**
* Prints a stack trace to stderr.
*

View File

@ -46,7 +46,7 @@ class BasicString {
constexpr explicit BasicString(StringLiteral const&str) noexcept;
constexpr explicit BasicString(CRStringView str) noexcept;
constexpr explicit BasicString(StringViewCR str) noexcept;
constexpr explicit BasicString(BasicString const&) noexcept;
@ -126,7 +126,7 @@ class BasicString {
constexpr BasicString &operator=(BasicString &&src) noexcept;
constexpr BasicString &operator=(CRStringView src) noexcept;
constexpr BasicString &operator=(StringViewCR src) noexcept;
constexpr BasicString &operator+=(const char *str) noexcept;
@ -148,7 +148,7 @@ class BasicString {
constexpr BasicString operator+(Integer_c auto i) const noexcept;
constexpr BasicString operator+(CRStringView src) const noexcept;
constexpr BasicString operator+(StringViewCR src) const noexcept;
constexpr BasicString operator+(BasicString const&src) const noexcept;
@ -243,7 +243,7 @@ class BasicString {
constexpr std::size_t bytes() const noexcept;
private:
constexpr void set(CRStringView str) noexcept;
constexpr void set(StringViewCR str) noexcept;
constexpr void set(const char8_t *str) noexcept;
};
@ -278,7 +278,7 @@ constexpr BasicString<SmallStringSize_v>::BasicString(StringLiteral const&str) n
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
constexpr BasicString<SmallStringSize_v>::BasicString(StringViewCR str) noexcept {
set(str);
}
@ -343,7 +343,7 @@ constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operat
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(CRStringView src) noexcept {
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(StringViewCR src) noexcept {
set(src);
return *this;
}
@ -416,7 +416,7 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(CRStringView src) const noexcept {
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(StringViewCR src) const noexcept {
const std::size_t strLen = src.len();
const auto currentLen = len();
BasicString<SmallStringSize_v> cpy(currentLen + strLen);
@ -521,7 +521,7 @@ constexpr std::size_t BasicString<SmallStringSize_v>::len() const noexcept {
}
template<std::size_t SmallStringSize_v>
constexpr void BasicString<SmallStringSize_v>::set(CRStringView str) noexcept {
constexpr void BasicString<SmallStringSize_v>::set(StringViewCR str) noexcept {
std::size_t const strBytes = str.bytes();
m_buff.resize(strBytes + 1);
copy_n(str.data(), strBytes, m_buff.data());
@ -539,11 +539,11 @@ constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept
extern template class BasicString<8>;
using String = BasicString<8>;
using CRString = String const&;
using StringCR = String const&;
[[nodiscard]]
constexpr ox::String toString(ox::CRStringView sv) noexcept {
constexpr ox::String toString(ox::StringViewCR sv) noexcept {
return ox::String(sv);
}

View File

@ -58,16 +58,16 @@ class StringView: public detail::BaseStringView {
};
using CRStringView = const StringView&;
using StringViewCR = const StringView&;
constexpr auto operator==(CRStringView s1, CRStringView s2) noexcept {
constexpr auto operator==(StringViewCR s1, StringViewCR s2) noexcept {
if (s2.len() != s1.len()) {
return false;
}
return ox::strncmp(s1.data(), s2.data(), s1.len()) == 0;
}
constexpr auto operator<=>(CRStringView s1, CRStringView s2) noexcept {
constexpr auto operator<=>(StringViewCR s1, StringViewCR s2) noexcept {
const auto maxLen = ox::min(s1.len(), s2.len());
const auto a = &s1.front();
const auto b = &s2.front();
@ -87,18 +87,18 @@ constexpr auto operator<=>(CRStringView s1, CRStringView s2) noexcept {
}
}
constexpr auto write(Writer_c auto &writer, ox::CRStringView sv) noexcept {
constexpr auto write(Writer_c auto &writer, ox::StringViewCR sv) noexcept {
return writer.write(sv.data(), sv.bytes());
}
#ifdef OX_USE_STDLIB
constexpr auto toStdStringView(CRStringView sv) noexcept {
constexpr auto toStdStringView(StringViewCR sv) noexcept {
return std::string_view(sv.data(), sv.bytes());
}
#endif
constexpr ox::Result<int> atoi(ox::CRStringView str) noexcept {
constexpr ox::Result<int> atoi(ox::StringViewCR str) noexcept {
int total = 0;
int multiplier = 1;
for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) {

View File

@ -34,19 +34,19 @@ constexpr ox::StringView substr(ox::StringView const&str, std::size_t start, std
}
[[nodiscard]]
constexpr bool beginsWith(CRStringView base, CRStringView beginning) noexcept {
constexpr bool beginsWith(StringViewCR base, StringViewCR beginning) noexcept {
const auto beginningLen = ox::min(beginning.len(), base.len());
return base.len() >= beginning.len() && ox::strncmp(base.data(), beginning, beginningLen) == 0;
}
[[nodiscard]]
constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept {
constexpr bool endsWith(StringViewCR base, StringViewCR ending) noexcept {
const auto endingLen = ending.len();
return base.len() >= endingLen && ox::strcmp(base.data() + (base.len() - endingLen), ending) == 0;
}
[[nodiscard]]
constexpr std::size_t find(CRStringView str, char search) noexcept {
constexpr std::size_t find(StringViewCR str, char search) noexcept {
std::size_t i = 0;
for (; i < str.len(); ++i) {
if (str[i] == search) {
@ -57,7 +57,7 @@ constexpr std::size_t find(CRStringView str, char search) noexcept {
}
[[nodiscard]]
constexpr std::size_t find(CRStringView str, CRStringView search) noexcept {
constexpr std::size_t find(StringViewCR str, StringViewCR search) noexcept {
std::size_t i = 0;
for (; i < str.len(); ++i) {
if (beginsWith(substr(str, i), search)) {
@ -69,9 +69,9 @@ constexpr std::size_t find(CRStringView str, CRStringView search) noexcept {
template<std::size_t smallSz = 0>
[[nodiscard]]
constexpr ox::Vector<ox::StringView, smallSz> split(CRStringView str, char del) noexcept {
constexpr ox::Vector<ox::StringView, smallSz> split(StringViewCR str, char del) noexcept {
ox::Vector<ox::StringView, smallSz> out;
constexpr auto nextSeg = [](CRStringView current, char del) {
constexpr auto nextSeg = [](StringViewCR current, char del) {
return substr(current, find(current, del) + 1);
};
for (auto current = str; current.len(); current = nextSeg(current, del)) {
@ -86,9 +86,9 @@ constexpr ox::Vector<ox::StringView, smallSz> split(CRStringView str, char del)
template<std::size_t smallSz = 0>
[[nodiscard]]
constexpr ox::Vector<ox::StringView, smallSz> split(CRStringView str, CRStringView del) noexcept {
constexpr ox::Vector<ox::StringView, smallSz> split(StringViewCR str, StringViewCR del) noexcept {
ox::Vector<ox::StringView, smallSz> out;
constexpr auto nextSeg = [](CRStringView current, CRStringView del) {
constexpr auto nextSeg = [](StringViewCR current, StringViewCR del) {
return substr(current, find(current, del) + del.len());
};
for (auto current = str; current.len(); current = nextSeg(current, del)) {
@ -102,7 +102,7 @@ constexpr ox::Vector<ox::StringView, smallSz> split(CRStringView str, CRStringVi
}
[[nodiscard]]
constexpr ox::Result<std::size_t> lastIndexOf(ox::CRStringView str, int character) noexcept {
constexpr ox::Result<std::size_t> lastIndexOf(ox::StringViewCR str, int character) noexcept {
ox::Result<std::size_t> retval = OxError(1, "Character not found");
for (auto i = static_cast<int>(str.bytes() - 1); i >= 0; --i) {
if (str[static_cast<std::size_t>(i)] == character) {

View File

@ -24,10 +24,10 @@ static const auto OxPrintTrace = std::getenv("OXTRACE") != nullptr;
#define REG_MGBA_DEBUG_FLAGS *reinterpret_cast<volatile uint16_t*>(0x4FFF700)
#define REG_MGBA_DEBUG_STRING (reinterpret_cast<char*>(0x4FFF600))
inline void nullLog(ox::CRStringView) {}
inline void (*infoLog)(ox::CRStringView) = nullLog;
inline void (*debugLog)(ox::CRStringView) = nullLog;
inline void (*errorLog)(ox::CRStringView) = nullLog;
inline void nullLog(ox::StringViewCR) {}
inline void (*infoLog)(ox::StringViewCR) = nullLog;
inline void (*debugLog)(ox::StringViewCR) = nullLog;
inline void (*errorLog)(ox::StringViewCR) = nullLog;
namespace mgba {
@ -40,7 +40,7 @@ enum LogChan {
};
template<LogChan chan>
static void log(ox::CRStringView str) {
static void log(ox::StringViewCR str) {
const auto sz = ox::min<std::size_t>(0x100, str.bytes());
ox::strncpy(REG_MGBA_DEBUG_STRING, str.data(), sz);
REG_MGBA_DEBUG_FLAGS = chan | 0x100;
@ -65,23 +65,28 @@ void oxTraceInitHook() {
void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
[[maybe_unused]] const char *ch, [[maybe_unused]] const char *msg) {
#if defined(OX_USE_STDLIB)
auto const chv = ox::StringView{ch};
if (OxPrintTrace) {
auto m = std::string_view{msg};
if (m.ends_with('\n')) {
m = std::string_view{msg, m.size() - 1};
}
std::cout << std::setw(53) << std::left << ch << "| ";
std::cout << std::setw(65) << std::left << msg << '|';
std::cout << std::setw(65) << std::left << m << '|';
std::cout << " " << file << ':' << line << "\n";
} else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) {
} else if (chv == "debug" || chv == "info") {
printf("%s\n", msg);
fflush(stdout);
} else if (ox::strcmp(ch, "stdout") == 0) {
std::ignore = fflush(stdout);
} else if (chv == "stdout") {
printf("%s", msg);
fflush(stdout);
} else if (ox::strcmp(ch, "stderr") == 0) {
printf("%s", msg);
fflush(stdout);
} else if (ox::strcmp(ch, "error") == 0) {
std::ignore = fflush(stdout);
} else if (chv == "stderr") {
std::ignore = fprintf(stderr, "%s", msg);
std::ignore = fflush(stderr);
} else if (chv == "error") {
//std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg);
fflush(stderr);
std::ignore = fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg);
std::ignore = fflush(stderr);
}
#else
if (ox::strcmp(ch, "info") == 0) {

View File

@ -31,7 +31,7 @@ constexpr auto isHexChar(char c) noexcept {
|| (c >= 'A' && c <= 'F');
}
constexpr ox::Result<uint8_t> fromHex(ox::CRStringView v) noexcept {
constexpr ox::Result<uint8_t> fromHex(ox::StringViewCR v) noexcept {
constexpr auto valMap = [] {
ox::Array<uint8_t, 128> out;
out['A'] = out['a'] = 10;
@ -125,7 +125,7 @@ class UUID {
}
}
static constexpr ox::Result<ox::UUID> fromString(ox::CRStringView s) noexcept {
static constexpr ox::Result<ox::UUID> fromString(ox::StringViewCR s) noexcept {
if (s.len() < 36) {
return OxError(1, "Insufficient data to contain a complete UUID");
}

View File

@ -222,8 +222,10 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
constexpr Vector &operator=(Vector &&other) noexcept;
[[nodiscard]]
constexpr T &operator[](std::size_t i) noexcept;
[[nodiscard]]
constexpr const T &operator[](std::size_t i) const noexcept;
[[nodiscard]]