lib/icu/Win32: throw exception on error

This commit is contained in:
Max Kellermann
2016-04-13 11:51:01 +02:00
parent e228144159
commit b9f535cd49
6 changed files with 72 additions and 40 deletions

View File

@@ -19,6 +19,7 @@
#include "config.h"
#include "Win32.hxx"
#include "system/Error.hxx"
#include "util/AllocatedString.hxx"
#include <memory>
@@ -31,14 +32,14 @@ WideCharToMultiByte(unsigned code_page, const wchar_t *src)
int length = WideCharToMultiByte(code_page, 0, src, -1, nullptr, 0,
nullptr, nullptr);
if (length <= 0)
return nullptr;
throw MakeLastError("Failed to convert from Unicode");
std::unique_ptr<char[]> buffer(new char[length]);
length = WideCharToMultiByte(code_page, 0, src, -1,
buffer.get(), length,
nullptr, nullptr);
if (length <= 0)
return nullptr;
throw MakeLastError("Failed to convert from Unicode");
return AllocatedString<char>::Donate(buffer.release());
}
@@ -48,13 +49,13 @@ MultiByteToWideChar(unsigned code_page, const char *src)
{
int length = MultiByteToWideChar(code_page, 0, src, -1, nullptr, 0);
if (length <= 0)
return nullptr;
throw MakeLastError("Failed to convert to Unicode");
std::unique_ptr<wchar_t[]> buffer(new wchar_t[length]);
length = MultiByteToWideChar(code_page, 0, src, -1,
buffer.get(), length);
if (length <= 0)
return nullptr;
throw MakeLastError("Failed to convert to Unicode");
return AllocatedString<wchar_t>::Donate(buffer.release());
}