fs/Traits: implement GetBase/GetParent/Build using templates
This commit is contained in:
parent
83e6e3e31f
commit
289fdcc52b
@ -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,
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Traits>
|
||||||
|
typename Traits::string
|
||||||
|
GetParentPathImpl(typename Traits::const_pointer p)
|
||||||
|
{
|
||||||
|
assert(p != nullptr);
|
||||||
|
|
||||||
|
typename Traits::const_pointer sep = Traits::FindLastSeparator(p);
|
||||||
|
if (sep == nullptr)
|
||||||
|
return typename Traits::string(".");
|
||||||
|
if (sep == p)
|
||||||
|
return typename Traits::string(p, p + 1);
|
||||||
|
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::string
|
||||||
PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p)
|
PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p)
|
||||||
{
|
{
|
||||||
assert(p != nullptr);
|
return GetParentPathImpl<PathTraitsUTF8>(p);
|
||||||
|
|
||||||
const char *slash = strrchr(p, SEPARATOR);
|
|
||||||
if (slash == nullptr)
|
|
||||||
return std::string(".");
|
|
||||||
if (slash == p)
|
|
||||||
return std::string(p, p + 1);
|
|
||||||
return std::string(p, slash);
|
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,21 @@ struct PathTraitsFS {
|
|||||||
return strlen(p);
|
return strlen(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,
|
||||||
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user