Commit 0a6c4c31 authored by Denis Krjuchkov's avatar Denis Krjuchkov

fs/Traits: split PathTraits type into PathTraitsFS and PathTraitsUTF8

parent 02fcf184
......@@ -188,7 +188,7 @@ static void help(void)
gcc_pure
static AllocatedPath
PathBuildChecked(const AllocatedPath &a, PathTraits::const_pointer b)
PathBuildChecked(const AllocatedPath &a, PathTraitsFS::const_pointer b)
{
if (a.IsNull())
return AllocatedPath::Null();
......
......@@ -119,7 +119,7 @@ ParsePath(const char *path, Error &error)
return AllocatedPath::Null();
return AllocatedPath::Build(home, path2);
} else if (!PathTraits::IsAbsoluteUTF8(path)) {
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
error.Format(path_domain,
"not an absolute path: %s", path);
return AllocatedPath::Null();
......
......@@ -145,7 +145,7 @@ decoder_file_decode(const DecoderPlugin &plugin,
assert(decoder.stream_tag == nullptr);
assert(decoder.decoder_tag == nullptr);
assert(path != nullptr);
assert(PathTraits::IsAbsoluteFS(path));
assert(PathTraitsFS::IsAbsolute(path));
assert(decoder.dc.state == DecoderState::START);
FormatDebug(decoder_thread_domain, "probing plugin %s", plugin.name);
......
......@@ -114,7 +114,7 @@ Directory::GetName() const
{
assert(!IsRoot());
return PathTraits::GetBaseUTF8(path);
return PathTraitsUTF8::GetBase(path);
}
Directory *
......
......@@ -148,7 +148,7 @@ map_to_relative_path(const char *path_utf8)
return !music_dir_utf8.empty() &&
memcmp(path_utf8, music_dir_utf8.c_str(),
music_dir_utf8_length) == 0 &&
PathTraits::IsSeparatorUTF8(path_utf8[music_dir_utf8_length])
PathTraitsUTF8::IsSeparator(path_utf8[music_dir_utf8_length])
? path_utf8 + music_dir_utf8_length + 1
: path_utf8;
}
......@@ -232,7 +232,7 @@ map_song_fs(const Song &song)
std::string
map_fs_to_utf8(const char *path_fs)
{
if (PathTraits::IsSeparatorFS(path_fs[0])) {
if (PathTraitsFS::IsSeparator(path_fs[0])) {
path_fs = music_dir_fs.RelativeFS(path_fs);
if (path_fs == nullptr || *path_fs == 0)
return std::string();
......
......@@ -246,7 +246,7 @@ LoadPlaylistFile(const char *utf8path, Error &error)
if (!uri_has_scheme(s)) {
uri_utf8 = map_fs_to_utf8(s);
if (uri_utf8.empty()) {
if (PathTraits::IsAbsoluteFS(s)) {
if (PathTraitsFS::IsAbsolute(s)) {
uri_utf8 = PathToUTF8(s);
if (uri_utf8.empty())
continue;
......
......@@ -148,7 +148,7 @@ playlist_provider_print(Client &client, const char *uri,
SongEnumerator &e, bool detail)
{
const std::string base_uri = uri != nullptr
? PathTraits::GetParentUTF8(uri)
? PathTraitsUTF8::GetParent(uri)
: std::string(".");
Song *song;
......
......@@ -35,7 +35,7 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
bool secure)
{
const std::string base_uri = uri != nullptr
? PathTraits::GetParentUTF8(uri)
? PathTraitsUTF8::GetParent(uri)
: std::string(".");
Song *song;
......
......@@ -56,7 +56,7 @@ void
playlist_print_uri(FILE *file, const char *uri)
{
auto path = playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
!PathTraits::IsAbsoluteUTF8(uri)
!PathTraitsUTF8::IsAbsolute(uri)
? map_uri_fs(uri)
: AllocatedPath::FromUTF8(uri);
......
......@@ -99,7 +99,7 @@ playlist_check_load_song(const Song *song, const char *uri, bool secure)
if (uri_has_scheme(uri)) {
dest = Song::NewRemote(uri);
} else if (PathTraits::IsAbsoluteUTF8(uri) && secure) {
} else if (PathTraitsUTF8::IsAbsolute(uri) && secure) {
dest = Song::LoadFile(uri, nullptr);
if (dest == nullptr)
return nullptr;
......@@ -142,13 +142,13 @@ playlist_check_translate_song(Song *song, const char *base_uri,
}
if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
/* PathTraits::GetParentUTF8() returns "." when there
/* PathTraitsUTF8::GetParent() returns "." when there
is no directory name in the given path; clear that
now, because it would break the database lookup
functions */
base_uri = nullptr;
if (PathTraits::IsAbsoluteUTF8(uri)) {
if (PathTraitsUTF8::IsAbsolute(uri)) {
/* XXX fs_charset vs utf8? */
const char *suffix = map_to_relative_path(uri);
assert(suffix != nullptr);
......
......@@ -90,7 +90,7 @@ queue_load_song(TextFile &file, const char *line, queue &queue)
if (StringStartsWith(line, SONG_BEGIN)) {
const char *uri = line + sizeof(SONG_BEGIN) - 1;
if (!uri_has_scheme(uri) && !PathTraits::IsAbsoluteUTF8(uri))
if (!uri_has_scheme(uri) && !PathTraitsUTF8::IsAbsolute(uri))
return;
Error error;
......
......@@ -43,7 +43,7 @@ Song::LoadFile(const char *path_utf8, Directory *parent)
Song *song;
bool ret;
assert((parent == nullptr) == PathTraits::IsAbsoluteUTF8(path_utf8));
assert((parent == nullptr) == PathTraitsUTF8::IsAbsolute(path_utf8));
assert(!uri_has_scheme(path_utf8));
assert(strchr(path_utf8, '\n') == nullptr);
......
......@@ -290,7 +290,7 @@ skip_symlink(const Directory *directory, const char *utf8_name)
const char *target_str = target.c_str();
if (PathTraits::IsAbsoluteFS(target_str)) {
if (PathTraitsFS::IsAbsolute(target_str)) {
/* if the symlink points to an absolute path, see if
that path is inside the music directory */
const char *relative = map_to_relative_path(target_str);
......@@ -301,7 +301,7 @@ skip_symlink(const Directory *directory, const char *utf8_name)
const char *p = target_str;
while (*p == '.') {
if (p[1] == '.' && PathTraits::IsSeparatorFS(p[2])) {
if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
/* "../" moves to parent directory */
directory = directory->parent;
if (directory == nullptr) {
......@@ -311,7 +311,7 @@ skip_symlink(const Directory *directory, const char *utf8_name)
return !follow_outside_symlinks;
}
p += 3;
} else if (PathTraits::IsSeparatorFS(p[1]))
} else if (PathTraitsFS::IsSeparator(p[1]))
/* eliminate "./" */
p += 2;
else
......@@ -454,7 +454,7 @@ update_uri(const char *uri)
if (parent == nullptr)
return;
const char *name = PathTraits::GetBaseUTF8(uri);
const char *name = PathTraitsUTF8::GetBase(uri);
struct stat st;
if (!skip_symlink(parent, name) &&
......
......@@ -51,7 +51,7 @@ public:
Bzip2ArchiveFile(const char *path, InputStream *_is)
:ArchiveFile(bz2_archive_plugin),
name(PathTraits::GetBaseUTF8(path)),
name(PathTraitsUTF8::GetBase(path)),
istream(_is) {
// remove .bz2 suffix
const size_t len = name.length();
......
......@@ -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__)
......
......@@ -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());
}
};
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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());
}
};
......
......@@ -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(".");
......
......@@ -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;
}
gcc_pure
static bool IsAbsoluteFS(const_pointer p) {
assert(p != nullptr);
#ifdef WIN32
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparatorFS(p[2]))
return true;
#endif
return IsSeparatorFS(*p);
ch == SEPARATOR;
}
gcc_pure
static bool IsAbsoluteUTF8(const char *p) {
static bool IsAbsolute(const_pointer p) {
assert(p != nullptr);
#ifdef WIN32
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparatorUTF8(p[2]))
if (IsAlphaASCII(p[0]) && p[1] == ':' && IsSeparator(p[2]))
return true;
#endif
return IsSeparatorUTF8(*p);
return IsSeparator(*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
......@@ -46,7 +46,7 @@ input_archive_open(const char *pathname,
const struct archive_plugin *arplug;
InputStream *is;
if (!PathTraits::IsAbsoluteFS(pathname))
if (!PathTraitsFS::IsAbsolute(pathname))
return nullptr;
char *pname = g_strdup(pathname);
......
......@@ -60,7 +60,7 @@ input_file_open(const char *filename,
int fd, ret;
struct stat st;
if (!PathTraits::IsAbsoluteFS(filename))
if (!PathTraitsFS::IsAbsolute(filename))
return nullptr;
fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0);
......
......@@ -93,7 +93,7 @@ embcue_playlist_open_uri(const char *uri,
gcc_unused Mutex &mutex,
gcc_unused Cond &cond)
{
if (!PathTraits::IsAbsoluteUTF8(uri))
if (!PathTraitsUTF8::IsAbsolute(uri))
/* only local files supported */
return nullptr;
......@@ -116,7 +116,7 @@ embcue_playlist_open_uri(const char *uri,
return nullptr;
}
playlist->filename = PathTraits::GetBaseUTF8(uri);
playlist->filename = PathTraitsUTF8::GetBase(uri);
playlist->next = &playlist->cuesheet[0];
playlist->parser = new CueParser();
......
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