Commit 6513ff92 authored by Max Kellermann's avatar Max Kellermann

fs/Charset: throw exception on error

parent a0eb6d09
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include "util/Error.hxx" #include "util/Error.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <stdexcept>
/* no inlining, please */ /* no inlining, please */
AllocatedPath::~AllocatedPath() {} AllocatedPath::~AllocatedPath() {}
...@@ -31,7 +33,11 @@ AllocatedPath ...@@ -31,7 +33,11 @@ AllocatedPath
AllocatedPath::FromUTF8(const char *path_utf8) AllocatedPath::FromUTF8(const char *path_utf8)
{ {
#if defined(HAVE_FS_CHARSET) || defined(WIN32) #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 #else
return FromFS(path_utf8); return FromFS(path_utf8);
#endif #endif
...@@ -58,7 +64,11 @@ AllocatedPath::GetDirectoryName() const ...@@ -58,7 +64,11 @@ AllocatedPath::GetDirectoryName() const
std::string std::string
AllocatedPath::ToUTF8() const AllocatedPath::ToUTF8() const
{ {
return ::PathToUTF8(c_str()); try {
return ::PathToUTF8(c_str());
} catch (const std::runtime_error &) {
return std::string();
}
} }
void void
......
...@@ -101,12 +101,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs) ...@@ -101,12 +101,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs)
#endif #endif
#ifdef WIN32 #ifdef WIN32
try { const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs);
const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs); return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
} catch (const std::runtime_error &) {
return PathTraitsUTF8::string();
}
#else #else
#ifdef HAVE_FS_CHARSET #ifdef HAVE_FS_CHARSET
if (fs_converter == nullptr) if (fs_converter == nullptr)
...@@ -114,12 +110,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs) ...@@ -114,12 +110,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs)
return FixSeparators(path_fs); return FixSeparators(path_fs);
#ifdef HAVE_FS_CHARSET #ifdef HAVE_FS_CHARSET
try { const auto buffer = fs_converter->ToUTF8(path_fs);
const auto buffer = fs_converter->ToUTF8(path_fs); return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
} catch (const std::runtime_error &) {
return PathTraitsUTF8::string();
}
#endif #endif
#endif #endif
} }
...@@ -135,20 +127,13 @@ PathFromUTF8(PathTraitsUTF8::const_pointer_type path_utf8) ...@@ -135,20 +127,13 @@ PathFromUTF8(PathTraitsUTF8::const_pointer_type path_utf8)
#endif #endif
#ifdef WIN32 #ifdef WIN32
try { const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8);
const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8); return PathTraitsFS::string(buffer.c_str());
return PathTraitsFS::string(buffer.c_str());
} catch (const std::runtime_error &) {
return PathTraitsFS::string();
}
#else #else
if (fs_converter == nullptr) if (fs_converter == nullptr)
return path_utf8; return path_utf8;
const auto buffer = fs_converter->FromUTF8(path_utf8); const auto buffer = fs_converter->FromUTF8(path_utf8);
if (buffer.IsNull())
return PathTraitsFS::string();
return PathTraitsFS::string(buffer.c_str()); return PathTraitsFS::string(buffer.c_str());
#endif #endif
} }
......
...@@ -48,7 +48,8 @@ DeinitFSCharset(); ...@@ -48,7 +48,8 @@ DeinitFSCharset();
/** /**
* Convert the path to UTF-8. * Convert the path to UTF-8.
* Returns empty string on error. *
* Throws std::runtime_error on error.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
PathTraitsUTF8::string PathTraitsUTF8::string
...@@ -56,7 +57,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs); ...@@ -56,7 +57,8 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs);
/** /**
* Convert the path from UTF-8. * Convert the path from UTF-8.
* Returns empty string on error. *
* Throws std::runtime_error on error.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
PathTraitsFS::string PathTraitsFS::string
......
...@@ -21,10 +21,16 @@ ...@@ -21,10 +21,16 @@
#include "Path.hxx" #include "Path.hxx"
#include "Charset.hxx" #include "Charset.hxx"
#include <stdexcept>
std::string std::string
Path::ToUTF8() const Path::ToUTF8() const
{ {
return ::PathToUTF8(c_str()); try {
return ::PathToUTF8(c_str());
} catch (const std::runtime_error &) {
return std::string();
}
} }
Path::const_pointer_type Path::const_pointer_type
......
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