/*] Copyright (c) 2009-2010, Charles McGarvey [************************** **] All rights reserved. * * vi:ts=4 sw=4 tw=75 * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * **************************************************************************/ #include #include "ConvertUTF.h" #include "StringTools.hh" namespace Mf { // TODO this code is ugly std::wstring multiToWide(const std::string& multiStr) { size_t length = multiStr.length(); if (sizeof(wchar_t) == 2) { wchar_t* wideStr = new wchar_t[length + 1]; const UTF8* srcStart = reinterpret_cast(multiStr.c_str()); const UTF8* srcEnd = srcStart + length; UTF16* targetStart = reinterpret_cast(wideStr); UTF16* targetEnd = targetStart + length+1; ConversionResult res = ConvertUTF8toUTF16(&srcStart, srcEnd, &targetStart, targetEnd, lenientConversion); if (res != conversionOK) { delete[] wideStr; throw std::runtime_error("bad conversion from multi to wide characters"); } *targetStart = 0; std::wstring convertedStr(wideStr); delete[] wideStr; return convertedStr; } else if (sizeof(wchar_t) == 4) { wchar_t* wideStr = new wchar_t[length]; const UTF8* srcStart = reinterpret_cast(multiStr.c_str()); const UTF8* srcEnd = srcStart + length; UTF32* targetStart = reinterpret_cast(wideStr); UTF32* targetEnd = targetStart + length; ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd, &targetStart, targetEnd, lenientConversion); if (res != conversionOK) { delete[] wideStr; throw std::runtime_error("bad conversion from multi to wide characters"); } *targetStart = 0; std::wstring convertedStr(wideStr); delete[] wideStr; return convertedStr; } else { throw std::runtime_error("unknown size of wide characters"); } return L""; } std::string wideToMulti(const std::wstring& wideStr) { size_t length = wideStr.length(); if (sizeof(wchar_t) == 2) { size_t multiLength = 3 * length + 1; char* multiStr = new char[multiLength]; const UTF16* srcStart = reinterpret_cast(wideStr.c_str()); const UTF16* srcEnd = srcStart + length; UTF8* targetStart = reinterpret_cast(multiStr); UTF8* targetEnd = targetStart + multiLength; ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd, &targetStart, targetEnd, lenientConversion); if (res != conversionOK) { delete[] multiStr; throw std::runtime_error("bad conversion from wide to multi-characters"); } *targetStart = 0; std::string convertedStr(multiStr); delete[] multiStr; return convertedStr; } else if (sizeof(wchar_t) == 4) { size_t multiLength = 4 * length + 1; char* multiStr = new char[multiLength]; const UTF32* srcStart = reinterpret_cast(wideStr.c_str()); const UTF32* srcEnd = srcStart + length; UTF8* targetStart = reinterpret_cast(multiStr); UTF8* targetEnd = targetStart + multiLength; ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd, &targetStart, targetEnd, lenientConversion); if (res != conversionOK) { delete[] multiStr; throw std::runtime_error("bad conversion from wide to multi-characters"); } *targetStart = 0; std::string convertedStr(multiStr); delete[] multiStr; return convertedStr; } else { throw std::runtime_error("unknown size of wide characters"); } return ""; } } // namespace Mf