2021-12-30 16:21:49 +00:00
|
|
|
#include <Coordination/pathUtils.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
static size_t findLastSlash(StringRef path)
|
|
|
|
{
|
|
|
|
if (path.size == 0)
|
|
|
|
return std::string::npos;
|
|
|
|
|
|
|
|
for (size_t i = path.size - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
if (path.data[i] == '/')
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.data[0] == '/')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return std::string::npos;
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:46:29 +00:00
|
|
|
StringRef parentPath(StringRef path)
|
2021-12-30 16:21:49 +00:00
|
|
|
{
|
|
|
|
auto rslash_pos = findLastSlash(path);
|
|
|
|
if (rslash_pos > 0)
|
2022-01-19 11:46:29 +00:00
|
|
|
return StringRef{path.data, rslash_pos};
|
2021-12-30 16:21:49 +00:00
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:46:29 +00:00
|
|
|
StringRef getBaseName(StringRef path)
|
2021-12-30 16:21:49 +00:00
|
|
|
{
|
|
|
|
size_t basename_start = findLastSlash(path);
|
2022-01-19 11:46:29 +00:00
|
|
|
return StringRef{path.data + basename_start + 1, path.size - basename_start - 1};
|
2021-12-30 16:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|