Commit 289fdcc5 authored by Denis Krjuchkov's avatar Denis Krjuchkov

fs/Traits: implement GetBase/GetParent/Build using templates

parent 83e6e3e3
...@@ -22,24 +22,25 @@ ...@@ -22,24 +22,25 @@
#include <string.h> #include <string.h>
PathTraitsFS::string template<typename Traits>
PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size, typename Traits::string
PathTraitsFS::const_pointer b, size_t b_size) BuildPathImpl(typename Traits::const_pointer a, size_t a_size,
typename Traits::const_pointer b, size_t b_size)
{ {
assert(a != nullptr); assert(a != nullptr);
assert(b != nullptr); assert(b != nullptr);
if (a_size == 0) if (a_size == 0)
return string(b, b_size); return typename Traits::string(b, b_size);
if (b_size == 0) if (b_size == 0)
return string(a, a_size); return typename Traits::string(a, a_size);
string result(a, a_size); typename Traits::string result(a, a_size);
if (!IsSeparator(a[a_size - 1])) if (!Traits::IsSeparator(a[a_size - 1]))
result.push_back(SEPARATOR); result.push_back(Traits::SEPARATOR);
if (IsSeparator(b[0])) if (Traits::IsSeparator(b[0]))
result.append(b + 1, b_size - 1); result.append(b + 1, b_size - 1);
else else
result.append(b, b_size); result.append(b, b_size);
...@@ -47,26 +48,66 @@ PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size, ...@@ -47,26 +48,66 @@ PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size,
return result; return result;
} }
PathTraitsUTF8::const_pointer template<typename Traits>
PathTraitsUTF8::GetBase(PathTraitsUTF8::const_pointer p) typename Traits::const_pointer
GetBasePathImpl(typename Traits::const_pointer p)
{ {
assert(p != nullptr); assert(p != nullptr);
const char *slash = strrchr(p, SEPARATOR); typename Traits::const_pointer sep = Traits::FindLastSeparator(p);
return slash != nullptr return sep != nullptr
? slash + 1 ? sep + 1
: p; : p;
} }
PathTraitsUTF8::string template<typename Traits>
PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p) typename Traits::string
GetParentPathImpl(typename Traits::const_pointer p)
{ {
assert(p != nullptr); assert(p != nullptr);
const char *slash = strrchr(p, SEPARATOR); typename Traits::const_pointer sep = Traits::FindLastSeparator(p);
if (slash == nullptr) if (sep == nullptr)
return std::string("."); return typename Traits::string(".");
if (slash == p) if (sep == p)
return std::string(p, p + 1); return typename Traits::string(p, p + 1);
return std::string(p, slash); return typename Traits::string(p, sep);
}
PathTraitsFS::string
PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size,
PathTraitsFS::const_pointer b, size_t b_size)
{
return BuildPathImpl<PathTraitsFS>(a, a_size, b, b_size);
}
PathTraitsFS::const_pointer
PathTraitsFS::GetBase(PathTraitsFS::const_pointer p)
{
return GetBasePathImpl<PathTraitsFS>(p);
}
PathTraitsFS::string
PathTraitsFS::GetParent(PathTraitsFS::const_pointer p)
{
return GetParentPathImpl<PathTraitsFS>(p);
}
PathTraitsUTF8::string
PathTraitsUTF8::Build(PathTraitsUTF8::const_pointer a, size_t a_size,
PathTraitsUTF8::const_pointer b, size_t b_size)
{
return BuildPathImpl<PathTraitsUTF8>(a, a_size, b, b_size);
}
PathTraitsUTF8::const_pointer
PathTraitsUTF8::GetBase(PathTraitsUTF8::const_pointer p)
{
return GetBasePathImpl<PathTraitsUTF8>(p);
}
PathTraitsUTF8::string
PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p)
{
return GetParentPathImpl<PathTraitsUTF8>(p);
} }
...@@ -84,6 +84,21 @@ struct PathTraitsFS { ...@@ -84,6 +84,21 @@ struct PathTraitsFS {
} }
/** /**
* Determine the "base" file name of the given native path.
* The return value points inside the given string.
*/
gcc_pure gcc_nonnull_all
static const_pointer GetBase(const_pointer p);
/**
* Determine the "parent" file name of the given native path.
* As a special case, returns the string "." if there is no
* separator in the given input string.
*/
gcc_pure gcc_nonnull_all
static string GetParent(const_pointer p);
/**
* Constructs the path from the given components. * Constructs the path from the given components.
* If either of the components is empty string, * If either of the components is empty string,
* remaining component is returned unchanged. * remaining component is returned unchanged.
...@@ -139,6 +154,16 @@ struct PathTraitsUTF8 { ...@@ -139,6 +154,16 @@ struct PathTraitsUTF8 {
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static string GetParent(const_pointer p); static string GetParent(const_pointer p);
/**
* Constructs the path from the given components.
* If either of the components is empty string,
* remaining component is returned unchanged.
* If both components are empty strings, empty string is returned.
*/
gcc_pure gcc_nonnull_all
static string Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size);
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment