From 6513ff92a7012819db464adad094b69e8b585127 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 12 Apr 2016 21:24:16 +0200 Subject: [PATCH] fs/Charset: throw exception on error --- src/fs/AllocatedPath.cxx | 14 ++++++++++++-- src/fs/Charset.cxx | 27 ++++++--------------------- src/fs/Charset.hxx | 6 ++++-- src/fs/Path.cxx | 8 +++++++- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/fs/AllocatedPath.cxx b/src/fs/AllocatedPath.cxx index 62284715a..eda4008da 100644 --- a/src/fs/AllocatedPath.cxx +++ b/src/fs/AllocatedPath.cxx @@ -24,6 +24,8 @@ #include "util/Error.hxx" #include "Compiler.h" +#include + /* no inlining, please */ AllocatedPath::~AllocatedPath() {} @@ -31,7 +33,11 @@ AllocatedPath AllocatedPath::FromUTF8(const char *path_utf8) { #if defined(HAVE_FS_CHARSET) || defined(WIN32) - return AllocatedPath(::PathFromUTF8(path_utf8)); + try { + return AllocatedPath(::PathFromUTF8(path_utf8)); + } catch (const std::runtime_error &) { + return nullptr; + } #else return FromFS(path_utf8); #endif @@ -58,7 +64,11 @@ AllocatedPath::GetDirectoryName() const std::string AllocatedPath::ToUTF8() const { - return ::PathToUTF8(c_str()); + try { + return ::PathToUTF8(c_str()); + } catch (const std::runtime_error &) { + return std::string(); + } } void diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index a331bd064..b721a6100 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -101,12 +101,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs) #endif #ifdef WIN32 - try { - const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs); - return FixSeparators(PathTraitsUTF8::string(buffer.c_str())); - } catch (const std::runtime_error &) { - return PathTraitsUTF8::string(); - } + const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs); + return FixSeparators(PathTraitsUTF8::string(buffer.c_str())); #else #ifdef HAVE_FS_CHARSET if (fs_converter == nullptr) @@ -114,12 +110,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs) return FixSeparators(path_fs); #ifdef HAVE_FS_CHARSET - try { - const auto buffer = fs_converter->ToUTF8(path_fs); - return FixSeparators(PathTraitsUTF8::string(buffer.c_str())); - } catch (const std::runtime_error &) { - return PathTraitsUTF8::string(); - } + const auto buffer = fs_converter->ToUTF8(path_fs); + return FixSeparators(PathTraitsUTF8::string(buffer.c_str())); #endif #endif } @@ -135,20 +127,13 @@ PathFromUTF8(PathTraitsUTF8::const_pointer_type path_utf8) #endif #ifdef WIN32 - try { - const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8); - return PathTraitsFS::string(buffer.c_str()); - } catch (const std::runtime_error &) { - return PathTraitsFS::string(); - } + const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8); + return PathTraitsFS::string(buffer.c_str()); #else if (fs_converter == nullptr) return path_utf8; const auto buffer = fs_converter->FromUTF8(path_utf8); - if (buffer.IsNull()) - return PathTraitsFS::string(); - return PathTraitsFS::string(buffer.c_str()); #endif } diff --git a/src/fs/Charset.hxx b/src/fs/Charset.hxx index b7497ca32..f0d6e15e6 100644 --- a/src/fs/Charset.hxx +++ b/src/fs/Charset.hxx @@ -48,7 +48,8 @@ DeinitFSCharset(); /** * Convert the path to UTF-8. - * Returns empty string on error. + * + * Throws std::runtime_error on error. */ gcc_pure gcc_nonnull_all PathTraitsUTF8::string @@ -56,7 +57,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs); /** * Convert the path from UTF-8. - * Returns empty string on error. + * + * Throws std::runtime_error on error. */ gcc_pure gcc_nonnull_all PathTraitsFS::string diff --git a/src/fs/Path.cxx b/src/fs/Path.cxx index cb8acd3fe..d28dbc1d4 100644 --- a/src/fs/Path.cxx +++ b/src/fs/Path.cxx @@ -21,10 +21,16 @@ #include "Path.hxx" #include "Charset.hxx" +#include + std::string Path::ToUTF8() const { - return ::PathToUTF8(c_str()); + try { + return ::PathToUTF8(c_str()); + } catch (const std::runtime_error &) { + return std::string(); + } } Path::const_pointer_type