[ox/fs] Cleanup

This commit is contained in:
2024-09-13 00:05:26 -05:00
parent 8dd837b359
commit 702b166b8d
5 changed files with 75 additions and 160 deletions

View File

@@ -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;
}