diff options
author | Denis Krjuchkov <denis@crazydev.net> | 2013-12-05 04:47:07 +0600 |
---|---|---|
committer | Denis Krjuchkov <denis@crazydev.net> | 2013-12-05 12:35:21 +0600 |
commit | 289fdcc52b34e3ac4ff08355c47cb0e8bac6f737 (patch) | |
tree | f9bc2e834f675050ab02e53c94c30e5ea673c6f0 | |
parent | 83e6e3e31f2ebf8d7777cdf7e4131996ab94c4a4 (diff) | |
download | mpd-289fdcc52b34e3ac4ff08355c47cb0e8bac6f737.tar.gz mpd-289fdcc52b34e3ac4ff08355c47cb0e8bac6f737.tar.xz mpd-289fdcc52b34e3ac4ff08355c47cb0e8bac6f737.zip |
fs/Traits: implement GetBase/GetParent/Build using templates
Diffstat (limited to '')
-rw-r--r-- | src/fs/Traits.cxx | 85 | ||||
-rw-r--r-- | src/fs/Traits.hxx | 25 |
2 files changed, 88 insertions, 22 deletions
diff --git a/src/fs/Traits.cxx b/src/fs/Traits.cxx index 7dd8d20b6..934a9e2d0 100644 --- a/src/fs/Traits.cxx +++ b/src/fs/Traits.cxx @@ -22,24 +22,25 @@ #include <string.h> -PathTraitsFS::string -PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size, - PathTraitsFS::const_pointer b, size_t b_size) +template<typename Traits> +typename Traits::string +BuildPathImpl(typename Traits::const_pointer a, size_t a_size, + typename Traits::const_pointer b, size_t b_size) { assert(a != nullptr); assert(b != nullptr); if (a_size == 0) - return string(b, b_size); + return typename Traits::string(b, b_size); 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])) - result.push_back(SEPARATOR); + if (!Traits::IsSeparator(a[a_size - 1])) + result.push_back(Traits::SEPARATOR); - if (IsSeparator(b[0])) + if (Traits::IsSeparator(b[0])) result.append(b + 1, b_size - 1); else result.append(b, b_size); @@ -47,26 +48,66 @@ PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size, return result; } -PathTraitsUTF8::const_pointer -PathTraitsUTF8::GetBase(PathTraitsUTF8::const_pointer p) +template<typename Traits> +typename Traits::const_pointer +GetBasePathImpl(typename Traits::const_pointer p) { assert(p != nullptr); - const char *slash = strrchr(p, SEPARATOR); - return slash != nullptr - ? slash + 1 + typename Traits::const_pointer sep = Traits::FindLastSeparator(p); + return sep != nullptr + ? sep + 1 : p; } -PathTraitsUTF8::string -PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p) +template<typename Traits> +typename Traits::string +GetParentPathImpl(typename Traits::const_pointer p) { assert(p != nullptr); - 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); + 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::GetParent(PathTraitsUTF8::const_pointer p) +{ + return GetParentPathImpl<PathTraitsUTF8>(p); } diff --git a/src/fs/Traits.hxx b/src/fs/Traits.hxx index 5ba82549e..bd65f53bf 100644 --- a/src/fs/Traits.hxx +++ b/src/fs/Traits.hxx @@ -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. * If either of the components is empty string, * remaining component is returned unchanged. @@ -139,6 +154,16 @@ struct PathTraitsUTF8 { */ gcc_pure gcc_nonnull_all 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 |