Add PathIterator class for file system
This commit is contained in:
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8)
|
||||
add_library(
|
||||
OxFS
|
||||
filesystem.cpp
|
||||
pathiterator.cpp
|
||||
)
|
||||
|
||||
set_property(
|
||||
@@ -26,6 +27,7 @@ install(
|
||||
filestore.hpp
|
||||
filesystem.hpp
|
||||
inodemgr.hpp
|
||||
pathiterator.hpp
|
||||
DESTINATION
|
||||
include/ox/fs
|
||||
)
|
||||
|
||||
@@ -74,7 +74,7 @@ template<typename FileStore, FsType FS_TYPE>
|
||||
class FileSystemTemplate: public FileSystem {
|
||||
|
||||
private:
|
||||
struct DirectoryEntry {
|
||||
struct __attribute__((packed)) DirectoryEntry {
|
||||
typename FileStore::InodeId_t inode;
|
||||
|
||||
char *getName() {
|
||||
@@ -89,7 +89,7 @@ class FileSystemTemplate: public FileSystem {
|
||||
}
|
||||
};
|
||||
|
||||
struct Directory {
|
||||
struct __attribute__((packed)) Directory {
|
||||
/**
|
||||
* Number of files in this directory.
|
||||
*/
|
||||
@@ -110,7 +110,7 @@ class FileSystemTemplate: public FileSystem {
|
||||
|
||||
int mkdir(const char *path);
|
||||
|
||||
int read(const char *path, void *buffer);
|
||||
int read(const char *path, void *buffer, size_t buffSize);
|
||||
|
||||
int read(uint64_t inode, void *buffer, size_t buffSize) override;
|
||||
|
||||
@@ -174,6 +174,17 @@ FileStat FileSystemTemplate<FileStore, FS_TYPE>::stat(uint64_t inode) {
|
||||
#pragma warning(default:4244)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
template<typename FileStore, FsType FS_TYPE>
|
||||
int FileSystemTemplate<FileStore, FS_TYPE>::read(const char *path, void *buffer, size_t buffSize) {
|
||||
return 0;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(default:4244)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2015 - 2017 gtalent2@gmail.com
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <ox/std/memops.hpp>
|
||||
#include <ox/std/strops.hpp>
|
||||
#include "pathiterator.hpp"
|
||||
|
||||
namespace ox {
|
||||
namespace fs {
|
||||
|
||||
PathIterator::PathIterator(const char *path, size_t maxSize) {
|
||||
m_path = path;
|
||||
m_maxSize = maxSize;
|
||||
}
|
||||
|
||||
int PathIterator::next(char *pathOut, size_t pathOutSize) {
|
||||
int size = 0;
|
||||
const char *substr = ox_strchar(m_path + m_iterator, '/', m_maxSize - m_iterator);
|
||||
m_iterator = (substr - m_path) + 1;
|
||||
if (substr && m_iterator < m_maxSize) {
|
||||
int start = m_iterator;
|
||||
int end = (ox_strchar(m_path + start, '/', m_maxSize - start) - m_path);
|
||||
if (end < 0) {
|
||||
end = m_maxSize;
|
||||
}
|
||||
size = end - start;
|
||||
ox_memcpy(pathOut, &m_path[start], size);
|
||||
}
|
||||
pathOut[size] = 0; // end with null terminator
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2015 - 2017 gtalent2@gmail.com
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/std/types.hpp>
|
||||
|
||||
namespace ox {
|
||||
namespace fs {
|
||||
|
||||
class PathIterator {
|
||||
private:
|
||||
const char *m_path = nullptr;
|
||||
int m_iterator = 0;
|
||||
int m_maxSize = 0;
|
||||
|
||||
public:
|
||||
PathIterator(const char *path, size_t maxSize);
|
||||
|
||||
int next(char *pathOut, size_t pathOutSize);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,10 +15,19 @@ add_executable(
|
||||
filestoreio.cpp
|
||||
)
|
||||
|
||||
add_executable(
|
||||
FSTests
|
||||
tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(FileStoreFormat OxFS OxStd)
|
||||
target_link_libraries(FileSystemFormat OxFS OxStd)
|
||||
target_link_libraries(FileStoreIO OxFS OxStd)
|
||||
target_link_libraries(FSTests OxFS OxStd)
|
||||
|
||||
add_test("FileStoreFormat" FileStoreFormat)
|
||||
add_test("FileSystemFormat" FileSystemFormat)
|
||||
add_test("FileStoreIO" FileStoreIO)
|
||||
add_test("Test\\ PathIterator1" FSTests PathIterator1)
|
||||
add_test("Test\\ PathIterator2" FSTests PathIterator2)
|
||||
add_test("Test\\ PathIterator3" FSTests PathIterator3)
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2015 - 2017 gtalent2@gmail.com
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <ox/fs/filesystem.hpp>
|
||||
#include <ox/fs/pathiterator.hpp>
|
||||
#include <ox/std/std.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace ox::fs;
|
||||
using namespace ox::std;
|
||||
|
||||
map<string, int(*)(string)> tests = {
|
||||
{
|
||||
{
|
||||
"PathIterator1",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
string path = "/usr/share/charset.gbag";
|
||||
PathIterator it(path.c_str(), path.size());
|
||||
const auto buffSize = path.size() + 1;
|
||||
char buff[buffSize];
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "usr") == 0);
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "share") == 0);
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "charset.gbag") == 0);
|
||||
return retval;
|
||||
}
|
||||
},
|
||||
{
|
||||
"PathIterator2",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
string path = "/usr/share/";
|
||||
PathIterator it(path.c_str(), path.size());
|
||||
const auto buffSize = path.size() + 1;
|
||||
char buff[buffSize];
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "usr") == 0);
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "share") == 0);
|
||||
return retval;
|
||||
}
|
||||
},
|
||||
{
|
||||
"PathIterator3",
|
||||
[](string) {
|
||||
int retval = 0;
|
||||
string path = "/";
|
||||
PathIterator it(path.c_str(), path.size());
|
||||
const auto buffSize = path.size() + 1;
|
||||
char buff[buffSize];
|
||||
retval |= !(it.next(buff, buffSize) == 0 && ox_strcmp(buff, "\0") == 0);
|
||||
return retval;
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
int main(int argc, const char **args) {
|
||||
int retval = -1;
|
||||
if (argc > 1) {
|
||||
auto testName = args[1];
|
||||
string testArg = "";
|
||||
if (args[2]) {
|
||||
testArg = args[2];
|
||||
}
|
||||
if (tests.find(testName) != tests.end()) {
|
||||
retval = tests[testName](testArg);
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
+20
-2
@@ -11,7 +11,7 @@
|
||||
int ox_strcmp(const char *str1, const char *str2) {
|
||||
auto retval = 0;
|
||||
auto i = 0;
|
||||
do {
|
||||
while (str1[i] || str2[i]) {
|
||||
if (str1[i] < str2[i]) {
|
||||
retval = -1;
|
||||
break;
|
||||
@@ -20,7 +20,7 @@ int ox_strcmp(const char *str1, const char *str2) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
} while (str1[i] || str2[i]);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,24 @@ int ox_strlen(const char *str1) {
|
||||
return len;
|
||||
}
|
||||
|
||||
const char *ox_strchar(const char *str, int character, size_t maxLen) {
|
||||
for (size_t i = 0; i < maxLen && str[i]; i++) {
|
||||
if (str[i] == character) {
|
||||
return &str[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
char *ox_strchar(char *str, int character, size_t maxLen) {
|
||||
for (size_t i = 0; i < maxLen && str[i]; i++) {
|
||||
if (str[i] == character) {
|
||||
return &str[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ox_atoi(const char *str) {
|
||||
int total = 0;
|
||||
int multiplier = 1;
|
||||
|
||||
@@ -13,4 +13,8 @@ int ox_strcmp(const char *str1, const char *str2);
|
||||
|
||||
int ox_strlen(const char *str1);
|
||||
|
||||
const char *ox_strchar(const char *str, int character, size_t maxLen = 0xFFFFFFFFFFFFFF);
|
||||
|
||||
char *ox_strchar(char *str, int character, size_t maxLen = 0xFFFFFFFFFFFFFF);
|
||||
|
||||
int ox_atoi(const char *str);
|
||||
|
||||
@@ -28,6 +28,7 @@ add_test("Test\\ ox_strcmp\\ hijk\\ !=\\ asdf" StrOpsTest "hijk > asdf")
|
||||
add_test("Test\\ ox_strcmp\\ read\\ !=\\ resize" StrOpsTest "read < resize")
|
||||
add_test("Test\\ ox_strcmp\\ resize\\ !=\\ read" StrOpsTest "resize > read")
|
||||
add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest "resize == resize")
|
||||
add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest " == ")
|
||||
|
||||
|
||||
################################################################################
|
||||
|
||||
@@ -43,6 +43,12 @@ map<string, function<int()>> tests = {
|
||||
return !(ox_strcmp("resize", "resize") == 0);
|
||||
}
|
||||
},
|
||||
{
|
||||
" == ",
|
||||
[]() {
|
||||
return !(ox_strcmp("", "") == 0);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
int main(int argc, const char **args) {
|
||||
|
||||
Reference in New Issue
Block a user