From d4e43f3501865348295d0d668be3549ba72d4fa4 Mon Sep 17 00:00:00 2001 From: waitti <waitti@foxmail.com> Date: Sat, 19 Jun 2021 14:56:56 +0800 Subject: [PATCH] feat: add PathUtil --- src/lib/stl/PathUtil.h | 76 ++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 ++ 2 files changed, 78 insertions(+) diff --git a/src/lib/stl/PathUtil.h b/src/lib/stl/PathUtil.h index 6e6b582..c85b44e 100644 --- a/src/lib/stl/PathUtil.h +++ b/src/lib/stl/PathUtil.h @@ -1,4 +1,80 @@ #ifndef OS_RISC_V_PATHUTIL_H #define OS_RISC_V_PATHUTIL_H +#include "list.h" +#include "string.h" + +class PathUtil { +public: + /** + * strpbrk - Find the first occurrence of a set of characters + * @cs: The string to be searched + * @ct: The characters to search for + */ + static char *strpbrk(const char *cs, const char *ct) { + const char *sc1, *sc2; + + for (sc1 = cs; *sc1 != '\0'; ++sc1) { + for (sc2 = ct; *sc2 != '\0'; ++sc2) { + if (*sc1 == *sc2) + return (char *) sc1; + } + } + return NULL; + } + +/** + * strsep - Split a string into tokens + * @s: The string to be searched + * @ct: The characters to search for + * + * strsep() updates @s to point after the token, ready for the next call. + * + * It returns empty tokens, too, behaving exactly like the libc function + * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. + * Same semantics, slimmer shape. ;) + */ + static char *strsep(char **s, const char *ct) { + char *sbegin = *s; + char *end; + + if (sbegin == NULL) + return NULL; + + end = strpbrk(sbegin, ct); + if (end) + *end++ = '\0'; + *s = end; + return sbegin; + } + + static List<String> split(const String& path){ + char *unit; + List<String> res; + char* c_str = path.c_str(); + while ((unit = strsep(&c_str, "/")) != NULL) { + if (*unit != '\0') { + res.push_back(unit); + } + } + return res; + } + + static String join(const List<String>& pathList){ + return ""; + } +}; + +class TestPathUtil{ +public: + TestPathUtil(){ + auto list = PathUtil::split("/dev/sdb"); + assert(list.start->data == "dev" && list.start->next->data == "sdb"); + + list = PathUtil::split("dev/sdb"); + assert(list.start->data == "dev" && list.start->next->data == "sdb"); + + } +}; + #endif //OS_RISC_V_PATHUTIL_H diff --git a/src/main.cpp b/src/main.cpp index 7d07570..18f64f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include "kernel/Test.h" #include "kernel/fs/file_describer.h" #include "lib/stl/Trie.h" +#include "lib/stl/PathUtil.h" extern "C"{ #include "driver/interface.h" @@ -25,6 +26,7 @@ void test_lib(){ t.test(); TestTrie testTrie; + TestPathUtil testPathUtil; } /** -- GitLab