fs/Traits: split PathTraits type into PathTraitsFS and PathTraitsUTF8

This commit is contained in:
Denis Krjuchkov
2013-12-05 03:53:43 +06:00
parent 02fcf184b5
commit 0a6c4c31b2
24 changed files with 95 additions and 90 deletions

View File

@@ -75,14 +75,14 @@ AllocatedPath::RelativeFS(const char *other_fs) const
other_fs += l;
if (*other_fs != 0) {
if (!PathTraits::IsSeparatorFS(*other_fs))
if (!PathTraitsFS::IsSeparator(*other_fs))
/* mismatch */
return nullptr;
/* skip remaining path separators */
do {
++other_fs;
} while (PathTraits::IsSeparatorFS(*other_fs));
} while (PathTraitsFS::IsSeparator(*other_fs));
}
return other_fs;
@@ -94,7 +94,7 @@ AllocatedPath::ChopSeparators()
size_t l = length();
const char *p = data();
while (l >= 2 && PathTraits::IsSeparatorFS(p[l - 1])) {
while (l >= 2 && PathTraitsFS::IsSeparator(p[l - 1])) {
--l;
#if GCC_CHECK_VERSION(4,7) && !defined(__clang__)

View File

@@ -37,10 +37,10 @@ class Error;
* stored.
*/
class AllocatedPath {
typedef PathTraits::string string;
typedef PathTraits::value_type value_type;
typedef PathTraits::pointer pointer;
typedef PathTraits::const_pointer const_pointer;
typedef PathTraitsFS::string string;
typedef PathTraitsFS::value_type value_type;
typedef PathTraitsFS::pointer pointer;
typedef PathTraitsFS::const_pointer const_pointer;
string value;
@@ -57,7 +57,7 @@ class AllocatedPath {
static AllocatedPath Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) {
return AllocatedPath(PathTraits::BuildFS(a, a_size, b, b_size));
return AllocatedPath(PathTraitsFS::Build(a, a_size, b, b_size));
}
public:
/**
@@ -93,20 +93,20 @@ public:
*/
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const_pointer a, const_pointer b) {
return Build(a, PathTraits::GetLengthFS(a),
b, PathTraits::GetLengthFS(b));
return Build(a, PathTraitsFS::GetLength(a),
b, PathTraitsFS::GetLength(b));
}
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const_pointer a, const AllocatedPath &b) {
return Build(a, PathTraits::GetLengthFS(a),
return Build(a, PathTraitsFS::GetLength(a),
b.value.c_str(), b.value.size());
}
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const AllocatedPath &a, const_pointer b) {
return Build(a.value.c_str(), a.value.size(),
b, PathTraits::GetLengthFS(b));
b, PathTraitsFS::GetLength(b));
}
gcc_pure
@@ -233,7 +233,7 @@ public:
gcc_pure
bool IsAbsolute() {
return PathTraits::IsAbsoluteFS(c_str());
return PathTraitsFS::IsAbsolute(c_str());
}
};

View File

@@ -36,39 +36,39 @@ namespace FOpenMode {
/**
* Open mode for reading text files.
*/
constexpr PathTraits::const_pointer ReadText = "r";
constexpr PathTraitsFS::const_pointer ReadText = "r";
/**
* Open mode for reading binary files.
*/
constexpr PathTraits::const_pointer ReadBinary = "rb";
constexpr PathTraitsFS::const_pointer ReadBinary = "rb";
/**
* Open mode for writing text files.
*/
constexpr PathTraits::const_pointer WriteText = "w";
constexpr PathTraitsFS::const_pointer WriteText = "w";
/**
* Open mode for writing binary files.
*/
constexpr PathTraits::const_pointer WriteBinary = "wb";
constexpr PathTraitsFS::const_pointer WriteBinary = "wb";
/**
* Open mode for appending text files.
*/
constexpr PathTraits::const_pointer AppendText = "a";
constexpr PathTraitsFS::const_pointer AppendText = "a";
/**
* Open mode for appending binary files.
*/
constexpr PathTraits::const_pointer AppendBinary = "ab";
constexpr PathTraitsFS::const_pointer AppendBinary = "ab";
}
/**
* Wrapper for fopen() that uses #Path names.
*/
static inline FILE *
FOpen(Path file, PathTraits::const_pointer mode)
FOpen(Path file, PathTraitsFS::const_pointer mode)
{
return fopen(file.c_str(), mode);
}

View File

@@ -36,14 +36,14 @@ Path::RelativeFS(const char *other_fs) const
other_fs += l;
if (*other_fs != 0) {
if (!PathTraits::IsSeparatorFS(*other_fs))
if (!PathTraitsFS::IsSeparator(*other_fs))
/* mismatch */
return nullptr;
/* skip remaining path separators */
do {
++other_fs;
} while (PathTraits::IsSeparatorFS(*other_fs));
} while (PathTraitsFS::IsSeparator(*other_fs));
}
return other_fs;

View File

