fs/Charset: throw exception on error

This commit is contained in:
Max Kellermann 2016-04-12 21:24:16 +02:00
parent a0eb6d0976
commit 6513ff92a7
4 changed files with 29 additions and 26 deletions

View File

@ -24,6 +24,8 @@
#include "util/Error.hxx"
#include "Compiler.h"
#include <stdexcept>
/* no inlining, please */
AllocatedPath::~AllocatedPath() {}
@ -31,7 +33,11 @@ AllocatedPath
AllocatedPath::FromUTF8(const char *path_utf8)
{
#if defined(HAVE_FS_CHARSET) || defined(WIN32)
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
{
try {
return ::PathToUTF8(c_str());
} catch (const std::runtime_error &) {
return std::string();
}
}
void

View File

@ -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();
}
#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();
}
#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();
}
#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
}

View File

@ -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

View File

@ -21,10 +21,16 @@
#include "Path.hxx"
#include "Charset.hxx"
#include <stdexcept>
std::string
Path::ToUTF8() const
{
try {
return ::PathToUTF8(c_str());
} catch (const std::runtime_error &) {
return std::string();
}
}
Path::const_pointer_type