diff --git a/src/ox/fs/pathiterator.cpp b/src/ox/fs/pathiterator.cpp index 17b7eca8d..596116959 100644 --- a/src/ox/fs/pathiterator.cpp +++ b/src/ox/fs/pathiterator.cpp @@ -20,24 +20,26 @@ PathIterator::PathIterator(const char *path, size_t maxSize) { int PathIterator::next(char *pathOut, size_t pathOutSize) { size_t size = 0; - const char *substr = ox_strchr(m_path + m_iterator, '/', m_maxSize - m_iterator); - if (substr) { - m_iterator = (substr - m_path) + 1; - if (m_iterator < m_maxSize) { - size_t start = m_iterator; - // end is at the next / - substr = ox_strchr(&m_path[start], '/', m_maxSize - start); - // correct end if it is invalid, which happens if there is no next / - if (!substr) { - substr = ox_strchr(&m_path[start], 0, m_maxSize - start); - } - size_t end = substr - m_path; - size = end - start; - ox_memcpy(pathOut, &m_path[start], size); + int retval = 1; + if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { + retval = 0; + if (m_path[m_iterator] == '/') { + m_iterator++; } + size_t start = m_iterator; + // end is at the next / + const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start); + // correct end if it is invalid, which happens if there is no next / + if (!substr) { + substr = ox_strchr(&m_path[start], 0, m_maxSize - start); + } + size_t end = substr - m_path; + size = end - start; + ox_memcpy(pathOut, &m_path[start], size); } pathOut[size] = 0; // end with null terminator - return 0; + m_iterator += size; + return retval; } } diff --git a/src/ox/fs/test/CMakeLists.txt b/src/ox/fs/test/CMakeLists.txt index aeb7ad754..2ee7a32a5 100644 --- a/src/ox/fs/test/CMakeLists.txt +++ b/src/ox/fs/test/CMakeLists.txt @@ -31,3 +31,4 @@ add_test("FileStoreIO" FileStoreIO) add_test("Test\\ PathIterator1" FSTests PathIterator1) add_test("Test\\ PathIterator2" FSTests PathIterator2) add_test("Test\\ PathIterator3" FSTests PathIterator3) +add_test("Test\\ PathIterator4" FSTests PathIterator4) diff --git a/src/ox/fs/test/tests.cpp b/src/ox/fs/test/tests.cpp index 903efa69b..8e6607b88 100644 --- a/src/ox/fs/test/tests.cpp +++ b/src/ox/fs/test/tests.cpp @@ -19,7 +19,7 @@ using namespace ox::std; map tests = { { - { + { "PathIterator1", [](string) { int retval = 0; @@ -34,7 +34,7 @@ map tests = { return retval; } }, - { + { "PathIterator2", [](string) { int retval = 0; @@ -48,7 +48,7 @@ map tests = { return retval; } }, - { + { "PathIterator3", [](string) { int retval = 0; @@ -61,6 +61,35 @@ map tests = { return retval; } }, + { + "PathIterator4", + [](string) { + int retval = 0; + string path = "usr/share/charset.gbag"; + PathIterator it(path.c_str(), path.size()); + const auto buffSize = 1024; + char buff[buffSize]; + assert(buffSize >= path.size()); + retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "usr") == 0); + retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "share") == 0); + retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "charset.gbag") == 0); + return retval; + } + }, + { + "PathIterator5", + [](string) { + int retval = 0; + string path = "usr/share/"; + PathIterator it(path.c_str(), path.size()); + const auto buffSize = 1024; + char buff[buffSize]; + assert(buffSize >= path.size()); + retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "usr") == 0); + retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "share") == 0); + return retval; + } + }, }, };