From fc3e0dfcd1a41bec76cb05f937acdb0a818cfd53 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 12 Apr 2016 21:20:32 +0200 Subject: [PATCH] fs/AllocatedPath: add method FromUTF8Throw() --- src/PlaylistSave.cxx | 7 +++---- src/fs/AllocatedPath.cxx | 10 ++++++++++ src/fs/AllocatedPath.hxx | 7 +++++++ src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx | 4 +--- src/playlist/plugins/FlacPlaylistPlugin.cxx | 4 +--- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index c8010b3d7..17e661b6c 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -43,9 +43,8 @@ playlist_print_song(BufferedOutputStream &os, const DetachedSong &song) : song.GetURI(); try { - const auto uri_fs = AllocatedPath::FromUTF8(uri_utf8); - if (!uri_fs.IsNull()) - os.Format("%s\n", NarrowPath(uri_fs).c_str()); + const auto uri_fs = AllocatedPath::FromUTF8Throw(uri_utf8); + os.Format("%s\n", NarrowPath(uri_fs).c_str()); } catch (const std::runtime_error &) { } } @@ -61,7 +60,7 @@ playlist_print_uri(BufferedOutputStream &os, const char *uri) ? map_uri_fs(uri) : #endif - AllocatedPath::FromUTF8(uri); + AllocatedPath::FromUTF8Throw(uri); if (!path.IsNull()) os.Format("%s\n", NarrowPath(path).c_str()); diff --git a/src/fs/AllocatedPath.cxx b/src/fs/AllocatedPath.cxx index eda4008da..1421a08f7 100644 --- a/src/fs/AllocatedPath.cxx +++ b/src/fs/AllocatedPath.cxx @@ -43,6 +43,16 @@ AllocatedPath::FromUTF8(const char *path_utf8) #endif } +AllocatedPath +AllocatedPath::FromUTF8Throw(const char *path_utf8) +{ +#if defined(HAVE_FS_CHARSET) || defined(WIN32) + return AllocatedPath(::PathFromUTF8(path_utf8)); +#else + return FromFS(path_utf8); +#endif +} + AllocatedPath AllocatedPath::FromUTF8(const char *path_utf8, Error &error) { diff --git a/src/fs/AllocatedPath.hxx b/src/fs/AllocatedPath.hxx index acdfdd0da..c1407e8f4 100644 --- a/src/fs/AllocatedPath.hxx +++ b/src/fs/AllocatedPath.hxx @@ -157,6 +157,13 @@ public: gcc_pure gcc_nonnull_all static AllocatedPath FromUTF8(const char *path_utf8); + /** + * Convert a UTF-8 C string to an #AllocatedPath instance. + * Throws a std::runtime_error on error. + */ + gcc_pure gcc_nonnull_all + static AllocatedPath FromUTF8Throw(const char *path_utf8); + gcc_pure gcc_nonnull_all static AllocatedPath FromUTF8(const char *path_utf8, Error &error); diff --git a/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx b/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx index 996183d0d..d55c9093d 100644 --- a/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx +++ b/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx @@ -96,9 +96,7 @@ embcue_playlist_open_uri(const char *uri, /* only local files supported */ return nullptr; - const auto path_fs = AllocatedPath::FromUTF8(uri); - if (path_fs.IsNull()) - return nullptr; + const auto path_fs = AllocatedPath::FromUTF8Throw(uri); const auto playlist = new EmbeddedCuePlaylist(); diff --git a/src/playlist/plugins/FlacPlaylistPlugin.cxx b/src/playlist/plugins/FlacPlaylistPlugin.cxx index 85eed0cdc..74aacf46c 100644 --- a/src/playlist/plugins/FlacPlaylistPlugin.cxx +++ b/src/playlist/plugins/FlacPlaylistPlugin.cxx @@ -94,9 +94,7 @@ flac_playlist_open_uri(const char *uri, /* only local files supported */ return nullptr; - const auto path_fs = AllocatedPath::FromUTF8(uri); - if (path_fs.IsNull()) - return nullptr; + const auto path_fs = AllocatedPath::FromUTF8Throw(uri); const NarrowPath narrow_path_fs(path_fs);