lib/icu/Converter: Create() throws exception on error

This commit is contained in:
Max Kellermann 2016-04-12 22:18:36 +02:00
parent 33fdaa5b6d
commit 01b68db30e
9 changed files with 45 additions and 45 deletions

View File

@ -1657,11 +1657,11 @@ test_DumpDatabase_LDADD = \
$(DB_LIBS) \ $(DB_LIBS) \
$(TAG_LIBS) \ $(TAG_LIBS) \
libconf.a \ libconf.a \
libutil.a \
libevent.a \ libevent.a \
$(FS_LIBS) \ $(FS_LIBS) \
libsystem.a \ libsystem.a \
$(ICU_LDADD) $(ICU_LDADD) \
libutil.a
test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \
src/protocol/Ack.cxx \ src/protocol/Ack.cxx \
src/Log.cxx src/LogBackend.cxx \ src/Log.cxx src/LogBackend.cxx \
@ -2002,8 +2002,8 @@ test_run_convert_LDADD = \
libconf.a \ libconf.a \
$(FS_LIBS) \ $(FS_LIBS) \
libsystem.a \ libsystem.a \
libutil.a \ $(ICU_LDADD) \
$(ICU_LDADD) libutil.a
test_run_output_LDADD = $(MPD_LIBS) \ test_run_output_LDADD = $(MPD_LIBS) \
$(PCM_LIBS) \ $(PCM_LIBS) \

View File

