Skip to content

Commit 4b80854

Browse files
committed
UPD | fs path: fix something
1 parent ce0bef4 commit 4b80854

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

include/ManapiString.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,12 @@ namespace manapi::string {
104104
* @return
105105
*/
106106
std::string fill (size_t s, char c);
107+
108+
/**
109+
* Counts number of |c| in |str|
110+
* @param c char
111+
* @param str source string
112+
* @return
113+
*/
114+
std::size_t count (char c, std::string_view str) MANAPIHTTP_NOEXCEPT;
107115
}

include/fs/ManapiFilesystem.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,20 @@ namespace manapi::filesystem::path {
144144

145145
std::string absolute (std::string_view path);
146146

147+
std::string root_directory ();
148+
147149
std::string_view back (std::string_view str);
148150

151+
void append (std::string &path, std::string_view next, bool root);
152+
149153
void append (std::string &path, std::string_view next);
150154

151155
std::string current_path ();
152156

153157
template <class... Args>
154158
std::string join(std::string path, Args&&...args) {
155-
(..., path::append(path, std::forward<Args>(args) ));
159+
std::size_t i = 0;
160+
(..., path::append(path, std::forward<Args>(args), !(i++) ));
156161
return std::move(path);
157162
}
158163
}

src/ManapiString.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,14 @@ std::string manapi::string::fill(size_t s, char c) {
146146
memset(b.data(), c, s);
147147
return std::move(b);
148148
}
149+
150+
std::size_t manapi::string::count(char c, std::string_view str) MANAPIHTTP_NOEXCEPT {
151+
std::size_t res = 0;
152+
auto it = str.find(c);
153+
while (it != std::string_view::npos) {
154+
res++;
155+
str = str.substr(it + 1);
156+
it = str.find(c);
157+
}
158+
return res;
159+
}

src/fs/ManapiFilesystem.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ std::string_view manapi::filesystem::path::back (std::string_view str) {
784784
return str;
785785
}
786786

787-
static void append_ (std::string &path, std::string_view next, int delimiter) {
787+
static void append_ (std::string &path, std::string_view next, bool delimiter, bool root) {
788788
using namespace manapi::filesystem;
789789

790790
if (delimiter) {
@@ -793,7 +793,7 @@ static void append_ (std::string &path, std::string_view next, int delimiter) {
793793
if (it == std::string_view::npos)
794794
break;
795795
if (it) {
796-
append_(path, next.substr(0, it), false);
796+
append_(path, next.substr(0, it), false, root && path.empty());
797797
}
798798
else if (path.empty() || path.back() != path::delimiter) {
799799
path.push_back(path::delimiter);
@@ -806,14 +806,18 @@ static void append_ (std::string &path, std::string_view next, int delimiter) {
806806
return;
807807

808808
if (next == ".") {
809-
if (path.empty()) {
809+
if (path.empty() && root) {
810810
path = path::current_path();
811811
}
812812

813813
return;
814814
}
815815

816816
if (next == "..") {
817+
if (path.empty() && root) {
818+
path = path::current_path();
819+
}
820+
817821
auto it = path.rfind(path::delimiter);
818822
if (it == std::string_view::npos) {
819823
path.clear();
@@ -832,7 +836,12 @@ static void append_ (std::string &path, std::string_view next, int delimiter) {
832836
}
833837
}
834838

839+
835840
void manapi::filesystem::path::append(std::string &path, std::string_view next) {
841+
path::append(path, next, false);
842+
}
843+
844+
void manapi::filesystem::path::append(std::string &path, std::string_view next, bool root) {
836845
auto const c_size = path.size();
837846
#ifdef _MSC_VER
838847
char *c = static_cast<char*>(alloca(c_size));
@@ -841,8 +850,8 @@ void manapi::filesystem::path::append(std::string &path, std::string_view next)
841850
#endif
842851
memcpy (c, path.data(), c_size);
843852
path.resize(0);
844-
append_(path, std::string_view(c, c_size), true);
845-
append_(path, next, true);
853+
append_(path, std::string_view(c, c_size), true, root);
854+
append_(path, next, true, false);
846855
}
847856

848857
std::string manapi::filesystem::path::current_path() {
@@ -851,14 +860,18 @@ std::string manapi::filesystem::path::current_path() {
851860

852861
std::string manapi::filesystem::path::serialize (std::string_view str) {
853862
std::string path;
854-
append_ (path, str, true);
863+
append_ (path, str, true, true);
855864
return std::move(path);
856865
}
857866

858867
std::string manapi::filesystem::path::absolute(std::string_view path) {
859868
return std::filesystem::absolute(path).string();
860869
}
861870

871+
std::string manapi::filesystem::path::root_directory() {
872+
return std::filesystem::current_path().root_directory().string();
873+
}
874+
862875
manapi::future<manapi::sys_error::status> manapi::filesystem::async_unlink (std::string path, async::cancellation_action cancellation) {
863876
co_return co_await async_fs_simple_operation ([path = std::move(path)] (std::shared_ptr<ev::fs> w)
864877
-> bool { return !w->unlink(path.data()); }, std::move(cancellation));

tests/test_fs.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ UTEST(fs_path, join_4) {
4949
ASSERT_TRUE (path == (s));
5050
}
5151

52+
UTEST(fs_path, join_5) {
53+
std::string path1 = manapi::filesystem::path::join(".", "..", "hello");
54+
std::string path2 = manapi::filesystem::path::join("..", "hello");
55+
ASSERT_TRUE (path1 == path2);
56+
}
57+
58+
UTEST(fs_path, join_6) {
59+
std::string b = manapi::filesystem::path::root_directory();
60+
std::string path = manapi::filesystem::path::join (b, "hello", "test", "no");
61+
std::string s = b;
62+
s += std::string_view{"hello"};
63+
s.append(manapi::filesystem::path::string_delimiter);
64+
s.append("test");
65+
s.append(manapi::filesystem::path::string_delimiter);
66+
s.append("no");
67+
ASSERT_TRUE (path == (s));
68+
}
69+
70+
UTEST(fs_path, join_7) {
71+
std::string b = manapi::filesystem::path::root_directory();
72+
std::string path = manapi::filesystem::path::join ("A", "B", "C", "..", "..", "..", "..", "..", "..", ".", "A");
73+
ASSERT_TRUE (path == "A");
74+
}
75+
76+
UTEST(fs_path, join_8) {
77+
std::string b = manapi::filesystem::path::root_directory();
78+
std::string path = manapi::filesystem::path::join ("A", "B", "C", "D");
79+
ASSERT_TRUE (manapi::string::count(manapi::filesystem::path::delimiter, path) == 3);
80+
}
81+
5282
UTEST(fs_path, serialize_1) {
5383
#ifdef _WIN32
5484
std::string path = manapi::filesystem::path::serialize("\\\\\\\\\\dev\\\\.\\.\\.\\shm\\\\shm\\..\\..\\..\\dev\\\\shm\\\\.\\.\\\\dev\\shm\\..\\..");

0 commit comments

Comments
 (0)