fs/Path: add method ToUTF8Throw()

This commit is contained in:
Max Kellermann 2018-07-18 16:30:46 +02:00
parent f87265a4d5
commit 32290d5eb8
10 changed files with 43 additions and 29 deletions

View File

@ -143,11 +143,13 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
return false;
const auto name = AllocatedPath::FromFS(name_fs_str, name_fs_end);
std::string name_utf8 = name.ToUTF8();
if (name_utf8.empty())
return false;
info.name = std::move(name_utf8);
try {
info.name = name.ToUTF8Throw();
} catch (...) {
return false;
}
info.mtime = fi.GetModificationTime();
return true;
}

View File

@ -41,7 +41,10 @@ playlist_print_path(BufferedOutputStream &os, const Path path)
/* on Windows, playlists always contain UTF-8, because its
"narrow" charset (i.e. CP_ACP) is incapable of storing all
Unicode paths */
os.Format("%s\n", path.ToUTF8().c_str());
try {
os.Format("%s\n", path.ToUTF8Throw().c_str());
} catch (...) {
}
#else
os.Format("%s\n", path.c_str());
#endif

View File

@ -253,6 +253,10 @@ public:
return ((Path)*this).ToUTF8();
}
std::string ToUTF8Throw() const {
return ((Path)*this).ToUTF8Throw();
}
/**
* Gets directory name of this path.
* Returns a "nulled" instance on error.

View File

@ -25,12 +25,18 @@ std::string
Path::ToUTF8() const noexcept
{
try {
return ::PathToUTF8(c_str());
return ToUTF8Throw();
} catch (...) {
return std::string();
}
}
std::string
Path::ToUTF8Throw() const
{
return ::PathToUTF8(c_str());
}
Path::const_pointer_type
Path::GetSuffix() const noexcept
{

View File

@ -134,6 +134,11 @@ public:
gcc_pure
std::string ToUTF8() const noexcept;
/**
* Like ToUTF8(), but throws on error.
*/
std::string ToUTF8Throw() const;
/**
* Determine the "base" file name.
* The return value points inside this object.

View File

@ -72,7 +72,7 @@ OpenFileInputStream(Path path, Mutex &mutex)
#endif
#endif
return std::make_unique<FileInputStream>(path.ToUTF8().c_str(),
return std::make_unique<FileInputStream>(path.ToUTF8Throw().c_str(),
std::move(reader), info.GetSize(),
mutex);
}

View File

@ -40,7 +40,7 @@ try {
if (suffix == nullptr)
return nullptr;
const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();
const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8Throw();
if (!playlist_suffix_supported(suffix_utf8.c_str()))
return nullptr;
@ -57,10 +57,8 @@ playlist_open_path(Path path, Mutex &mutex)
try {
assert(!path.IsNull());
const std::string uri_utf8 = path.ToUTF8();
auto playlist = !uri_utf8.empty()
? playlist_list_open_uri(uri_utf8.c_str(), mutex)
: nullptr;
const std::string uri_utf8 = path.ToUTF8Throw();
auto playlist = playlist_list_open_uri(uri_utf8.c_str(), mutex);
if (playlist == nullptr)
playlist = playlist_open_path_suffix(path, mutex);

View File

@ -51,7 +51,7 @@ class LocalStorage final : public Storage {
public:
explicit LocalStorage(Path _base_fs)
:base_fs(_base_fs), base_utf8(base_fs.ToUTF8()) {
:base_fs(_base_fs), base_utf8(base_fs.ToUTF8Throw()) {
assert(!base_fs.IsNull());
assert(!base_utf8.empty());
}
@ -162,11 +162,11 @@ LocalDirectoryReader::Read() noexcept
if (SkipNameFS(name_fs.c_str()))
continue;
name_utf8 = name_fs.ToUTF8();
if (name_utf8.empty())
continue;
try {
name_utf8 = name_fs.ToUTF8Throw();
return name_utf8.c_str();
} catch (...) {
}
}
return nullptr;

View File

@ -378,14 +378,13 @@ NfsListDirectoryOperation::CollectEntries(struct nfsdir *dir)
if (SkipNameFS(name_fs.c_str()))
continue;
std::string name_utf8 = name_fs.ToUTF8();
if (name_utf8.empty())
try {
entries.emplace_front(name_fs.ToUTF8Throw());
Copy(entries.front().info, *ent);
} catch (...) {
/* ignore files whose name cannot be converted
to UTF-8 */
continue;
entries.emplace_front(std::move(name_utf8));
Copy(entries.front().info, *ent);
}
}
}

View File

@ -48,12 +48,9 @@ FindContainerDecoderPlugin(const char *suffix)
static const DecoderPlugin *
FindContainerDecoderPlugin(Path path)
{
const auto utf8 = path.ToUTF8();
if (utf8.empty())
return nullptr;
UriSuffixBuffer suffix_buffer;
const char *const suffix = uri_get_suffix(utf8.c_str(), suffix_buffer);
const char *const suffix = uri_get_suffix(path.ToUTF8Throw().c_str(),
suffix_buffer);
if (suffix == nullptr)
return nullptr;