@ -510,10 +510,7 @@ static int mpd_main_after_fork(struct options options)
try { try {
Error error; Error error;
if (!ConfigureFS(error)) { ConfigureFS();
LogError(error);
return EXIT_FAILURE;
}
if (!glue_mapper_init(error)) { if (!glue_mapper_init(error)) {
LogError(error); LogError(error);

View File

@ -40,19 +40,17 @@ static std::string fs_charset;
static IcuConverter *fs_converter; static IcuConverter *fs_converter;
bool void
SetFSCharset(const char *charset, Error &error) SetFSCharset(const char *charset)
{ {
assert(charset != nullptr); assert(charset != nullptr);
assert(fs_converter == nullptr); assert(fs_converter == nullptr);
fs_converter = IcuConverter::Create(charset, error); fs_converter = IcuConverter::Create(charset);
if (fs_converter == nullptr) assert(fs_converter != nullptr);
return false;
FormatDebug(path_domain, FormatDebug(path_domain,
"SetFSCharset: fs charset is: %s", fs_charset.c_str()); "SetFSCharset: fs charset is: %s", fs_charset.c_str());
return true;
} }
#endif #endif

View File

@ -37,8 +37,11 @@ gcc_const
const char * const char *
GetFSCharset(); GetFSCharset();
bool /**
SetFSCharset(const char *charset, Error &error); * Throws std::runtime_error on error.
*/
void
SetFSCharset(const char *charset);
void void
DeinitFSCharset(); DeinitFSCharset();

View File

@ -22,15 +22,13 @@
#include "Charset.hxx" #include "Charset.hxx"
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
bool void
ConfigureFS(Error &error) ConfigureFS()
{ {
#ifdef HAVE_FS_CHARSET #ifdef HAVE_FS_CHARSET
const char *charset = config_get_string(ConfigOption::FS_CHARSET); const char *charset = config_get_string(ConfigOption::FS_CHARSET);
return charset == nullptr || SetFSCharset(charset, error); if (charset != nullptr)
#else SetFSCharset(charset);
(void)error;
return true;
#endif #endif
} }

View File

@ -26,9 +26,11 @@ class Error;
/** /**
* Performs global one-time initialization of this class. * Performs global one-time initialization of this class.
*
* Throws std::runtime_error on error.
*/ */
bool void
ConfigureFS(Error &error); ConfigureFS();
void void
DeinitFS(); DeinitFS();

View File

@ -19,12 +19,13 @@
#include "config.h" #include "config.h"
#include "Converter.hxx" #include "Converter.hxx"
#include "Error.hxx"
#include "util/Error.hxx"
#include "util/Macros.hxx" #include "util/Macros.hxx"
#include "util/AllocatedString.hxx" #include "util/AllocatedString.hxx"
#include "util/AllocatedArray.hxx" #include "util/AllocatedArray.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/FormatString.hxx"
#include <stdexcept>
#include <string.h> #include <string.h>
@ -32,8 +33,7 @@
#include "Util.hxx" #include "Util.hxx"
#include <unicode/ucnv.h> #include <unicode/ucnv.h>
#elif defined(HAVE_ICONV) #elif defined(HAVE_ICONV)
#include "util/Domain.hxx" #include "system/Error.hxx"
static constexpr Domain iconv_domain("iconv");
#endif #endif
#ifdef HAVE_ICU #ifdef HAVE_ICU
@ -48,30 +48,27 @@ IcuConverter::~IcuConverter()
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
IcuConverter * IcuConverter *
IcuConverter::Create(const char *charset, Error &error) IcuConverter::Create(const char *charset)
{ {
#ifdef HAVE_ICU #ifdef HAVE_ICU
UErrorCode code = U_ZERO_ERROR; UErrorCode code = U_ZERO_ERROR;
UConverter *converter = ucnv_open(charset, &code); UConverter *converter = ucnv_open(charset, &code);
if (converter == nullptr) { if (converter == nullptr)
error.Format(icu_domain, int(code), throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
"Failed to initialize charset '%s': %s", charset, u_errorName(code)).c_str());
charset, u_errorName(code));
return nullptr;
}
return new IcuConverter(converter); return new IcuConverter(converter);
#elif defined(HAVE_ICONV) #elif defined(HAVE_ICONV)
iconv_t to = iconv_open("utf-8", charset); iconv_t to = iconv_open("utf-8", charset);
iconv_t from = iconv_open(charset, "utf-8"); iconv_t from = iconv_open(charset, "utf-8");
if (to == (iconv_t)-1 || from == (iconv_t)-1) { if (to == (iconv_t)-1 || from == (iconv_t)-1) {
error.FormatErrno("Failed to initialize charset '%s'", int e = errno;
charset);
if (to != (iconv_t)-1) if (to != (iconv_t)-1)
iconv_close(to); iconv_close(to);
if (from != (iconv_t)-1) if (from != (iconv_t)-1)
iconv_close(from); iconv_close(from);
return nullptr; throw FormatErrno(e, "Failed to initialize charset '%s'",
charset);
} }
return new IcuConverter(to, from); return new IcuConverter(to, from);

View File

@ -33,8 +33,6 @@
#ifdef HAVE_ICU_CONVERTER #ifdef HAVE_ICU_CONVERTER
class Error;
#ifdef HAVE_ICU #ifdef HAVE_ICU
struct UConverter; struct UConverter;
#endif #endif
@ -73,7 +71,10 @@ public:
} }
#endif #endif
static IcuConverter *Create(const char *charset, Error &error); /**
* Throws std::runtime_error on error.
*/
static IcuConverter *Create(const char *charset);
/** /**
* Convert the string to UTF-8. * Convert the string to UTF-8.

View File

@ -13,6 +13,8 @@
#include <cppunit/ui/text/TestRunner.h> #include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
#include <stdexcept>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -39,14 +41,16 @@ class TestIcuConverter : public CppUnit::TestFixture {
public: public:
void TestInvalidCharset() { void TestInvalidCharset() {
CPPUNIT_ASSERT_EQUAL((IcuConverter *)nullptr, try {
IcuConverter::Create("doesntexist", IcuConverter::Create("doesntexist");
IgnoreError())); CPPUNIT_FAIL("Exception expected");
} catch (const std::runtime_error &) {
}
} }
void TestLatin1() { void TestLatin1() {
IcuConverter *const converter = IcuConverter *const converter =
IcuConverter::Create("iso-8859-1", IgnoreError()); IcuConverter::Create("iso-8859-1");
CPPUNIT_ASSERT(converter != nullptr); CPPUNIT_ASSERT(converter != nullptr);
for (const auto i : invalid_utf8) { for (const auto i : invalid_utf8) {