flatten 20260225
This commit is contained in:
169
tjp/core/file/Path.cpp
Normal file
169
tjp/core/file/Path.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#ifdef TJP_CORE_HEADER_ONLY
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Path.h"
|
||||
|
||||
namespace tjp::core::file {
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string_view removeExtensions(const std::string_view &s)
|
||||
{
|
||||
auto lastSlash = s.rfind('/');
|
||||
auto firstDotAfterLastSlash = s.find('.', lastSlash+1);
|
||||
return s.substr(0, firstDotAfterLastSlash);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string joinDirs(const std::vector<std::string_view> &dirs)
|
||||
{
|
||||
std::string joined;
|
||||
for (auto &dir : dirs)
|
||||
{
|
||||
if (dir.empty())
|
||||
continue;
|
||||
|
||||
bool first = joined.empty();
|
||||
|
||||
if (dir.front() == '/' && !first)
|
||||
joined += dir.substr(1);
|
||||
else
|
||||
joined += dir;
|
||||
|
||||
if (joined.back() != '/')
|
||||
joined.push_back('/');
|
||||
}
|
||||
|
||||
return joined;
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string joinPaths(const std::vector<std::string_view> &dirs)
|
||||
{
|
||||
std::string joined;
|
||||
for (auto &dir : dirs)
|
||||
{
|
||||
if (dir.empty())
|
||||
continue;
|
||||
|
||||
bool first = joined.empty();
|
||||
|
||||
if (!first)
|
||||
if (joined.back() != '/')
|
||||
joined.push_back('/');
|
||||
|
||||
if (dir.front() == '/' && !first)
|
||||
joined += dir.substr(1);
|
||||
else
|
||||
joined += dir;
|
||||
}
|
||||
return joined;
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string joinDirsAndFile(const std::vector<std::string_view> &dirs, const std::string &fileName)
|
||||
{
|
||||
return joinDirs(dirs) + fileName;
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string convertDelimiter (const std::string &str, char from, char to)
|
||||
{
|
||||
if (from==to) return str;
|
||||
if (str.empty()) return str;
|
||||
|
||||
std::string copy = str;
|
||||
for (auto &c : copy)
|
||||
if (c == from)
|
||||
c = to;
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string convertDelimiter (const std::string &path)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T popFileName_(const T &path)
|
||||
{
|
||||
auto lastDelimiter = path.rfind('/');
|
||||
return path.substr(0, lastDelimiter+1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T popLast_(const T &path)
|
||||
{
|
||||
if (path.empty())
|
||||
return {};
|
||||
|
||||
if (path.back() != '/')
|
||||
return popFileName(path);
|
||||
|
||||
if (path.size() < 2)
|
||||
return {};
|
||||
|
||||
auto lastDelimiter = path.rfind('/', path.size()-2);
|
||||
return path.substr(0, lastDelimiter+1);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string_view popFileName(const std::string_view &path)
|
||||
{
|
||||
return popFileName_(path);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string_view popLast(const std::string_view &path)
|
||||
{
|
||||
return popLast_(path);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string popFileName(const std::string &path)
|
||||
{
|
||||
return popFileName_(path);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string popLast(const std::string &path)
|
||||
{
|
||||
return popLast_(path);
|
||||
}
|
||||
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string fileName(const std::string &path)
|
||||
{
|
||||
auto lastDelimiter = path.rfind('/');
|
||||
if (lastDelimiter == std::string::npos)
|
||||
return path;
|
||||
|
||||
return path.substr(lastDelimiter+1);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string_view fileName(const std::string_view &path)
|
||||
{
|
||||
auto lastDelimiter = path.rfind('/');
|
||||
if (lastDelimiter == std::string::npos)
|
||||
return path;
|
||||
|
||||
return path.substr(lastDelimiter+1);
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::string extension(const std::string &path)
|
||||
{
|
||||
auto lastDelimiter = path.rfind('.');
|
||||
if (lastDelimiter == std::string::npos)
|
||||
return "";
|
||||
|
||||
return path.substr(lastDelimiter);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user