Fix PathIterator to allow paths that don't start with /
This commit is contained in:
+17
-15
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -19,7 +19,7 @@ using namespace ox::std;
|
||||
|
||||
map<string, int(*)(string)> tests = {
|
||||
{
|
||||
{
|
||||
{
|
||||
"PathIterator1",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
@@ -34,7 +34,7 @@ map<string, int(*)(string)> tests = {
|
||||
return retval;
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"PathIterator2",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
@@ -48,7 +48,7 @@ map<string, int(*)(string)> tests = {
|
||||
return retval;
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
"PathIterator3",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
@@ -61,6 +61,35 @@ map<string, int(*)(string)> 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;
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user