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; return false;
const auto name = AllocatedPath::FromFS(name_fs_str, name_fs_end); 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(); info.mtime = fi.GetModificationTime();
return true; 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 /* on Windows, playlists always contain UTF-8, because its
"narrow" charset (i.e. CP_ACP) is incapable of storing all "narrow" charset (i.e. CP_ACP) is incapable of storing all
Unicode paths */ Unicode paths */
os.Format("%s\n", path.ToUTF8().c_str()); try {
os.Format("%s\n", path.ToUTF8Throw().c_str());
} catch (...) {
}
#else #else
os.Format("%s\n", path.c_str()); os.Format("%s\n", path.c_str());
#endif #endif

View File

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

View File

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

View File

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

View File

@ -72,7 +72,7 @@ OpenFileInputStream(Path path, Mutex &mutex)
#endif #endif
#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(), std::move(reader), info.GetSize(),
mutex); mutex);
} }

View File

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

View File

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

View File

@ -378,14 +378,13 @@ NfsListDirectoryOperation::CollectEntries(struct nfsdir *dir)
if (SkipNameFS(name_fs.c_str())) if (SkipNameFS(name_fs.c_str()))
continue; continue;
std::string name_utf8 = name_fs.ToUTF8(); try {
if (name_utf8.empty()) entries.emplace_front(name_fs.ToUTF8Throw());
Copy(entries.front().info, *ent);
} catch (...) {
/* ignore files whose name cannot be converted /* ignore files whose name cannot be converted
to UTF-8 */ 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 * static const DecoderPlugin *
FindContainerDecoderPlugin(Path path) FindContainerDecoderPlugin(Path path)
{ {
const auto utf8 = path.ToUTF8();
if (utf8.empty())
return nullptr;
UriSuffixBuffer suffix_buffer; 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) if (suffix == nullptr)
return nullptr; return nullptr;