@@ -36,9 +36,9 @@
* instance lives, the string must not be invalidated.
*/
class Path {
typedef PathTraits::value_type value_type;
typedef PathTraits::pointer pointer;
typedef PathTraits::const_pointer const_pointer;
typedef PathTraitsFS::value_type value_type;
typedef PathTraitsFS::pointer pointer;
typedef PathTraitsFS::const_pointer const_pointer;
const char *value;
@@ -139,7 +139,7 @@ public:
gcc_pure
bool IsAbsolute() {
return PathTraits::IsAbsoluteFS(c_str());
return PathTraitsFS::IsAbsolute(c_str());
}
};

View File

@@ -22,9 +22,9 @@
#include <string.h>
PathTraits::string
PathTraits::BuildFS(PathTraits::const_pointer a, size_t a_size,
PathTraits::const_pointer b, size_t b_size)
PathTraitsFS::string
PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size,
PathTraitsFS::const_pointer b, size_t b_size)
{
assert(a != nullptr);
assert(b != nullptr);
@@ -36,10 +36,10 @@ PathTraits::BuildFS(PathTraits::const_pointer a, size_t a_size,
string result(a, a_size);
if (!IsSeparatorFS(a[a_size - 1]))
result.push_back(SEPARATOR_FS);
if (!IsSeparator(a[a_size - 1]))
result.push_back(SEPARATOR);
if (IsSeparatorFS(b[0]))
if (IsSeparator(b[0]))
result.append(b + 1, b_size - 1);
else
result.append(b, b_size);
@@ -48,22 +48,22 @@ PathTraits::BuildFS(PathTraits::const_pointer a, size_t a_size,
}
const char *
PathTraits::GetBaseUTF8(const char *p)
PathTraitsUTF8::GetBase(const char *p)
{
assert(p != nullptr);
const char *slash = strrchr(p, SEPARATOR_UTF8);
const char *slash = strrchr(p, SEPARATOR);
return slash != nullptr
? slash + 1
: p;
}
std::string
PathTraits::GetParentUTF8(const char *p)
PathTraitsUTF8::GetParent(const char *p)
{
assert(p != nullptr);
const char *slash = strrchr(p, SEPARATOR_UTF8);
const char *slash = strrchr(p, SEPARATOR);
return slash != nullptr
? std::string(p, slash)
: std::string(".");

View File

@@ -33,56 +33,40 @@
#include <assert.h>
/**
* This class describes the nature of a filesystem path.
* This class describes the nature of a native filesystem path.
*/
struct PathTraits {
struct PathTraitsFS {
typedef std::string string;
typedef char value_type;
typedef char *pointer;
typedef const char *const_pointer;
#ifdef WIN32
static constexpr value_type SEPARATOR_FS = '\\';
static constexpr value_type SEPARATOR = '\\';
#else
static constexpr value_type SEPARATOR_FS = '/';
static constexpr value_type SEPARATOR = '/';
#endif
static constexpr char SEPARATOR_UTF8 = '/';
static constexpr bool IsSeparatorFS(value_type ch) {
static constexpr bool IsSeparator(value_type ch) {
return
#ifdef WIN32
ch == '/' ||
#endif
ch == SEPARATOR_FS;
}
static constexpr bool IsSeparatorUTF8(char ch) {
return ch == SEPARATOR_UTF8;
ch == SEPARATOR;
}
gcc_pure
static bool IsAbsoluteFS(const_pointer p) {
static bool IsAbsolute(const_pointer p) {
assert(p != nullptr);
#ifdef WIN32
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparatorFS(p[2]))
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparator(p[2]))
return true;
#endif
return IsSeparatorFS(*p);
return IsSeparator(*p);
}
gcc_pure
static bool IsAbsoluteUTF8(const char *p) {
assert(p != nullptr);
#ifdef WIN32
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparatorUTF8(p[2]))
return true;
#endif
return IsSeparatorUTF8(*p);
}
gcc_pure
static size_t GetLengthFS(const_pointer p) {
static size_t GetLength(const_pointer p) {
return strlen(p);
}
@@ -93,15 +77,36 @@ struct PathTraits {
* If both components are empty strings, empty string is returned.
*/
gcc_pure gcc_nonnull_all
static string BuildFS(const_pointer a, size_t a_size,
const_pointer b, size_t b_size);
static string Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size);
};
/**
* This class describes the nature of a MPD internal filesystem path.
*/
struct PathTraitsUTF8 {
static constexpr char SEPARATOR = '/';
static constexpr bool IsSeparator(char ch) {
return ch == SEPARATOR;
}
gcc_pure
static bool IsAbsolute(const char *p) {
assert(p != nullptr);
#ifdef WIN32
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparator(p[2]))
return true;
#endif
return IsSeparator(*p);
}
/**
* Determine the "base" file name of the given UTF-8 path.
* The return value points inside the given string.
*/
gcc_pure gcc_nonnull_all
static const char *GetBaseUTF8(const char *p);
static const char *GetBase(const char *p);
/**
* Determine the "parent" file name of the given UTF-8 path.
@@ -109,7 +114,7 @@ struct PathTraits {
* separator in the given input string.
*/
gcc_pure gcc_nonnull_all
static std::string GetParentUTF8(const char *p);
static std::string GetParent(const char *p);
};
#endif